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