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 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      // 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    protected void tearDown() throws Exception {
50      super.tearDown();
51      // call this again so there is no possibility of messing up tests that will
52      // run after this one
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     * This method allows us access to the _registeredFactoryNames field so we can
72     * test the content of that map during the running of this test.
73     * 
74     * @return Returns the _registeredFactoryNames Map from the FactoryFinder
75     * class. @throws NoSuchFieldException @throws IllegalAccessException
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     * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
95     */
96    public void testGetFactory() throws Exception {
97      // no catch because if this fails the test fails, i.e. not trying to test
98      // setFactory here
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    * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
113    */
114   public void testGetFactoryTwice() throws Exception {
115     // this test just makes sure that things work when the get has been called
116     // more than once
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    * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
136    */
137   public void testGetFactoryNoFactory() throws Exception {
138     // no catch because if this fails the test fails, i.e. not trying to test
139     // setFactory here
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    * No configuration test, this should throw and deliver a useful message Test
152    * method for 'javax.faces.FactoryFinder.getFactory(String)'
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    * Bogus factory name test Test method for
167    * 'javax.faces.FactoryFinder.setFactory(String, String)'
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    * Test method for 'javax.faces.FactoryFinder.setFactory(String, String)'
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    * If a factory has ever been handed out then setFactory is not supposed to
195    * change the factory layout. This test checks to see if that is true. Test
196    * method for 'javax.faces.FactoryFinder.setFactory(String, String)'
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       // getFactory should cause setFactory to stop changing the
207       // registered classes
208       FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
209       // this should essentially be a no-op
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    * Adding factories should add the class name to the list of avalable class
223    * names Test method for 'javax.faces.FactoryFinder.setFactory(String,
224    * String)'
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    * Test method for 'javax.faces.FactoryFinder.releaseFactories()'
247    */
248   public void testReleaseFactories() {
249 
250   }
251 
252 }