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 junit.framework.TestCase;
27 import org.apache.myfaces.mock.api.Mock2ApplicationFactory;
28 import org.apache.myfaces.mock.api.MockApplicationFactory;
29
30 public class FactoryFinderTest extends TestCase {
31
32 public static void main(String[] args) {
33 junit.textui.TestRunner.run(FactoryFinderTest.class);
34
35 }
36
37 public FactoryFinderTest(String name) {
38 super(name);
39 }
40
41 protected void setUp() throws Exception {
42 super.setUp();
43
44
45
46 FactoryFinder.releaseFactories();
47 }
48
49 protected void tearDown() throws Exception {
50 super.tearDown();
51
52
53 FactoryFinder.releaseFactories();
54 releaseRegisteredFactoryNames();
55 }
56
57 private void releaseRegisteredFactoryNames() throws Exception {
58 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
59 Map _registeredFactoryNames = getRegisteredFactoryNames();
60 _registeredFactoryNames.remove(classLoader);
61 }
62
63 private List registeredFactoryNames(String factoryName) throws Exception {
64 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
65 Map _registeredFactoryNames = getRegisteredFactoryNames();
66 Map map = (Map) _registeredFactoryNames.get(classLoader);
67 return (List) map.get(factoryName);
68 }
69
70
71
72
73
74
75
76
77 private Map getRegisteredFactoryNames() throws NoSuchFieldException,
78 IllegalAccessException {
79 Class factoryFinderClass = FactoryFinder.class;
80 Field fields[] = factoryFinderClass.getDeclaredFields();
81 Field field = null;
82 for (int i = 0; i < fields.length; i++) {
83 if (fields[i].getName().equals("_registeredFactoryNames")) {
84 field = fields[i];
85 field.setAccessible(true);
86 break;
87 }
88 }
89 Map _registeredFactoryNames = (Map) field.get(null);
90 return _registeredFactoryNames;
91 }
92
93
94
95
96 public void testGetFactory() throws Exception {
97
98
99 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
100 MockApplicationFactory.class.getName());
101 try {
102 Object factory = FactoryFinder
103 .getFactory(FactoryFinder.APPLICATION_FACTORY);
104 assertNotNull(factory);
105 assertTrue(factory.getClass().equals(MockApplicationFactory.class));
106 } catch (IllegalStateException e) {
107 fail("Should not throw an illegal state exception");
108 }
109 }
110
111
112
113
114 public void testGetFactoryTwice() throws Exception {
115
116
117 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
118 MockApplicationFactory.class.getName());
119 try {
120 Object factory1 = FactoryFinder
121 .getFactory(FactoryFinder.APPLICATION_FACTORY);
122 assertNotNull(factory1);
123 assertTrue(factory1.getClass().equals(MockApplicationFactory.class));
124 Object factory2 = FactoryFinder
125 .getFactory(FactoryFinder.APPLICATION_FACTORY);
126 assertNotNull(factory2);
127 assertTrue(factory2.getClass().equals(MockApplicationFactory.class));
128 assertEquals(factory1, factory2);
129 } catch (IllegalStateException e) {
130 fail("Should not throw an illegal state exception");
131 }
132 }
133
134
135
136
137 public void testGetFactoryNoFactory() throws Exception {
138
139
140 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
141 MockApplicationFactory.class.getName());
142 try {
143 FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
144 fail("Should have thrown an illegal state exception");
145 } catch (IllegalArgumentException e) {
146 assertNotNull(e.getMessage());
147 }
148 }
149
150
151
152
153
154 public void testGetFactoryNoConfiguration() throws Exception {
155 try {
156 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
157 fail("Should have thrown an illegal state exception");
158 } catch (IllegalStateException e) {
159 assertNotNull(e.getMessage());
160 assertTrue(e.getMessage().startsWith(
161 "No Factories configured for this Application"));
162 }
163 }
164
165
166
167
168
169 public void testSetFactoryBogusName() {
170 try {
171 FactoryFinder.setFactory("BogusFactoryName", MockApplicationFactory.class
172 .getName());
173 fail("Should have thrown an illegal argument exception");
174 } catch (IllegalArgumentException e) {
175 assertNotNull(e.getMessage());
176 }
177 }
178
179
180
181
182 public void testSetFactory() throws Exception {
183 try {
184 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
185 MockApplicationFactory.class.getName());
186 assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
187 .contains(MockApplicationFactory.class.getName()));
188 } catch (IllegalArgumentException e) {
189 fail("Should not throw an illegal argument exception");
190 }
191 }
192
193
194
195
196
197
198 public void testSetFactoryNoEffect() throws Exception {
199 try {
200 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
201 MockApplicationFactory.class.getName());
202 assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
203 .contains(MockApplicationFactory.class.getName()));
204 assertFalse(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
205 .contains(Mock2ApplicationFactory.class.getName()));
206
207
208 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
209
210 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
211 Mock2ApplicationFactory.class.getName());
212 assertFalse(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
213 .contains(Mock2ApplicationFactory.class.getName()));
214 assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
215 .contains(MockApplicationFactory.class.getName()));
216 } catch (IllegalArgumentException e) {
217 fail("Should not throw an illegal argument exception");
218 }
219 }
220
221
222
223
224
225
226 public void testSetFactoryAdditiveClassNames() throws Exception {
227 try {
228 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
229 MockApplicationFactory.class.getName());
230 assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
231 .contains(MockApplicationFactory.class.getName()));
232 assertFalse(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
233 .contains(Mock2ApplicationFactory.class.getName()));
234 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
235 Mock2ApplicationFactory.class.getName());
236 assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
237 .contains(Mock2ApplicationFactory.class.getName()));
238 assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
239 .contains(MockApplicationFactory.class.getName()));
240 } catch (IllegalArgumentException e) {
241 fail("Should not throw an illegal argument exception");
242 }
243 }
244
245
246
247
248 public void testReleaseFactories() {
249
250 }
251
252 }