View Javadoc

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.Collections;
7   
8   import javax.faces.context.FacesContext;
9   import javax.faces.FacesException;
10  
11  import org.testng.annotations.BeforeMethod;
12  import org.testng.annotations.Test;
13  import org.testng.Assert;
14  import org.apache.myfaces.TestRunner;
15  import org.easymock.EasyMock;
16  
17  /**
18       * Tests for
19   * {@link UIComponent#invokeOnComponent(javax.faces.context.FacesContext, String, ContextCallback)}.
20   */
21  public class UIComponentInvokeOnComponentTest extends UIComponentTestBase
22  {
23      private UIComponent _testimpl;
24      private ContextCallback _contextCallback;
25  
26      @Override
27      @BeforeMethod(alwaysRun = true)
28      protected void setUp() throws Exception
29      {
30          super.setUp();
31          Collection<Method> mockedMethods = new ArrayList<Method>();
32          Class<UIComponent> clazz = UIComponent.class;
33          mockedMethods.add(clazz.getDeclaredMethod("getClientId", new Class[] { FacesContext.class }));
34          mockedMethods.add(clazz.getDeclaredMethod("getFacetsAndChildren", null));
35  
36          _testimpl = _mocksControl.createMock(clazz, mockedMethods.toArray(new Method[mockedMethods.size()]));
37          _contextCallback = _mocksControl.createMock(ContextCallback.class);
38          _mocksControl.checkOrder(true);
39      }
40  
41      @Test
42      public void testInvokeOnComponentWithSameClientId() throws Exception
43      {
44          EasyMock.expect(_testimpl.getClientId(EasyMock.same(_facesContext))).andReturn("xxxId");
45          _contextCallback.invokeContextCallback(EasyMock.same(_facesContext), EasyMock.same(_testimpl));
46          _mocksControl.replay();
47          Assert.assertTrue(_testimpl.invokeOnComponent(_facesContext, "xxxId", _contextCallback));
48          _mocksControl.verify();
49      }
50  
51      @Test
52      public void testInvokeOnComponentWithException() throws Exception
53      {
54          EasyMock.expect(_testimpl.getClientId(EasyMock.same(_facesContext))).andReturn("xxxId");
55          _contextCallback.invokeContextCallback(EasyMock.same(_facesContext), EasyMock.same(_testimpl));
56          EasyMock.expectLastCall().andThrow(new RuntimeException());
57          _mocksControl.replay();
58          org.apache.myfaces.Assert.assertException(FacesException.class, new TestRunner()
59          {
60              public void run() throws Throwable
61              {
62                  Assert.assertTrue(_testimpl.invokeOnComponent(_facesContext, "xxxId", _contextCallback));
63              }
64          });
65      }
66  
67      @Test
68      public void testInvokeOnComponentAndNotFindComponentWithClientId() throws Exception
69      {
70          EasyMock.expect(_testimpl.getClientId(EasyMock.same(_facesContext))).andReturn("xxxId");
71          EasyMock.expect(_testimpl.getFacetsAndChildren()).andReturn(Collections.EMPTY_LIST.iterator());
72          _mocksControl.replay();
73          Assert.assertFalse(_testimpl.invokeOnComponent(_facesContext, "xxId", _contextCallback));
74          _mocksControl.verify();
75      }
76  
77      @Test
78      public void testInvokeOnComponentOnChild() throws Exception
79      {
80          EasyMock.expect(_testimpl.getClientId(EasyMock.same(_facesContext))).andReturn("xxxId");
81          String childId = "childId";
82          UIComponent child = _mocksControl.createMock(UIComponent.class);
83          EasyMock.expect(_testimpl.getFacetsAndChildren()).andReturn(Collections.singletonList(child).iterator());
84          EasyMock.expect(child.invokeOnComponent(EasyMock.same(_facesContext), EasyMock.eq(childId), EasyMock.same(_contextCallback))).andReturn(true);
85          _mocksControl.replay();
86          Assert.assertTrue(_testimpl.invokeOnComponent(_facesContext, "childId", _contextCallback));
87          _mocksControl.verify();
88      }
89  
90      @Test
91      public void testInvokeOnComponentExceptions() throws Exception
92      {
93          org.apache.myfaces.Assert.assertException(NullPointerException.class, new TestRunner()
94          {
95              public void run() throws Throwable
96              {
97                  _testimpl.invokeOnComponent(null, "xxx", _contextCallback);
98              }
99          });
100         org.apache.myfaces.Assert.assertException(NullPointerException.class, new TestRunner()
101         {
102             public void run() throws Throwable
103             {
104                 _testimpl.invokeOnComponent(_facesContext, null, _contextCallback);
105             }
106         });
107         org.apache.myfaces.Assert.assertException(NullPointerException.class, new TestRunner()
108         {
109             public void run() throws Throwable
110             {
111                 _testimpl.invokeOnComponent(_facesContext, "xxx", null);
112             }
113         });
114     }
115 }