View Javadoc

1   /*
2    * $Id: FieldErrorTagTest.java 471756 2006-11-06 15:01:43Z husted $
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  package org.apache.struts2.views.jsp.ui;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.struts2.views.jsp.AbstractUITagTest;
30  import org.apache.struts2.views.jsp.ParamTag;
31  
32  import com.opensymphony.xwork2.Action;
33  import com.opensymphony.xwork2.ActionSupport;
34  
35  /***
36   * FieldError Tag Test Case.
37   *
38   */
39  public class FieldErrorTagTest extends AbstractUITagTest {
40  
41  
42      public void testWithoutParamsWithFieldErrors() throws Exception {
43          FieldErrorTag tag = new FieldErrorTag();
44          ((InternalAction)action).setHaveFieldErrors(true);
45          tag.setPageContext(pageContext);
46          tag.doStartTag();
47          tag.doEndTag();
48  
49          verify(FieldErrorTagTest.class.getResource("fielderror-1.txt"));
50      }
51  
52      public void testWithoutParamsWithoutFieldErrors() throws Exception {
53          FieldErrorTag tag = new FieldErrorTag();
54          ((InternalAction)action).setHaveFieldErrors(false);
55          tag.setPageContext(pageContext);
56          tag.doStartTag();
57          tag.doEndTag();
58  
59          verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
60      }
61  
62      public void testWithParamsWithFieldErrors1() throws Exception {
63          FieldErrorTag tag = new FieldErrorTag();
64          ((InternalAction)action).setHaveFieldErrors(true);
65          tag.setPageContext(pageContext);
66          tag.doStartTag();
67              ParamTag pTag1 = new ParamTag();
68              pTag1.setPageContext(pageContext);
69              pTag1.setValue("%{'field1'}");
70              pTag1.doStartTag();
71              pTag1.doEndTag();
72  
73              ParamTag pTag2 = new ParamTag();
74              pTag2.setPageContext(pageContext);
75              pTag2.setValue("%{'field3'}");
76              pTag2.doStartTag();
77              pTag2.doEndTag();
78  
79          tag.doEndTag();
80  
81          verify(FieldErrorTagTest.class.getResource("fielderror-3.txt"));
82      }
83  
84      public void testWithParamsWithFieldErrors2() throws Exception {
85          FieldErrorTag tag = new FieldErrorTag();
86          ((InternalAction)action).setHaveFieldErrors(true);
87          tag.setPageContext(pageContext);
88          tag.doStartTag();
89              ParamTag pTag1 = new ParamTag();
90              pTag1.setPageContext(pageContext);
91              pTag1.setValue("%{'field1'}");
92              pTag1.doStartTag();
93              pTag1.doEndTag();
94  
95              ParamTag pTag2 = new ParamTag();
96              pTag2.setPageContext(pageContext);
97              pTag2.setValue("%{'field2'}");
98              pTag2.doStartTag();
99              pTag2.doEndTag();
100 
101         tag.doEndTag();
102 
103         verify(FieldErrorTagTest.class.getResource("fielderror-4.txt"));
104     }
105 
106 
107     public void testWithParamsWithFieldErrors3() throws Exception {
108         FieldErrorTag tag = new FieldErrorTag();
109         ((InternalAction)action).setHaveFieldErrors(true);
110         tag.setPageContext(pageContext);
111         tag.doStartTag();
112             ParamTag pTag1 = new ParamTag();
113             pTag1.setPageContext(pageContext);
114             pTag1.setValue("%{'field2'}");
115             pTag1.doStartTag();
116             pTag1.doEndTag();
117 
118         tag.doEndTag();
119 
120         verify(FieldErrorTagTest.class.getResource("fielderror-5.txt"));
121     }
122 
123     public void testWithParamsWithoutFieldErrors1() throws Exception {
124         FieldErrorTag tag = new FieldErrorTag();
125         ((InternalAction)action).setHaveFieldErrors(false);
126         tag.setPageContext(pageContext);
127         tag.doStartTag();
128             ParamTag pTag1 = new ParamTag();
129             pTag1.setPageContext(pageContext);
130             pTag1.setValue("%{'field1'}");
131             pTag1.doStartTag();
132             pTag1.doEndTag();
133 
134             ParamTag pTag2 = new ParamTag();
135             pTag2.setPageContext(pageContext);
136             pTag2.setValue("%{'field3'}");
137             pTag2.doStartTag();
138             pTag2.doEndTag();
139         tag.doEndTag();
140 
141         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
142     }
143 
144     public void testWithParamsWithoutFieldErrors2() throws Exception {
145         FieldErrorTag tag = new FieldErrorTag();
146         ((InternalAction)action).setHaveFieldErrors(false);
147         tag.setPageContext(pageContext);
148         tag.doStartTag();
149             ParamTag pTag1 = new ParamTag();
150             pTag1.setPageContext(pageContext);
151             pTag1.setValue("%{'field1'}");
152             pTag1.doStartTag();
153             pTag1.doEndTag();
154 
155             ParamTag pTag2 = new ParamTag();
156             pTag2.setPageContext(pageContext);
157             pTag2.setValue("%{'field3'}");
158             pTag2.doStartTag();
159             pTag2.doEndTag();
160         tag.doEndTag();
161 
162         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
163     }
164 
165 
166     public void testWithParamsWithoutFieldErrors3() throws Exception {
167         FieldErrorTag tag = new FieldErrorTag();
168         ((InternalAction)action).setHaveFieldErrors(false);
169         tag.setPageContext(pageContext);
170         tag.doStartTag();
171             ParamTag pTag1 = new ParamTag();
172             pTag1.setPageContext(pageContext);
173             pTag1.setValue("%{'field2'}");
174             pTag1.doStartTag();
175             pTag1.doEndTag();
176 
177         tag.doEndTag();
178 
179         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
180     }
181 
182     public void testWithNullFieldErrors() throws Exception {
183         FieldErrorTag tag = new FieldErrorTag();
184         ((InternalAction)action).setHaveFieldErrors(false);
185         ((InternalAction)action).setReturnNullForFieldErrors(true);
186         tag.setPageContext(pageContext);
187         tag.doStartTag();
188             ParamTag pTag1 = new ParamTag();
189             pTag1.setPageContext(pageContext);
190             pTag1.setValue("%{'field2'}");
191             pTag1.doStartTag();
192             pTag1.doEndTag();
193 
194         tag.doEndTag();
195 
196         verify(FieldErrorTagTest.class.getResource("fielderror-2.txt"));
197     }
198 
199 
200     public Action getAction() {
201         return new InternalAction();
202     }
203 
204 
205     public class InternalAction extends ActionSupport {
206 
207         private boolean haveFieldErrors = false;
208         private boolean returnNullForFieldErrors = false;
209 
210         public void setHaveFieldErrors(boolean haveFieldErrors) {
211             this.haveFieldErrors = haveFieldErrors;
212         }
213 
214         public void setReturnNullForFieldErrors(boolean returnNullForFieldErrors) {
215             this.returnNullForFieldErrors = returnNullForFieldErrors;
216         }
217 
218         public Map getFieldErrors() {
219             if (haveFieldErrors) {
220                 List err1 = new ArrayList();
221                 err1.add("field error message number 1");
222                 List err2 = new ArrayList();
223                 err2.add("field error message number 2");
224                 List err3 = new ArrayList();
225                 err3.add("field error message number 3");
226                 Map fieldErrors = new LinkedHashMap();
227                 fieldErrors.put("field1", err1);
228                 fieldErrors.put("field2", err2);
229                 fieldErrors.put("field3", err3);
230                 return fieldErrors;
231             }
232             else if (returnNullForFieldErrors) {
233                 return null;
234             }
235             else {
236                 return Collections.EMPTY_MAP;
237             }
238         }
239 
240         public boolean hasFieldErrors() {
241             return haveFieldErrors;
242         }
243     }
244 }
245