1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.jsp.ui;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.struts2.views.jsp.AbstractUITagTest;
31 import org.apache.struts2.views.jsp.ParamTag;
32 import org.apache.struts2.TestAction;
33
34 import com.opensymphony.xwork2.Action;
35 import com.opensymphony.xwork2.ActionSupport;
36
37 /***
38 * FieldError Tag Test Case.
39 *
40 */
41 public class FieldErrorTagTest extends AbstractUITagTest {
42
43
44 public void testWithoutParamsWithFieldErrors() throws Exception {
45 FieldErrorTag tag = new FieldErrorTag();
46 ((InternalAction)action).setHaveFieldErrors(true);
47 tag.setPageContext(pageContext);
48 tag.doStartTag();
49 tag.doEndTag();
50
51 verify(FieldErrorTagTest.class.getResource("fielderror-1.txt"));
52 }
53
54 public void testWithoutParamsWithoutFieldErrors() throws Exception {
55 FieldErrorTag tag = new FieldErrorTag();
56 ((InternalAction)action).setHaveFieldErrors(false);
57 tag.setPageContext(pageContext);
58 tag.doStartTag();
59 tag.doEndTag();
60
61 verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
62 }
63
64 public void testFieldErrorsEscape() throws Exception {
65
66 FieldErrorTag tag = new FieldErrorTag();
67 TestAction testAction = new TestAction();
68 testAction.addFieldError("f", "<p>hey</p>");
69 stack.pop();
70 stack.push(testAction);
71 tag.setEscape(true);
72 tag.setPageContext(pageContext);
73 tag.doStartTag();
74 tag.doEndTag();
75
76 assertEquals(normalize("<ul class=\"errorMessage\"><li><span><p>hey</p></span></li></ul>", true),
77 normalize(writer.toString(), true));
78 }
79
80 public void testFieldErrorsDontEscape() throws Exception {
81
82 FieldErrorTag tag = new FieldErrorTag();
83 TestAction testAction = new TestAction();
84 testAction.addFieldError("f", "<p>hey</p>");
85 stack.pop();
86 stack.push(testAction);
87 tag.setEscape(false);
88 tag.setPageContext(pageContext);
89 tag.doStartTag();
90 tag.doEndTag();
91
92 assertEquals(normalize("<ul class=\"errorMessage\"><li><span><p>hey</p></span></li></ul>", true),
93 normalize(writer.toString(), true));
94 }
95
96 public void testWithParamsWithFieldErrors1() throws Exception {
97 FieldErrorTag tag = new FieldErrorTag();
98 tag.setId("someid");
99 ((InternalAction)action).setHaveFieldErrors(true);
100 tag.setPageContext(pageContext);
101 tag.doStartTag();
102 ParamTag pTag1 = new ParamTag();
103 pTag1.setPageContext(pageContext);
104 pTag1.setValue("%{'field1'}");
105 pTag1.doStartTag();
106 pTag1.doEndTag();
107
108 ParamTag pTag2 = new ParamTag();
109 pTag2.setPageContext(pageContext);
110 pTag2.setValue("%{'field3'}");
111 pTag2.doStartTag();
112 pTag2.doEndTag();
113
114 tag.doEndTag();
115
116 verify(FieldErrorTagTest.class.getResource("fielderror-3.txt"));
117 }
118
119 public void testWithFieldName() throws Exception {
120 FieldErrorTag tag = new FieldErrorTag();
121 tag.setFieldName("field1");
122 ((InternalAction)action).setHaveFieldErrors(true);
123 tag.setPageContext(pageContext);
124 tag.doStartTag();
125 tag.doEndTag();
126
127 verify(FieldErrorTagTest.class.getResource("fielderror-6.txt"));
128 }
129
130 public void testWithParamsWithFieldErrors2() throws Exception {
131 FieldErrorTag tag = new FieldErrorTag();
132 ((InternalAction)action).setHaveFieldErrors(true);
133 tag.setPageContext(pageContext);
134 tag.doStartTag();
135 ParamTag pTag1 = new ParamTag();
136 pTag1.setPageContext(pageContext);
137 pTag1.setValue("%{'field1'}");
138 pTag1.doStartTag();
139 pTag1.doEndTag();
140
141 ParamTag pTag2 = new ParamTag();
142 pTag2.setPageContext(pageContext);
143 pTag2.setValue("%{'field2'}");
144 pTag2.doStartTag();
145 pTag2.doEndTag();
146
147 tag.doEndTag();
148
149 verify(FieldErrorTagTest.class.getResource("fielderror-4.txt"));
150 }
151
152
153 public void testWithParamsWithFieldErrors3() throws Exception {
154 FieldErrorTag tag = new FieldErrorTag();
155 ((InternalAction)action).setHaveFieldErrors(true);
156 tag.setPageContext(pageContext);
157 tag.doStartTag();
158 ParamTag pTag1 = new ParamTag();
159 pTag1.setPageContext(pageContext);
160 pTag1.setValue("%{'field2'}");
161 pTag1.doStartTag();
162 pTag1.doEndTag();
163
164 tag.doEndTag();
165
166 verify(FieldErrorTagTest.class.getResource("fielderror-5.txt"));
167 }
168
169 public void testWithParamsWithoutFieldErrors1() throws Exception {
170 FieldErrorTag tag = new FieldErrorTag();
171 ((InternalAction)action).setHaveFieldErrors(false);
172 tag.setPageContext(pageContext);
173 tag.doStartTag();
174 ParamTag pTag1 = new ParamTag();
175 pTag1.setPageContext(pageContext);
176 pTag1.setValue("%{'field1'}");
177 pTag1.doStartTag();
178 pTag1.doEndTag();
179
180 ParamTag pTag2 = new ParamTag();
181 pTag2.setPageContext(pageContext);
182 pTag2.setValue("%{'field3'}");
183 pTag2.doStartTag();
184 pTag2.doEndTag();
185 tag.doEndTag();
186
187 verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
188 }
189
190 public void testWithParamsWithoutFieldErrors2() throws Exception {
191 FieldErrorTag tag = new FieldErrorTag();
192 ((InternalAction)action).setHaveFieldErrors(false);
193 tag.setPageContext(pageContext);
194 tag.doStartTag();
195 ParamTag pTag1 = new ParamTag();
196 pTag1.setPageContext(pageContext);
197 pTag1.setValue("%{'field1'}");
198 pTag1.doStartTag();
199 pTag1.doEndTag();
200
201 ParamTag pTag2 = new ParamTag();
202 pTag2.setPageContext(pageContext);
203 pTag2.setValue("%{'field3'}");
204 pTag2.doStartTag();
205 pTag2.doEndTag();
206 tag.doEndTag();
207
208 verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
209 }
210
211
212 public void testWithParamsWithoutFieldErrors3() throws Exception {
213 FieldErrorTag tag = new FieldErrorTag();
214 ((InternalAction)action).setHaveFieldErrors(false);
215 tag.setPageContext(pageContext);
216 tag.doStartTag();
217 ParamTag pTag1 = new ParamTag();
218 pTag1.setPageContext(pageContext);
219 pTag1.setValue("%{'field2'}");
220 pTag1.doStartTag();
221 pTag1.doEndTag();
222
223 tag.doEndTag();
224
225 verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
226 }
227
228 public void testWithNullFieldErrors() throws Exception {
229 FieldErrorTag tag = new FieldErrorTag();
230 ((InternalAction)action).setHaveFieldErrors(false);
231 ((InternalAction)action).setReturnNullForFieldErrors(true);
232 tag.setPageContext(pageContext);
233 tag.doStartTag();
234 ParamTag pTag1 = new ParamTag();
235 pTag1.setPageContext(pageContext);
236 pTag1.setValue("%{'field2'}");
237 pTag1.doStartTag();
238 pTag1.doEndTag();
239
240 tag.doEndTag();
241
242 verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
243 }
244
245
246 public Action getAction() {
247 return new InternalAction();
248 }
249
250
251 public class InternalAction extends ActionSupport {
252
253 private boolean haveFieldErrors = false;
254 private boolean returnNullForFieldErrors = false;
255
256 public void setHaveFieldErrors(boolean haveFieldErrors) {
257 this.haveFieldErrors = haveFieldErrors;
258 }
259
260 public void setReturnNullForFieldErrors(boolean returnNullForFieldErrors) {
261 this.returnNullForFieldErrors = returnNullForFieldErrors;
262 }
263
264 public Map<String, List<String>> getFieldErrors() {
265 if (haveFieldErrors) {
266 List err1 = new ArrayList();
267 err1.add("field error message number 1");
268 List err2 = new ArrayList();
269 err2.add("field error message number 2");
270 List err3 = new ArrayList();
271 err3.add("field error message number 3");
272 Map fieldErrors = new LinkedHashMap();
273 fieldErrors.put("field1", err1);
274 fieldErrors.put("field2", err2);
275 fieldErrors.put("field3", err3);
276 return fieldErrors;
277 }
278 else if (returnNullForFieldErrors) {
279 return null;
280 }
281 else {
282 return Collections.emptyMap();
283 }
284 }
285
286 public boolean hasFieldErrors() {
287 return haveFieldErrors;
288 }
289 }
290 }
291