diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java index e0d1700c29..1f58d10d3a 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java @@ -25,8 +25,10 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.util.Arrays; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -35,7 +37,12 @@ import org.springframework.util.ClassUtils; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.method.HandlerMethod; +import org.springframework.web.method.annotation.support.ModelMethodProcessor; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.method.annotation.support.ServletRequestMethodArgumentResolver; +import org.springframework.web.servlet.mvc.method.annotation.support.ViewNameMethodReturnValueHandler; /** * Test fixture with {@link ExceptionHandlerExceptionResolver}. @@ -46,39 +53,92 @@ import org.springframework.web.servlet.ModelAndView; */ public class ExceptionHandlerExceptionResolverTests { + private static int RESOLVER_COUNT; + + private static int HANDLER_COUNT; + private ExceptionHandlerExceptionResolver resolver; private MockHttpServletRequest request; private MockHttpServletResponse response; + @BeforeClass + public static void setupOnce() { + ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); + resolver.afterPropertiesSet(); + RESOLVER_COUNT = resolver.getArgumentResolvers().getResolvers().size(); + HANDLER_COUNT = resolver.getReturnValueHandlers().getHandlers().size(); + } + @Before public void setUp() throws Exception { this.resolver = new ExceptionHandlerExceptionResolver(); - this.resolver.afterPropertiesSet(); this.request = new MockHttpServletRequest("GET", "/"); this.response = new MockHttpServletResponse(); } @Test - public void nullHandlerMethod() { - ModelAndView mav = this.resolver.resolveException(this.request, this.response, null, null); - assertNull(mav); + public void nullHandler() { + Object handler = null; + this.resolver.afterPropertiesSet(); + ModelAndView mav = this.resolver.resolveException(this.request, this.response, handler, null); + assertNull("Exception can be resolved only if there is a HandlerMethod", mav); + } + + @Test + public void setCustomArgumentResolvers() throws Exception { + HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver(); + this.resolver.setCustomArgumentResolvers(Arrays.asList(resolver)); + this.resolver.afterPropertiesSet(); + + assertTrue(this.resolver.getArgumentResolvers().getResolvers().contains(resolver)); + assertMethodProcessorCount(RESOLVER_COUNT + 1, HANDLER_COUNT); + } + + @Test + public void setArgumentResolvers() throws Exception { + HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver(); + this.resolver.setArgumentResolvers(Arrays.asList(resolver)); + this.resolver.afterPropertiesSet(); + + assertMethodProcessorCount(1, HANDLER_COUNT); + } + + @Test + public void setCustomReturnValueHandlers() { + HandlerMethodReturnValueHandler handler = new ViewNameMethodReturnValueHandler(); + this.resolver.setCustomReturnValueHandlers(Arrays.asList(handler)); + this.resolver.afterPropertiesSet(); + + assertTrue(this.resolver.getReturnValueHandlers().getHandlers().contains(handler)); + assertMethodProcessorCount(RESOLVER_COUNT, HANDLER_COUNT + 1); } @Test - public void noExceptionHandlerMethod() throws NoSuchMethodException { - Exception exception = new NullPointerException(); + public void setReturnValueHandlers() { + HandlerMethodReturnValueHandler handler = new ModelMethodProcessor(); + this.resolver.setReturnValueHandlers(Arrays.asList(handler)); + this.resolver.afterPropertiesSet(); + + assertMethodProcessorCount(RESOLVER_COUNT, 1); + } + + @Test + public void resolveNoExceptionHandlerForException() throws NoSuchMethodException { + Exception npe = new NullPointerException(); HandlerMethod handlerMethod = new HandlerMethod(new IoExceptionController(), "handle"); - ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, exception); + this.resolver.afterPropertiesSet(); + ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, npe); - assertNull(mav); + assertNull("NPE should not have been handled", mav); } @Test - public void modelAndViewController() throws NoSuchMethodException { + public void resolveExceptionModelAndView() throws NoSuchMethodException { IllegalArgumentException ex = new IllegalArgumentException("Bad argument"); HandlerMethod handlerMethod = new HandlerMethod(new ModelAndViewController(), "handle"); + this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); @@ -86,29 +146,37 @@ public class ExceptionHandlerExceptionResolverTests { assertEquals("errorView", mav.getViewName()); assertEquals("Bad argument", mav.getModel().get("detail")); } - + @Test - public void noModelAndView() throws UnsupportedEncodingException, NoSuchMethodException { + public void resolveExceptionResponseBody() throws UnsupportedEncodingException, NoSuchMethodException { IllegalArgumentException ex = new IllegalArgumentException(); - HandlerMethod handlerMethod = new HandlerMethod(new NoModelAndViewController(), "handle"); + HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); + this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); - + assertNotNull(mav); assertTrue(mav.isEmpty()); assertEquals("IllegalArgumentException", this.response.getContentAsString()); } - + @Test - public void responseBody() throws UnsupportedEncodingException, NoSuchMethodException { + public void resolveExceptionResponseWriter() throws UnsupportedEncodingException, NoSuchMethodException { IllegalArgumentException ex = new IllegalArgumentException(); - HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); + HandlerMethod handlerMethod = new HandlerMethod(new ResponseWriterController(), "handle"); + this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); - + assertNotNull(mav); assertTrue(mav.isEmpty()); assertEquals("IllegalArgumentException", this.response.getContentAsString()); } + + private void assertMethodProcessorCount(int resolverCount, int handlerCount) { + assertEquals(resolverCount, this.resolver.getArgumentResolvers().getResolvers().size()); + assertEquals(handlerCount, this.resolver.getReturnValueHandlers().getHandlers().size()); + } + @Controller static class ModelAndViewController { @@ -121,12 +189,12 @@ public class ExceptionHandlerExceptionResolverTests { } @Controller - static class NoModelAndViewController { + static class ResponseWriterController { public void handle() {} @ExceptionHandler - public void handle(Exception ex, Writer writer) throws IOException { + public void handleException(Exception ex, Writer writer) throws IOException { writer.write(ClassUtils.getShortName(ex.getClass())); } } @@ -138,7 +206,7 @@ public class ExceptionHandlerExceptionResolverTests { @ExceptionHandler @ResponseBody - public String handle(Exception ex) { + public String handleException(Exception ex) { return ClassUtils.getShortName(ex.getClass()); } } @@ -146,8 +214,10 @@ public class ExceptionHandlerExceptionResolverTests { @Controller static class IoExceptionController { + public void handle() {} + @ExceptionHandler(value=IOException.class) - public void handle() { + public void handleException() { } } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java index 1b1b648f87..d7f4328a7e 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java @@ -86,9 +86,9 @@ public class RequestMappingHandlerAdapterTests { @Test public void cacheControlWithoutSessionAttributes() throws Exception { HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle"); - handlerAdapter.afterPropertiesSet(); - handlerAdapter.setCacheSeconds(100); - handlerAdapter.handle(request, response, handlerMethod); + this.handlerAdapter.afterPropertiesSet(); + this.handlerAdapter.setCacheSeconds(100); + this.handlerAdapter.handle(this.request, this.response, handlerMethod); assertTrue(response.getHeader("Cache-Control").toString().contains("max-age")); } @@ -96,11 +96,11 @@ public class RequestMappingHandlerAdapterTests { @Test public void cacheControlWithSessionAttributes() throws Exception { SessionAttributeController handler = new SessionAttributeController(); - handlerAdapter.afterPropertiesSet(); - handlerAdapter.setCacheSeconds(100); - handlerAdapter.handle(request, response, handlerMethod(handler, "handle")); + this.handlerAdapter.afterPropertiesSet(); + this.handlerAdapter.setCacheSeconds(100); + this.handlerAdapter.handle(this.request, this.response, handlerMethod(handler, "handle")); - assertEquals("no-cache", response.getHeader("Cache-Control")); + assertEquals("no-cache", this.response.getHeader("Cache-Control")); } @Test @@ -109,15 +109,15 @@ public class RequestMappingHandlerAdapterTests { HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor(); HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler(); - handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver)); - handlerAdapter.setReturnValueHandlers(Arrays.asList(viewHandler)); - handlerAdapter.setIgnoreDefaultModelOnRedirect(true); - handlerAdapter.afterPropertiesSet(); + this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver)); + this.handlerAdapter.setReturnValueHandlers(Arrays.asList(viewHandler)); + this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true); + this.handlerAdapter.afterPropertiesSet(); - request.setAttribute(FlashMapManager.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); + this.request.setAttribute(FlashMapManager.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); HandlerMethod handlerMethod = handlerMethod(new RedirectAttributeController(), "handle", Model.class); - ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod); + ModelAndView mav = this.handlerAdapter.handle(request, response, handlerMethod); assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty()); } @@ -144,8 +144,8 @@ public class RequestMappingHandlerAdapterTests { @Test public void setInitBinderArgumentResolvers() throws Exception { HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver(); - handlerAdapter.setInitBinderArgumentResolvers(Arrays.asList(resolver)); - handlerAdapter.afterPropertiesSet(); + this.handlerAdapter.setInitBinderArgumentResolvers(Arrays.asList(resolver)); + this.handlerAdapter.afterPropertiesSet(); assertMethodProcessorCount(RESOLVER_COUNT, 1, HANDLER_COUNT); } @@ -153,8 +153,8 @@ public class RequestMappingHandlerAdapterTests { @Test public void setCustomReturnValueHandlers() { HandlerMethodReturnValueHandler handler = new ViewNameMethodReturnValueHandler(); - handlerAdapter.setCustomReturnValueHandlers(Arrays.asList(handler)); - handlerAdapter.afterPropertiesSet(); + this.handlerAdapter.setCustomReturnValueHandlers(Arrays.asList(handler)); + this.handlerAdapter.afterPropertiesSet(); assertTrue(this.handlerAdapter.getReturnValueHandlers().getHandlers().contains(handler)); assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, HANDLER_COUNT + 1); @@ -163,8 +163,8 @@ public class RequestMappingHandlerAdapterTests { @Test public void setReturnValueHandlers() { HandlerMethodReturnValueHandler handler = new ModelMethodProcessor(); - handlerAdapter.setReturnValueHandlers(Arrays.asList(handler)); - handlerAdapter.afterPropertiesSet(); + this.handlerAdapter.setReturnValueHandlers(Arrays.asList(handler)); + this.handlerAdapter.afterPropertiesSet(); assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, 1); }