KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > WizardFormControllerTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.mvc;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.springframework.beans.TestBean;
32 import org.springframework.mock.web.MockHttpServletRequest;
33 import org.springframework.mock.web.MockHttpServletResponse;
34 import org.springframework.util.ObjectUtils;
35 import org.springframework.validation.BindException;
36 import org.springframework.validation.Errors;
37 import org.springframework.web.servlet.ModelAndView;
38
39 /**
40  * @author Juergen Hoeller
41  * @since 29.04.2003
42  */

43 public class WizardFormControllerTests extends TestCase {
44
45     public void testNoDirtyPageChange() throws Exception JavaDoc {
46         AbstractWizardFormController wizard = new TestWizardController();
47         wizard.setAllowDirtyBack(false);
48         wizard.setAllowDirtyForward(false);
49         wizard.setPageAttribute("currentPage");
50
51         assertTrue(wizard.getFormSessionAttributeName() != wizard.getPageSessionAttributeName());
52         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
53
54         Properties JavaDoc params = new Properties JavaDoc();
55         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
56         performRequest(wizard, session, null, 0, null, 0, "currentPage");
57         // not allowed to go to 1
58

59         params.clear();
60         params.setProperty("name", "myname");
61         params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
62         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
63         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
64         // name set -> now allowed to go to 1
65

66         params.clear();
67         params.setProperty("name", "myname");
68         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
69         // name set -> now allowed to go to 1
70

71         params.clear();
72         params.setProperty("name", "myname");
73         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.x", "value");
74         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
75         // name set -> now allowed to go to 1
76

77         params.clear();
78         params.setProperty("name", "myname");
79         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.y", "value");
80         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
81         // name set -> now allowed to go to 1
82

83         params.clear();
84         params.setProperty("date", "not a date");
85         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1.y", "value");
86         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
87         // name set -> now allowed to go to 1
88

89         params.clear();
90         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
91         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
92         // not allowed to go to 0
93

94         params.clear();
95         params.setProperty("age", "32");
96         params.setProperty(AbstractWizardFormController.PARAM_PAGE, "1");
97         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
98         performRequest(wizard, session, params, 0, "myname", 32, "currentPage");
99         // age set -> now allowed to go to 0
100

101         params.clear();
102         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
103         performRequest(wizard, session, params, -1, "myname", 32, null);
104     }
105
106     public void testCustomSessionAttributes() throws Exception JavaDoc {
107         AbstractWizardFormController wizard = new TestWizardController() {
108             protected String JavaDoc getFormSessionAttributeName() {
109                 return "myFormAttr";
110             }
111             protected String JavaDoc getPageSessionAttributeName() {
112                 return "myPageAttr";
113             }
114         };
115         wizard.setAllowDirtyBack(false);
116         wizard.setAllowDirtyForward(false);
117         wizard.setPageAttribute("currentPage");
118
119         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
120         assertTrue(session.getAttribute("myFormAttr") instanceof TestBean);
121         assertEquals(new Integer JavaDoc(0), session.getAttribute("myPageAttr"));
122
123         Properties JavaDoc params = new Properties JavaDoc();
124         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
125         performRequest(wizard, session, null, 0, null, 0, "currentPage");
126         // not allowed to go to 1
127

128         params.clear();
129         params.setProperty("name", "myname");
130         params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
131         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
132         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
133         // name set -> now allowed to go to 1
134

135         params.clear();
136         params.setProperty("age", "32");
137         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
138         performRequest(wizard, session, params, -1, "myname", 32, "currentPage");
139     }
140
141     public void testCustomRequestDependentSessionAttributes() throws Exception JavaDoc {
142         AbstractWizardFormController wizard = new TestWizardController() {
143             protected String JavaDoc getFormSessionAttributeName(HttpServletRequest JavaDoc request) {
144                 return "myFormAttr" + request.getParameter("formAttr");
145             }
146             protected String JavaDoc getPageSessionAttributeName(HttpServletRequest JavaDoc request) {
147                 return "myPageAttr" + request.getParameter("pageAttr");
148             }
149         };
150         wizard.setAllowDirtyBack(false);
151         wizard.setAllowDirtyForward(false);
152         wizard.setPageAttribute("currentPage");
153
154         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
155         assertTrue(session.getAttribute("myFormAttr1") instanceof TestBean);
156         assertEquals(new Integer JavaDoc(0), session.getAttribute("myPageAttr2"));
157
158         Properties JavaDoc params = new Properties JavaDoc();
159         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
160         performRequest(wizard, session, null, 0, null, 0, "currentPage");
161         // not allowed to go to 1
162

163         params.clear();
164         params.setProperty("name", "myname");
165         params.setProperty(AbstractWizardFormController.PARAM_PAGE, "0");
166         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
167         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
168         // name set -> now allowed to go to 1
169

170         params.clear();
171         params.setProperty("age", "32");
172         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
173         performRequest(wizard, session, params, -1, "myname", 32, "currentPage");
174     }
175
176     public void testDirtyBack() throws Exception JavaDoc {
177         AbstractWizardFormController wizard = new TestWizardController();
178         wizard.setAllowDirtyBack(true);
179         wizard.setAllowDirtyForward(false);
180         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, null);
181
182         Properties JavaDoc params = new Properties JavaDoc();
183         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
184         performRequest(wizard, session, params, 0, null, 0, null);
185         // not allowed to go to 1
186

187         params.clear();
188         params.setProperty("name", "myname");
189         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
190         performRequest(wizard, session, params, 1, "myname", 0, null);
191         // name set -> now allowed to go to 1
192

193         params.clear();
194         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
195         performRequest(wizard, session, params, 0, "myname", 0, null);
196         // dirty back -> allowed to go to 0
197

198         params.clear();
199         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
200         performRequest(wizard, session, params, 1, "myname", 0, null);
201         // finish while dirty -> show dirty page (1)
202

203         params.clear();
204         params.setProperty("age", "32");
205         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
206         performRequest(wizard, session, params, -1, "myname", 32, null);
207         // age set -> now allowed to finish
208
}
209
210     public void testDirtyForward() throws Exception JavaDoc {
211         AbstractWizardFormController wizard = new TestWizardController();
212         wizard.setAllowDirtyBack(false);
213         wizard.setAllowDirtyForward(true);
214         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, null);
215
216         Properties JavaDoc params = new Properties JavaDoc();
217         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
218         performRequest(wizard, session, params, 1, null, 0, null);
219         // dirty forward -> allowed to go to 1
220

221         params.clear();
222         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
223         performRequest(wizard, session, params, 1, null, 0, null);
224         // not allowed to go to 0
225

226         params.clear();
227         params.setProperty("age", "32");
228         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
229         performRequest(wizard, session, params, 0, null, 32, null);
230         // age set -> now allowed to go to 0
231

232         params.clear();
233         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
234         performRequest(wizard, session, params, 0, null, 32, null);
235         // finish while dirty -> show dirty page (0)
236

237         params.clear();
238         params.setProperty("name", "myname");
239         params.setProperty(AbstractWizardFormController.PARAM_FINISH + ".x", "value");
240         performRequest(wizard, session, params, -1, "myname", 32, null);
241         // name set -> now allowed to finish
242
}
243
244     public void testSubmitWithoutValidation() throws Exception JavaDoc {
245         AbstractWizardFormController wizard = new TestWizardController();
246         wizard.setAllowDirtyBack(false);
247         wizard.setAllowDirtyForward(false);
248         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, null);
249
250         Properties JavaDoc params = new Properties JavaDoc();
251         params.setProperty("formChange", "true");
252         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
253         performRequest(wizard, session, params, 1, null, 0, null);
254         // no validation -> allowed to go to 1
255

256         params.clear();
257         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
258         performRequest(wizard, session, params, 1, null, 0, null);
259         // not allowed to go to 0
260

261         params.clear();
262         params.setProperty("age", "32");
263         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
264         performRequest(wizard, session, params, 0, null, 32, null);
265         // age set -> now allowed to go to 0
266

267         params.clear();
268         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
269         performRequest(wizard, session, params, 0, null, 32, null);
270         // finish while dirty -> show dirty page (0)
271

272         params.clear();
273         params.setProperty("name", "myname");
274         params.setProperty(AbstractWizardFormController.PARAM_FINISH + ".x", "value");
275         performRequest(wizard, session, params, -1, "myname", 32, null);
276         // name set -> now allowed to finish
277
}
278
279     public void testCancel() throws Exception JavaDoc {
280         AbstractWizardFormController wizard = new TestWizardController();
281         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, null);
282         Properties JavaDoc params = new Properties JavaDoc();
283         params.setProperty(AbstractWizardFormController.PARAM_CANCEL, "value");
284         performRequest(wizard, session, params, -2, null, 0, null);
285
286         assertTrue(session.getAttribute(wizard.getFormSessionAttributeName()) == null);
287         assertTrue(session.getAttribute(wizard.getPageSessionAttributeName()) == null);
288
289         session = performRequest(wizard, null, null, 0, null, 0, null);
290         params = new Properties JavaDoc();
291         params.setProperty(AbstractWizardFormController.PARAM_CANCEL + ".y", "value");
292         performRequest(wizard, session, params, -2, null, 0, null);
293     }
294
295     public void testInvalidSubmit() throws Exception JavaDoc {
296         AbstractWizardFormController wizard = new TestWizardController();
297         wizard.setAllowDirtyBack(false);
298         wizard.setAllowDirtyForward(false);
299         wizard.setPageAttribute("currentPage");
300         HttpSession JavaDoc session = performRequest(wizard, null, null, 0, null, 0, "currentPage");
301
302         Properties JavaDoc params = new Properties JavaDoc();
303         params.setProperty("name", "myname");
304         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value");
305         performRequest(wizard, session, params, 1, "myname", 0, "currentPage");
306
307         params.clear();
308         params.setProperty("age", "32");
309         params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value");
310         performRequest(wizard, session, params, 0, "myname", 32, "currentPage");
311
312         params.clear();
313         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
314         performRequest(wizard, session, params, -1, "myname", 32, null);
315
316         params.clear();
317         params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value");
318         performRequest(wizard, session, params, 0, null, 0, "currentPage");
319         // returned to initial page of new wizard form
320
}
321
322     private HttpSession JavaDoc performRequest(
323             AbstractWizardFormController wizard, HttpSession JavaDoc session, Properties JavaDoc params,
324             int target, String JavaDoc name, int age, String JavaDoc pageAttr) throws Exception JavaDoc {
325
326         MockHttpServletRequest request = new MockHttpServletRequest((params != null ? "POST" : "GET"), "/wizard");
327         request.addParameter("formAttr", "1");
328         request.addParameter("pageAttr", "2");
329         if (params != null) {
330             for (Iterator JavaDoc it = params.keySet().iterator(); it.hasNext();) {
331                 String JavaDoc param = (String JavaDoc) it.next();
332                 request.addParameter(param, params.getProperty(param));
333             }
334         }
335         request.setSession(session);
336         request.setAttribute("target", new Integer JavaDoc(target));
337         MockHttpServletResponse response = new MockHttpServletResponse();
338         ModelAndView mv = wizard.handleRequest(request, response);
339         if (target >= 0) {
340             assertTrue("Page " + target + " returned", ("page" + target).equals(mv.getViewName()));
341             if (pageAttr != null) {
342                 assertTrue("Page attribute set", (new Integer JavaDoc(target)).equals(mv.getModel().get(pageAttr)));
343                 assertTrue("Correct model size", mv.getModel().size() == 3);
344             }
345             else {
346                 assertTrue("Correct model size", mv.getModel().size() == 2);
347             }
348             assertTrue(
349                     request.getSession().getAttribute(wizard.getFormSessionAttributeName(request)) instanceof TestBean);
350             assertEquals(new Integer JavaDoc(target),
351                     request.getSession().getAttribute(wizard.getPageSessionAttributeName(request)));
352         }
353         else if (target == -1) {
354             assertTrue("Success target returned", "success".equals(mv.getViewName()));
355             assertTrue("Correct model size", mv.getModel().size() == 1);
356             assertTrue(request.getSession().getAttribute(wizard.getFormSessionAttributeName(request)) == null);
357             assertTrue(request.getSession().getAttribute(wizard.getPageSessionAttributeName(request)) == null);
358         }
359         else if (target == -2) {
360             assertTrue("Cancel view returned", "cancel".equals(mv.getViewName()));
361             assertTrue("Correct model size", mv.getModel().size() == 1);
362             assertTrue(request.getSession().getAttribute(wizard.getFormSessionAttributeName(request)) == null);
363             assertTrue(request.getSession().getAttribute(wizard.getPageSessionAttributeName(request)) == null);
364         }
365         TestBean tb = (TestBean) mv.getModel().get("tb");
366         assertTrue("Has model", tb != null);
367         assertTrue("Name is " + name, ObjectUtils.nullSafeEquals(name, tb.getName()));
368         assertTrue("Age is " + age, tb.getAge() == age);
369         Errors errors = (Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + "tb");
370         if (params != null && params.containsKey("formChange")) {
371             assertNotNull(errors);
372             assertFalse(errors.hasErrors());
373         }
374         return request.getSession(false);
375     }
376
377
378     private static class TestWizardController extends AbstractWizardFormController {
379
380         public TestWizardController() {
381             setCommandClass(TestBean.class);
382             setCommandName("tb");
383             setPages(new String JavaDoc[] {"page0", "page1"});
384         }
385
386         protected Map JavaDoc referenceData(HttpServletRequest JavaDoc request, int page) throws Exception JavaDoc {
387             assertEquals(new Integer JavaDoc(page), request.getAttribute("target"));
388             return super.referenceData(request, page);
389         }
390
391         protected boolean suppressValidation(HttpServletRequest JavaDoc request) {
392             return (request.getParameter("formChange") != null);
393         }
394
395         protected void validatePage(Object JavaDoc command, Errors errors, int page) {
396             TestBean tb = (TestBean) command;
397             switch (page) {
398                 case 0:
399                     if (tb.getName() == null) {
400                         errors.rejectValue("name", "NAME_REQUIRED", null, "Name is required");
401                     }
402                     break;
403                 case 1:
404                     if (tb.getAge() == 0) {
405                         errors.rejectValue("age", "AGE_REQUIRED", null, "Age is required");
406                     }
407                     break;
408               default:
409                     throw new IllegalArgumentException JavaDoc("Invalid page number");
410             }
411         }
412
413         protected ModelAndView processFinish(
414                 HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
415             throws ServletException JavaDoc, IOException JavaDoc {
416             assertTrue(getCurrentPage(request) == 0 || getCurrentPage(request) == 1);
417             return new ModelAndView("success", getCommandName(), command);
418         }
419
420         protected ModelAndView processCancel(
421                 HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
422             throws ServletException JavaDoc, IOException JavaDoc {
423             assertTrue(getCurrentPage(request) == 0 || getCurrentPage(request) == 1);
424             return new ModelAndView("cancel", getCommandName(), command);
425         }
426     }
427
428 }
429
Popular Tags