1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
103
104 @Test
105 public void testGetAttributes()
106 {
107
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
319 public void testProcessDecodesWithRenderedFalse() throws Exception
320 {
321 _testImpl.setRendered(false);
322 _mocksControl.replay();
323 _testImpl.processDecodes(_facesContext);
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344 @Test(expected = NullPointerException.class )
345 public void testProcessValidatorsArgNPE() throws Exception
346 {
347 _testImpl.processValidators(null);
348 }
349
350
351 public void testProcessValidatorsWithRenderedFalse() throws Exception
352 {
353 _testImpl.setRendered(false);
354 _mocksControl.replay();
355 _testImpl.processValidators(_facesContext);
356 }
357
358
359
360
361
362
363
364
365
366
367
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
387 public void testProcessUpdatesWithRenderedFalse() throws Exception
388 {
389 _testImpl.setRendered(false);
390 _mocksControl.replay();
391 _testImpl.processUpdates(_facesContext);
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 }