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  package org.apache.myfaces.component.html.ext;
20  
21  import java.io.IOException;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.List;
27  
28  import javax.faces.component.UIColumn;
29  import javax.faces.component.UIComponent;
30  import javax.faces.component.UIData;
31  import javax.faces.component.UIInput;
32  import javax.faces.component.UIOutput;
33  import javax.faces.component.UIPanel;
34  import javax.faces.component.UIViewRoot;
35  import javax.faces.context.FacesContext;
36  import javax.faces.el.ValueBinding;
37  import javax.faces.model.ListDataModel;
38  
39  import junit.framework.Test;
40  
41  import org.apache.myfaces.component.html.ext.HtmlDataTablePreserveRowComponentStateTest.RowData;
42  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
43  import org.apache.myfaces.test.utils.TestUtils;
44  
45  /**
46   * @author Mathias Br´┐Żkelmann (latest modification by $Author: paulsp $)
47   * @version $Revision: 491829 $ $Date: 2007-01-02 11:13:29 -0500 (mar, 02 ene 2007) $
48   */
49  public class HtmlDataTableTest extends AbstractTomahawkViewControllerTestCase
50  {
51  
52      private HtmlDataTable _dataTable;
53  
54      public HtmlDataTableTest(String name)
55      {
56          super(name);
57      }
58  
59      protected void setUp() throws Exception
60      {
61          super.setUp();
62          _dataTable = new HtmlDataTable();
63      }
64  
65      protected void tearDown() throws Exception
66      {
67          super.tearDown();
68          _dataTable = null;
69      }
70  
71      /*
72       * Test method for
73       * 'org.apache.myfaces.component.html.ext.HtmlDataTable.getClientId(FacesContext)'
74       */
75      public void testGetClientIdFacesContext()
76      {
77          assertEquals(-1, _dataTable.getRowIndex());
78          String baseClientId = _dataTable.getClientId(facesContext);
79          assertNotNull(baseClientId);
80          Collection rowClientIds = new HashSet();
81          for (int i = 0; i < 10; i++)
82          {
83              _dataTable.setRowIndex(i);
84              assertTrue("Duplicate client id while iterating rows", rowClientIds
85                      .add(_dataTable.getContainerClientId(facesContext)));
86          }
87          _dataTable.setRowIndex(-1);
88          assertEquals(baseClientId, _dataTable.getContainerClientId(facesContext));
89      }
90  
91      /*
92       * Test method for
93       * 'org.apache.myfaces.component.html.ext.HtmlDataTable.findComponent(String clientId)'
94       */
95      public void testFindComponent()
96      {
97          UIInput input = createInputInTree(facesContext);
98  
99          UIData data = (UIData) input.getParent().getParent();
100 
101         UIComponent comp = data.findComponent(":data:1:input");
102 
103         assertTrue(comp instanceof UIInput);
104 
105         UIInput uiInput = (UIInput) comp;
106 
107         assertEquals(uiInput.getValue(), "test2");
108 
109     }
110 
111     /**
112      * Verify component renders with the default renderer.
113      */
114     public void testDefaultRenderer()
115     {
116         // Define the component
117         UIData component = new HtmlDataTable();
118         component.setId("TestComponent");
119 
120         // Add rows to the table
121         List rows = new ArrayList();
122         rows.add(new TestData("test1", "test1"));
123         rows.add(new TestData("test2", "test2"));
124         rows.add(new TestData("test3", "test3"));
125         component.setValue(new ListDataModel(rows));
126 
127         // Render the component
128         try
129         {
130             TestUtils.renderComponent(facesContext, component);
131         }
132         catch (IOException e)
133         {
134             fail(e.getMessage());
135         }
136 
137         // Verify component was rendered
138         assertIdExists(component.getId());
139     }
140 
141     private UIInput createInputInTree(FacesContext context)
142     {
143 
144         UIData uiData = new HtmlDataTable();
145         uiData.setId("data");
146 
147         List li = new ArrayList();
148         li.add(new TestData("test1", "test1"));
149         li.add(new TestData("test2", "test2"));
150         li.add(new TestData("test3", "test3"));
151 
152         uiData.setValue(new ListDataModel(li));
153         uiData.setVar("detail");
154 
155         UIColumn column = new UIColumn();
156 
157         uiData.getChildren().add(column);
158 
159         UIInput input = new UIInput();
160         input.setId("input");
161         input.setValueBinding("value",
162                 createValueBinding("#{detail.description}"));
163 
164         column.getChildren().add(input);
165 
166         facesContext.getViewRoot().getChildren().add(uiData);
167 
168         return input;
169     }
170 
171     private ValueBinding createValueBinding(String expr)
172     {
173         return facesContext.getApplication().createValueBinding(expr);
174     }
175 
176     public static class TestData implements Serializable
177     {
178         private String _description;
179         private String _value;
180 
181         public TestData()
182         {
183 
184         }
185 
186         TestData(String description, String value)
187         {
188             _description = description;
189             _value = value;
190         }
191 
192         public String getDescription()
193         {
194             return _description;
195         }
196 
197         public void setDescription(String description)
198         {
199             _description = description;
200         }
201 
202         public String getValue()
203         {
204             return _value;
205         }
206 
207         public void setValue(String value)
208         {
209             _value = value;
210         }
211     }
212 
213     public void testDetailStampRowState1() throws Exception
214     {
215         List<RowData> model = new ArrayList<RowData>();
216         model.add(new RowData("text1","style1"));
217         model.add(new RowData("text1","style2"));
218         model.add(new RowData("text1","style3"));
219         model.add(new RowData("text1","style4"));
220         
221         //Put on request map to be resolved later
222         request.setAttribute("list", model);
223         
224         UIViewRoot root = facesContext.getViewRoot();
225         HtmlDataTable table = new HtmlDataTable();
226         UIColumn column = new UIColumn();
227         UIPanel detailStampPanel = new UIPanel();
228         UIOutput text = new UIOutput();
229         UIOutput detailStampText = new UIOutput();
230         
231         //This is only required if markInitiaState fix is not used 
232         root.setId("root");
233         table.setId("table");
234         detailStampPanel.setId("detailStamp");
235         column.setId("column");
236         text.setId("text");
237         detailStampText.setId("detailStampText");
238         
239         table.setVar("row");
240         table.setPreserveRowComponentState(true);
241         table.setValueExpression("value", application.
242                 getExpressionFactory().createValueExpression(
243                         facesContext.getELContext(),"#{list}",List.class));
244         
245         text.setValueExpression("value", application.
246                 getExpressionFactory().createValueExpression(
247                         facesContext.getELContext(),"#{row.text}",String.class));
248 
249         detailStampText.setValueExpression("value", application.
250                 getExpressionFactory().createValueExpression(
251                         facesContext.getELContext(),"#{row.text}",String.class));
252         
253         root.getChildren().add(table);
254         table.getChildren().add(column);
255         table.getFacets().put(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME, detailStampPanel);
256         column.getChildren().add(text);
257         detailStampPanel.getChildren().add(detailStampText);
258 
259         //Check the value expressions are working and change the component state 
260         for (int i = 0; i < model.size(); i++)
261         {
262             RowData rowData = model.get(i); 
263             table.setRowIndex(i);
264             assertEquals(rowData.getText(), text.getValue());
265             assertEquals(rowData.getText(), detailStampText.getValue());
266         }
267         
268         //Reset row index
269         table.setRowIndex(-1);
270 
271         //Check the values were not lost
272         for (int i = 0; i < model.size(); i++)
273         {
274             table.setRowIndex(i);
275             assertEquals(model.get(i).getText(), text.getValue());
276             assertEquals(model.get(i).getText(), detailStampText.getValue());
277         }
278         
279     }
280     
281     public void testDetailStampRowState2() throws Exception
282     {
283         List<RowData> model = new ArrayList<RowData>();
284         model.add(new RowData("text1","style1"));
285         model.add(new RowData("text1","style2"));
286         model.add(new RowData("text1","style3"));
287         model.add(new RowData("text1","style4"));
288         
289         //Put on request map to be resolved later
290         request.setAttribute("list", model);
291         
292         UIViewRoot root = facesContext.getViewRoot();
293         HtmlDataTable table = new HtmlDataTable();
294         UIColumn column = new UIColumn();
295         UIPanel detailStampPanel = new UIPanel();
296         UIOutput text = new UIOutput();
297         UIOutput detailStampText = new UIOutput();
298         
299         //This is only required if markInitiaState fix is not used 
300         root.setId("root");
301         table.setId("table");
302         detailStampPanel.setId("detailStamp");
303         column.setId("column");
304         text.setId("text");
305         detailStampText.setId("detailStampText");
306         
307         table.setVar("row");
308         table.setPreserveRowComponentState(true);
309         table.setValueExpression("value", application.
310                 getExpressionFactory().createValueExpression(
311                         facesContext.getELContext(),"#{list}",List.class));
312         
313         text.setValueExpression("value", application.
314                 getExpressionFactory().createValueExpression(
315                         facesContext.getELContext(),"#{row.text}",String.class));
316 
317         detailStampText.setValueExpression("value", application.
318                 getExpressionFactory().createValueExpression(
319                         facesContext.getELContext(),"#{row.text}",String.class));
320         
321         root.getChildren().add(table);
322         table.getChildren().add(column);
323         table.getFacets().put(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME, detailStampPanel);
324         column.getChildren().add(text);
325         detailStampPanel.getChildren().add(detailStampText);
326 
327         //Check the value expressions are working and change the component state 
328         for (int i = 0; i < model.size(); i++)
329         {
330             RowData rowData = model.get(i); 
331             table.setRowIndex(i);
332             assertEquals(rowData.getText(), text.getValue());
333             assertEquals(rowData.getText(), detailStampText.getValue());
334         }
335         
336         //Reset row index
337         table.setRowIndex(-1);
338 
339         //Remove a row
340         table.deleteRowStateForRow(1);
341         model.remove(1);
342 
343         //Check the values were not lost
344         for (int i = 0; i < model.size(); i++)
345         {
346             table.setRowIndex(i);
347             assertEquals(model.get(i).getText(), text.getValue());
348             assertEquals(model.get(i).getText(), detailStampText.getValue());
349         }
350     }
351 
352 }