@Test public void setDefaultStatusCode() { exceptionResolver.setDefaultErrorView("default-view"); exceptionResolver.setDefaultStatusCode(HttpServletResponse.SC_BAD_REQUEST); exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus()); }
@Test public void defaultErrorViewDifferentHandlerClass() { exceptionResolver.setDefaultErrorView("default-view"); exceptionResolver.setMappedHandlerClasses(new Class[] {String.class}); ModelAndView mav = exceptionResolver.resolveException(request, response, handler2, genericException); assertNull(mav); }
@Test public void defaultErrorViewDifferentHandler() { exceptionResolver.setDefaultErrorView("default-view"); exceptionResolver.setMappedHandlers(Collections.singleton(handler1)); ModelAndView mav = exceptionResolver.resolveException(request, response, handler2, genericException); assertNull(mav); }
@Test public void noDefaultStatusCodeInInclude() { exceptionResolver.setDefaultErrorView("default-view"); exceptionResolver.setDefaultStatusCode(HttpServletResponse.SC_BAD_REQUEST); request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "some path"); exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); }
@Test public void nullExceptionMappings() { exceptionResolver.setExceptionMappings(null); exceptionResolver.setDefaultErrorView("default-view"); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("default-view", mav.getViewName()); }
@Test public void nullExceptionAttribute() { exceptionResolver.setDefaultErrorView("default-view"); exceptionResolver.setExceptionAttribute(null); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("default-view", mav.getViewName()); assertNull(mav.getModel().get(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)); }
@Test public void missingExceptionInMapping() { Properties props = new Properties(); props.setProperty("SomeFooThrowable", "error"); exceptionResolver.setWarnLogCategory("HANDLER_EXCEPTION"); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertNull(mav); }
@Test public void simpleExceptionMappingWithHandlerClassSpecifiedButWrongHandler() { Properties props = new Properties(); props.setProperty("Exception", "error"); exceptionResolver.setMappedHandlerClasses(new Class[] {String.class}); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler2, genericException); assertNull(mav); }
@Test public void exactExceptionMappingWithHandlerInterfaceSpecified() { Properties props = new Properties(); props.setProperty("java.lang.Exception", "error"); exceptionResolver.setMappedHandlerClasses(new Class[] {Comparable.class}); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("error", mav.getViewName()); }
@Test public void exactExceptionMappingWithHandlerSpecified() { Properties props = new Properties(); props.setProperty("java.lang.Exception", "error"); exceptionResolver.setMappedHandlers(Collections.singleton(handler1)); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("error", mav.getViewName()); }
@Test public void simpleExceptionMapping() { Properties props = new Properties(); props.setProperty("Exception", "error"); exceptionResolver.setWarnLogCategory("HANDLER_EXCEPTION"); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("error", mav.getViewName()); }
@Test public void specificStatusCode() { exceptionResolver.setDefaultErrorView("default-view"); exceptionResolver.setDefaultStatusCode(HttpServletResponse.SC_BAD_REQUEST); Properties statusCodes = new Properties(); statusCodes.setProperty("default-view", "406"); exceptionResolver.setStatusCodes(statusCodes); exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals(HttpServletResponse.SC_NOT_ACCEPTABLE, response.getStatus()); }
@Test public void twoMappingsOneShortOneLong() { Properties props = new Properties(); props.setProperty("Exception", "error"); props.setProperty("AnotherException", "another-error"); exceptionResolver.setMappedHandlers(Collections.singleton(handler1)); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("error", mav.getViewName()); }
@Test public void twoMappingsThrowOddExceptionUseLongExceptionMapping() { Exception oddException = new SomeOddException(); Properties props = new Properties(); props.setProperty("java.lang.Exception", "error"); props.setProperty("SomeOddException", "another-error"); exceptionResolver.setMappedHandlers(Collections.singleton(handler1)); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, oddException); assertEquals("another-error", mav.getViewName()); }