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