1 16 17 package org.springframework.web.servlet.mvc; 18 19 import java.text.DateFormat ; 20 import java.text.NumberFormat ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import junit.framework.TestCase; 32 33 import org.springframework.beans.TestBean; 34 import org.springframework.beans.propertyeditors.CustomDateEditor; 35 import org.springframework.beans.propertyeditors.CustomNumberEditor; 36 import org.springframework.mock.web.MockHttpServletRequest; 37 import org.springframework.mock.web.MockHttpServletResponse; 38 import org.springframework.mock.web.MockHttpSession; 39 import org.springframework.validation.BindException; 40 import org.springframework.validation.Errors; 41 import org.springframework.validation.FieldError; 42 import org.springframework.web.bind.ServletRequestDataBinder; 43 import org.springframework.web.servlet.ModelAndView; 44 45 48 public class CommandControllerTests extends TestCase { 49 50 public void testNoArgsNoErrors() throws Exception { 51 TestController mc = new TestController(); 52 HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 53 MockHttpServletResponse response = new MockHttpServletResponse(); 54 ModelAndView mv = mc.handleRequest(request, response); 55 assertTrue("returned correct view name", mv.getViewName().equals(request.getServletPath())); 56 TestBean person = (TestBean) mv.getModel().get("command"); 57 Errors errors = (Errors) mv.getModel().get("errors"); 58 assertTrue("command and errors non null", person != null && errors != null); 59 assertTrue("no errors", !errors.hasErrors()); 60 assertTrue("Correct caching", response.getHeader("Cache-Control") == null); 61 assertTrue("Correct expires header", response.getHeader("Expires") == null); 62 } 63 64 public void test2ArgsNoErrors() throws Exception { 65 TestController mc = new TestController(); 66 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 67 String name = "Rod"; 68 int age = 32; 69 request.addParameter("name", name); 70 request.addParameter("age", "" + age); 71 HttpServletResponse response = new MockHttpServletResponse(); 72 ModelAndView mv = mc.handleRequest(request, response); 73 assertTrue("returned correct view name", mv.getViewName().equals(request.getServletPath())); 74 TestBean person = (TestBean) mv.getModel().get("command"); 75 Errors errors = (Errors) mv.getModel().get("errors"); 76 assertTrue("command and errors non null", person != null && errors != null); 77 assertTrue("no errors", !errors.hasErrors()); 78 assertTrue("command name bound ok", person.getName().equals(name)); 79 assertTrue("command age bound ok", person.getAge() == age); 80 } 81 82 public void test2Args1Mismatch() throws Exception { 83 TestController mc = new TestController(); 84 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 85 String name = "Rod"; 86 String age = "32x"; 87 request.addParameter("name", name); 88 request.addParameter("age", age); 89 HttpServletResponse response = new MockHttpServletResponse(); 90 ModelAndView mv = mc.handleRequest(request, response); 91 assertTrue("returned correct view name", mv.getViewName().equals(request.getServletPath())); 92 TestBean person = (TestBean) mv.getModel().get("command"); 93 Errors errors = (Errors) mv.getModel().get("errors"); 94 assertTrue("command and errors non null", person != null && errors != null); 95 assertTrue("has 1 errors", errors.getErrorCount() == 1); 96 assertTrue("command name bound ok", person.getName().equals(name)); 97 assertTrue("command age default", person.getAge() == new TestBean().getAge()); 98 assertTrue("has error on field age", errors.hasFieldErrors("age")); 99 FieldError fe = errors.getFieldError("age"); 100 assertTrue("Saved invalid value", fe.getRejectedValue().equals(age)); 101 assertTrue("Correct field", fe.getField().equals("age")); 102 } 103 104 public void testSupportedMethods() throws Exception { 105 TestController mc = new TestController(); 106 mc.setSupportedMethods(new String [] {"POST"}); 107 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 108 HttpServletResponse response = new MockHttpServletResponse(); 109 try { 110 mc.handleRequest(request, response); 111 fail("Should have thrown ServletException"); 112 } 113 catch (ServletException ex) { 114 } 116 } 117 118 public void testRequireSessionWithoutSession() throws Exception { 119 TestController mc = new TestController(); 120 mc.setRequireSession(true); 121 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 122 HttpServletResponse response = new MockHttpServletResponse(); 123 try { 124 mc.handleRequest(request, response); 125 fail("Should have thrown ServletException"); 126 } 127 catch (ServletException ex) { 128 } 130 } 131 132 public void testRequireSessionWithSession() throws Exception { 133 TestController mc = new TestController(); 134 mc.setRequireSession(true); 135 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 136 request.setSession(new MockHttpSession(null)); 137 HttpServletResponse response = new MockHttpServletResponse(); 138 mc.handleRequest(request, response); 139 } 140 141 public void testNoCaching() throws Exception { 142 TestController mc = new TestController(); 143 mc.setCacheSeconds(0); 144 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 145 MockHttpServletResponse response = new MockHttpServletResponse(); 146 mc.handleRequest(request, response); 147 assertTrue("Correct expires header", response.getHeader("Expires").equals(new Long (1))); 148 List cacheControl = response.getHeaders("Cache-Control"); 149 assertTrue("Correct cache control", cacheControl.contains("no-cache")); 150 assertTrue("Correct cache control", cacheControl.contains("no-store")); 151 } 152 153 public void testNoCachingWithoutExpires() throws Exception { 154 TestController mc = new TestController(); 155 mc.setCacheSeconds(0); 156 mc.setUseExpiresHeader(false); 157 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 158 MockHttpServletResponse response = new MockHttpServletResponse(); 159 mc.handleRequest(request, response); 160 assertTrue("No expires header", response.getHeader("Expires") == null); 161 List cacheControl = response.getHeaders("Cache-Control"); 162 assertTrue("Correct cache control", cacheControl.contains("no-cache")); 163 assertTrue("Correct cache control", cacheControl.contains("no-store")); 164 } 165 166 public void testNoCachingWithoutCacheControl() throws Exception { 167 TestController mc = new TestController(); 168 mc.setCacheSeconds(0); 169 mc.setUseCacheControlHeader(false); 170 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 171 MockHttpServletResponse response = new MockHttpServletResponse(); 172 mc.handleRequest(request, response); 173 assertTrue("Correct expires header", response.getHeader("Expires").equals(new Long (1))); 174 assertTrue("No cache control", response.getHeader("Cache-Control") == null); 175 } 176 177 public void testCaching() throws Exception { 178 TestController mc = new TestController(); 179 mc.setCacheSeconds(10); 180 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 181 MockHttpServletResponse response = new MockHttpServletResponse(); 182 mc.handleRequest(request, response); 183 assertTrue("Correct expires header", response.getHeader("Expires") != null); 184 assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=10")); 185 } 186 187 public void testCachingWithoutExpires() throws Exception { 188 TestController mc = new TestController(); 189 mc.setCacheSeconds(10); 190 mc.setUseExpiresHeader(false); 191 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 192 MockHttpServletResponse response = new MockHttpServletResponse(); 193 mc.handleRequest(request, response); 194 assertTrue("No expires header", response.getHeader("Expires") == null); 195 assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=10")); 196 } 197 198 public void testCachingWithoutCacheControl() throws Exception { 199 TestController mc = new TestController(); 200 mc.setCacheSeconds(10); 201 mc.setUseCacheControlHeader(false); 202 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 203 MockHttpServletResponse response = new MockHttpServletResponse(); 204 mc.handleRequest(request, response); 205 assertTrue("Correct expires header", response.getHeader("Expires") != null); 206 assertTrue("No cache control", response.getHeader("Cache-Control") == null); 207 } 208 209 public void testCachingWithLastModified() throws Exception { 210 class LastModifiedTestController extends TestController implements LastModified { 211 public long getLastModified(HttpServletRequest request) { 212 return 0; 213 } 214 } 215 LastModifiedTestController mc = new LastModifiedTestController(); 216 mc.setCacheSeconds(10); 217 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 218 MockHttpServletResponse response = new MockHttpServletResponse(); 219 mc.handleRequest(request, response); 220 assertTrue("Correct expires header", response.getHeader("Expires") != null); 221 assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=10, must-revalidate")); 222 } 223 224 public void testCachingWithCustomCacheForSecondsCall() throws Exception { 225 TestController mc = new TestController() { 226 protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { 227 cacheForSeconds(response, 5); 228 return super.handle(request, response, command, errors); 229 } 230 }; 231 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 232 MockHttpServletResponse response = new MockHttpServletResponse(); 233 mc.handleRequest(request, response); 234 assertTrue("Correct expires header", response.getHeader("Expires") != null); 235 assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=5")); 236 } 237 238 public void testCachingWithCustomApplyCacheSecondsCall1() throws Exception { 239 TestController mc = new TestController() { 240 protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { 241 applyCacheSeconds(response, 5); 242 return super.handle(request, response, command, errors); 243 } 244 }; 245 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 246 MockHttpServletResponse response = new MockHttpServletResponse(); 247 mc.handleRequest(request, response); 248 assertTrue("Correct expires header", response.getHeader("Expires") != null); 249 assertTrue("Correct cache control", response.getHeader("Cache-Control").equals("max-age=5")); 250 } 251 252 public void testCachingWithCustomApplyCacheSecondsCall2() throws Exception { 253 TestController mc = new TestController() { 254 protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { 255 applyCacheSeconds(response, 0); 256 return super.handle(request, response, command, errors); 257 } 258 }; 259 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 260 MockHttpServletResponse response = new MockHttpServletResponse(); 261 mc.handleRequest(request, response); 262 assertTrue("Correct expires header", response.getHeader("Expires").equals(new Long (1))); 263 List cacheControl = response.getHeaders("Cache-Control"); 264 assertTrue("Correct cache control", cacheControl.contains("no-cache")); 265 assertTrue("Correct cache control", cacheControl.contains("no-store")); 266 } 267 268 public void testCachingWithCustomApplyCacheSecondsCall3() throws Exception { 269 TestController mc = new TestController() { 270 protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { 271 applyCacheSeconds(response, -1); 272 return super.handle(request, response, command, errors); 273 } 274 }; 275 HttpServletRequest request = new MockHttpServletRequest("GET", "/ok.html"); 276 MockHttpServletResponse response = new MockHttpServletResponse(); 277 mc.handleRequest(request, response); 278 assertTrue("No expires header", response.getHeader("Expires") == null); 279 assertTrue("No cache control", response.getHeader("Cache-Control") == null); 280 } 281 282 public void testCustomDateEditorWithAllowEmpty() throws Exception { 283 final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN); 284 TestController mc = new TestController() { 285 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { 286 binder.registerCustomEditor(Date .class, new CustomDateEditor(df, true)); 287 } 288 }; 289 290 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 291 request.addParameter("date", "1.5.2003"); 292 MockHttpServletResponse response = new MockHttpServletResponse(); 293 ModelAndView mv = mc.handleRequest(request, response); 294 TestBean tb = (TestBean) mv.getModel().get("command"); 295 Errors errors = (Errors) mv.getModel().get("errors"); 296 assertTrue("No field error", !errors.hasFieldErrors("date")); 297 assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate())); 298 assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date"))); 299 300 request = new MockHttpServletRequest("GET", "/welcome.html"); 301 request.addParameter("date", ""); 302 response = new MockHttpServletResponse(); 303 mv = mc.handleRequest(request, response); 304 tb = (TestBean) mv.getModel().get("command"); 305 errors = (Errors) mv.getModel().get("errors"); 306 assertTrue("No field error", !errors.hasFieldErrors("date")); 307 assertTrue("Correct date property", tb.getDate() == null); 308 assertTrue("Correct date value", "".equals(errors.getFieldValue("date"))); 309 } 310 311 public void testCustomDateEditorWithoutAllowEmpty() throws Exception { 312 final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN); 313 TestController mc = new TestController() { 314 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { 315 binder.registerCustomEditor(Date .class, new CustomDateEditor(df, false)); 316 } 317 }; 318 319 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 320 request.addParameter("date", "1.5.2003"); 321 MockHttpServletResponse response = new MockHttpServletResponse(); 322 ModelAndView mv = mc.handleRequest(request, response); 323 TestBean tb = (TestBean) mv.getModel().get("command"); 324 Errors errors = (Errors) mv.getModel().get("errors"); 325 assertTrue("No field error", !errors.hasFieldErrors("date")); 326 assertTrue("Correct date property", df.parse("1.5.2003").equals(tb.getDate())); 327 assertTrue("Correct date value", "01.05.2003".equals(errors.getFieldValue("date"))); 328 329 request = new MockHttpServletRequest("GET", "/welcome.html"); 330 request.addParameter("date", ""); 331 response = new MockHttpServletResponse(); 332 mv = mc.handleRequest(request, response); 333 tb = (TestBean) mv.getModel().get("command"); 334 errors = (Errors) mv.getModel().get("errors"); 335 assertTrue("Has field error", errors.hasFieldErrors("date")); 336 assertTrue("Correct date property", tb.getDate() != null); 337 assertTrue("Correct date value", errors.getFieldValue("date") != null); 338 } 339 340 public void testCustomNumberEditorWithAllowEmpty() throws Exception { 341 final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); 342 343 TestController mc = new TestController() { 344 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { 345 binder.registerCustomEditor(Float .class, new CustomNumberEditor(Float .class, nf, true)); 346 } 347 }; 348 349 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 350 request.addParameter("myFloat", "5,1"); 351 MockHttpServletResponse response = new MockHttpServletResponse(); 352 ModelAndView mv = mc.handleRequest(request, response); 353 TestBean tb = (TestBean) mv.getModel().get("command"); 354 Errors errors = (Errors) mv.getModel().get("errors"); 355 assertTrue("No field error", !errors.hasFieldErrors("myFloat")); 356 assertTrue("Correct float property", (new Float (5.1)).equals(tb.getMyFloat())); 357 assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat"))); 358 359 request = new MockHttpServletRequest("GET", "/welcome.html"); 360 request.addParameter("myFloat", ""); 361 response = new MockHttpServletResponse(); 362 mv = mc.handleRequest(request, response); 363 tb = (TestBean) mv.getModel().get("command"); 364 errors = (Errors) mv.getModel().get("errors"); 365 assertTrue("No field error", !errors.hasFieldErrors("myFloat")); 366 assertTrue("Correct float property", tb.getMyFloat() == null); 367 assertTrue("Correct float value", "".equals(errors.getFieldValue("myFloat"))); 368 } 369 370 public void testCustomNumberEditorWithoutAllowEmpty() throws Exception { 371 final NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); 372 373 TestController mc = new TestController() { 374 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { 375 binder.registerCustomEditor(Float .class, new CustomNumberEditor(Float .class, nf, false)); 376 } 377 }; 378 379 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 380 request.addParameter("myFloat", "5,1"); 381 MockHttpServletResponse response = new MockHttpServletResponse(); 382 ModelAndView mv = mc.handleRequest(request, response); 383 TestBean tb = (TestBean) mv.getModel().get("command"); 384 Errors errors = (Errors) mv.getModel().get("errors"); 385 assertTrue("No field error", !errors.hasFieldErrors("myFloat")); 386 assertTrue("Correct float property", (new Float (5.1)).equals(tb.getMyFloat())); 387 assertTrue("Correct float value", "5,1".equals(errors.getFieldValue("myFloat"))); 388 389 request = new MockHttpServletRequest("GET", "/welcome.html"); 390 request.addParameter("myFloat", ""); 391 response = new MockHttpServletResponse(); 392 mv = mc.handleRequest(request, response); 393 tb = (TestBean) mv.getModel().get("command"); 394 errors = (Errors) mv.getModel().get("errors"); 395 assertTrue("Has field error", errors.hasFieldErrors("myFloat")); 396 assertTrue("Correct float property", tb.getMyFloat() != null); 397 assertTrue("Correct float value", errors.getFieldValue("myFloat") != null); 398 } 399 400 public void testResetEmptyStringField() throws Exception { 401 TestController mc = new TestController(); 402 403 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 404 request.addParameter("_name", "visible"); 405 request.addParameter("name", "test"); 406 MockHttpServletResponse response = new MockHttpServletResponse(); 407 ModelAndView mv = mc.handleRequest(request, response); 408 TestBean tb = (TestBean) mv.getModel().get("command"); 409 Errors errors = (Errors) mv.getModel().get("errors"); 410 assertTrue("Correct name property", "test".equals(tb.getName())); 411 assertTrue("Correct name value", "test".equals(errors.getFieldValue("name"))); 412 413 request = new MockHttpServletRequest("GET", "/welcome.html"); 414 request.addParameter("_name", "visible"); 415 request.addParameter("_someNonExistingField", "visible"); 416 mv = mc.handleRequest(request, response); 417 tb = (TestBean) mv.getModel().get("command"); 418 errors = (Errors) mv.getModel().get("errors"); 419 assertTrue("Correct name property", tb.getName() == null); 420 assertTrue("Correct name value", errors.getFieldValue("name") == null); 421 } 422 423 public void testResetEmptyBooleanField() throws Exception { 424 TestController mc = new TestController(); 425 426 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 427 request.addParameter("_postProcessed", "visible"); 428 request.addParameter("postProcessed", "true"); 429 MockHttpServletResponse response = new MockHttpServletResponse(); 430 ModelAndView mv = mc.handleRequest(request, response); 431 TestBean tb = (TestBean) mv.getModel().get("command"); 432 Errors errors = (Errors) mv.getModel().get("errors"); 433 assertTrue("Correct postProcessed property", tb.isPostProcessed()); 434 assertTrue("Correct postProcessed value", Boolean.TRUE.equals(errors.getFieldValue("postProcessed"))); 435 436 request = new MockHttpServletRequest("GET", "/welcome.html"); 437 request.addParameter("_postProcessed", "visible"); 438 mv = mc.handleRequest(request, response); 439 tb = (TestBean) mv.getModel().get("command"); 440 errors = (Errors) mv.getModel().get("errors"); 441 assertTrue("Correct postProcessed property", !tb.isPostProcessed()); 442 assertTrue("Correct postProcessed value", Boolean.FALSE.equals(errors.getFieldValue("postProcessed"))); 443 } 444 445 public void testResetEmptyStringArrayField() throws Exception { 446 TestController mc = new TestController(); 447 448 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 449 request.addParameter("_stringArray", "visible"); 450 request.addParameter("stringArray", "value1"); 451 MockHttpServletResponse response = new MockHttpServletResponse(); 452 ModelAndView mv = mc.handleRequest(request, response); 453 TestBean tb = (TestBean) mv.getModel().get("command"); 454 assertTrue("Correct stringArray property", 455 tb.getStringArray() != null && "value1".equals(tb.getStringArray()[0])); 456 457 request = new MockHttpServletRequest("GET", "/welcome.html"); 458 request.addParameter("_stringArray", "visible"); 459 mv = mc.handleRequest(request, response); 460 tb = (TestBean) mv.getModel().get("command"); 461 assertTrue("Correct stringArray property", tb.getStringArray() != null && tb.getStringArray().length == 0); 462 } 463 464 public void testResetEmptyFieldsTurnedOff() throws Exception { 465 TestController mc = new TestController() { 466 protected Object getCommand(HttpServletRequest request) throws Exception { 467 return new TestBean("original", 99); 468 } 469 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { 470 binder.setFieldMarkerPrefix(null); 471 } 472 }; 473 474 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 475 request.addParameter("_name", "visible"); 476 request.addParameter("name", "test"); 477 MockHttpServletResponse response = new MockHttpServletResponse(); 478 ModelAndView mv = mc.handleRequest(request, response); 479 TestBean tb = (TestBean) mv.getModel().get("command"); 480 Errors errors = (Errors) mv.getModel().get("errors"); 481 assertTrue("Correct name property", "test".equals(tb.getName())); 482 assertTrue("Correct name value", "test".equals(errors.getFieldValue("name"))); 483 484 request = new MockHttpServletRequest("GET", "/welcome.html"); 485 request.addParameter("_name", "true"); 486 mv = mc.handleRequest(request, response); 487 tb = (TestBean) mv.getModel().get("command"); 488 errors = (Errors) mv.getModel().get("errors"); 489 assertTrue("Correct name property", "original".equals(tb.getName())); 490 assertTrue("Correct name value", "original".equals(errors.getFieldValue("name"))); 491 } 492 493 494 private static class TestController extends AbstractCommandController { 495 496 private TestController() { 497 super(TestBean.class, "person"); 498 } 499 500 protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { 501 Map m = new HashMap (); 502 assertTrue("Command not null", command != null); 503 assertTrue("errors not null", errors != null); 504 m.put("errors", errors); 505 m.put("command", command); 506 return new ModelAndView(request.getServletPath(), m); 507 } 508 } 509 510 } 511 | Popular Tags |