1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.datalist;
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 HtmlDataListPreserveRowComponentStateTest extends AbstractJsfTestCase
38 {
39
40 public HtmlDataListPreserveRowComponentStateTest(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
95 request.setAttribute("list", model);
96
97 UIViewRoot root = facesContext.getViewRoot();
98 HtmlDataList table = new HtmlDataList();
99
100 UIOutput text = new UIOutput();
101
102
103 root.setId("root");
104 table.setId("table");
105
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
120
121 table.getChildren().add(text);
122
123
124 facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
125 root.markInitialState();
126 table.markInitialState();
127
128 text.markInitialState();
129 facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
130
131
132 for (int i = 0; i < model.size(); i++)
133 {
134 RowData rowData = model.get(i);
135 table.setRowIndex(i);
136 assertEquals(rowData.getText(), text.getValue());
137 text.getAttributes().put("style", rowData.getStyle());
138 }
139
140
141 table.setRowIndex(-1);
142
143
144 for (int i = 0; i < model.size(); i++)
145 {
146 table.setRowIndex(i);
147 assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
148 }
149
150 }
151
152 public void testPreserveRowComponentState1Facet() throws Exception
153 {
154 List<RowData> model = new ArrayList<RowData>();
155 model.add(new RowData("text1","style1"));
156 model.add(new RowData("text1","style2"));
157 model.add(new RowData("text1","style3"));
158 model.add(new RowData("text1","style4"));
159
160
161 request.setAttribute("list", model);
162
163 UIViewRoot root = facesContext.getViewRoot();
164 HtmlDataList table = new HtmlDataList();
165 UIPanel panel = new UIPanel();
166 UIOutput text = new UIOutput();
167
168
169 root.setId("root");
170 table.setId("table");
171 panel.setId("column");
172 text.setId("text");
173
174 table.setVar("row");
175 table.setPreserveRowComponentState(true);
176 table.setValueExpression("value", application.
177 getExpressionFactory().createValueExpression(
178 facesContext.getELContext(),"#{list}",List.class));
179
180 text.setValueExpression("value", application.
181 getExpressionFactory().createValueExpression(
182 facesContext.getELContext(),"#{row.text}",String.class));
183
184 root.getChildren().add(table);
185 table.getChildren().add(panel);
186 panel.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, text);
187
188
189 facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
190 root.markInitialState();
191 table.markInitialState();
192 panel.markInitialState();
193 text.markInitialState();
194 facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
195
196
197 for (int i = 0; i < model.size(); i++)
198 {
199 RowData rowData = model.get(i);
200 table.setRowIndex(i);
201 assertEquals(rowData.getText(), text.getValue());
202 text.getAttributes().put("style", rowData.getStyle());
203 }
204
205
206 table.setRowIndex(-1);
207
208
209 for (int i = 0; i < model.size(); i++)
210 {
211 table.setRowIndex(i);
212 assertEquals(model.get(i).getText(), text.getAttributes().get("value"));
213 assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
214 }
215
216 }
217
218 public void testState1Facet() throws Exception
219 {
220 List<RowData> model = new ArrayList<RowData>();
221 model.add(new RowData("text1","style1"));
222 model.add(new RowData("text1","style2"));
223 model.add(new RowData("text1","style3"));
224 model.add(new RowData("text1","style4"));
225
226
227 request.setAttribute("list", model);
228
229 UIViewRoot root = facesContext.getViewRoot();
230 HtmlDataList table = new HtmlDataList();
231 UIPanel panel = new UIPanel();
232 UIOutput text = new UIOutput();
233
234
235 root.setId("root");
236 table.setId("table");
237 panel.setId("column");
238 text.setId("text");
239
240 table.setVar("row");
241
242 table.setValueExpression("value", application.
243 getExpressionFactory().createValueExpression(
244 facesContext.getELContext(),"#{list}",List.class));
245
246 text.setValueExpression("value", application.
247 getExpressionFactory().createValueExpression(
248 facesContext.getELContext(),"#{row.text}",String.class));
249
250 root.getChildren().add(table);
251 table.getChildren().add(panel);
252 panel.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, text);
253
254
255 facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
256 root.markInitialState();
257 table.markInitialState();
258 panel.markInitialState();
259 text.markInitialState();
260 facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
261
262
263 for (int i = 0; i < model.size(); i++)
264 {
265 RowData rowData = model.get(i);
266 table.setRowIndex(i);
267 assertEquals(rowData.getText(), text.getValue());
268 text.getAttributes().put("style", rowData.getStyle());
269 }
270
271
272 table.setRowIndex(-1);
273
274
275 for (int i = 0; i < model.size(); i++)
276 {
277 table.setRowIndex(i);
278 assertEquals(model.get(i).getText(), text.getAttributes().get("value"));
279 }
280
281 }
282
283 public void testPreserveRowComponentState2() throws Exception
284 {
285 List<RowData> model = new ArrayList<RowData>();
286 model.add(new RowData("text1","style1"));
287 model.add(new RowData("text1","style2"));
288 model.add(new RowData("text1","style3"));
289 model.add(new RowData("text1","style4"));
290
291
292 request.setAttribute("list", model);
293
294 UIViewRoot root = facesContext.getViewRoot();
295 HtmlDataList table = new HtmlDataList();
296 UIPanel column = new UIPanel();
297 UIOutput text = new UIOutput();
298
299
300 root.setId("root");
301 table.setId("table");
302 column.setId("column");
303 text.setId("text");
304
305 table.setVar("row");
306 table.setPreserveRowComponentState(true);
307 table.setValueExpression("value", application.
308 getExpressionFactory().createValueExpression(
309 facesContext.getELContext(),"#{list}",List.class));
310
311 text.setValueExpression("value", application.
312 getExpressionFactory().createValueExpression(
313 facesContext.getELContext(),"#{row.text}",String.class));
314
315 root.getChildren().add(table);
316 table.getChildren().add(column);
317 column.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, text);
318
319
320 facesContext.getAttributes().put("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE", Boolean.TRUE);
321 root.markInitialState();
322 table.markInitialState();
323 column.markInitialState();
324 text.markInitialState();
325 facesContext.getAttributes().remove("javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE");
326
327
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 text.getAttributes().put("style", rowData.getStyle());
334 }
335
336
337 table.setRowIndex(-1);
338
339
340 table.deleteRowStateForRow(1);
341 model.remove(1);
342
343
344 for (int i = 0; i < model.size(); i++)
345 {
346 table.setRowIndex(i);
347 assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
348 }
349
350 }
351
352 }