1 16 17 package org.springframework.web.servlet.mvc; 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.http.HttpSession ; 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 43 public class WizardFormControllerTests extends TestCase { 44 45 public void testNoDirtyPageChange() throws Exception { 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 session = performRequest(wizard, null, null, 0, null, 0, "currentPage"); 53 54 Properties params = new Properties (); 55 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value"); 56 performRequest(wizard, session, null, 0, null, 0, "currentPage"); 57 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 66 params.clear(); 67 params.setProperty("name", "myname"); 68 performRequest(wizard, session, params, 1, "myname", 0, "currentPage"); 69 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 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 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 89 params.clear(); 90 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value"); 91 performRequest(wizard, session, params, 1, "myname", 0, "currentPage"); 92 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 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 { 107 AbstractWizardFormController wizard = new TestWizardController() { 108 protected String getFormSessionAttributeName() { 109 return "myFormAttr"; 110 } 111 protected String getPageSessionAttributeName() { 112 return "myPageAttr"; 113 } 114 }; 115 wizard.setAllowDirtyBack(false); 116 wizard.setAllowDirtyForward(false); 117 wizard.setPageAttribute("currentPage"); 118 119 HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage"); 120 assertTrue(session.getAttribute("myFormAttr") instanceof TestBean); 121 assertEquals(new Integer (0), session.getAttribute("myPageAttr")); 122 123 Properties params = new Properties (); 124 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value"); 125 performRequest(wizard, session, null, 0, null, 0, "currentPage"); 126 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 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 { 142 AbstractWizardFormController wizard = new TestWizardController() { 143 protected String getFormSessionAttributeName(HttpServletRequest request) { 144 return "myFormAttr" + request.getParameter("formAttr"); 145 } 146 protected String getPageSessionAttributeName(HttpServletRequest request) { 147 return "myPageAttr" + request.getParameter("pageAttr"); 148 } 149 }; 150 wizard.setAllowDirtyBack(false); 151 wizard.setAllowDirtyForward(false); 152 wizard.setPageAttribute("currentPage"); 153 154 HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage"); 155 assertTrue(session.getAttribute("myFormAttr1") instanceof TestBean); 156 assertEquals(new Integer (0), session.getAttribute("myPageAttr2")); 157 158 Properties params = new Properties (); 159 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value"); 160 performRequest(wizard, session, null, 0, null, 0, "currentPage"); 161 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 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 { 177 AbstractWizardFormController wizard = new TestWizardController(); 178 wizard.setAllowDirtyBack(true); 179 wizard.setAllowDirtyForward(false); 180 HttpSession session = performRequest(wizard, null, null, 0, null, 0, null); 181 182 Properties params = new Properties (); 183 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value"); 184 performRequest(wizard, session, params, 0, null, 0, null); 185 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 193 params.clear(); 194 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value"); 195 performRequest(wizard, session, params, 0, "myname", 0, null); 196 198 params.clear(); 199 params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value"); 200 performRequest(wizard, session, params, 1, "myname", 0, null); 201 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 } 209 210 public void testDirtyForward() throws Exception { 211 AbstractWizardFormController wizard = new TestWizardController(); 212 wizard.setAllowDirtyBack(false); 213 wizard.setAllowDirtyForward(true); 214 HttpSession session = performRequest(wizard, null, null, 0, null, 0, null); 215 216 Properties params = new Properties (); 217 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value"); 218 performRequest(wizard, session, params, 1, null, 0, null); 219 221 params.clear(); 222 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value"); 223 performRequest(wizard, session, params, 1, null, 0, null); 224 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 232 params.clear(); 233 params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value"); 234 performRequest(wizard, session, params, 0, null, 32, null); 235 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 } 243 244 public void testSubmitWithoutValidation() throws Exception { 245 AbstractWizardFormController wizard = new TestWizardController(); 246 wizard.setAllowDirtyBack(false); 247 wizard.setAllowDirtyForward(false); 248 HttpSession session = performRequest(wizard, null, null, 0, null, 0, null); 249 250 Properties params = new Properties (); 251 params.setProperty("formChange", "true"); 252 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "1", "value"); 253 performRequest(wizard, session, params, 1, null, 0, null); 254 256 params.clear(); 257 params.setProperty(AbstractWizardFormController.PARAM_TARGET + "0", "value"); 258 performRequest(wizard, session, params, 1, null, 0, null); 259 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 267 params.clear(); 268 params.setProperty(AbstractWizardFormController.PARAM_FINISH, "value"); 269 performRequest(wizard, session, params, 0, null, 32, null); 270 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 } 278 279 public void testCancel() throws Exception { 280 AbstractWizardFormController wizard = new TestWizardController(); 281 HttpSession session = performRequest(wizard, null, null, 0, null, 0, null); 282 Properties params = new Properties (); 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 (); 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 { 296 AbstractWizardFormController wizard = new TestWizardController(); 297 wizard.setAllowDirtyBack(false); 298 wizard.setAllowDirtyForward(false); 299 wizard.setPageAttribute("currentPage"); 300 HttpSession session = performRequest(wizard, null, null, 0, null, 0, "currentPage"); 301 302 Properties params = new Properties (); 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 } 321 322 private HttpSession performRequest( 323 AbstractWizardFormController wizard, HttpSession session, Properties params, 324 int target, String name, int age, String pageAttr) throws Exception { 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 it = params.keySet().iterator(); it.hasNext();) { 331 String param = (String ) it.next(); 332 request.addParameter(param, params.getProperty(param)); 333 } 334 } 335 request.setSession(session); 336 request.setAttribute("target", new Integer (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 (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 (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 [] {"page0", "page1"}); 384 } 385 386 protected Map referenceData(HttpServletRequest request, int page) throws Exception { 387 assertEquals(new Integer (page), request.getAttribute("target")); 388 return super.referenceData(request, page); 389 } 390 391 protected boolean suppressValidation(HttpServletRequest request) { 392 return (request.getParameter("formChange") != null); 393 } 394 395 protected void validatePage(Object 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 ("Invalid page number"); 410 } 411 } 412 413 protected ModelAndView processFinish( 414 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) 415 throws ServletException , IOException { 416 assertTrue(getCurrentPage(request) == 0 || getCurrentPage(request) == 1); 417 return new ModelAndView("success", getCommandName(), command); 418 } 419 420 protected ModelAndView processCancel( 421 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) 422 throws ServletException , IOException { 423 assertTrue(getCurrentPage(request) == 0 || getCurrentPage(request) == 1); 424 return new ModelAndView("cancel", getCommandName(), command); 425 } 426 } 427 428 } 429 | Popular Tags |