1 16 17 package org.springframework.web.servlet.mvc; 18 19 import java.sql.SQLException ; 20 import java.util.HashMap ; 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.FatalBeanException; 32 import org.springframework.beans.TestBean; 33 import org.springframework.mock.web.MockHttpServletRequest; 34 import org.springframework.mock.web.MockHttpServletResponse; 35 import org.springframework.mock.web.MockHttpSession; 36 import org.springframework.web.bind.ServletRequestBindingException; 37 import org.springframework.web.servlet.ModelAndView; 38 import org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver; 39 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 40 import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; 41 import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver; 42 import org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver; 43 import org.springframework.web.servlet.support.SessionRequiredException; 44 45 50 public class MultiActionControllerTests extends TestCase { 51 52 public void testDefaultInternalPathMethodNameResolver() throws Exception { 53 doDefaultTestInternalPathMethodNameResolver("/foo.html", "foo"); 54 doDefaultTestInternalPathMethodNameResolver("/foo/bar.html", "bar"); 55 doDefaultTestInternalPathMethodNameResolver("/bugal.xyz", "bugal"); 56 doDefaultTestInternalPathMethodNameResolver("/x/y/z/q/foo.html", "foo"); 57 doDefaultTestInternalPathMethodNameResolver("qqq.q", "qqq"); 58 } 59 60 private void doDefaultTestInternalPathMethodNameResolver(String in, String expected) throws Exception { 61 MultiActionController rc = new MultiActionController(); 62 HttpServletRequest request = new MockHttpServletRequest("GET", in); 63 String actual = rc.getMethodNameResolver().getHandlerMethodName(request); 64 assertEquals("Wrong method name resolved", expected, actual); 65 } 66 67 public void testCustomizedInternalPathMethodNameResolver() throws Exception { 68 doTestCustomizedInternalPathMethodNameResolver("/foo.html", "my", null, "myfoo"); 69 doTestCustomizedInternalPathMethodNameResolver("/foo/bar.html", null, "Handler", "barHandler"); 70 doTestCustomizedInternalPathMethodNameResolver("/Bugal.xyz", "your", "Method", "yourBugalMethod"); 71 } 72 73 private void doTestCustomizedInternalPathMethodNameResolver( 74 String in, String prefix, String suffix, String expected) throws Exception { 75 MultiActionController rc = new MultiActionController(); 76 InternalPathMethodNameResolver resolver = new InternalPathMethodNameResolver(); 77 if (prefix != null) { 78 resolver.setPrefix(prefix); 79 } 80 if (suffix != null) { 81 resolver.setSuffix(suffix); 82 } 83 rc.setMethodNameResolver(resolver); 84 HttpServletRequest request = new MockHttpServletRequest("GET", in); 85 String actual = rc.getMethodNameResolver().getHandlerMethodName(request); 86 assertEquals("Wrong method name resolved", expected, actual); 87 } 88 89 public void testParameterMethodNameResolver() throws NoSuchRequestHandlingMethodException { 90 ParameterMethodNameResolver mnr = new ParameterMethodNameResolver(); 91 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html"); 92 request.addParameter("action", "bar"); 93 assertEquals("bar", mnr.getHandlerMethodName(request)); 94 request = new MockHttpServletRequest("GET", "/foo.html"); 95 try { 96 mnr.getHandlerMethodName(request); 97 } 99 catch (NoSuchRequestHandlingMethodException ex) { 100 } 102 } 103 104 public void testParameterMethodNameResolverWithCustomParamName() throws NoSuchRequestHandlingMethodException { 105 ParameterMethodNameResolver mnr = new ParameterMethodNameResolver(); 106 mnr.setParamName("myparam"); 107 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html"); 108 request.addParameter("myparam", "bar"); 109 assertEquals("bar", mnr.getHandlerMethodName(request)); 110 } 111 112 public void testParameterMethodNameResolverWithParamNames() throws NoSuchRequestHandlingMethodException { 113 ParameterMethodNameResolver resolver = new ParameterMethodNameResolver(); 114 resolver.setDefaultMethodName("default"); 115 resolver.setMethodParamNames(new String [] {"hello", "spring", "colin"}); 116 Properties logicalMappings = new Properties (); 117 logicalMappings.setProperty("hello", "goodbye"); 118 logicalMappings.setProperty("nina", "colin"); 119 resolver.setLogicalMappings(logicalMappings); 120 121 MockHttpServletRequest request = new MockHttpServletRequest(); 122 request.addParameter("nomatch", "whatever"); 123 try { 124 resolver.getHandlerMethodName(request); 125 } 126 catch (NoSuchRequestHandlingMethodException ex) { 127 } 129 130 request = new MockHttpServletRequest(); 132 request.addParameter("this will not match anything", "whatever"); 133 assertEquals("default", resolver.getHandlerMethodName(request)); 134 135 request = new MockHttpServletRequest(); 137 request.addParameter("action", "reset"); 138 assertEquals("reset", resolver.getHandlerMethodName(request)); 139 request = new MockHttpServletRequest(); 141 request.addParameter("action", "nina"); 142 assertEquals("colin", resolver.getHandlerMethodName(request)); 143 144 request = new MockHttpServletRequest(); 147 request.addParameter("hello", "whatever"); 148 assertEquals("goodbye", resolver.getHandlerMethodName(request)); 149 150 request = new MockHttpServletRequest(); 151 request.addParameter("spring", "whatever"); 152 assertEquals("spring", resolver.getHandlerMethodName(request)); 153 154 request = new MockHttpServletRequest(); 155 request.addParameter("hello", "whatever"); 156 request.addParameter("spring", "whatever"); 157 assertEquals("goodbye", resolver.getHandlerMethodName(request)); 158 159 request = new MockHttpServletRequest(); 160 request.addParameter("colin", "whatever"); 161 request.addParameter("spring", "whatever"); 162 assertEquals("spring", resolver.getHandlerMethodName(request)); 163 164 request = new MockHttpServletRequest(); 166 request.addParameter("spring.x", "whatever"); 167 assertEquals("spring", resolver.getHandlerMethodName(request)); 168 169 request = new MockHttpServletRequest(); 170 request.addParameter("hello.x", "whatever"); 171 request.addParameter("spring", "whatever"); 172 assertEquals("goodbye", resolver.getHandlerMethodName(request)); 173 } 174 175 public void testParameterMethodNameResolverWithDefaultMethodName() throws NoSuchRequestHandlingMethodException { 176 ParameterMethodNameResolver mnr = new ParameterMethodNameResolver(); 177 mnr.setDefaultMethodName("foo"); 178 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.html"); 179 request.addParameter("action", "bar"); 180 assertEquals("bar", mnr.getHandlerMethodName(request)); 181 request = new MockHttpServletRequest("GET", "/foo.html"); 182 assertEquals("foo", mnr.getHandlerMethodName(request)); 183 } 184 185 public void testInvokesCorrectMethod() throws Exception { 186 TestMaController mc = new TestMaController(); 187 HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 188 HttpServletResponse response = new MockHttpServletResponse(); 189 Properties p = new Properties (); 190 p.put("/welcome.html", "welcome"); 191 PropertiesMethodNameResolver mnr = new PropertiesMethodNameResolver(); 192 mnr.setMappings(p); 193 mc.setMethodNameResolver(mnr); 194 195 ModelAndView mv = mc.handleRequest(request, response); 196 assertTrue("Invoked welcome method", mc.wasInvoked("welcome")); 197 assertTrue("view name is welcome", mv.getViewName().equals("welcome")); 198 assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); 199 200 mc = new TestMaController(); 201 request = new MockHttpServletRequest("GET", "/subdir/test.html"); 202 response = new MockHttpServletResponse(); 203 mv = mc.handleRequest(request, response); 204 assertTrue("Invoked test method", mc.wasInvoked("test")); 205 assertTrue("view name is subdir_test", mv.getViewName().equals("test")); 206 assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); 207 } 208 209 public void testPathMatching() throws Exception { 210 TestMaController mc = new TestMaController(); 211 HttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 212 HttpServletResponse response = new MockHttpServletResponse(); 213 Properties p = new Properties (); 214 p.put("/welc*.html", "welcome"); 215 PropertiesMethodNameResolver mn = new PropertiesMethodNameResolver(); 216 mn.setMappings(p); 217 mc.setMethodNameResolver(mn); 218 219 ModelAndView mv = mc.handleRequest(request, response); 220 assertTrue("Invoked welcome method", mc.wasInvoked("welcome")); 221 assertTrue("view name is welcome", mv.getViewName().equals("welcome")); 222 assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); 223 224 mc = new TestMaController(); 225 mc.setMethodNameResolver(mn); 226 request = new MockHttpServletRequest("GET", "/nomatch"); 227 response = new MockHttpServletResponse(); 228 try { 229 mv = mc.handleRequest(request, response); 230 } 231 catch (Exception e) { 232 } 234 assertFalse("Not invoking welcome method", mc.wasInvoked("welcome")); 235 assertTrue("No method invoked", mc.getInvokedMethods() == 0); 236 } 237 238 public static class TestDelegate { 239 240 boolean invoked; 241 242 public ModelAndView test(HttpServletRequest request, HttpServletResponse response) { 243 invoked = true; 244 return new ModelAndView("test"); 245 } 246 } 247 248 public void testInvokesCorrectMethodOnDelegate() throws Exception { 249 MultiActionController mac = new MultiActionController(); 250 TestDelegate d = new TestDelegate(); 251 mac.setDelegate(d); 252 HttpServletRequest request = new MockHttpServletRequest("GET", "/test.html"); 253 HttpServletResponse response = new MockHttpServletResponse(); 254 ModelAndView mv = mac.handleRequest(request, response); 255 assertTrue("view name is test", mv.getViewName().equals("test")); 256 assertTrue("Delegate was invoked", d.invoked); 257 } 258 259 public void testInvokesCorrectMethodWithSession() throws Exception { 260 TestMaController mc = new TestMaController(); 261 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/inSession.html"); 262 request.setSession(new MockHttpSession(null)); 263 HttpServletResponse response = new MockHttpServletResponse(); 264 ModelAndView mv = mc.handleRequest(request, response); 265 assertTrue("Invoked inSession method", mc.wasInvoked("inSession")); 266 assertTrue("view name is welcome", mv.getViewName().equals("inSession")); 267 assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); 268 269 request = new MockHttpServletRequest("GET", "/inSession.html"); 270 response = new MockHttpServletResponse(); 271 try { 272 273 mc.handleRequest(request, response); 274 fail("Should have rejected request without session"); 275 } 276 catch (ServletException ex) { 277 } 279 } 280 281 public void testInvokesCommandMethodNoSession() throws Exception { 282 TestMaController mc = new TestMaController(); 283 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandNoSession.html"); 284 request.addParameter("name", "rod"); 285 request.addParameter("age", "32"); 286 HttpServletResponse response = new MockHttpServletResponse(); 287 ModelAndView mv = mc.handleRequest(request, response); 288 assertTrue("Invoked commandNoSession method", mc.wasInvoked("commandNoSession")); 289 assertTrue("view name is commandNoSession", mv.getViewName().equals("commandNoSession")); 290 assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); 291 292 } 300 301 302 public void testInvokesCommandMethodWithSession() throws Exception { 303 TestMaController mc = new TestMaController(); 304 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/commandInSession.html"); 305 request.addParameter("name", "rod"); 306 request.addParameter("age", "32"); 307 308 request.setSession(new MockHttpSession(null)); 309 HttpServletResponse response = new MockHttpServletResponse(); 310 ModelAndView mv = mc.handleRequest(request, response); 311 assertTrue("Invoked commandInSession method", mc.wasInvoked("commandInSession")); 312 assertTrue("view name is commandInSession", mv.getViewName().equals("commandInSession")); 313 assertTrue("Only one method invoked", mc.getInvokedMethods() == 1); 314 315 request = new MockHttpServletRequest("GET", "/commandInSession.html"); 316 response = new MockHttpServletResponse(); 317 try { 318 319 mc.handleRequest(request, response); 320 fail("Should have rejected request without session"); 321 } 322 catch (ServletException ex) { 323 } 325 } 326 327 public void testSessionRequiredCatchable() throws Exception { 328 HttpServletRequest request = new MockHttpServletRequest("GET", "/testSession.html"); 329 HttpServletResponse response = new MockHttpServletResponse(); 330 TestMaController contr = new TestSessionRequiredController(); 331 try { 332 contr.handleRequest(request, response); 333 fail("Should have thrown exception"); 334 } 335 catch (SessionRequiredException ex) { 336 } 338 request = new MockHttpServletRequest("GET", "/testSession.html"); 339 response = new MockHttpServletResponse(); 340 contr = new TestSessionRequiredExceptionHandler(); 341 ModelAndView mv = contr.handleRequest(request, response); 342 assertTrue("Name is ok", mv.getViewName().equals("handle(SRE)")); 343 } 344 345 private void testExceptionNoHandler(TestMaController mc, Throwable t) throws Exception { 346 HttpServletRequest request = new MockHttpServletRequest("GET", "/testException.html"); 347 request.setAttribute(TestMaController.THROWABLE_ATT, t); 348 HttpServletResponse response = new MockHttpServletResponse(); 349 try { 350 mc.handleRequest(request, response); 351 fail("Should have thrown exception"); 352 } 353 catch (Throwable ex) { 354 assertTrue(ex.equals(t)); 355 } 356 } 357 358 private void testExceptionNoHandler(Throwable t) throws Exception { 359 testExceptionNoHandler(new TestMaController(), t); 360 } 361 362 public void testExceptionNoHandler() throws Exception { 363 testExceptionNoHandler(new Exception ()); 364 365 testExceptionNoHandler(new ServletException ()); 367 368 testExceptionNoHandler(new ServletRequestBindingException("foo")); 370 testExceptionNoHandler(new RuntimeException ()); 371 testExceptionNoHandler(new Error ()); 372 } 373 374 public void testLastModifiedDefault() throws Exception { 375 TestMaController mc = new TestMaController(); 376 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 377 long lastMod = mc.getLastModified(request); 378 assertTrue("default last modified is -1", lastMod == -1L); 379 } 380 381 public void testLastModifiedWithMethod() throws Exception { 382 LastModController mc = new LastModController(); 383 MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome.html"); 384 long lastMod = mc.getLastModified(request); 385 assertTrue("last modified with method is > -1", lastMod == mc.getLastModified(request)); 386 } 387 388 private ModelAndView testHandlerCaughtException(TestMaController mc, Throwable t) throws Exception { 389 HttpServletRequest request = new MockHttpServletRequest("GET", "/testException.html"); 390 request.setAttribute(TestMaController.THROWABLE_ATT, t); 391 HttpServletResponse response = new MockHttpServletResponse(); 392 ModelAndView mv = mc.handleRequest(request, response); 393 return mv; 394 } 395 396 public void testHandlerCaughtException() throws Exception { 397 TestMaController mc = new TestExceptionHandler(); 398 ModelAndView mv = testHandlerCaughtException(mc, new Exception ()); 399 assertTrue("mv name is handle(Exception)", mv.getViewName().equals("handle(Exception)")); 400 assertTrue("Invoked correct method", mc.wasInvoked("handle(Exception)")); 401 402 testExceptionNoHandler(mc, new Error ()); 404 405 mc = new TestServletExceptionHandler(); 406 mv = testHandlerCaughtException(mc, new ServletException ()); 407 assertTrue(mv.getViewName().equals("handle(ServletException)")); 408 assertTrue("Invoke correct method", mc.wasInvoked("handle(ServletException)")); 409 410 mv = testHandlerCaughtException(mc, new ServletRequestBindingException("foo")); 411 assertTrue(mv.getViewName().equals("handle(ServletException)")); 412 assertTrue("Invoke correct method", mc.wasInvoked("handle(ServletException)")); 413 414 testExceptionNoHandler(mc, new RuntimeException ()); 416 testExceptionNoHandler(mc, new Error ()); 417 testExceptionNoHandler(mc, new SQLException ()); 418 testExceptionNoHandler(mc, new Exception ()); 419 420 421 mc = new TestRuntimeExceptionHandler(); 422 mv = testHandlerCaughtException(mc, new RuntimeException ()); 423 assertTrue(mv.getViewName().equals("handle(RTE)")); 424 assertTrue("Invoke correct method", mc.wasInvoked("handle(RTE)")); 425 mv = testHandlerCaughtException(mc, new FatalBeanException(null, null)); 426 assertTrue(mv.getViewName().equals("handle(RTE)")); 427 assertTrue("Invoke correct method", mc.wasInvoked("handle(RTE)")); 428 429 testExceptionNoHandler(mc, new SQLException ()); 430 testExceptionNoHandler(mc, new Exception ()); 431 } 432 433 434 435 public static class TestMaController extends MultiActionController { 436 437 public static final String THROWABLE_ATT = "throwable"; 438 439 440 protected Map invoked = new HashMap (); 441 442 public void clear() { 443 invoked.clear(); 444 } 445 446 public ModelAndView welcome(HttpServletRequest request, HttpServletResponse response) { 447 invoked.put("welcome", Boolean.TRUE); 448 return new ModelAndView("welcome"); 449 } 450 451 public ModelAndView commandNoSession(HttpServletRequest request, HttpServletResponse response, TestBean command) { 452 invoked.put("commandNoSession", Boolean.TRUE); 453 454 String pname = request.getParameter("name"); 455 String page = request.getParameter("age"); 456 if (pname == null) 458 assertTrue("name null", command.getName() == null); 459 else 460 assertTrue("name param set", pname.equals(command.getName())); 461 return new ModelAndView("commandNoSession"); 467 } 468 469 public ModelAndView inSession(HttpServletRequest request, HttpServletResponse response, HttpSession session) { 470 invoked.put("inSession", Boolean.TRUE); 471 assertTrue("session non null", session != null); 472 return new ModelAndView("inSession"); 473 } 474 475 public ModelAndView commandInSession(HttpServletRequest request, HttpServletResponse response, HttpSession session, TestBean command) { 476 invoked.put("commandInSession", Boolean.TRUE); 477 assertTrue("session non null", session != null); 478 return new ModelAndView("commandInSession"); 479 } 480 481 public ModelAndView test(HttpServletRequest request, HttpServletResponse response) { 482 invoked.put("test", Boolean.TRUE); 483 return new ModelAndView("test"); 484 } 485 486 public ModelAndView testException(HttpServletRequest request, HttpServletResponse response) throws Throwable { 487 invoked.put("testException", Boolean.TRUE); 488 Throwable t = (Throwable ) request.getAttribute(THROWABLE_ATT); 489 if (t != null) 490 throw t; 491 else 492 return new ModelAndView("no throwable"); 493 } 494 495 public boolean wasInvoked(String method) { 496 return invoked.get(method) != null; 497 } 498 499 public int getInvokedMethods() { 500 return invoked.size(); 501 } 502 } 503 504 505 public static class TestExceptionHandler extends TestMaController { 506 507 public ModelAndView handleAnyException(HttpServletRequest request, HttpServletResponse response, Exception ex) { 508 invoked.put("handle(Exception)", Boolean.TRUE); 509 return new ModelAndView("handle(Exception)"); 510 } 511 } 512 513 514 public static class TestRuntimeExceptionHandler extends TestMaController { 515 516 public ModelAndView handleRuntimeProblem(HttpServletRequest request, HttpServletResponse response, RuntimeException ex) { 517 invoked.put("handle(RTE)", Boolean.TRUE); 518 return new ModelAndView("handle(RTE)"); 519 } 520 } 521 522 523 public static class TestSessionRequiredController extends TestMaController { 524 525 public ModelAndView testSession(HttpServletRequest request, HttpServletResponse response, HttpSession sess) { 526 return null; 527 } 528 } 529 530 531 532 public static class TestSessionRequiredExceptionHandler extends TestSessionRequiredController { 533 534 public ModelAndView handleServletException(HttpServletRequest request, HttpServletResponse response, SessionRequiredException ex) { 535 invoked.put("handle(SRE)", Boolean.TRUE); 536 return new ModelAndView("handle(SRE)"); 537 } 538 } 539 540 541 public static class TestServletExceptionHandler extends TestMaController { 542 543 public ModelAndView handleServletException(HttpServletRequest request, HttpServletResponse response, ServletException ex) { 544 invoked.put("handle(ServletException)", Boolean.TRUE); 545 return new ModelAndView("handle(ServletException)"); 546 } 547 } 548 549 550 public static class LastModController extends MultiActionController { 551 552 public static final String THROWABLE_ATT = "throwable"; 553 554 555 protected HashMap invoked = new HashMap (); 556 557 public void clear() { 558 invoked.clear(); 559 } 560 561 public ModelAndView welcome(HttpServletRequest request, HttpServletResponse response) { 562 invoked.put("welcome", Boolean.TRUE); 563 return new ModelAndView("welcome"); 564 } 565 566 567 public long welcomeLastModified(HttpServletRequest request) { 568 return 1111L; 569 } 570 } 571 572 } 573 | Popular Tags |