1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.faces;
21
22 import java.lang.reflect.Field;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.myfaces.mock.api.Mock2ApplicationFactory;
27 import org.apache.myfaces.mock.api.MockApplicationFactory;
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class FactoryFinderTest
34 {
35
36 public FactoryFinderTest()
37 {
38 }
39
40 @Before
41 public void setUp() throws Exception
42 {
43
44
45
46 FactoryFinder.releaseFactories();
47 }
48
49 @After
50 public void tearDown() throws Exception
51 {
52
53
54 FactoryFinder.releaseFactories();
55 releaseRegisteredFactoryNames();
56 }
57
58 private void releaseRegisteredFactoryNames() throws Exception
59 {
60 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
61 Map<ClassLoader, Map<String, List<String>>> _registeredFactoryNames = getRegisteredFactoryNames();
62 _registeredFactoryNames.remove(classLoader);
63 }
64
65 private List<String> registeredFactoryNames(String factoryName) throws Exception
66 {
67 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
68 Map<ClassLoader, Map<String, List<String>>> _registeredFactoryNames = getRegisteredFactoryNames();
69 Map<String, List<String>> map = _registeredFactoryNames.get(classLoader);
70 return map.get(factoryName);
71 }
72
73
74
75
76
77
78
79
80
81 @SuppressWarnings("unchecked")
82 private Map<ClassLoader, Map<String, List<String>>> getRegisteredFactoryNames() throws IllegalAccessException
83 {
84 Class<FactoryFinder> factoryFinderClass = FactoryFinder.class;
85 Field fields[] = factoryFinderClass.getDeclaredFields();
86 Field field = null;
87 for (int i = 0; i < fields.length; i++)
88 {
89 if (fields[i].getName().equals("registeredFactoryNames"))
90 {
91 field = fields[i];
92 field.setAccessible(true);
93 break;
94 }
95 }
96
97 Map<ClassLoader, Map<String, List<String>>> _registeredFactoryNames = (Map<ClassLoader, Map<String, List<String>>>) field
98 .get(null);
99
100 return _registeredFactoryNames;
101 }
102
103
104
105
106 @Test
107 public void testGetFactory() throws Exception
108 {
109
110
111 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
112 try
113 {
114 Object factory = FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
115 Assert.assertNotNull(factory);
116 Assert.assertTrue(factory.getClass().equals(MockApplicationFactory.class));
117 }
118 catch (IllegalStateException e)
119 {
120 Assert.fail("Should not throw an illegal state exception");
121 }
122 }
123
124
125
126
127 @Test
128 public void testGetFactoryTwice() throws Exception
129 {
130
131
132 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
133 try
134 {
135 Object factory1 = FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
136 Assert.assertNotNull(factory1);
137 Assert.assertTrue(factory1.getClass().equals(MockApplicationFactory.class));
138 Object factory2 = FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
139 Assert.assertNotNull(factory2);
140 Assert.assertTrue(factory2.getClass().equals(MockApplicationFactory.class));
141 Assert.assertEquals(factory1, factory2);
142 }
143 catch (IllegalStateException e)
144 {
145 Assert.fail("Should not throw an illegal state exception");
146 }
147 }
148
149
150
151
152 @Test
153 public void testGetFactoryNoFactory() throws Exception
154 {
155
156
157 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
158 try
159 {
160 FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
161 Assert.fail("Should have thrown an illegal state exception");
162 }
163 catch (IllegalArgumentException e)
164 {
165 Assert.assertNotNull(e.getMessage());
166 }
167 }
168
169
170
171
172
173 @Test
174 public void testGetFactoryNoConfiguration() throws Exception
175 {
176 try
177 {
178 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
179 Assert.fail("Should have thrown an illegal state exception");
180 }
181 catch (IllegalStateException e)
182 {
183 Assert.assertNotNull(e.getMessage());
184 Assert.assertTrue(e.getMessage().startsWith("No Factories configured for this Application"));
185 }
186 }
187
188
189
190
191 @Test
192 public void testSetFactoryBogusName()
193 {
194 try
195 {
196 FactoryFinder.setFactory("BogusFactoryName", MockApplicationFactory.class.getName());
197 Assert.fail("Should have thrown an illegal argument exception");
198 }
199 catch (IllegalArgumentException e)
200 {
201 Assert.assertNotNull(e.getMessage());
202 }
203 }
204
205
206
207
208 @Test
209 public void testSetFactory() throws Exception
210 {
211 try
212 {
213 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
214 Assert.assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
215 MockApplicationFactory.class.getName()));
216 }
217 catch (IllegalArgumentException e)
218 {
219 Assert.fail("Should not throw an illegal argument exception");
220 }
221 }
222
223
224
225
226
227 @Test
228 public void testSetFactoryNoEffect() throws Exception
229 {
230 try
231 {
232 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
233 Assert.assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
234 MockApplicationFactory.class.getName()));
235 Assert.assertFalse(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
236 Mock2ApplicationFactory.class.getName()));
237
238
239 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
240
241 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, Mock2ApplicationFactory.class.getName());
242 Assert.assertFalse(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
243 Mock2ApplicationFactory.class.getName()));
244 Assert.assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
245 MockApplicationFactory.class.getName()));
246 }
247 catch (IllegalArgumentException e)
248 {
249 Assert.fail("Should not throw an illegal argument exception");
250 }
251 }
252
253
254
255
256
257 @Test
258 public void testSetFactoryAdditiveClassNames() throws Exception
259 {
260 try
261 {
262 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
263 Assert.assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
264 MockApplicationFactory.class.getName()));
265 Assert.assertFalse(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
266 Mock2ApplicationFactory.class.getName()));
267 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, Mock2ApplicationFactory.class.getName());
268 Assert.assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
269 Mock2ApplicationFactory.class.getName()));
270 Assert.assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY).contains(
271 MockApplicationFactory.class.getName()));
272 }
273 catch (IllegalArgumentException e)
274 {
275 Assert.fail("Should not throw an illegal argument exception");
276 }
277 }
278
279
280
281
282 @Test
283 public void testReleaseFactories()
284 {
285
286 }
287 }