View Javadoc

1   /*
2    * Copyright 2011 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package javax.faces.component;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import javax.faces.component.html.HtmlColumn;
21  import javax.faces.component.html.HtmlDataTable;
22  import javax.faces.component.html.HtmlInputText;
23  import javax.faces.component.html.HtmlOutputText;
24  import javax.faces.model.ListDataModel;
25  import javax.faces.render.Renderer;
26  import junit.framework.Assert;
27  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
28  import org.junit.Test;
29  
30  /**
31   *
32   * @author Leonardo Uribe
33   */
34  public class UIDataRowStateTest extends AbstractJsfTestCase
35  {
36      
37      public static class Item
38      {
39          
40          private Integer id;
41          private String name;
42          private String lastName;
43  
44          public Item(Integer id, String name, String lastName)
45          {
46              this.id = id;
47              this.name = name;
48              this.lastName = lastName;
49          }
50          /**
51           * @return the id
52           */
53          public Integer getId() {
54              return id;
55          }
56  
57          /**
58           * @param id the id to set
59           */
60          public void setId(Integer id) {
61              this.id = id;
62          }
63  
64          /**
65           * @return the name
66           */
67          public String getName() {
68              return name;
69          }
70  
71          /**
72           * @param name the name to set
73           */
74          public void setName(String name) {
75              this.name = name;
76          }
77  
78          /**
79           * @return the lastName
80           */
81          public String getLastName() {
82              return lastName;
83          }
84  
85          /**
86           * @param lastName the lastName to set
87           */
88          public void setLastName(String lastName) {
89              this.lastName = lastName;
90          }
91      }
92  
93      @Override
94      protected void setUpRenderKit() throws Exception {
95          super.setUpRenderKit();
96          renderKit.addRenderer(HtmlDataTable.COMPONENT_FAMILY, new HtmlDataTable().getRendererType(), new Renderer(){});
97          renderKit.addRenderer(HtmlOutputText.COMPONENT_FAMILY, new HtmlOutputText().getRendererType(), new Renderer(){});
98          renderKit.addRenderer(HtmlInputText.COMPONENT_FAMILY, new HtmlInputText().getRendererType(), new Renderer(){});
99      }
100     
101     /**
102      * 
103      */
104     @Test
105     public void testChangeIdsAfterSetRowIndex()
106     {
107         List<Item> list = new ArrayList<Item>();
108         int rowCount = 10;
109         for (int i = 0; i < rowCount; i++)
110         {
111             list.add(new Item(i, "name"+i, "lastName"+i));
112         }
113         
114         facesContext.getExternalContext().getRequestMap().put("items", list);
115         
116         UIViewRoot root = facesContext.getViewRoot();
117         UIData data = new HtmlDataTable();
118         data.setId("table");
119         root.getChildren().add(data);
120         data.setValue(new ListDataModel(list));
121         data.setVar("item");
122         data.setRows(rowCount);
123         
124         UIColumn col = new HtmlColumn();
125         data.getChildren().add(col);
126 
127         UIOutput text = new HtmlOutputText();
128         text.setId("text");
129         text.setValue(facesContext.getApplication().
130                 getExpressionFactory().createValueExpression(
131                 facesContext.getELContext(), "#{item.name}", String.class));
132         col.getChildren().add(text);
133         
134         for (int i = 0; i < rowCount ; i++)
135         {
136             data.setRowIndex(i);
137             Assert.assertEquals(data.getId()+":"+i+":"+text.getId(), text.getClientId());
138         }
139         data.setRowIndex(-1);
140         Assert.assertEquals(data.getId()+":"+text.getId(), text.getClientId());
141     }
142     
143     @Test
144     public void testChangeIdsAfterSetRowIndex2()
145     {
146         List<Item> list = new ArrayList<Item>();
147         int rowCount = 10;
148         for (int i = 0; i < rowCount; i++)
149         {
150             list.add(new Item(i, "name"+i, "lastName"+i));
151         }
152         
153         facesContext.getExternalContext().getRequestMap().put("items", list);
154         
155         UIViewRoot root = facesContext.getViewRoot();
156         UIData data = new HtmlDataTable();
157         data.setId("table");
158         root.getChildren().add(data);
159         data.setValue(new ListDataModel(list));
160         data.setVar("item");
161         data.setRows(rowCount);
162         
163         UIColumn col = new HtmlColumn();
164         data.getChildren().add(col);
165 
166         UIOutput text = new HtmlOutputText();
167         text.setId("text");
168         text.setValue(facesContext.getApplication().
169                 getExpressionFactory().createValueExpression(
170                 facesContext.getELContext(), "#{item.name}", String.class));
171         col.getChildren().add(text);
172         
173         UIInput inputText = new HtmlInputText();
174         inputText.setId("text");
175         inputText.setValue(facesContext.getApplication().
176                 getExpressionFactory().createValueExpression(
177                 facesContext.getELContext(), "#{item.lastName}", String.class));
178         col.getChildren().add(inputText);
179 
180         for (int i = 0; i < rowCount ; i++)
181         {
182             data.setRowIndex(i);
183             Assert.assertEquals(data.getId()+":"+i+":"+text.getId(), text.getClientId());
184             Assert.assertEquals(data.getId()+":"+i+":"+inputText.getId(), inputText.getClientId());
185         }
186         data.setRowIndex(-1);
187         Assert.assertEquals(data.getId()+":"+text.getId(), text.getClientId());
188         Assert.assertEquals(data.getId()+":"+inputText.getId(), inputText.getClientId());
189     }
190     
191     @Test
192     public void testAddRowAfterSetRowIndex()
193     {
194         List<Item> list = new ArrayList<Item>();
195         int rowCount = 10;
196 
197         facesContext.getExternalContext().getRequestMap().put("items", list);
198         
199         UIViewRoot root = facesContext.getViewRoot();
200         UIData data = new HtmlDataTable();
201         data.setId("table");
202         root.getChildren().add(data);
203         data.setValue(new ListDataModel(list));
204         data.setVar("item");
205         data.setRows(rowCount);
206         
207         UIColumn col = new HtmlColumn();
208         data.getChildren().add(col);
209 
210         UIOutput text = new HtmlOutputText();
211         text.setId("text");
212         text.setValue(facesContext.getApplication().
213                 getExpressionFactory().createValueExpression(
214                 facesContext.getELContext(), "#{item.name}", String.class));
215         col.getChildren().add(text);
216         
217         data.setRowIndex(-1);
218         
219         data.processDecodes(facesContext);
220         
221         for (int i = 0; i < rowCount; i++)
222         {
223             list.add(new Item(i, "name"+i, "lastName"+i));
224         }
225         
226         data.processDecodes(facesContext);
227     }
228     
229     /**
230      * Check if EditableValueHolder is being saved and restored.
231      */
232     @Test
233     public void testEditableValueHolderState()
234     {
235         List<Item> list = new ArrayList<Item>();
236         int rowCount = 10;
237         for (int i = 0; i < rowCount; i++)
238         {
239             list.add(new Item(i, "name"+i, "lastName"+i));
240         }
241         
242         facesContext.getExternalContext().getRequestMap().put("items", list);
243         
244         UIViewRoot root = facesContext.getViewRoot();
245         UIData data = new HtmlDataTable();
246         data.setId("table");
247         root.getChildren().add(data);
248         data.setValue(new ListDataModel(list));
249         data.setVar("item");
250         data.setRows(rowCount);
251         
252         UIColumn col = new HtmlColumn();
253         data.getChildren().add(col);
254 
255         UIOutput text = new HtmlOutputText();
256         text.setId("text");
257         text.setValue(facesContext.getApplication().
258                 getExpressionFactory().createValueExpression(
259                 facesContext.getELContext(), "#{item.name}", String.class));
260         col.getChildren().add(text);
261         
262         UIInput inputText = new HtmlInputText();
263         inputText.setId("text");
264         inputText.setValue(facesContext.getApplication().
265                 getExpressionFactory().createValueExpression(
266                 facesContext.getELContext(), "#{item.lastName}", String.class));
267         col.getChildren().add(inputText);
268 
269         for (int i = 0; i < rowCount ; i++)
270         {
271             data.setRowIndex(i);
272             inputText.setSubmittedValue("someString"+i);
273         }
274         data.setRowIndex(-1);
275         
276         for (int i = 0; i < rowCount ; i++)
277         {
278             data.setRowIndex(i);
279             Assert.assertEquals("someString"+i, inputText.getSubmittedValue());
280         }
281     }
282 }