1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.component;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.faces.application.StateManager;
29 import javax.faces.component.UIColumn;
30 import javax.faces.component.UIData;
31 import javax.faces.component.UIDataTest.RowData;
32 import javax.faces.component.UIInput;
33 import javax.faces.component.UIViewRoot;
34 import javax.faces.event.PhaseId;
35 import javax.faces.render.Renderer;
36 import static junit.framework.TestCase.assertEquals;
37
38 import org.apache.myfaces.Assert;
39 import org.apache.myfaces.TestRunner;
40 import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
41 import org.easymock.classextension.EasyMock;
42 import org.easymock.classextension.IMocksControl;
43 import org.junit.Test;
44
45 public class UIRepeatTest extends AbstractJsfTestCase
46 {
47
48 public UIRepeatTest()
49 {
50 super();
51 }
52
53 private IMocksControl _mocksControl;
54 private UIData _testImpl;
55
56 @Override
57 public void setUp() throws Exception
58 {
59 super.setUp();
60 _mocksControl = EasyMock.createControl();
61 _testImpl = new UIData();
62 }
63
64
65
66
67
68 public void testValueExpression()
69 {
70 assertSetValueExpressionException(IllegalArgumentException.class, "rowIndex");
71 assertSetValueExpressionException(NullPointerException.class, null);
72 }
73
74 private void assertSetValueExpressionException(Class<? extends Throwable> expected, final String name)
75 {
76 Assert.assertException(expected, new TestRunner()
77 {
78 public void run() throws Throwable
79 {
80 _testImpl.setValueExpression(name, null);
81 }
82 });
83 }
84
85
86
87
88
89
90
91
92
93
94
95 @Test
96 public void testSaveAndRestorePortletLifecycleWithoutPss1() throws Exception
97 {
98 List<RowData> model = new ArrayList<RowData>();
99 model.add(new RowData("text1","style1"));
100 model.add(new RowData("text2","style2"));
101 model.add(new RowData("text3","style3"));
102 model.add(new RowData("text4","style4"));
103
104
105 request.setAttribute("list", model);
106
107 UIViewRoot root = facesContext.getViewRoot();
108 createSimpleTable(root);
109 UIRepeat table = (UIRepeat) root.getChildren().get(0);
110
111 UIInput text = (UIInput) table.getChildren().get(0);
112
113 facesContext.setCurrentPhaseId(PhaseId.APPLY_REQUEST_VALUES);
114
115
116 for (int i = 0; i < model.size(); i++)
117 {
118 RowData rowData = model.get(i);
119 table.setRowIndex(i);
120 assertEquals(rowData.getText(), text.getValue());
121 text.setSubmittedValue("value"+(i+1));
122
123 }
124
125
126 table.setRowIndex(-1);
127
128 facesContext.setCurrentPhaseId(PhaseId.INVOKE_APPLICATION);
129
130 Object state = table.saveState(facesContext);
131
132 ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
133 ObjectOutputStream oos = new ObjectOutputStream(baos);
134 oos.writeObject(state);
135 oos.flush();
136 baos.flush();
137 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
138 ObjectInputStream ois = new ObjectInputStream(bais);
139 Object restoredState = (Object) ois.readObject();
140 oos.close();
141 ois.close();
142
143 facesContext.setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
144
145 facesContext.setViewRoot(new UIViewRoot());
146 root = facesContext.getViewRoot();
147 createSimpleTable(root);
148 table = (UIRepeat) root.getChildren().get(0);
149
150 text = (UIInput) table.getChildren().get(0);
151
152 table.restoreState(facesContext, restoredState);
153
154
155 for (int i = 0; i < model.size(); i++)
156 {
157 RowData rowData = model.get(i);
158 table.setRowIndex(i);
159 assertEquals("value"+(i+1), text.getSubmittedValue());
160
161 }
162 }
163
164 private void createSimpleTable(UIViewRoot root)
165 {
166 createSimpleTable(root, false);
167 }
168
169 private void createSimpleTable(UIViewRoot root, boolean rowStatePreserved)
170 {
171 UIRepeat table = new UIRepeat();
172
173 UIInput text = new UIInput();
174
175
176 root.setId(root.createUniqueId());
177 table.setId(root.createUniqueId());
178
179 text.setId(root.createUniqueId());
180
181 table.setVar("row");
182
183
184
185
186
187 table.setValueExpression("value", application.
188 getExpressionFactory().createValueExpression(
189 facesContext.getELContext(),"#{list}",List.class));
190
191 text.setValueExpression("value", application.
192 getExpressionFactory().createValueExpression(
193 facesContext.getELContext(),"#{row.text}",String.class));
194
195 root.getChildren().add(table);
196
197
198 table.getChildren().add(text);
199 }
200 }