Commit 67b2e6e4584e5a92411e1d6129b151fcbb506d0c
1 parent
e25dbfbb
Exists in
master
Teste unitário: AbstractLifecycleBootstrapTest
Showing
1 changed file
with
169 additions
and
0 deletions
Show diff stats
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrapTest.java
0 → 100644
@@ -0,0 +1,169 @@ | @@ -0,0 +1,169 @@ | ||
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.bootstrap; | ||
38 | + | ||
39 | +import static junit.framework.Assert.assertFalse; | ||
40 | +import static junit.framework.Assert.assertNotNull; | ||
41 | +import static junit.framework.Assert.assertTrue; | ||
42 | +import static org.easymock.EasyMock.createMock; | ||
43 | +import static org.easymock.EasyMock.expect; | ||
44 | +import static org.powermock.api.easymock.PowerMock.replayAll; | ||
45 | +import static org.powermock.api.easymock.PowerMock.verifyAll; | ||
46 | + | ||
47 | +import java.lang.reflect.Field; | ||
48 | +import java.util.HashSet; | ||
49 | +import java.util.List; | ||
50 | +import java.util.Set; | ||
51 | + | ||
52 | +import javax.enterprise.context.ConversationScoped; | ||
53 | +import javax.enterprise.context.RequestScoped; | ||
54 | +import javax.enterprise.context.SessionScoped; | ||
55 | +import javax.enterprise.inject.spi.AnnotatedMethod; | ||
56 | +import javax.enterprise.inject.spi.AnnotatedType; | ||
57 | +import javax.enterprise.inject.spi.BeanManager; | ||
58 | +import javax.enterprise.inject.spi.ProcessAnnotatedType; | ||
59 | + | ||
60 | +import org.easymock.EasyMock; | ||
61 | +import org.junit.Assert; | ||
62 | +import org.junit.Before; | ||
63 | +import org.junit.Test; | ||
64 | +import org.junit.runner.RunWith; | ||
65 | +import org.powermock.core.classloader.annotations.PrepareForTest; | ||
66 | +import org.powermock.modules.junit4.PowerMockRunner; | ||
67 | +import org.powermock.reflect.Whitebox; | ||
68 | + | ||
69 | +import br.gov.frameworkdemoiselle.annotation.ViewScoped; | ||
70 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | ||
71 | +import br.gov.frameworkdemoiselle.internal.context.Contexts; | ||
72 | +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; | ||
73 | +import br.gov.frameworkdemoiselle.internal.implementation.AnnotatedMethodProcessor; | ||
74 | +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; | ||
75 | +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | ||
76 | +import br.gov.frameworkdemoiselle.lifecycle.Startup; | ||
77 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
78 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
79 | + | ||
80 | +@RunWith(PowerMockRunner.class) | ||
81 | +@PrepareForTest({ Contexts.class, LoggerProducer.class, ResourceBundle.class, ResourceBundleProducer.class, | ||
82 | + Beans.class, ConfigurationLoader.class }) | ||
83 | +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) | ||
84 | +public class AbstractLifecycleBootstrapTest { | ||
85 | + | ||
86 | + private ProcessAnnotatedType event; | ||
87 | + | ||
88 | + private BeanManager beanManager; | ||
89 | + | ||
90 | + private AnnotatedType annotatedType; | ||
91 | + | ||
92 | + private StartupBootstrap startupBootstrap; | ||
93 | + | ||
94 | + private ShutdownBootstrap shutdownBootstrap; | ||
95 | + | ||
96 | + private Class annotationClass; | ||
97 | + | ||
98 | + @Before | ||
99 | + public void before() { | ||
100 | + event = createMock(ProcessAnnotatedType.class); | ||
101 | + annotatedType = createMock(AnnotatedType.class); | ||
102 | + beanManager = null; | ||
103 | + annotationClass = null; | ||
104 | + } | ||
105 | + | ||
106 | + private List<AnnotatedMethodProcessor> getActions(AbstractLifecycleBootstrap bootstrap) | ||
107 | + throws IllegalArgumentException, IllegalAccessException { | ||
108 | + | ||
109 | + Field fields = Whitebox.getField(AbstractLifecycleBootstrap.class, "processors"); | ||
110 | + List<AnnotatedMethodProcessor> list = (List<AnnotatedMethodProcessor>) fields.get(bootstrap); | ||
111 | + return list; | ||
112 | + } | ||
113 | + | ||
114 | + @Test | ||
115 | + public void processAnnotatedTypeStartup() throws IllegalArgumentException, IllegalAccessException { | ||
116 | + startupBootstrap = new StartupBootstrap(); | ||
117 | + List<AnnotatedMethodProcessor> list = getActions(startupBootstrap); | ||
118 | + | ||
119 | + assertNotNull(list); | ||
120 | + assertTrue(list.isEmpty()); | ||
121 | + | ||
122 | + AnnotatedMethod am1 = createMock(AnnotatedMethod.class); | ||
123 | + AnnotatedMethod am2 = createMock(AnnotatedMethod.class); | ||
124 | + AnnotatedMethod am3 = createMock(AnnotatedMethod.class); | ||
125 | + | ||
126 | + Set<AnnotatedMethod> set = new HashSet<AnnotatedMethod>(); | ||
127 | + set.add(am1); | ||
128 | + set.add(am2); | ||
129 | + set.add(am3); | ||
130 | + | ||
131 | + expect(am1.isAnnotationPresent(Startup.class)).andReturn(true); | ||
132 | + expect(am2.isAnnotationPresent(Startup.class)).andReturn(true); | ||
133 | + expect(am3.isAnnotationPresent(Startup.class)).andReturn(false); | ||
134 | + expect(event.getAnnotatedType()).andReturn(annotatedType); | ||
135 | + expect(annotatedType.getMethods()).andReturn(set); | ||
136 | + | ||
137 | + EasyMock.replay(event, annotatedType, am1, am2, am3); | ||
138 | + startupBootstrap.processAnnotatedType(event); | ||
139 | + EasyMock.verify(event, annotatedType); | ||
140 | + | ||
141 | + list = getActions(startupBootstrap); | ||
142 | + assertNotNull(list); | ||
143 | + assertFalse(list.isEmpty()); | ||
144 | + assertTrue(list.size() == 2); | ||
145 | + } | ||
146 | + | ||
147 | + @Test | ||
148 | + public void testLoadTempContexts() { | ||
149 | + startupBootstrap = new StartupBootstrap(); | ||
150 | + | ||
151 | + List<ThreadLocalContext> tempContexts = Whitebox.getInternalState(startupBootstrap, "tempContexts"); | ||
152 | + | ||
153 | + assertNotNull(tempContexts); | ||
154 | + assertTrue(tempContexts.isEmpty()); | ||
155 | + | ||
156 | + replayAll(); | ||
157 | + startupBootstrap.loadTempContexts(null); | ||
158 | + verifyAll(); | ||
159 | + | ||
160 | + assertNotNull(tempContexts); | ||
161 | + Assert.assertEquals(4, tempContexts.size()); | ||
162 | + for (ThreadLocalContext tlc : tempContexts) { | ||
163 | + if (!tlc.getScope().equals(SessionScoped.class) && !tlc.getScope().equals(ConversationScoped.class) | ||
164 | + && !tlc.getScope().equals(RequestScoped.class) && !tlc.getScope().equals(ViewScoped.class)) { | ||
165 | + Assert.fail(); | ||
166 | + } | ||
167 | + } | ||
168 | + } | ||
169 | +} |