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.util.ArrayList;
22  import java.util.List;
23  
24  import javax.faces.component.ContextCallback;
25  import javax.faces.component.UIColumn;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIOutput;
28  import javax.faces.component.UIPanel;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.context.FacesContext;
31  
32  import junit.framework.Assert;
33  
34  import org.apache.myfaces.test.base.AbstractJsfTestCase;
35  import org.apache.myfaces.test.utils.TestUtils;
36  
37  public class HtmlDataTablePreserveRowComponentStateTest extends AbstractJsfTestCase
38  {
39  
40      public HtmlDataTablePreserveRowComponentStateTest(String name)
41      {
42          super(name);
43      }
44      
45      @Override
46      protected void setUpRenderKit() throws Exception
47      {
48          super.setUpRenderKit();
49          TestUtils.addDefaultRenderers(facesContext);
50      }
51  
52      public static class RowData
53      {
54          private String text;
55  
56          public RowData(String text, String style)
57          {
58              super();
59              this.text = text;
60              this.style = style;
61          }
62  
63          private String style;
64          
65          public String getText()
66          {
67              return text;
68          }
69  
70          public void setText(String text)
71          {
72              this.text = text;
73          }
74  
75          public String getStyle()
76          {
77              return style;
78          }
79  
80          public void setStyle(String style)
81          {
82              this.style = style;
83          }
84      }
85      
86      public void testPreserveRowComponentState1() throws Exception
87      {
88          List<RowData> model = new ArrayList<RowData>();
89          model.add(new RowData("text1","style1"));
90          model.add(new RowData("text1","style2"));
91          model.add(new RowData("text1","style3"));
92          model.add(new RowData("text1","style4"));
93          
94          //Put on request map to be resolved later
95          request.setAttribute("list", model);
96          
97          UIViewRoot root = facesContext.getViewRoot();
98          HtmlDataTable table = new HtmlDataTable();
99          UIColumn column = new UIColumn();
100         UIOutput text = new UIOutput();
101         
102         //This is only required if markInitiaState fix is not used 
103         root.setId("root");
104         table.setId("table");
105         column.setId("column");
106         text.setId("text");
107         
108         table.setVar("row");
109         table.setPreserveRowComponentState(true);
110         table.setValueExpression("value", application.
111                 getExpressionFactory().createValueExpression(
112                         facesContext.getELContext(),"#{list}",List.class));
113         
114         text.setValueExpression("value", application.
115                 getExpressionFactory().createValueExpression(
116                         facesContext.getELContext(),"#{row.text}",String.class));
117         
118         root.getChildren().add(table);
119         table.getChildren().add(column);
120         column.getChildren().add(text);
121 
122         //Simulate markInitialState call.
123         facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
124         root.markInitialState();
125         table.markInitialState();
126         column.markInitialState();
127         text.markInitialState();
128         facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
129         
130         //Check the value expressions are working and change the component state 
131         for (int i = 0; i < model.size(); i++)
132         {
133             RowData rowData = model.get(i); 
134             table.setRowIndex(i);
135             assertEquals(rowData.getText(), text.getValue());
136             text.getAttributes().put("style", rowData.getStyle());
137         }
138         
139         //Reset row index
140         table.setRowIndex(-1);
141 
142         //Check the values were not lost
143         for (int i = 0; i < model.size(); i++)
144         {
145             table.setRowIndex(i);
146             assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
147         }
148         
149     }
150     
151     public void testPreserveRowComponentState2() throws Exception
152     {
153         List<RowData> model = new ArrayList<RowData>();
154         model.add(new RowData("text1","style1"));
155         model.add(new RowData("text1","style2"));
156         model.add(new RowData("text1","style3"));
157         model.add(new RowData("text1","style4"));
158         
159         //Put on request map to be resolved later
160         request.setAttribute("list", model);
161         
162         UIViewRoot root = facesContext.getViewRoot();
163         HtmlDataTable table = new HtmlDataTable();
164         UIColumn column = new UIColumn();
165         UIOutput text = new UIOutput();
166         
167         //This is only required if markInitiaState fix is not used 
168         root.setId("root");
169         table.setId("table");
170         column.setId("column");
171         text.setId("text");
172         
173         table.setVar("row");
174         table.setPreserveRowComponentState(true);
175         table.setValueExpression("value", application.
176                 getExpressionFactory().createValueExpression(
177                         facesContext.getELContext(),"#{list}",List.class));
178         
179         text.setValueExpression("value", application.
180                 getExpressionFactory().createValueExpression(
181                         facesContext.getELContext(),"#{row.text}",String.class));
182         
183         root.getChildren().add(table);
184         table.getChildren().add(column);
185         column.getChildren().add(text);
186 
187         //Simulate markInitialState call.
188         facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
189         root.markInitialState();
190         table.markInitialState();
191         column.markInitialState();
192         text.markInitialState();
193         facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
194         
195         //Check the value expressions are working and change the component state 
196         for (int i = 0; i < model.size(); i++)
197         {
198             RowData rowData = model.get(i); 
199             table.setRowIndex(i);
200             assertEquals(rowData.getText(), text.getValue());
201             text.getAttributes().put("style", rowData.getStyle());
202         }
203         
204         //Reset row index
205         table.setRowIndex(-1);
206 
207         //Remove a row
208         table.deleteRowStateForRow(1);
209         model.remove(1);
210 
211         //Check the values were not lost
212         for (int i = 0; i < model.size(); i++)
213         {
214             table.setRowIndex(i);
215             assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
216         }
217         
218     }
219 
220     public void testPreserveRowComponentStateDetailStamp1() throws Exception
221     {
222         List<RowData> model = new ArrayList<RowData>();
223         model.add(new RowData("text1","style1"));
224         model.add(new RowData("text1","style2"));
225         model.add(new RowData("text1","style3"));
226         model.add(new RowData("text1","style4"));
227         
228         //Put on request map to be resolved later
229         request.setAttribute("list", model);
230         
231         UIViewRoot root = facesContext.getViewRoot();
232         HtmlDataTable table = new HtmlDataTable();
233         UIColumn column = new UIColumn();
234         UIPanel detailStampPanel = new UIPanel();
235         UIOutput text = new UIOutput();
236         UIOutput detailStampText = new UIOutput();
237         
238         //This is only required if markInitiaState fix is not used 
239         root.setId("root");
240         table.setId("table");
241         detailStampPanel.setId("detailStamp");
242         column.setId("column");
243         text.setId("text");
244         detailStampText.setId("detailStampText");
245         
246         table.setVar("row");
247         table.setPreserveRowComponentState(true);
248         table.setValueExpression("value", application.
249                 getExpressionFactory().createValueExpression(
250                         facesContext.getELContext(),"#{list}",List.class));
251         
252         text.setValueExpression("value", application.
253                 getExpressionFactory().createValueExpression(
254                         facesContext.getELContext(),"#{row.text}",String.class));
255 
256         detailStampText.setValueExpression("value", application.
257                 getExpressionFactory().createValueExpression(
258                         facesContext.getELContext(),"#{row.text}",String.class));
259         
260         root.getChildren().add(table);
261         table.getChildren().add(column);
262         table.getFacets().put(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME, detailStampPanel);
263         column.getChildren().add(text);
264         detailStampPanel.getChildren().add(detailStampText);
265 
266         //Simulate markInitialState call.
267         facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
268         root.markInitialState();
269         table.markInitialState();
270         detailStampPanel.markInitialState();
271         detailStampText.markInitialState();
272         column.markInitialState();
273         text.markInitialState();
274         facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
275         
276         //Check the value expressions are working and change the component state 
277         for (int i = 0; i < model.size(); i++)
278         {
279             RowData rowData = model.get(i); 
280             table.setRowIndex(i);
281             assertEquals(rowData.getText(), text.getValue());
282             assertEquals(rowData.getText(), detailStampText.getValue());
283             text.getAttributes().put("style", rowData.getStyle());
284             detailStampText.getAttributes().put("style", rowData.getStyle());
285         }
286         
287         //Reset row index
288         table.setRowIndex(-1);
289 
290         //Check the values were not lost
291         for (int i = 0; i < model.size(); i++)
292         {
293             table.setRowIndex(i);
294             assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
295             assertEquals(model.get(i).getStyle(), detailStampText.getAttributes().get("style"));
296         }
297         
298     }
299 
300     public void testPreserveRowComponentStateDetailStamp2() throws Exception
301     {
302         List<RowData> model = new ArrayList<RowData>();
303         model.add(new RowData("text1","style1"));
304         model.add(new RowData("text1","style2"));
305         model.add(new RowData("text1","style3"));
306         model.add(new RowData("text1","style4"));
307         
308         //Put on request map to be resolved later
309         request.setAttribute("list", model);
310         
311         UIViewRoot root = facesContext.getViewRoot();
312         HtmlDataTable table = new HtmlDataTable();
313         UIColumn column = new UIColumn();
314         UIPanel detailStampPanel = new UIPanel();
315         UIOutput text = new UIOutput();
316         UIOutput detailStampText = new UIOutput();
317         
318         //This is only required if markInitiaState fix is not used 
319         root.setId("root");
320         table.setId("table");
321         detailStampPanel.setId("detailStamp");
322         column.setId("column");
323         text.setId("text");
324         detailStampText.setId("detailStampText");
325         
326         table.setVar("row");
327         table.setPreserveRowComponentState(true);
328         table.setValueExpression("value", application.
329                 getExpressionFactory().createValueExpression(
330                         facesContext.getELContext(),"#{list}",List.class));
331         
332         text.setValueExpression("value", application.
333                 getExpressionFactory().createValueExpression(
334                         facesContext.getELContext(),"#{row.text}",String.class));
335 
336         detailStampText.setValueExpression("value", application.
337                 getExpressionFactory().createValueExpression(
338                         facesContext.getELContext(),"#{row.text}",String.class));
339         
340         root.getChildren().add(table);
341         table.getChildren().add(column);
342         table.getFacets().put(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME, detailStampPanel);
343         column.getChildren().add(text);
344         detailStampPanel.getChildren().add(detailStampText);
345 
346         //Simulate markInitialState call.
347         facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
348         root.markInitialState();
349         table.markInitialState();
350         detailStampPanel.markInitialState();
351         detailStampText.markInitialState();
352         column.markInitialState();
353         text.markInitialState();
354         facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
355         
356         //Check the value expressions are working and change the component state 
357         for (int i = 0; i < model.size(); i++)
358         {
359             RowData rowData = model.get(i); 
360             table.setRowIndex(i);
361             assertEquals(rowData.getText(), text.getValue());
362             assertEquals(rowData.getText(), detailStampText.getValue());
363             text.getAttributes().put("style", rowData.getStyle());
364             detailStampText.getAttributes().put("style", rowData.getStyle());
365         }
366         
367         //Reset row index
368         table.setRowIndex(-1);
369 
370         //Remove a row
371         table.deleteRowStateForRow(1);
372         model.remove(1);
373 
374         //Check the values were not lost
375         for (int i = 0; i < model.size(); i++)
376         {
377             table.setRowIndex(i);
378             assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
379             assertEquals(model.get(i).getStyle(), detailStampText.getAttributes().get("style"));
380         }
381         
382     }
383     
384     public void testPreserveRowComponentStateDetailStamp3() throws Exception
385     {
386         List<RowData> model = new ArrayList<RowData>();
387         model.add(new RowData("text1","style1"));
388         model.add(new RowData("text2","style2"));
389         model.add(new RowData("text3","style3"));
390         model.add(new RowData("text4","style4"));
391         
392         //Put on request map to be resolved later
393         request.setAttribute("list", model);
394         
395         UIViewRoot root = facesContext.getViewRoot();
396         HtmlDataTable table = new HtmlDataTable();
397         UIColumn column = new UIColumn();
398         UIPanel detailStampPanel = new UIPanel();
399         UIOutput text = new UIOutput();
400         UIOutput detailStampText = new UIOutput();
401         
402         //This is only required if markInitiaState fix is not used 
403         root.setId("root");
404         table.setId("table");
405         detailStampPanel.setId("detailStamp");
406         column.setId("column");
407         text.setId("text");
408         detailStampText.setId("detailStampText");
409         
410         table.setVar("row");
411         table.setPreserveRowComponentState(true);
412         table.setValueExpression("value", application.
413                 getExpressionFactory().createValueExpression(
414                         facesContext.getELContext(),"#{list}",List.class));
415         
416         table.setValueExpression("rowKey", application.
417                 getExpressionFactory().createValueExpression(
418                         facesContext.getELContext(),"#{row.text}",String.class));
419         
420         text.setValueExpression("value", application.
421                 getExpressionFactory().createValueExpression(
422                         facesContext.getELContext(),"#{row.text}",String.class));
423 
424         detailStampText.setValueExpression("value", application.
425                 getExpressionFactory().createValueExpression(
426                         facesContext.getELContext(),"#{row.text}",String.class));
427         
428         root.getChildren().add(table);
429         table.getChildren().add(column);
430         table.getFacets().put(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME, detailStampPanel);
431         column.getChildren().add(text);
432         detailStampPanel.getChildren().add(detailStampText);
433 
434         //Simulate markInitialState call.
435         facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
436         root.markInitialState();
437         table.markInitialState();
438         detailStampPanel.markInitialState();
439         detailStampText.markInitialState();
440         column.markInitialState();
441         text.markInitialState();
442         facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
443         
444         //Check the value expressions are working and change the component state 
445         for (int i = 0; i < model.size(); i++)
446         {
447             RowData rowData = model.get(i); 
448             table.setRowIndex(i);
449             assertEquals(rowData.getText(), text.getValue());
450             assertEquals(rowData.getText(), detailStampText.getValue());
451             text.getAttributes().put("style", rowData.getStyle());
452             detailStampText.getAttributes().put("style", rowData.getStyle());
453         }
454         
455         //Reset row index
456         table.setRowIndex(-1);
457 
458         //Remove a row
459         table.deleteRowStateForRow(1);
460         model.remove(1);
461 
462         //Check the values were not lost
463         for (int i = 0; i < model.size(); i++)
464         {
465             table.setRowIndex(i);
466             assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
467             assertEquals(model.get(i).getStyle(), detailStampText.getAttributes().get("style"));
468         }
469         
470     }    
471 
472     public void testEncodeDecodeSubId() throws Exception
473     {
474         String test = "someKey_+"+((char)6789);
475         String resp = _SubIdConverter.encode(test);
476         String test2 = _SubIdConverter.decode(resp);
477         //System.out.println(resp+" "+Integer.toHexString(6789));
478         assertEquals(test, test2);
479     }
480     
481     public void testPreserveRowComponentStateDetailStamp4() throws Exception
482     {
483         List<RowData> model = new ArrayList<RowData>();
484         model.add(new RowData("text1","style1"));
485         model.add(new RowData("text2","style2"));
486         model.add(new RowData("text3","style3"));
487         model.add(new RowData("text4","style4"));
488         
489         //Put on request map to be resolved later
490         request.setAttribute("list", model);
491         
492         UIViewRoot root = facesContext.getViewRoot();
493         HtmlDataTable table = new HtmlDataTable();
494         UIColumn column = new UIColumn();
495         UIPanel detailStampPanel = new UIPanel();
496         UIOutput text = new UIOutput();
497         UIOutput detailStampText = new UIOutput();
498         
499         //This is only required if markInitiaState fix is not used 
500         root.setId("root");
501         table.setId("table");
502         detailStampPanel.setId("detailStamp");
503         column.setId("column");
504         text.setId("text");
505         detailStampText.setId("detailStampText");
506         
507         table.setVar("row");
508         table.setPreserveRowComponentState(true);
509         table.setValueExpression("value", application.
510                 getExpressionFactory().createValueExpression(
511                         facesContext.getELContext(),"#{list}",List.class));
512         
513         table.setValueExpression("rowKey", application.
514                 getExpressionFactory().createValueExpression(
515                         facesContext.getELContext(),"#{row.text}",String.class));
516         
517         text.setValueExpression("value", application.
518                 getExpressionFactory().createValueExpression(
519                         facesContext.getELContext(),"#{row.text}",String.class));
520 
521         detailStampText.setValueExpression("value", application.
522                 getExpressionFactory().createValueExpression(
523                         facesContext.getELContext(),"#{row.text}",String.class));
524         
525         root.getChildren().add(table);
526         table.getChildren().add(column);
527         table.getFacets().put(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME, detailStampPanel);
528         column.getChildren().add(text);
529         detailStampPanel.getChildren().add(detailStampText);
530 
531         //Simulate markInitialState call.
532         facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
533         root.markInitialState();
534         table.markInitialState();
535         detailStampPanel.markInitialState();
536         detailStampText.markInitialState();
537         column.markInitialState();
538         text.markInitialState();
539         facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
540         
541         root.invokeOnComponent(facesContext, "table:r_id_text2:text", new ContextCallback()
542         {
543             public void invokeContextCallback(FacesContext context, UIComponent target)
544             {
545                 Assert.assertEquals("text2", target.getAttributes().get("value"));
546             }
547         });
548         
549         root.invokeOnComponent(facesContext, "table:r_id_text3:detailStampText", new ContextCallback()
550         {
551             public void invokeContextCallback(FacesContext context, UIComponent target)
552             {
553                 Assert.assertEquals("text3", target.getAttributes().get("value"));
554             }
555         });
556         
557     }
558 
559 }