View Javadoc

1   /*
2    * $Id: FieldErrorTagTest.java 722198 2008-12-01 19:49:00Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  
33  import com.opensymphony.xwork2.Action;
34  import com.opensymphony.xwork2.ActionSupport;
35  
36  /***
37   * FieldError Tag Test Case.
38   *
39   */
40  public class FieldErrorTagTest extends AbstractUITagTest {
41  
42  
43      public void testWithoutParamsWithFieldErrors() throws Exception {
44          FieldErrorTag tag = new FieldErrorTag();
45          ((InternalAction)action).setHaveFieldErrors(true);
46          tag.setPageContext(pageContext);
47          tag.doStartTag();
48          tag.doEndTag();
49  
50          verify(FieldErrorTagTest.class.getResource("fielderror-1.txt"));
51      }
52  
53      public void testWithoutParamsWithoutFieldErrors() throws Exception {
54          FieldErrorTag tag = new FieldErrorTag();
55          ((InternalAction)action).setHaveFieldErrors(false);
56          tag.setPageContext(pageContext);
57          tag.doStartTag();
58          tag.doEndTag();
59  
60          verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
61      }
62  
63      public void testWithParamsWithFieldErrors1() throws Exception {
64          FieldErrorTag tag = new FieldErrorTag();
65          ((InternalAction)action).setHaveFieldErrors(true);
66          tag.setPageContext(pageContext);
67          tag.doStartTag();
68              ParamTag pTag1 = new ParamTag();
69              pTag1.setPageContext(pageContext);
70              pTag1.setValue("%{'field1'}");
71              pTag1.doStartTag();
72              pTag1.doEndTag();
73  
74              ParamTag pTag2 = new ParamTag();
75              pTag2.setPageContext(pageContext);
76              pTag2.setValue("%{'field3'}");
77              pTag2.doStartTag();
78              pTag2.doEndTag();
79  
80          tag.doEndTag();
81  
82          verify(FieldErrorTagTest.class.getResource("fielderror-3.txt"));
83      }
84  
85       public void testWithFieldName() throws Exception {
86          FieldErrorTag tag = new FieldErrorTag();
87          tag.setFieldName("field1");
88          ((InternalAction)action).setHaveFieldErrors(true);
89          tag.setPageContext(pageContext);
90          tag.doStartTag();
91          tag.doEndTag();
92  
93          verify(FieldErrorTagTest.class.getResource("fielderror-6.txt"));
94      }
95  
96      public void testWithParamsWithFieldErrors2() throws Exception {
97          FieldErrorTag tag = new FieldErrorTag();
98          ((InternalAction)action).setHaveFieldErrors(true);
99          tag.setPageContext(pageContext);
100         tag.doStartTag();
101             ParamTag pTag1 = new ParamTag();
102             pTag1.setPageContext(pageContext);
103             pTag1.setValue("%{'field1'}");
104             pTag1.doStartTag();
105             pTag1.doEndTag();
106 
107             ParamTag pTag2 = new ParamTag();
108             pTag2.setPageContext(pageContext);
109             pTag2.setValue("%{'field2'}");
110             pTag2.doStartTag();
111             pTag2.doEndTag();
112 
113         tag.doEndTag();
114 
115         verify(FieldErrorTagTest.class.getResource("fielderror-4.txt"));
116     }
117 
118 
119     public void testWithParamsWithFieldErrors3() throws Exception {
120         FieldErrorTag tag = new FieldErrorTag();
121         ((InternalAction)action).setHaveFieldErrors(true);
122         tag.setPageContext(pageContext);
123         tag.doStartTag();
124             ParamTag pTag1 = new ParamTag();
125             pTag1.setPageContext(pageContext);
126             pTag1.setValue("%{'field2'}");
127             pTag1.doStartTag();
128             pTag1.doEndTag();
129 
130         tag.doEndTag();
131 
132         verify(FieldErrorTagTest.class.getResource("fielderror-5.txt"));
133     }
134 
135     public void testWithParamsWithoutFieldErrors1() throws Exception {
136         FieldErrorTag tag = new FieldErrorTag();
137         ((InternalAction)action).setHaveFieldErrors(false);
138         tag.setPageContext(pageContext);
139         tag.doStartTag();
140             ParamTag pTag1 = new ParamTag();
141             pTag1.setPageContext(pageContext);
142             pTag1.setValue("%{'field1'}");
143             pTag1.doStartTag();
144             pTag1.doEndTag();
145 
146             ParamTag pTag2 = new ParamTag();
147             pTag2.setPageContext(pageContext);
148             pTag2.setValue("%{'field3'}");
149             pTag2.doStartTag();
150             pTag2.doEndTag();
151         tag.doEndTag();
152 
153         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
154     }
155 
156     public void testWithParamsWithoutFieldErrors2() throws Exception {
157         FieldErrorTag tag = new FieldErrorTag();
158         ((InternalAction)action).setHaveFieldErrors(false);
159         tag.setPageContext(pageContext);
160         tag.doStartTag();
161             ParamTag pTag1 = new ParamTag();
162             pTag1.setPageContext(pageContext);
163             pTag1.setValue("%{'field1'}");
164             pTag1.doStartTag();
165             pTag1.doEndTag();
166 
167             ParamTag pTag2 = new ParamTag();
168             pTag2.setPageContext(pageContext);
169             pTag2.setValue("%{'field3'}");
170             pTag2.doStartTag();
171             pTag2.doEndTag();
172         tag.doEndTag();
173 
174         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
175     }
176 
177 
178     public void testWithParamsWithoutFieldErrors3() throws Exception {
179         FieldErrorTag tag = new FieldErrorTag();
180         ((InternalAction)action).setHaveFieldErrors(false);
181         tag.setPageContext(pageContext);
182         tag.doStartTag();
183             ParamTag pTag1 = new ParamTag();
184             pTag1.setPageContext(pageContext);
185             pTag1.setValue("%{'field2'}");
186             pTag1.doStartTag();
187             pTag1.doEndTag();
188 
189         tag.doEndTag();
190 
191         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
192     }
193 
194     public void testWithNullFieldErrors() throws Exception {
195         FieldErrorTag tag = new FieldErrorTag();
196         ((InternalAction)action).setHaveFieldErrors(false);
197         ((InternalAction)action).setReturnNullForFieldErrors(true);
198         tag.setPageContext(pageContext);
199         tag.doStartTag();
200             ParamTag pTag1 = new ParamTag();
201             pTag1.setPageContext(pageContext);
202             pTag1.setValue("%{'field2'}");
203             pTag1.doStartTag();
204             pTag1.doEndTag();
205 
206         tag.doEndTag();
207 
208         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
209     }
210 
211 
212     public Action getAction() {
213         return new InternalAction();
214     }
215 
216 
217     public class InternalAction extends ActionSupport {
218 
219         private boolean haveFieldErrors = false;
220         private boolean returnNullForFieldErrors = false;
221 
222         public void setHaveFieldErrors(boolean haveFieldErrors) {
223             this.haveFieldErrors = haveFieldErrors;
224         }
225 
226         public void setReturnNullForFieldErrors(boolean returnNullForFieldErrors) {
227             this.returnNullForFieldErrors = returnNullForFieldErrors;
228         }
229 
230         public Map<String, List<String>> getFieldErrors() {
231             if (haveFieldErrors) {
232                 List err1 = new ArrayList();
233                 err1.add("field error message number 1");
234                 List err2 = new ArrayList();
235                 err2.add("field error message number 2");
236                 List err3 = new ArrayList();
237                 err3.add("field error message number 3");
238                 Map fieldErrors = new LinkedHashMap();
239                 fieldErrors.put("field1", err1);
240                 fieldErrors.put("field2", err2);
241                 fieldErrors.put("field3", err3);
242                 return fieldErrors;
243             }
244             else if (returnNullForFieldErrors) {
245                 return null;
246             }
247             else {
248                 return Collections.emptyMap();
249             }
250         }
251 
252         public boolean hasFieldErrors() {
253             return haveFieldErrors;
254         }
255     }
256 }
257