ValidationFailureViewHandlerExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { if (!(ex instanceof BindException)) { return null; } if (!(handler instanceof HandlerMethod)) { return null; } ModelAndView mav = null; ValidationFailureViews failViews = (ValidationFailureViews) ((HandlerMethod) handler).getMethodAnnotation(ValidationFailureViews.class); ValidationFailureView failView = (ValidationFailureView) ((HandlerMethod) handler).getMethodAnnotation(ValidationFailureView.class); String viewName = null; if (failView != null) { viewName = failView.value(); } else if (failViews != null) { String[] controllerPaths = RequestUtils.getControllerRequestPaths((HandlerMethod) handler); viewName = findMatchingTargetUrl(failViews.value(), controllerPaths, request); } if (viewName == null) { return null; } viewName = RequestUtils.replaceRestPathVariables(viewName, ((BindException)ex).getModel(), request); mav = new ModelAndView(viewName, ((BindException)ex).getModel()); return mav; }  ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex);  }
@Test public void testResolveUnsupportedException() { ModelAndView modelAndView = resolver.resolveException(request, response, null, argException); assertNull(modelAndView); }
@Test public void testResolveExceptionUnsupportedHandlerClass() { ModelAndView modelAndView = resolver.resolveException(request, response, resourceHandler, exception); assertNull(modelAndView); }
@Test public void testNoAnnotations() { HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testNoAnnotations")); ModelAndView modelAndView = resolver.resolveException(request, response, handlerMethod, exception); assertNull(modelAndView); }
@Test @ValidationFailureView("/foo/new") public void testSingleAnnotation() { HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testSingleAnnotation")); ModelAndView modelAndView = resolver.resolveException(request, response, handlerMethod, exception); assertEquals("/foo/new", modelAndView.getViewName()); }
@Test @ValidationFailureView(sourceUrl="/foo/new", value="/foo/new") public void testSourceUrlIgnoredForSingleAnnotation() { HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testSourceUrlIgnoredForSingleAnnotation")); ModelAndView modelAndView = resolver.resolveException(request, response, handlerMethod, exception); assertEquals("/foo/new", modelAndView.getViewName()); }
@Test(expected=IllegalArgumentException.class) @ValidationFailureViews({ @ValidationFailureView(sourceUrl="/foo/new", value="/foo/{id}"), @ValidationFailureView("/foo/new") }) public void testMultipleAnnotationsWithMissingSourceUrl() { HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testMultipleAnnotationsWithMissingSourceUrl")); resolver.resolveException(request, response, handlerMethod, exception); }
@Test(expected=IllegalArgumentException.class) @ValidationFailureViews({ @ValidationFailureView(sourceUrl="/foo/new", value="/foo/{id}"), @ValidationFailureView(sourceUrl="/foo/new", value="/foo/new") }) public void testMultipleAnnotationsWithDuplicateSourceUrl() { HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testMultipleAnnotationsWithDuplicateSourceUrl")); resolver.resolveException(request, response, handlerMethod, exception); }
@Test @ValidationFailureViews({ @ValidationFailureView(sourceUrl="/foo/new", value="/foo/{id}"), @ValidationFailureView(sourceUrl="/foo/{id}/edit", value="/foo/{id}") }) public void testMultipleAnnotationsNoMatch() { Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).thenReturn("/foo/list"); HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testMultipleAnnotationsNoMatch")); ModelAndView modelAndView = resolver.resolveException(request, response, handlerMethod, exception); assertNull(modelAndView); }
@Test @ValidationFailureViews({ @ValidationFailureView(sourceUrl="/foo/new", value="/foo/win"), @ValidationFailureView(sourceUrl="/foo/{id}/edit", value="/foo/lose") }) public void testMultipleAnnotations() { Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).thenReturn("/foo/new"); HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testMultipleAnnotations")); ModelAndView modelAndView = resolver.resolveException(request, response, handlerMethod, exception); assertEquals("/foo/win", modelAndView.getViewName()); }
@Test @ValidationFailureViews({ @ValidationFailureView(sourceUrl="/foo/new", value="/foo/{id}"), @ValidationFailureView(sourceUrl="/foo/{id}/edit", value="/foo/{id}") }) public void testRestPathVariableSubstitutionFromUriTemplateVariables() { Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> uriVariables = new HashMap<String, Object>(); Mockito.when(exception.getModel()).thenReturn(model); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriVariables); uriVariables.put("id", 5); Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).thenReturn("/foo/new"); HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testRestPathVariableSubstitutionFromUriTemplateVariables")); ModelAndView modelAndView = resolver.resolveException(request, response, handlerMethod, exception); assertEquals("/foo/5", modelAndView.getViewName()); }
@Test @ValidationFailureViews({ @ValidationFailureView(sourceUrl="/foo/new", value="/foo/{id}"), @ValidationFailureView(sourceUrl="/foo/{id}/edit", value="/foo/{id}") }) public void testRestPathVariableSubstitutionFromModel() { Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> uriVariables = new HashMap<String, Object>(); Mockito.when(exception.getModel()).thenReturn(model); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriVariables); model.put("id", 5); Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).thenReturn("/foo/new"); HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testRestPathVariableSubstitutionFromModel")); ModelAndView modelAndView = resolver.resolveException(request, response, handlerMethod, exception); assertEquals("/foo/5", modelAndView.getViewName()); }
@Test(expected=IllegalArgumentException.class) @ValidationFailureViews({ @ValidationFailureView(sourceUrl="/foo/new", value="/foo/{id}"), @ValidationFailureView(sourceUrl="/foo/{id}/edit", value="/foo/{id}") }) public void testRestPathVariableSubstitutionMissingVariable() { Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> uriVariables = new HashMap<String, Object>(); Mockito.when(exception.getModel()).thenReturn(model); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriVariables); Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).thenReturn("/foo/new"); HandlerMethod handlerMethod = new HandlerMethod(this, ReflectionUtils.findMethod(getClass(), "testRestPathVariableSubstitutionFromUriTemplateVariables")); resolver.resolveException(request, response, handlerMethod, exception); }