1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package nl.openedge.baritus.test;
33
34 import java.util.HashMap;
35 import java.util.Locale;
36 import java.util.Map;
37
38 import javax.servlet.ServletException;
39
40 import nl.openedge.baritus.FormBeanContext;
41 import nl.openedge.baritus.FormBeanCtrlBase;
42 import nl.openedge.baritus.test.mock.MockHttpServletRequest;
43 import nl.openedge.baritus.test.mock.MockHttpServletResponse;
44
45 import org.infohazard.maverick.flow.MaverickContext;
46
47 import com.mockobjects.servlet.MockHttpSession;
48 import com.mockobjects.servlet.MockRequestDispatcher;
49 import com.mockobjects.servlet.MockServletConfig;
50 import com.mockobjects.servlet.MockServletContext;
51
52 import junit.framework.TestCase;
53
54 /***
55 * Testcase for population of form beans and interceptors
56 *
57 * @author Eelco Hillenius
58 */
59 public class ValidationTest extends TestCase
60 {
61
62 private Locale dutch = new Locale("nl", "NL");
63
64 private MockRequestDispatcher requestDispatcher = null;
65 private MockServletContext servletContext = null;
66 private MockServletConfig servletConfig = null;
67 private MockHttpSession session = null;
68 private MockHttpServletResponse response = null;
69 private MockHttpServletRequest request = null;
70
71 /***
72 * construct
73 * @param name
74 */
75 public ValidationTest(String name)
76 {
77 super(name);
78 }
79
80 /***
81 * @see junit.framework.TestCase#setUp()
82 */
83 protected void setUp() throws Exception
84 {
85 this.requestDispatcher = new MockRequestDispatcher();
86 this.servletContext = new MockServletContext();
87 this.servletContext.setupGetRequestDispatcher(requestDispatcher);
88 this.servletConfig = new MockServletConfig();
89 this.servletConfig.setServletContext(servletContext);
90 this.session = new MockHttpSession();
91 this.session.setupGetAttribute(
92 FormBeanCtrlBase.SESSION_KEY_CURRENT_LOCALE, dutch);
93
94 this.session.setupServletContext(servletContext);
95 this.response = new MockHttpServletResponse();
96 this.request = new MockHttpServletRequest();
97 this.request.setupGetAttribute("__formBeanContext");
98 this.request.setSession(session);
99 this.request.setupGetRequestDispatcher(requestDispatcher);
100 }
101
102 public void testValidFieldValidation()
103 {
104 TestCtrl ctrl = new TestCtrl();
105 Map requestParams = new HashMap();
106 requestParams.put("toValidate1", "validValue");
107 request.setupGetParameterMap(requestParams);
108 MaverickContext mockMavCtx = new MaverickContext(
109 null, request, response);
110 try
111 {
112 ctrl.init(null);
113 ctrl.go(mockMavCtx);
114 TestBean bean = ctrl.getTestBean();
115 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
116 }
117 catch (ServletException e)
118 {
119 e.printStackTrace();
120 fail(e.getMessage());
121 }
122 }
123
124 public void testNonValidFieldValidation1()
125 {
126 TestCtrl ctrl = new TestCtrl();
127 Map requestParams = new HashMap();
128 requestParams.put("toValidate1", "nonValidValue");
129 request.setupGetParameterMap(requestParams);
130 MaverickContext mockMavCtx = new MaverickContext(
131 null, request, response);
132 try
133 {
134 ctrl.init(null);
135 ctrl.go(mockMavCtx);
136 TestBean bean = ctrl.getTestBean();
137 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
138 }
139 catch (ServletException e)
140 {
141 e.printStackTrace();
142 fail(e.getMessage());
143 }
144 }
145
146 public void testNonValidFieldValidation2()
147 {
148 TestCtrl ctrl = new TestCtrl();
149 Map requestParams = new HashMap();
150 requestParams.put("toValidate1", "kill");
151 request.setupGetParameterMap(requestParams);
152 MaverickContext mockMavCtx = new MaverickContext(
153 null, request, response);
154 try
155 {
156 ctrl.init(null);
157 ctrl.go(mockMavCtx);
158 TestBean bean = ctrl.getTestBean();
159 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
160 }
161 catch (ServletException e)
162 {
163 e.printStackTrace();
164 fail(e.getMessage());
165 }
166 }
167
168 public void testIndexedFieldValidation1()
169 {
170 TestCtrl ctrl = new TestCtrl();
171 Map requestParams = new HashMap();
172 requestParams.put("toValidate2[0]", "validValue");
173 requestParams.put("toValidate2[1]", "nonValidValue");
174 request.setupGetParameterMap(requestParams);
175 MaverickContext mockMavCtx = new MaverickContext(
176 null, request, response);
177 try
178 {
179 ctrl.init(null);
180 ctrl.go(mockMavCtx);
181 TestBean bean = ctrl.getTestBean();
182 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
183 FormBeanContext fbc = ctrl.getFormBeanContext();
184 }
185 catch (ServletException e)
186 {
187 e.printStackTrace();
188 fail(e.getMessage());
189 }
190 }
191
192 public void testIndexedFieldValidation2()
193 {
194 TestCtrl ctrl = new TestCtrl();
195 Map requestParams = new HashMap();
196 requestParams.put("toValidate3[0]", "nonValidValue");
197 request.setupGetParameterMap(requestParams);
198 MaverickContext mockMavCtx = new MaverickContext(
199 null, request, response);
200 try
201 {
202 ctrl.init(null);
203 ctrl.go(mockMavCtx);
204 TestBean bean = ctrl.getTestBean();
205 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
206 }
207 catch (ServletException e)
208 {
209 e.printStackTrace();
210 fail(e.getMessage());
211 }
212 }
213
214 public void testIndexedFieldValidation3()
215 {
216 TestCtrl ctrl = new TestCtrl();
217 Map requestParams = new HashMap();
218 requestParams.put("toValidate3[0]", "validValue");
219 requestParams.put("toValidate3[1]", "nonValidValue");
220 request.setupGetParameterMap(requestParams);
221 MaverickContext mockMavCtx = new MaverickContext(
222 null, request, response);
223 try
224 {
225 ctrl.init(null);
226 ctrl.go(mockMavCtx);
227 TestBean bean = ctrl.getTestBean();
228 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
229 }
230 catch (ServletException e)
231 {
232 e.printStackTrace();
233 fail(e.getMessage());
234 }
235 }
236
237 public void testFormValidation1()
238 {
239 TestCtrl ctrl = new TestCtrl();
240 Map requestParams = new HashMap();
241 requestParams.put("toValidate4", "validValue");
242 request.setupGetParameterMap(requestParams);
243 MaverickContext mockMavCtx = new MaverickContext(
244 null, request, response);
245 try
246 {
247 ctrl.init(null);
248 ctrl.go(mockMavCtx);
249 TestBean bean = ctrl.getTestBean();
250 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
251 }
252 catch (ServletException e)
253 {
254 e.printStackTrace();
255 fail(e.getMessage());
256 }
257 }
258
259 public void testFormValidation2()
260 {
261 TestCtrl ctrl = new TestCtrl();
262 Map requestParams = new HashMap();
263 requestParams.put("toValidate4", "nonValidValue");
264 request.setupGetParameterMap(requestParams);
265 MaverickContext mockMavCtx = new MaverickContext(
266 null, request, response);
267 try
268 {
269 ctrl.init(null);
270 ctrl.go(mockMavCtx);
271 TestBean bean = ctrl.getTestBean();
272 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
273 }
274 catch (ServletException e)
275 {
276 e.printStackTrace();
277 fail(e.getMessage());
278 }
279 }
280
281 public void testFormValidation3()
282 {
283 TestCtrl ctrl = new TestCtrl();
284 Map requestParams = new HashMap();
285 requestParams.put("toValidate4", "kill");
286 request.setupGetParameterMap(requestParams);
287 MaverickContext mockMavCtx = new MaverickContext(
288 null, request, response);
289 try
290 {
291 ctrl.init(null);
292 ctrl.go(mockMavCtx);
293 TestBean bean = ctrl.getTestBean();
294 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
295 }
296 catch (ServletException e)
297 {
298 e.printStackTrace();
299 fail(e.getMessage());
300 }
301 }
302
303 }