View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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          // this needs to be called *before* the first Test test is run,
44          // as there may be left over FactoryFinder configurations from
45          // that previous tests that may interfere with the first test here.
46          FactoryFinder.releaseFactories();
47      }
48  
49      @After
50      public void tearDown() throws Exception
51      {
52          // call this again so there is no possibility of messing up tests that will
53          // run after this one
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       * This method allows us access to the _registeredFactoryNames field so we can test the content of that map during
75       * the running of this test.
76       * 
77       * @return Returns the _registeredFactoryNames Map from the FactoryFinder class. @throws NoSuchFieldException
78       * 
79       * @throws IllegalAccessException
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      * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
105      */
106     @Test
107     public void testGetFactory() throws Exception
108     {
109         // no catch because if this fails the test fails, i.e. not trying to test
110         // setFactory here
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      * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
126      */
127     @Test
128     public void testGetFactoryTwice() throws Exception
129     {
130         // this test just makes sure that things work when the get has been called
131         // more than once
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      * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
151      */
152     @Test
153     public void testGetFactoryNoFactory() throws Exception
154     {
155         // no catch because if this fails the test fails, i.e. not trying to test
156         // setFactory here
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      * No configuration test, this should throw and deliver a useful message Test method for
171      * 'javax.faces.FactoryFinder.getFactory(String)'
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      * Bogus factory name test Test method for 'javax.faces.FactoryFinder.setFactory(String, String)'
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      * Test method for 'javax.faces.FactoryFinder.setFactory(String, String)'
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      * If a factory has ever been handed out then setFactory is not supposed to change the factory layout. This test
225      * checks to see if that is true. Test method for 'javax.faces.FactoryFinder.setFactory(String, String)'
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             // getFactory should cause setFactory to stop changing the
238             // registered classes
239             FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
240             // this should essentially be a no-op
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      * Adding factories should add the class name to the list of avalable class names Test method for
255      * 'javax.faces.FactoryFinder.setFactory(String, String)'
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      * Test method for 'javax.faces.FactoryFinder.releaseFactories()'
281      */
282     @Test
283     public void testReleaseFactories()
284     {
285 
286     }
287 }