View Javadoc

1   package javax.faces.component;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.faces.model.DataModel;
7   import javax.faces.model.ListDataModel;
8   
9   import junit.framework.Test;
10  import junit.framework.TestSuite;
11  
12  import org.apache.myfaces.dummy.data.Data;
13  import org.jmock.Mock;
14  
15  public class InvokeOnComponentTest extends AbstractComponentTest
16  {
17    
18    Mock mock = null;
19    ContextCallback cc = null;
20  
21    public InvokeOnComponentTest(String arg0)
22    {
23        super(arg0);
24    }
25    
26    protected void setUp() throws Exception
27    {
28        super.setUp();
29        mock = mock(ContextCallback.class);
30        cc = (ContextCallback) mock.proxy();
31    }
32  
33    protected void tearDown() throws Exception
34    {
35        //mock.verify();
36        cc = null;
37        mock = null;
38        super.tearDown();
39    }
40    
41    public void atestInvokeOnComp() throws Exception
42    {
43      UIForm form = new UIForm();
44      UIInput i1 = new UIInput();
45      i1.setId("_id1");
46      UIInput i2 = new UIInput();
47      i2.setId("_id2");
48      UIInput i3 = new UIInput();
49      i3.setId("_id3");
50      UIInput i4 = new UIInput();
51      i4.setId("_id4");
52      form.getChildren().add(i1);
53      form.getChildren().add(i4);
54      form.getChildren().add(i2);
55      form.getChildren().add(i3);
56      this.facesContext.getViewRoot().getChildren().add(form);
57     
58      mock.expects(once()).method("invokeContextCallback").with(eq(facesContext), eq(i2));
59      mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(i1));
60      mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(i3));
61      mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(i4));
62      
63      this.facesContext.getViewRoot().invokeOnComponent(facesContext, i2.getClientId(facesContext), cc);
64      
65    }
66  
67    public void btestInvokeOnCompOnUIData() throws Exception
68    {
69      //column1
70      UIColumn c1 = new UIColumn();
71      c1.setId("col1");
72      
73      UIOutput headerFacet = new UIOutput();
74      headerFacet.setValue("HEADER");
75      headerFacet.setId("header");
76      c1.setHeader(headerFacet);
77      
78      UIOutput name = new UIOutput();
79      name.setValue("#{data.username}");
80      c1.getChildren().add(name);
81      
82      //column2
83      UIColumn c2 = new UIColumn();
84      c2.setId("col2");
85      
86      UIOutput secondheaderFacet = new UIOutput();
87      secondheaderFacet.setValue("New HEADER");
88      secondheaderFacet.setId("header2");
89      c2.setHeader(secondheaderFacet);
90      
91      UIOutput passwd = new UIOutput();
92      passwd.setValue("#{data.password}");
93      c2.getChildren().add(passwd);
94      
95      //main table
96      UIData table = new UIData();
97      table.setId("table");
98      
99      table.setVar("data");
100     
101     table.getChildren().add(c1);
102     table.getChildren().add(c2);
103 
104     DataModel model = new ListDataModel(createTestData());
105     table.setValue(model);
106     this.facesContext.getViewRoot().getChildren().add(table);
107     
108     //there should be no call on passwd yet, b/c for UIData the invokeOnComp isn't implemented yet...
109     mock.expects(once()).method("invokeContextCallback").with(eq(facesContext), eq(table));
110     mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(passwd));
111     mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(c1));
112     mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(name));
113 
114     this.facesContext.getViewRoot().invokeOnComponent(facesContext, table.getClientId(facesContext), cc);
115     
116   }
117 
118   public void testInvokeOnCompOnUIDataChildren() throws Exception
119   {
120     //column1
121     UIColumn c1 = new UIColumn();
122     c1.setId("col1");
123     
124     UIOutput headerFacet = new UIOutput();
125     headerFacet.setValue("HEADER");
126     headerFacet.setId("header");
127     c1.setHeader(headerFacet);
128     
129     UIOutput name = new UIOutput();
130     name.setValue("#{data.username}");
131     c1.getChildren().add(name);
132     
133     //column2
134     UIColumn c2 = new UIColumn();
135     c2.setId("col2");
136     
137     UIOutput secondheaderFacet = new UIOutput();
138     secondheaderFacet.setValue("New HEADER");
139     secondheaderFacet.setId("header2");
140     c2.setHeader(secondheaderFacet);
141     
142     UIOutput passwd = new UIOutput();
143     passwd.setValue("#{data.password}");
144     c2.getChildren().add(passwd);
145     
146     //main table
147     UIData table = new UIData();
148     table.setId("table");
149     
150     table.setVar("data");
151     
152     table.getChildren().add(c1);
153     table.getChildren().add(c2);
154 
155     DataModel model = new ListDataModel(createTestData());
156     table.setValue(model);
157     this.facesContext.getViewRoot().getChildren().add(table);
158     
159     System.out.println("RC; " +table.getRowCount());
160     table.encodeBegin(facesContext);
161     System.out.println("RC; " +table.getRowCount());
162     //there should be no call on passwd yet, b/c for UIData the invokeOnComp isn't implemented yet...
163     mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(table));
164     mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(passwd));
165     mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(c1));
166     mock.expects(never()).method("invokeContextCallback").with(eq(facesContext), eq(name));
167 
168     this.facesContext.getViewRoot().invokeOnComponent(facesContext, passwd.getClientId(facesContext), cc);
169     
170   }
171 
172   protected List<Data> createTestData()
173   {
174     List<Data> data = new ArrayList<Data>();
175     
176     
177     Data d1 = new Data();
178     d1.setPassword("secret");
179     d1.setUsername("mr fumakilla");
180     Data d2 = new Data();
181     d2.setPassword("top secret");
182     d2.setUsername("mr funk");
183     
184     data.add(d1);
185     data.add(d2);
186     
187     return data;
188   }
189   
190 
191   
192 }