Commit 2dce45971c37d4ab6a9d30f31491bedd15e26851
1 parent
0cce422f
Exists in
master
Ajustes na criação dos Locales.
Showing
4 changed files
with
10 additions
and
187 deletions
Show diff stats
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/JsfBootstrap.java
... | ... | @@ -38,22 +38,11 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; |
38 | 38 | |
39 | 39 | import javax.enterprise.event.Observes; |
40 | 40 | import javax.enterprise.inject.spi.AfterBeanDiscovery; |
41 | -import javax.enterprise.inject.spi.AnnotatedType; | |
42 | -import javax.enterprise.inject.spi.ProcessAnnotatedType; | |
43 | 41 | |
44 | 42 | import br.gov.frameworkdemoiselle.internal.context.ViewContext; |
45 | -import br.gov.frameworkdemoiselle.internal.producer.ServletLocaleProducer; | |
46 | 43 | |
47 | 44 | public class JsfBootstrap extends AbstractBootstrap { |
48 | 45 | |
49 | - protected <T> void cancelServletLocaleProducer(@Observes final ProcessAnnotatedType<T> event) { | |
50 | - final AnnotatedType<T> annotatedType = event.getAnnotatedType(); | |
51 | - | |
52 | - if (annotatedType.getJavaClass() == ServletLocaleProducer.class) { | |
53 | - event.veto(); | |
54 | - } | |
55 | - } | |
56 | - | |
57 | 46 | public void loadContexts(@Observes final AfterBeanDiscovery event) { |
58 | 47 | addContext(new ViewContext(), event); |
59 | 48 | } | ... | ... |
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/producer/JsfLocaleProducer.java
... | ... | @@ -1,66 +0,0 @@ |
1 | -/* | |
2 | - * Demoiselle Framework Copyright (C) 2010 SERPRO | |
3 | - * ---------------------------------------------------------------------------- This file is part of Demoiselle | |
4 | - * Framework. Demoiselle Framework is free software; you can redistribute it and/or modify it under the terms of the GNU | |
5 | - * Lesser General Public License version 3 as published by the Free Software Foundation. This program is distributed in | |
6 | - * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
7 | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a | |
8 | - * copy of the GNU Lesser General Public License version 3 along with this program; if not, see | |
9 | - * <http://www.gnu.org/licenses/> or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
10 | - * Boston, MA 02110-1301, USA. ---------------------------------------------------------------------------- Este arquivo | |
11 | - * é parte do Framework Demoiselle. O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
12 | - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação do Software Livre (FSF). Este | |
13 | - * programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA GARANTIA; sem uma garantia implícita de | |
14 | - * ADEQUAÇÃO a qualquer MERCADO ou APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português para | |
15 | - * maiores detalhes. Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título "LICENCA.txt", junto com esse | |
16 | - * programa. Se não, acesse <http://www.gnu.org/licenses/> ou escreva para a Fundação do Software Livre (FSF) Inc., 51 | |
17 | - * Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
18 | - */ | |
19 | -package br.gov.frameworkdemoiselle.internal.producer; | |
20 | - | |
21 | -import java.io.Serializable; | |
22 | -import java.util.Locale; | |
23 | - | |
24 | -import javax.enterprise.context.RequestScoped; | |
25 | -import javax.enterprise.inject.Default; | |
26 | -import javax.enterprise.inject.Produces; | |
27 | -import javax.faces.context.FacesContext; | |
28 | -import javax.inject.Named; | |
29 | -import javax.servlet.http.HttpServletRequest; | |
30 | - | |
31 | -import br.gov.frameworkdemoiselle.util.Beans; | |
32 | - | |
33 | -@RequestScoped | |
34 | -public class JsfLocaleProducer implements Serializable { | |
35 | - | |
36 | - private static final long serialVersionUID = 1L; | |
37 | - | |
38 | - private boolean loaded = false; | |
39 | - | |
40 | - @Produces | |
41 | - @Default | |
42 | - @Named("currentLocale") | |
43 | - public Locale create() { | |
44 | - Locale locale; | |
45 | - | |
46 | - try { | |
47 | - FacesContext facesContext = Beans.getReference(FacesContext.class); | |
48 | - | |
49 | - if (!loaded) { | |
50 | - if (facesContext != null) { | |
51 | - locale = Beans.getReference(HttpServletRequest.class).getLocale(); | |
52 | - facesContext.getApplication().setDefaultLocale(locale); | |
53 | - } | |
54 | - | |
55 | - loaded = true; | |
56 | - } | |
57 | - | |
58 | - locale = facesContext.getApplication().getDefaultLocale(); | |
59 | - | |
60 | - } catch (Exception cause) { | |
61 | - locale = Locale.getDefault(); | |
62 | - } | |
63 | - | |
64 | - return locale; | |
65 | - } | |
66 | -} |
impl/extension/jsf/src/test/java/br/gov/frameworkdemoiselle/internal/producer/JsfLocaleProducerTest.java
... | ... | @@ -1,109 +0,0 @@ |
1 | -/* | |
2 | - * Demoiselle Framework | |
3 | - * Copyright (C) 2010 SERPRO | |
4 | - * ---------------------------------------------------------------------------- | |
5 | - * This file is part of Demoiselle Framework. | |
6 | - * | |
7 | - * Demoiselle Framework is free software; you can redistribute it and/or | |
8 | - * modify it under the terms of the GNU Lesser General Public License version 3 | |
9 | - * as published by the Free Software Foundation. | |
10 | - * | |
11 | - * This program is distributed in the hope that it will be useful, | |
12 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | - * GNU General Public License for more details. | |
15 | - * | |
16 | - * You should have received a copy of the GNU Lesser General Public License version 3 | |
17 | - * along with this program; if not, see <http://www.gnu.org/licenses/> | |
18 | - * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
19 | - * Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | - * ---------------------------------------------------------------------------- | |
21 | - * Este arquivo é parte do Framework Demoiselle. | |
22 | - * | |
23 | - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
24 | - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
25 | - * do Software Livre (FSF). | |
26 | - * | |
27 | - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
28 | - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
29 | - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
30 | - * para maiores detalhes. | |
31 | - * | |
32 | - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
33 | - * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
34 | - * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
35 | - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
36 | - */ | |
37 | -package br.gov.frameworkdemoiselle.internal.producer; | |
38 | -import org.junit.Ignore; | |
39 | -import static junit.framework.Assert.assertEquals; | |
40 | -import static org.easymock.EasyMock.expect; | |
41 | -import static org.powermock.api.easymock.PowerMock.mockStatic; | |
42 | -import static org.powermock.api.easymock.PowerMock.replayAll; | |
43 | -import static org.powermock.api.easymock.PowerMock.verifyAll; | |
44 | - | |
45 | -import java.util.Locale; | |
46 | - | |
47 | -import javax.enterprise.context.ContextNotActiveException; | |
48 | -import javax.faces.application.Application; | |
49 | -import javax.faces.context.ExternalContext; | |
50 | -import javax.faces.context.FacesContext; | |
51 | -import javax.servlet.http.HttpServletRequest; | |
52 | - | |
53 | -import org.junit.Before; | |
54 | -import org.junit.Test; | |
55 | -import org.junit.runner.RunWith; | |
56 | -import org.powermock.api.easymock.PowerMock; | |
57 | -import org.powermock.core.classloader.annotations.PrepareForTest; | |
58 | -import org.powermock.modules.junit4.PowerMockRunner; | |
59 | - | |
60 | -import br.gov.frameworkdemoiselle.util.Beans; | |
61 | -@Ignore | |
62 | -@RunWith(PowerMockRunner.class) | |
63 | -@PrepareForTest({ Beans.class }) | |
64 | -public class JsfLocaleProducerTest { | |
65 | - | |
66 | - private JsfLocaleProducer producer; | |
67 | - | |
68 | - @Before | |
69 | - public void before() { | |
70 | - producer = new JsfLocaleProducer(); | |
71 | - } | |
72 | - | |
73 | - @Test | |
74 | - public void testCreateOK() { | |
75 | - mockStatic(Beans.class); | |
76 | - | |
77 | - Locale locale = PowerMock.createMock(Locale.class); | |
78 | - Application application = PowerMock.createMock(Application.class); | |
79 | - FacesContext facesContext = PowerMock.createMock(FacesContext.class); | |
80 | - ExternalContext externalContext = PowerMock.createMock(ExternalContext.class); | |
81 | - HttpServletRequest httpServletRequest = PowerMock.createMock(HttpServletRequest.class); | |
82 | - | |
83 | - expect(Beans.getReference(FacesContext.class)).andReturn(facesContext); | |
84 | - expect(facesContext.getExternalContext()).andReturn(externalContext); | |
85 | - expect(externalContext.getRequest()).andReturn(httpServletRequest); | |
86 | - expect(httpServletRequest.getLocale()).andReturn(locale); | |
87 | - expect(facesContext.getApplication()).andReturn(application).anyTimes(); | |
88 | - application.setDefaultLocale(locale); | |
89 | - expect(application.getDefaultLocale()).andReturn(locale); | |
90 | - | |
91 | - replayAll(); | |
92 | - | |
93 | - Locale returned = producer.create(); | |
94 | - assertEquals(returned, locale); | |
95 | - | |
96 | - verifyAll(); | |
97 | - } | |
98 | - | |
99 | - @Test | |
100 | - public void testCreateNOK() { | |
101 | - mockStatic(Beans.class); | |
102 | - expect(Beans.getReference(FacesContext.class)).andThrow(new ContextNotActiveException()); | |
103 | - replayAll(); | |
104 | - Locale returned = producer.create(); | |
105 | - assertEquals(Locale.getDefault(), returned); | |
106 | - verifyAll(); | |
107 | - } | |
108 | - | |
109 | -} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ServletLocaleProducer.java
... | ... | @@ -43,14 +43,23 @@ import javax.enterprise.inject.Default; |
43 | 43 | import javax.enterprise.inject.Produces; |
44 | 44 | import javax.servlet.http.HttpServletRequest; |
45 | 45 | |
46 | +import br.gov.frameworkdemoiselle.util.Beans; | |
47 | + | |
46 | 48 | public class ServletLocaleProducer implements Serializable { |
47 | 49 | |
48 | 50 | private static final long serialVersionUID = 1L; |
49 | 51 | |
50 | 52 | @Default |
51 | 53 | @Produces |
52 | - public Locale create(HttpServletRequest request) { | |
54 | + public Locale create() { | |
53 | 55 | Locale result; |
56 | + HttpServletRequest request; | |
57 | + | |
58 | + try { | |
59 | + request = Beans.getReference(HttpServletRequest.class); | |
60 | + } catch (Exception cause) { | |
61 | + request = null; | |
62 | + } | |
54 | 63 | |
55 | 64 | if (request == null) { |
56 | 65 | result = Locale.getDefault(); | ... | ... |