View Javadoc

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