/** * Check to see if the view name in the ModelAndView matches the given * {@code expectedName}. * @param mav the ModelAndView to test against (never {@code null}) * @param expectedName the name of the model value */ public static void assertViewName(ModelAndView mav, String expectedName) { assertTrue("View name is not equal to '" + expectedName + "' but was '" + mav.getViewName() + "'", ObjectUtils.nullSafeEquals(expectedName, mav.getViewName())); }
/** * Assert the selected view name. */ public ResultMatcher name(final String expectedViewName) { return result -> { ModelAndView mav = result.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } assertEquals("View name", expectedViewName, mav.getViewName()); }; }
@Test public void threeMappings() { Exception oddException = new AnotherOddException(); Properties props = new Properties(); props.setProperty("java.lang.Exception", "error"); props.setProperty("SomeOddException", "another-error"); props.setProperty("AnotherOddException", "another-some-error"); exceptionResolver.setMappedHandlers(Collections.singleton(handler1)); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, oddException); assertEquals("another-some-error", mav.getViewName()); }
@Test public void twoMappings() { Properties props = new Properties(); props.setProperty("java.lang.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 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 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 exactExceptionMappingWithHandlerClassSpecified() { Properties props = new Properties(); props.setProperty("java.lang.Exception", "error"); exceptionResolver.setMappedHandlerClasses(String.class); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("error", mav.getViewName()); }
@Test public void exactExceptionMappingWithHandlerInterfaceSpecified() { Properties props = new Properties(); props.setProperty("java.lang.Exception", "error"); exceptionResolver.setMappedHandlerClasses(Comparable.class); exceptionResolver.setExceptionMappings(props); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("error", mav.getViewName()); }
@Test public void viewName() throws Exception { this.controller.setViewName("view"); ModelAndView modelAndView = this.controller.handleRequest(this.request, this.response); assertEquals("view", modelAndView.getViewName()); }
@Test public void defaultErrorView() { exceptionResolver.setDefaultErrorView("default-view"); ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, genericException); assertEquals("default-view", mav.getViewName()); assertEquals(genericException, mav.getModel().get(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)); }
@Test public void handleRequestWithoutViewName() throws Exception { ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse()); assertNull(mav.getViewName()); assertTrue(mav.getModel().isEmpty()); }
@Test public void parameterizableViewController() throws Exception { String viewName = "viewName"; ParameterizableViewController pvc = new ParameterizableViewController(); pvc.setViewName(viewName); // We don't care about the params. ModelAndView mv = pvc.handleRequest(new MockHttpServletRequest("GET", "foo.html"), new MockHttpServletResponse()); assertTrue("model has no data", mv.getModel().size() == 0); assertTrue("model has correct viewname", mv.getViewName().equals(viewName)); assertTrue("getViewName matches", pvc.getViewName().equals(viewName)); }
@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 handleRequestWithViewName() throws Exception { String viewName = "testView"; this.controller.setViewName(viewName); ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse()); assertEquals(viewName, mav.getViewName()); assertTrue(mav.getModel().isEmpty()); }
@Test public void withPlainFilename() throws Exception { UrlFilenameViewController ctrl = new UrlFilenameViewController(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index"); MockHttpServletResponse response = new MockHttpServletResponse(); ModelAndView mv = ctrl.handleRequest(request, response); assertEquals("index", mv.getViewName()); assertTrue(mv.getModel().isEmpty()); }
@Test public void withFilenamePlusExtension() throws Exception { UrlFilenameViewController ctrl = new UrlFilenameViewController(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); MockHttpServletResponse response = new MockHttpServletResponse(); ModelAndView mv = ctrl.handleRequest(request, response); assertEquals("index", mv.getViewName()); assertTrue(mv.getModel().isEmpty()); }
@Test public void multiLevel() throws Exception { UrlFilenameViewController ctrl = new UrlFilenameViewController(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html"); MockHttpServletResponse response = new MockHttpServletResponse(); ModelAndView mv = ctrl.handleRequest(request, response); assertEquals("docs/cvs/commit", mv.getViewName()); assertTrue(mv.getModel().isEmpty()); }
@Test 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); assertFalse(mav.isEmpty()); assertEquals("errorView", mav.getViewName()); assertEquals("Bad argument", mav.getModel().get("detail")); }
@Test public void multiLevelMappingWithFallback() throws Exception { UrlFilenameViewController ctrl = new UrlFilenameViewController(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html"); exposePathInMapping(request, "/docs/cvs/commit.html"); MockHttpServletResponse response = new MockHttpServletResponse(); ModelAndView mv = ctrl.handleRequest(request, response); assertEquals("docs/cvs/commit", mv.getViewName()); assertTrue(mv.getModel().isEmpty()); }
@Test public void withPrefixAndSuffix() throws Exception { UrlFilenameViewController ctrl = new UrlFilenameViewController(); ctrl.setPrefix("mypre_"); ctrl.setSuffix("_mysuf"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html"); MockHttpServletResponse response = new MockHttpServletResponse(); ModelAndView mv = ctrl.handleRequest(request, response); assertEquals("mypre_index_mysuf", mv.getViewName()); assertTrue(mv.getModel().isEmpty()); }