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         {
118             expect(renderer.getRendersChildren()).andReturn(expectedValue);
119         }
120         _mocksControl.replay();
121         assertEquals(expectedValue, _testImpl.getRendersChildren());
122         _mocksControl.verify();
123         _mocksControl.reset();
124     }
125 
126 // FIXME: The children map now calls FacesContext.getCurrentInstance which returns null thus throwing a 
127 //        npe, I don't know how to fix it yet.
128 //    @Test
129 //    public void testGetChildCount() throws Exception
130 //    {
131 //        assertEquals(0, _testImpl.getChildCount());
132 //        UIComponent child = _mocksControl.createMock(UIComponent.class);
133 //        List<UIComponent> children = _testImpl.getChildren();
134 //        expect(child.getParent()).andReturn(null);
135 //        child.setParent(same(_testImpl));
136 //        _mocksControl.replay();
137 //        children.add(child);
138 //        assertEquals(1, _testImpl.getChildCount());
139 //        _mocksControl.reset();
140 //        child.setParent((UIComponent) isNull());
141 //        _mocksControl.replay();
142 //        children.remove(child);
143 //        assertEquals(0, _testImpl.getChildCount());
144 //    }
145 
146     @Test(expectedExceptions = { NullPointerException.class })
147     public void testBroadcastArgNPE() throws Exception
148     {
149         _testImpl.broadcast(null);
150     }
151 
152     @Test
153     public void testBroadcast() throws Exception
154     {
155         FacesEvent event = _mocksControl.createMock(FacesEvent.class);
156         _testImpl.broadcast(event);
157 
158         FacesListener listener1 = _mocksControl.createMock(FacesListener.class);
159         FacesListener listener2 = _mocksControl.createMock(FacesListener.class);
160         _testImpl.addFacesListener(listener1);
161         _testImpl.addFacesListener(listener2);
162 
163         expect(event.isAppropriateListener(same(listener1))).andReturn(false);
164         expect(event.isAppropriateListener(same(listener2))).andReturn(true);
165         event.processListener(same(listener2));
166 
167         _mocksControl.replay();
168         _testImpl.broadcast(event);
169         _mocksControl.verify();
170     }
171 
172     @Test(expectedExceptions = { NullPointerException.class })
173     public void testDecodeArgNPE() throws Exception
174     {
175         _testImpl.decode(null);
176     }
177 
178     @Test
179     public void testDecode() throws Exception
180     {
181         expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
182         _renderer.decode(same(_facesContext), same(_testImpl));
183         _mocksControl.replay();
184         _testImpl.decode(_facesContext);
185         _mocksControl.verify();
186     }
187 
188     @Test(expectedExceptions = { NullPointerException.class })
189     public void testEncodeBeginArgNPE() throws Exception
190     {
191         _testImpl.encodeBegin(null);
192     }
193 
194 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
195 //  easy mock to fix the test error.
196 //    @Test
197 //    public void testEncodeBegin() throws Exception
198 //    {
199 //        _testImpl.setRendered(false);
200 //        _mocksControl.replay();
201 //        _testImpl.encodeBegin(_facesContext);
202 //
203 //        _mocksControl.reset();
204 //        _testImpl.setRendered(true);
205 //        expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
206 //        _renderer.encodeBegin(same(_facesContext), same(_testImpl));
207 //        _mocksControl.replay();
208 //        _testImpl.encodeBegin(_facesContext);
209 //        _mocksControl.verify();
210 //    }
211 
212     @Test(expectedExceptions = { NullPointerException.class })
213     public void testEncodeChildrenArgNPE() throws Exception
214     {
215         _testImpl.encodeChildren(null);
216     }
217 
218     @Test
219     public void testEncodeChildren() throws Exception
220     {
221         _testImpl.setRendered(false);
222         _mocksControl.replay();
223         _testImpl.encodeChildren(_facesContext);
224 
225         _mocksControl.reset();
226         _testImpl.setRendered(true);
227         expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
228         _renderer.encodeChildren(same(_facesContext), same(_testImpl));
229         _mocksControl.replay();
230         _testImpl.encodeChildren(_facesContext);
231         _mocksControl.verify();
232     }
233 
234     @Test(expectedExceptions = { NullPointerException.class })
235     public void testEncodeEndArgNPE() throws Exception
236     {
237         _testImpl.encodeEnd(null);
238     }
239 
240 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
241 //  easy mock to fix the test error.
242 //    @Test
243 //    public void testEncodeEnd() throws Exception
244 //    {
245 //        _testImpl.setRendered(false);
246 //        _mocksControl.replay();
247 //        _testImpl.encodeEnd(_facesContext);
248 //
249 //        _mocksControl.reset();
250 //        _testImpl.setRendered(true);
251 //        expect(_testImpl.getRenderer(same(_facesContext))).andReturn(_renderer);
252 //        _renderer.encodeEnd(same(_facesContext), same(_testImpl));
253 //        _mocksControl.replay();
254 //        _testImpl.encodeEnd(_facesContext);
255 //        _mocksControl.verify();
256 //    }
257 
258     @Test(expectedExceptions = { NullPointerException.class })
259     public void testQueueEventArgNPE() throws Exception
260     {
261         _testImpl.queueEvent(null);
262     }
263 
264     @Test(expectedExceptions = { IllegalStateException.class })
265     public void testQueueEventWithoutParent() throws Exception
266     {
267         FacesEvent event = _mocksControl.createMock(FacesEvent.class);
268         expect(_testImpl.getParent()).andReturn(null);
269         _mocksControl.replay();
270         _testImpl.queueEvent(event);
271     }
272 
273     @Test
274     public void testQueueEvent() throws Exception
275     {
276         FacesEvent event = _mocksControl.createMock(FacesEvent.class);
277         UIComponent parent = _mocksControl.createMock(UIComponent.class);
278         expect(_testImpl.getParent()).andReturn(parent);
279         parent.queueEvent(same(event));
280         _mocksControl.replay();
281         _testImpl.queueEvent(event);
282         _mocksControl.verify();
283     }
284 
285     @Test(expectedExceptions = { NullPointerException.class })
286     public void testProcessDecodesArgNPE() throws Exception
287     {
288         _testImpl.processDecodes(null);
289     }
290 
291     @Test(expectedExceptions = { RuntimeException.class })
292     public void testProcessDecodesCallsRenderResoponseIfDecodeThrowsException()
293     {
294         List<UIComponent> emptyList = Collections.emptyList();
295         
296         expect(_testImpl.getFacetsAndChildren()).andReturn(emptyList.iterator());
297         _testImpl.decode(same(_facesContext));
298         expectLastCall().andThrow(new RuntimeException());
299         _facesContext.renderResponse();
300         _mocksControl.replay();
301         try
302         {
303             _testImpl.processDecodes(_facesContext);
304         }
305         finally
306         {
307             _mocksControl.verify();
308         }
309     }
310 
311     @Test(enabled=false)
312     public void testProcessDecodesWithRenderedFalse() throws Exception
313     {
314         _testImpl.setRendered(false);
315         _mocksControl.replay();
316         _testImpl.processDecodes(_facesContext);
317     }
318 
319 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
320 //  easy mock to fix the test error.
321 //    @Test
322 //    public void testProcessDecodesWithRenderedTrue() throws Exception
323 //    {
324 //        Collection<Method> methods = getMockedMethods();
325 //        methods.add(UIComponentBase.class.getDeclaredMethod("getFacetsAndChildren", (Class<?>[])null));
326 //        methods.add(UIComponentBase.class.getDeclaredMethod("decode", new Class[] { FacesContext.class }));
327 //        _testImpl = _mocksControl.createMock(UIComponentBase.class, methods.toArray(new Method[methods.size()]));
328 //        UIComponent child = _mocksControl.createMock(UIComponent.class);
329 //        expect(_testImpl.getFacetsAndChildren()).andReturn(Arrays.asList(new UIComponent[] { child }).iterator());
330 //        child.processDecodes(same(_facesContext));
331 //        _testImpl.decode(same(_facesContext));
332 //        _mocksControl.replay();
333 //        _testImpl.processDecodes(_facesContext);
334 //        _mocksControl.verify();
335 //    }
336 
337     @Test(expectedExceptions = { NullPointerException.class })
338     public void testProcessValidatorsArgNPE() throws Exception
339     {
340         _testImpl.processValidators(null);
341     }
342 
343     @Test(enabled=false)
344     public void testProcessValidatorsWithRenderedFalse() throws Exception
345     {
346         _testImpl.setRendered(false);
347         _mocksControl.replay();
348         _testImpl.processValidators(_facesContext);
349     }
350 
351 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
352 //  easy mock to fix the test error.
353 //    @Test
354 //    public void testProcessValidatorsWithRenderedTrue() throws Exception
355 //    {
356 //        UIComponent child = setupProcessXYZTest();
357 //        child.processValidators(same(_facesContext));
358 //        _mocksControl.replay();
359 //        _testImpl.processValidators(_facesContext);
360 //        _mocksControl.verify();
361 //    }
362 
363     private UIComponent setupProcessXYZTest() throws Exception
364     {
365         Collection<Method> methods = getMockedMethods();
366         methods.add(UIComponentBase.class.getDeclaredMethod("getFacetsAndChildren", (Class<?>[])null));
367         _testImpl = _mocksControl.createMock(UIComponentBase.class, methods.toArray(new Method[methods.size()]));
368         UIComponent child = _mocksControl.createMock(UIComponent.class);
369         expect(_testImpl.getFacetsAndChildren()).andReturn(Arrays.asList(new UIComponent[] { child }).iterator());
370         return child;
371     }
372 
373     @Test(expectedExceptions = { NullPointerException.class })
374     public void testProcessUpdatesArgNPE() throws Exception
375     {
376         _testImpl.processUpdates(null);
377     }
378 
379     @Test(enabled=false)
380     public void testProcessUpdatesWithRenderedFalse() throws Exception
381     {
382         _testImpl.setRendered(false);
383         _mocksControl.replay();
384         _testImpl.processUpdates(_facesContext);
385     }
386 
387 // FIXME: Need to add some expectation for FacesContext.getAttributes. I'll have to read a bit more about 
388 //  easy mock to fix the test error.
389 //    @Test
390 //    public void testProcessUpdatesWithRenderedTrue() throws Exception
391 //    {
392 //        UIComponent child = setupProcessXYZTest();
393 //        child.processUpdates(same(_facesContext));
394 //        _mocksControl.replay();
395 //        _testImpl.processUpdates(_facesContext);
396 //        _mocksControl.verify();
397 //    }
398 
399     @Factory
400     public Object[] createPropertyTests() throws Exception
401     {
402         return new Object[] {
403                 new AbstractUIComponentPropertyTest<Boolean>("rendered", true, new Boolean[] { false, true })
404                 {
405                     @Override
406                     protected UIComponent createComponent()
407                     {
408                         return getMocksControl().createMock(UIComponentBase.class, new Method[0]);
409                     }
410                 }, new AbstractUIComponentPropertyTest<String>("rendererType", null, new String[] { null, null })
411                 {
412                     @Override
413                     protected UIComponent createComponent()
414                     {
415                         return getMocksControl().createMock(UIComponentBase.class, new Method[0]);
416                     }
417                 } };
418     }
419 }