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.component;
21  
22  import static org.easymock.EasyMock.expect;
23  import static org.easymock.EasyMock.expectLastCall;
24  import static org.easymock.EasyMock.isNull;
25  import static org.easymock.EasyMock.same;
26  import static org.easymock.classextension.EasyMock.createControl;
27  import static org.testng.Assert.assertEquals;
28  import static org.testng.Assert.assertTrue;
29  
30  import java.lang.reflect.Method;
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.List;
36  
37  import javax.faces.context.FacesContext;
38  import javax.faces.event.FacesEvent;
39  import javax.faces.event.FacesListener;
40  import javax.faces.render.Renderer;
41  
42  import org.easymock.classextension.IMocksControl;
43  import org.testng.annotations.BeforeMethod;
44  import org.testng.annotations.Factory;
45  import org.testng.annotations.Test;
46  
47  @SuppressWarnings("deprecation")
48  public class UIComponentBaseTest
49  {
50      private UIComponentBase _testImpl;
51  
52      private IMocksControl _mocksControl;
53      private FacesContext _facesContext;
54      private Renderer _renderer;
55  
56      @BeforeMethod(alwaysRun = true)
57      protected void setUp() throws Exception
58      {
59          _mocksControl = createControl();
60          _facesContext = _mocksControl.createMock(FacesContext.class);
61          _testImpl = _mocksControl.createMock(UIComponentBase.class, getMockedMethodsArray());
62          _renderer = _mocksControl.createMock(Renderer.class);
63      }
64  
65      protected final Method[] getMockedMethodsArray()
66      {
67          Collection<Method> mockedMethods = getMockedMethods();
68          return mockedMethods.toArray(new Method[mockedMethods.size()]);
69      }
70  
71      protected Collection<Method> getMockedMethods()
72      {
73          try
74          {
75              Collection<Method> methods = new ArrayList<Method>();
76              methods.add(UIComponentBase.class.getDeclaredMethod("getRenderer", new Class[] { FacesContext.class }));
77              methods.add(UIComponentBase.class.getDeclaredMethod("getFacesContext", (Class<?>[])null));
78              methods.add(UIComponentBase.class.getDeclaredMethod("getParent", (Class<?>[])null));
79              methods.add(UIComponentBase.class
80                      .getDeclaredMethod("getPathToComponent", new Class[] { UIComponent.class }));
81  
82              return methods;
83          }
84          catch (RuntimeException e)
85          {
86              throw e;
87          }
88          catch (Exception e)
89          {
90              throw new RuntimeException(e);
91          }
92      }
93  
94      /*
95       * Test method for 'javax.faces.component.UIComponentBase.getAttributes()'
96       */
97      @Test
98      public void testGetAttributes()
99      {
100         // TODO implement tests for _ComponentAttributesMap
101         assertTrue(_testImpl.getAttributes() instanceof _ComponentAttributesMap);
102     }
103 
104     @Test
105     public void testGetRendersChildren()
106     {
107         assertGetRendersChildren(false, null);
108         assertGetRendersChildren(true, _renderer);
109         assertGetRendersChildren(false, _renderer);
110     }
111 
112     private void assertGetRendersChildren(boolean expectedValue, Renderer renderer)
113     {
114         expect(_testImpl.getFacesContext()).andReturn(_facesContext);
115         expect(_testImpl.getRenderer(same(_facesContext))).andReturn(renderer);
116         if (renderer != null)
117             expect(renderer.getRendersChildren()).andReturn(expectedValue);
118         _mocksControl.replay();
119         assertEquals(expectedValue, _testImpl.getRendersChildren());
120         _mocksControl.verify();
121         _mocksControl.reset();
122     }
123 
124 // FIXME: The children map now calls FacesContext.getCurrentInstance which returns null thus throwing a 
125 //        npe, I don't know how to fix it yet.
126 //    @Test
127 //    public void testGetChildCount() throws Exception
128 //    {
129 //        assertEquals(0, _testImpl.getChildCount());
130 //        UIComponent child = _mocksControl.createMock(UIComponent.class);
131 //        List<UIComponent> children = _testImpl.getChildren();
132 //        expect(child.getParent()).andReturn(null);
133 //        child.setParent(same(_testImpl));
134 //        _mocksControl.replay();
135 //        children.add(child);
136 //        assertEquals(1, _testImpl.getChildCount());
137 //        _mocksControl.reset();
138 //        child.setParent((UIComponent) isNull());
139 //        _mocksControl.replay();
140 //        children.remove(child);
141 //        assertEquals(0, _testImpl.getChildCount());
142 //    }
143 
144     @Test(expectedExceptions = { NullPointerException.class })
145     public void testBroadcastArgNPE() throws Exception
146     {
147         _testImpl.broadcast(null);
148     }
149 
150     @Test
151     public void testBroadcast() throws Exception
152     {
153         FacesEvent event = _mocksControl.createMock(FacesEvent.class);
154         _testImpl.broadcast(event);
155 
156         FacesListener listener1 = _mocksControl.createMock(FacesListener.class);
157         FacesListener listener2 = _mocksControl.createMock(FacesListener.class);
158         _testImpl.addFacesListener(listener1);
159         _testImpl.addFacesListener(listener2);
160 
161         expect(event.isAppropriateListener(same(listener1))).andReturn(false);
162         expect(event.isAppropriateListener(same(listener2))).andReturn(true);
163         event.processListener(same(listener2));
164 
165         _mocksControl.replay();
166         _testImpl.broadcast(event);
167         _mocksControl.verify();
168     }
169 
170     @Test(expectedExceptions = { NullPointerException.class })
171     public void testDecodeArgNPE() throws Exception
172     {
173         _testImpl.decode(null);
174     }
175 
176     @Test
177     public void testDecode() throws Exception
178     {
179         expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
180         _renderer.decode(same(_facesContext), same(_testImpl));
181         _mocksControl.replay();
182         _testImpl.decode(_facesContext);
183         _mocksControl.verify();
184     }
185 
186     @Test(expectedExceptions = { NullPointerException.class })
187     public void testEncodeBeginArgNPE() throws Exception
188     {
189         _testImpl.encodeBegin(null);
190     }
191 
192 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
193 //  easy mock to fix the test error.
194 //    @Test
195 //    public void testEncodeBegin() throws Exception
196 //    {
197 //        _testImpl.setRendered(false);
198 //        _mocksControl.replay();
199 //        _testImpl.encodeBegin(_facesContext);
200 //
201 //        _mocksControl.reset();
202 //        _testImpl.setRendered(true);
203 //        expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
204 //        _renderer.encodeBegin(same(_facesContext), same(_testImpl));
205 //        _mocksControl.replay();
206 //        _testImpl.encodeBegin(_facesContext);
207 //        _mocksControl.verify();
208 //    }
209 
210     @Test(expectedExceptions = { NullPointerException.class })
211     public void testEncodeChildrenArgNPE() throws Exception
212     {
213         _testImpl.encodeChildren(null);
214     }
215 
216     @Test
217     public void testEncodeChildren() throws Exception
218     {
219         _testImpl.setRendered(false);
220         _mocksControl.replay();
221         _testImpl.encodeChildren(_facesContext);
222 
223         _mocksControl.reset();
224         _testImpl.setRendered(true);
225         expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
226         _renderer.encodeChildren(same(_facesContext), same(_testImpl));
227         _mocksControl.replay();
228         _testImpl.encodeChildren(_facesContext);
229         _mocksControl.verify();
230     }
231 
232     @Test(expectedExceptions = { NullPointerException.class })
233     public void testEncodeEndArgNPE() throws Exception
234     {
235         _testImpl.encodeEnd(null);
236     }
237 
238 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
239 //  easy mock to fix the test error.
240 //    @Test
241 //    public void testEncodeEnd() throws Exception
242 //    {
243 //        _testImpl.setRendered(false);
244 //        _mocksControl.replay();
245 //        _testImpl.encodeEnd(_facesContext);
246 //
247 //        _mocksControl.reset();
248 //        _testImpl.setRendered(true);
249 //        expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
250 //        _renderer.encodeEnd(same(_facesContext), same(_testImpl));
251 //        _mocksControl.replay();
252 //        _testImpl.encodeEnd(_facesContext);
253 //        _mocksControl.verify();
254 //    }
255 
256     @Test(expectedExceptions = { NullPointerException.class })
257     public void testQueueEventArgNPE() throws Exception
258     {
259         _testImpl.queueEvent(null);
260     }
261 
262     @Test(expectedExceptions = { IllegalStateException.class })
263     public void testQueueEventWithoutParent() throws Exception
264     {
265         FacesEvent event = _mocksControl.createMock(FacesEvent.class);
266         expect(_testImpl.getParent()).andReturn(null);
267         _mocksControl.replay();
268         _testImpl.queueEvent(event);
269     }
270 
271     @Test
272     public void testQueueEvent() throws Exception
273     {
274         FacesEvent event = _mocksControl.createMock(FacesEvent.class);
275         UIComponent parent = _mocksControl.createMock(UIComponent.class);
276         expect(_testImpl.getParent()).andReturn(parent);
277         parent.queueEvent(same(event));
278         _mocksControl.replay();
279         _testImpl.queueEvent(event);
280         _mocksControl.verify();
281     }
282 
283     @Test(expectedExceptions = { NullPointerException.class })
284     public void testProcessDecodesArgNPE() throws Exception
285     {
286         _testImpl.processDecodes(null);
287     }
288 
289     @Test(expectedExceptions = { RuntimeException.class })
290     public void testProcessDecodesCallsRenderResoponseIfDecodeThrowsException()
291     {
292         List<UIComponent> emptyList = Collections.emptyList();
293         
294         expect(_testImpl.getFacetsAndChildren()).andReturn(emptyList.iterator());
295         _testImpl.decode(same(_facesContext));
296         expectLastCall().andThrow(new RuntimeException());
297         _facesContext.renderResponse();
298         _mocksControl.replay();
299         try
300         {
301             _testImpl.processDecodes(_facesContext);
302         }
303         finally
304         {
305             _mocksControl.verify();
306         }
307     }
308 
309     @Test(enabled=false)
310     public void testProcessDecodesWithRenderedFalse() throws Exception
311     {
312         _testImpl.setRendered(false);
313         _mocksControl.replay();
314         _testImpl.processDecodes(_facesContext);
315     }
316 
317 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
318 //  easy mock to fix the test error.
319 //    @Test
320 //    public void testProcessDecodesWithRenderedTrue() throws Exception
321 //    {
322 //        Collection<Method> methods = getMockedMethods();
323 //        methods.add(UIComponentBase.class.getDeclaredMethod("getFacetsAndChildren", (Class<?>[])null));
324 //        methods.add(UIComponentBase.class.getDeclaredMethod("decode", new Class[] { FacesContext.class }));
325 //        _testImpl = _mocksControl.createMock(UIComponentBase.class, methods.toArray(new Method[methods.size()]));
326 //        UIComponent child = _mocksControl.createMock(UIComponent.class);
327 //        expect(_testImpl.getFacetsAndChildren()).andReturn(Arrays.asList(new UIComponent[] { child }).iterator());
328 //        child.processDecodes(same(_facesContext));
329 //        _testImpl.decode(same(_facesContext));
330 //        _mocksControl.replay();
331 //        _testImpl.processDecodes(_facesContext);
332 //        _mocksControl.verify();
333 //    }
334 
335     @Test(expectedExceptions = { NullPointerException.class })
336     public void testProcessValidatorsArgNPE() throws Exception
337     {
338         _testImpl.processValidators(null);
339     }
340 
341     @Test(enabled=false)
342     public void testProcessValidatorsWithRenderedFalse() throws Exception
343     {
344         _testImpl.setRendered(false);
345         _mocksControl.replay();
346         _testImpl.processValidators(_facesContext);
347     }
348 
349 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
350 //  easy mock to fix the test error.
351 //    @Test
352 //    public void testProcessValidatorsWithRenderedTrue() throws Exception
353 //    {
354 //        UIComponent child = setupProcessXYZTest();
355 //        child.processValidators(same(_facesContext));
356 //        _mocksControl.replay();
357 //        _testImpl.processValidators(_facesContext);
358 //        _mocksControl.verify();
359 //    }
360 
361     private UIComponent setupProcessXYZTest() throws Exception
362     {
363         Collection<Method> methods = getMockedMethods();
364         methods.add(UIComponentBase.class.getDeclaredMethod("getFacetsAndChildren", (Class<?>[])null));
365         _testImpl = _mocksControl.createMock(UIComponentBase.class, methods.toArray(new Method[methods.size()]));
366         UIComponent child = _mocksControl.createMock(UIComponent.class);
367         expect(_testImpl.getFacetsAndChildren()).andReturn(Arrays.asList(new UIComponent[] { child }).iterator());
368         return child;
369     }
370 
371     @Test(expectedExceptions = { NullPointerException.class })
372     public void testProcessUpdatesArgNPE() throws Exception
373     {
374         _testImpl.processUpdates(null);
375     }
376 
377     @Test(enabled=false)
378     public void testProcessUpdatesWithRenderedFalse() throws Exception
379     {
380         _testImpl.setRendered(false);
381         _mocksControl.replay();
382         _testImpl.processUpdates(_facesContext);
383     }
384 
385 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
386 //  easy mock to fix the test error.
387 //    @Test
388 //    public void testProcessUpdatesWithRenderedTrue() throws Exception
389 //    {
390 //        UIComponent child = setupProcessXYZTest();
391 //        child.processUpdates(same(_facesContext));
392 //        _mocksControl.replay();
393 //        _testImpl.processUpdates(_facesContext);
394 //        _mocksControl.verify();
395 //    }
396 
397     @Factory
398     public Object[] createPropertyTests() throws Exception
399     {
400         return new Object[] {
401                 new AbstractUIComponentPropertyTest<Boolean>("rendered", true, new Boolean[] { false, true })
402                 {
403                     @Override
404                     protected UIComponent createComponent()
405                     {
406                         return getMocksControl().createMock(UIComponentBase.class, new Method[0]);
407                     }
408                 }, new AbstractUIComponentPropertyTest<String>("rendererType", null, new String[] { null, null })
409                 {
410                     @Override
411                     protected UIComponent createComponent()
412                     {
413                         return getMocksControl().createMock(UIComponentBase.class, new Method[0]);
414                     }
415                 } };
416     }
417 }