View Javadoc

1   /*
2    * $Id: FieldErrorTagTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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 testWithParamsWithFieldErrors2() throws Exception {
86          FieldErrorTag tag = new FieldErrorTag();
87          ((InternalAction)action).setHaveFieldErrors(true);
88          tag.setPageContext(pageContext);
89          tag.doStartTag();
90              ParamTag pTag1 = new ParamTag();
91              pTag1.setPageContext(pageContext);
92              pTag1.setValue("%{'field1'}");
93              pTag1.doStartTag();
94              pTag1.doEndTag();
95  
96              ParamTag pTag2 = new ParamTag();
97              pTag2.setPageContext(pageContext);
98              pTag2.setValue("%{'field2'}");
99              pTag2.doStartTag();
100             pTag2.doEndTag();
101 
102         tag.doEndTag();
103 
104         verify(FieldErrorTagTest.class.getResource("fielderror-4.txt"));
105     }
106 
107 
108     public void testWithParamsWithFieldErrors3() throws Exception {
109         FieldErrorTag tag = new FieldErrorTag();
110         ((InternalAction)action).setHaveFieldErrors(true);
111         tag.setPageContext(pageContext);
112         tag.doStartTag();
113             ParamTag pTag1 = new ParamTag();
114             pTag1.setPageContext(pageContext);
115             pTag1.setValue("%{'field2'}");
116             pTag1.doStartTag();
117             pTag1.doEndTag();
118 
119         tag.doEndTag();
120 
121         verify(FieldErrorTagTest.class.getResource("fielderror-5.txt"));
122     }
123 
124     public void testWithParamsWithoutFieldErrors1() throws Exception {
125         FieldErrorTag tag = new FieldErrorTag();
126         ((InternalAction)action).setHaveFieldErrors(false);
127         tag.setPageContext(pageContext);
128         tag.doStartTag();
129             ParamTag pTag1 = new ParamTag();
130             pTag1.setPageContext(pageContext);
131             pTag1.setValue("%{'field1'}");
132             pTag1.doStartTag();
133             pTag1.doEndTag();
134 
135             ParamTag pTag2 = new ParamTag();
136             pTag2.setPageContext(pageContext);
137             pTag2.setValue("%{'field3'}");
138             pTag2.doStartTag();
139             pTag2.doEndTag();
140         tag.doEndTag();
141 
142         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
143     }
144 
145     public void testWithParamsWithoutFieldErrors2() throws Exception {
146         FieldErrorTag tag = new FieldErrorTag();
147         ((InternalAction)action).setHaveFieldErrors(false);
148         tag.setPageContext(pageContext);
149         tag.doStartTag();
150             ParamTag pTag1 = new ParamTag();
151             pTag1.setPageContext(pageContext);
152             pTag1.setValue("%{'field1'}");
153             pTag1.doStartTag();
154             pTag1.doEndTag();
155 
156             ParamTag pTag2 = new ParamTag();
157             pTag2.setPageContext(pageContext);
158             pTag2.setValue("%{'field3'}");
159             pTag2.doStartTag();
160             pTag2.doEndTag();
161         tag.doEndTag();
162 
163         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
164     }
165 
166 
167     public void testWithParamsWithoutFieldErrors3() throws Exception {
168         FieldErrorTag tag = new FieldErrorTag();
169         ((InternalAction)action).setHaveFieldErrors(false);
170         tag.setPageContext(pageContext);
171         tag.doStartTag();
172             ParamTag pTag1 = new ParamTag();
173             pTag1.setPageContext(pageContext);
174             pTag1.setValue("%{'field2'}");
175             pTag1.doStartTag();
176             pTag1.doEndTag();
177 
178         tag.doEndTag();
179 
180         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
181     }
182 
183     public void testWithNullFieldErrors() throws Exception {
184         FieldErrorTag tag = new FieldErrorTag();
185         ((InternalAction)action).setHaveFieldErrors(false);
186         ((InternalAction)action).setReturnNullForFieldErrors(true);
187         tag.setPageContext(pageContext);
188         tag.doStartTag();
189             ParamTag pTag1 = new ParamTag();
190             pTag1.setPageContext(pageContext);
191             pTag1.setValue("%{'field2'}");
192             pTag1.doStartTag();
193             pTag1.doEndTag();
194 
195         tag.doEndTag();
196 
197         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
198     }
199 
200 
201     public Action getAction() {
202         return new InternalAction();
203     }
204 
205 
206     public class InternalAction extends ActionSupport {
207 
208         private boolean haveFieldErrors = false;
209         private boolean returnNullForFieldErrors = false;
210 
211         public void setHaveFieldErrors(boolean haveFieldErrors) {
212             this.haveFieldErrors = haveFieldErrors;
213         }
214 
215         public void setReturnNullForFieldErrors(boolean returnNullForFieldErrors) {
216             this.returnNullForFieldErrors = returnNullForFieldErrors;
217         }
218 
219         public Map getFieldErrors() {
220             if (haveFieldErrors) {
221                 List err1 = new ArrayList();
222                 err1.add("field error message number 1");
223                 List err2 = new ArrayList();
224                 err2.add("field error message number 2");
225                 List err3 = new ArrayList();
226                 err3.add("field error message number 3");
227                 Map fieldErrors = new LinkedHashMap();
228                 fieldErrors.put("field1", err1);
229                 fieldErrors.put("field2", err2);
230                 fieldErrors.put("field3", err3);
231                 return fieldErrors;
232             }
233             else if (returnNullForFieldErrors) {
234                 return null;
235             }
236             else {
237                 return Collections.EMPTY_MAP;
238             }
239         }
240 
241         public boolean hasFieldErrors() {
242             return haveFieldErrors;
243         }
244     }
245 }
246