Add ExceptionHandlerExceptionResolver tests for custom arg resolvers
This commit is contained in:
parent
7fec9d7fa8
commit
a33d277509
|
|
@ -25,8 +25,10 @@ import static org.junit.Assert.assertTrue;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockHttpServletResponse;
|
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.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.method.HandlerMethod;
|
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.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}.
|
* Test fixture with {@link ExceptionHandlerExceptionResolver}.
|
||||||
|
|
@ -46,39 +53,92 @@ import org.springframework.web.servlet.ModelAndView;
|
||||||
*/
|
*/
|
||||||
public class ExceptionHandlerExceptionResolverTests {
|
public class ExceptionHandlerExceptionResolverTests {
|
||||||
|
|
||||||
|
private static int RESOLVER_COUNT;
|
||||||
|
|
||||||
|
private static int HANDLER_COUNT;
|
||||||
|
|
||||||
private ExceptionHandlerExceptionResolver resolver;
|
private ExceptionHandlerExceptionResolver resolver;
|
||||||
|
|
||||||
private MockHttpServletRequest request;
|
private MockHttpServletRequest request;
|
||||||
|
|
||||||
private MockHttpServletResponse response;
|
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
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
this.resolver = new ExceptionHandlerExceptionResolver();
|
this.resolver = new ExceptionHandlerExceptionResolver();
|
||||||
this.resolver.afterPropertiesSet();
|
|
||||||
this.request = new MockHttpServletRequest("GET", "/");
|
this.request = new MockHttpServletRequest("GET", "/");
|
||||||
this.response = new MockHttpServletResponse();
|
this.response = new MockHttpServletResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nullHandlerMethod() {
|
public void nullHandler() {
|
||||||
ModelAndView mav = this.resolver.resolveException(this.request, this.response, null, null);
|
Object handler = null;
|
||||||
assertNull(mav);
|
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
|
@Test
|
||||||
public void noExceptionHandlerMethod() throws NoSuchMethodException {
|
public void setReturnValueHandlers() {
|
||||||
Exception exception = new NullPointerException();
|
HandlerMethodReturnValueHandler handler = new ModelMethodProcessor();
|
||||||
HandlerMethod handlerMethod = new HandlerMethod(new IoExceptionController(), "handle");
|
this.resolver.setReturnValueHandlers(Arrays.asList(handler));
|
||||||
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, exception);
|
this.resolver.afterPropertiesSet();
|
||||||
|
|
||||||
assertNull(mav);
|
assertMethodProcessorCount(RESOLVER_COUNT, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void modelAndViewController() throws NoSuchMethodException {
|
public void resolveNoExceptionHandlerForException() throws NoSuchMethodException {
|
||||||
|
Exception npe = new NullPointerException();
|
||||||
|
HandlerMethod handlerMethod = new HandlerMethod(new IoExceptionController(), "handle");
|
||||||
|
this.resolver.afterPropertiesSet();
|
||||||
|
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, npe);
|
||||||
|
|
||||||
|
assertNull("NPE should not have been handled", mav);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void resolveExceptionModelAndView() throws NoSuchMethodException {
|
||||||
IllegalArgumentException ex = new IllegalArgumentException("Bad argument");
|
IllegalArgumentException ex = new IllegalArgumentException("Bad argument");
|
||||||
HandlerMethod handlerMethod = new HandlerMethod(new ModelAndViewController(), "handle");
|
HandlerMethod handlerMethod = new HandlerMethod(new ModelAndViewController(), "handle");
|
||||||
|
this.resolver.afterPropertiesSet();
|
||||||
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
|
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
|
||||||
|
|
||||||
assertNotNull(mav);
|
assertNotNull(mav);
|
||||||
|
|
@ -86,27 +146,35 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
assertEquals("errorView", mav.getViewName());
|
assertEquals("errorView", mav.getViewName());
|
||||||
assertEquals("Bad argument", mav.getModel().get("detail"));
|
assertEquals("Bad argument", mav.getModel().get("detail"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void noModelAndView() throws UnsupportedEncodingException, NoSuchMethodException {
|
public void resolveExceptionResponseBody() throws UnsupportedEncodingException, NoSuchMethodException {
|
||||||
IllegalArgumentException ex = new IllegalArgumentException();
|
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 resolveExceptionResponseWriter() throws UnsupportedEncodingException, NoSuchMethodException {
|
||||||
|
IllegalArgumentException ex = new IllegalArgumentException();
|
||||||
|
HandlerMethod handlerMethod = new HandlerMethod(new ResponseWriterController(), "handle");
|
||||||
|
this.resolver.afterPropertiesSet();
|
||||||
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
|
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
|
||||||
|
|
||||||
assertNotNull(mav);
|
assertNotNull(mav);
|
||||||
assertTrue(mav.isEmpty());
|
assertTrue(mav.isEmpty());
|
||||||
assertEquals("IllegalArgumentException", this.response.getContentAsString());
|
assertEquals("IllegalArgumentException", this.response.getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
private void assertMethodProcessorCount(int resolverCount, int handlerCount) {
|
||||||
public void responseBody() throws UnsupportedEncodingException, NoSuchMethodException {
|
assertEquals(resolverCount, this.resolver.getArgumentResolvers().getResolvers().size());
|
||||||
IllegalArgumentException ex = new IllegalArgumentException();
|
assertEquals(handlerCount, this.resolver.getReturnValueHandlers().getHandlers().size());
|
||||||
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
|
|
||||||
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
|
|
||||||
|
|
||||||
assertNotNull(mav);
|
|
||||||
assertTrue(mav.isEmpty());
|
|
||||||
assertEquals("IllegalArgumentException", this.response.getContentAsString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
|
@ -121,12 +189,12 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
static class NoModelAndViewController {
|
static class ResponseWriterController {
|
||||||
|
|
||||||
public void handle() {}
|
public void handle() {}
|
||||||
|
|
||||||
@ExceptionHandler
|
@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()));
|
writer.write(ClassUtils.getShortName(ex.getClass()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -138,7 +206,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
|
|
||||||
@ExceptionHandler
|
@ExceptionHandler
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String handle(Exception ex) {
|
public String handleException(Exception ex) {
|
||||||
return ClassUtils.getShortName(ex.getClass());
|
return ClassUtils.getShortName(ex.getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -146,8 +214,10 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
@Controller
|
@Controller
|
||||||
static class IoExceptionController {
|
static class IoExceptionController {
|
||||||
|
|
||||||
|
public void handle() {}
|
||||||
|
|
||||||
@ExceptionHandler(value=IOException.class)
|
@ExceptionHandler(value=IOException.class)
|
||||||
public void handle() {
|
public void handleException() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,9 +86,9 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
@Test
|
@Test
|
||||||
public void cacheControlWithoutSessionAttributes() throws Exception {
|
public void cacheControlWithoutSessionAttributes() throws Exception {
|
||||||
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
|
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
|
||||||
handlerAdapter.afterPropertiesSet();
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
handlerAdapter.setCacheSeconds(100);
|
this.handlerAdapter.setCacheSeconds(100);
|
||||||
handlerAdapter.handle(request, response, handlerMethod);
|
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
||||||
|
|
||||||
assertTrue(response.getHeader("Cache-Control").toString().contains("max-age"));
|
assertTrue(response.getHeader("Cache-Control").toString().contains("max-age"));
|
||||||
}
|
}
|
||||||
|
|
@ -96,11 +96,11 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
@Test
|
@Test
|
||||||
public void cacheControlWithSessionAttributes() throws Exception {
|
public void cacheControlWithSessionAttributes() throws Exception {
|
||||||
SessionAttributeController handler = new SessionAttributeController();
|
SessionAttributeController handler = new SessionAttributeController();
|
||||||
handlerAdapter.afterPropertiesSet();
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
handlerAdapter.setCacheSeconds(100);
|
this.handlerAdapter.setCacheSeconds(100);
|
||||||
handlerAdapter.handle(request, response, handlerMethod(handler, "handle"));
|
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
|
@Test
|
||||||
|
|
@ -109,15 +109,15 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
|
HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
|
||||||
HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();
|
HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();
|
||||||
|
|
||||||
handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
|
this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
|
||||||
handlerAdapter.setReturnValueHandlers(Arrays.asList(viewHandler));
|
this.handlerAdapter.setReturnValueHandlers(Arrays.asList(viewHandler));
|
||||||
handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
|
this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
|
||||||
handlerAdapter.afterPropertiesSet();
|
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);
|
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());
|
assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
@ -144,8 +144,8 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
@Test
|
@Test
|
||||||
public void setInitBinderArgumentResolvers() throws Exception {
|
public void setInitBinderArgumentResolvers() throws Exception {
|
||||||
HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
|
HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
|
||||||
handlerAdapter.setInitBinderArgumentResolvers(Arrays.<HandlerMethodArgumentResolver>asList(resolver));
|
this.handlerAdapter.setInitBinderArgumentResolvers(Arrays.<HandlerMethodArgumentResolver>asList(resolver));
|
||||||
handlerAdapter.afterPropertiesSet();
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
|
|
||||||
assertMethodProcessorCount(RESOLVER_COUNT, 1, HANDLER_COUNT);
|
assertMethodProcessorCount(RESOLVER_COUNT, 1, HANDLER_COUNT);
|
||||||
}
|
}
|
||||||
|
|
@ -153,8 +153,8 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
@Test
|
@Test
|
||||||
public void setCustomReturnValueHandlers() {
|
public void setCustomReturnValueHandlers() {
|
||||||
HandlerMethodReturnValueHandler handler = new ViewNameMethodReturnValueHandler();
|
HandlerMethodReturnValueHandler handler = new ViewNameMethodReturnValueHandler();
|
||||||
handlerAdapter.setCustomReturnValueHandlers(Arrays.asList(handler));
|
this.handlerAdapter.setCustomReturnValueHandlers(Arrays.asList(handler));
|
||||||
handlerAdapter.afterPropertiesSet();
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
|
|
||||||
assertTrue(this.handlerAdapter.getReturnValueHandlers().getHandlers().contains(handler));
|
assertTrue(this.handlerAdapter.getReturnValueHandlers().getHandlers().contains(handler));
|
||||||
assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, HANDLER_COUNT + 1);
|
assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, HANDLER_COUNT + 1);
|
||||||
|
|
@ -163,8 +163,8 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
@Test
|
@Test
|
||||||
public void setReturnValueHandlers() {
|
public void setReturnValueHandlers() {
|
||||||
HandlerMethodReturnValueHandler handler = new ModelMethodProcessor();
|
HandlerMethodReturnValueHandler handler = new ModelMethodProcessor();
|
||||||
handlerAdapter.setReturnValueHandlers(Arrays.asList(handler));
|
this.handlerAdapter.setReturnValueHandlers(Arrays.asList(handler));
|
||||||
handlerAdapter.afterPropertiesSet();
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
|
|
||||||
assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, 1);
|
assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, 1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue