1 package javax.faces.component;
2
3 import java.lang.reflect.Method;
4 import java.util.Collection;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import javax.faces.context.FacesContext;
9
10 import org.testng.annotations.BeforeMethod;
11 import org.testng.annotations.Test;
12 import org.apache.myfaces.TestRunner;
13 import org.apache.myfaces.Assert;
14 import org.easymock.EasyMock;
15
16
17
18
19 public class UIComponentEncodeAllTest extends UIComponentTestBase
20 {
21 private UIComponent _testimpl;
22
23 @Override
24 @BeforeMethod(alwaysRun = true)
25 protected void setUp() throws Exception
26 {
27 super.setUp();
28 Collection<Method> mockedMethods = new ArrayList<Method>();
29 Class<UIComponent> clazz = UIComponent.class;
30 mockedMethods.add(clazz.getDeclaredMethod("isRendered", null));
31 mockedMethods.add(clazz.getDeclaredMethod("encodeBegin", new Class[] { FacesContext.class }));
32 mockedMethods.add(clazz.getDeclaredMethod("getRendersChildren", null));
33 mockedMethods.add(clazz.getDeclaredMethod("encodeChildren", new Class[] { FacesContext.class }));
34 mockedMethods.add(clazz.getDeclaredMethod("getChildren", null));
35 mockedMethods.add(clazz.getDeclaredMethod("getChildCount", null));
36 mockedMethods.add(clazz.getDeclaredMethod("encodeEnd", new Class[] { FacesContext.class }));
37
38 _testimpl = _mocksControl.createMock(clazz, mockedMethods.toArray(new Method[mockedMethods.size()]));
39 _mocksControl.checkOrder(true);
40 }
41
42 @Test
43 public void testEncodeAllNullContext() throws Exception
44 {
45 Assert.assertException(NullPointerException.class, new TestRunner()
46 {
47 public void run() throws Throwable
48 {
49 _testimpl.encodeAll(null);
50 }
51 });
52 }
53
54 @Test
55 public void testEncodeAllNotRendered() throws Exception
56 {
57 EasyMock.expect(_testimpl.isRendered()).andReturn(false);
58 _mocksControl.replay();
59 _testimpl.encodeAll(_facesContext);
60 _mocksControl.verify();
61 }
62
63 @Test
64 public void testEncodeAllRenderesChildren() throws Exception
65 {
66 EasyMock.expect(_testimpl.isRendered()).andReturn(true);
67 _testimpl.encodeBegin(EasyMock.same(_facesContext));
68 EasyMock.expect(_testimpl.getRendersChildren()).andReturn(true);
69 _testimpl.encodeChildren(EasyMock.same(_facesContext));
70 _testimpl.encodeEnd(EasyMock.same(_facesContext));
71 _mocksControl.replay();
72 _testimpl.encodeAll(_facesContext);
73 _mocksControl.verify();
74 }
75
76 @Test
77 public void testEncodeAllNotRenderesChildren() throws Exception
78 {
79 EasyMock.expect(_testimpl.isRendered()).andReturn(true);
80 _testimpl.encodeBegin(EasyMock.same(_facesContext));
81 EasyMock.expect(_testimpl.getRendersChildren()).andReturn(false);
82
83 List<UIComponent> childs = new ArrayList<UIComponent>();
84 UIComponent testChild = _mocksControl.createMock(UIComponent.class);
85 childs.add(testChild);
86 EasyMock.expect(_testimpl.getChildCount()).andReturn(childs.size());
87 EasyMock.expect(_testimpl.getChildren()).andReturn(childs);
88 testChild.encodeAll(EasyMock.same(_facesContext));
89
90 _testimpl.encodeEnd(EasyMock.same(_facesContext));
91 _mocksControl.replay();
92 _testimpl.encodeAll(_facesContext);
93 _mocksControl.verify();
94 }
95 }