Search in sources :

Example 6 with BeanNameUrlHandlerMapping

use of org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewControllers.

@Test
public void testViewControllers() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml", 19);
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertNotNull(mapping);
    mapping.setDefaultHandler(handlerMethod);
    BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
    assertNotNull(beanNameMapping);
    assertEquals(2, beanNameMapping.getOrder());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertEquals(3, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(mapping2);
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    assertNotNull(adapter);
    request = new MockHttpServletRequest("GET", "/foo");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    ModelAndView mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertNull(mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/bar");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("baz", mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("root", mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/old");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    request.setQueryString("a=b");
    chain = mapping2.getHandler(request);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertNotNull(mv.getView());
    assertEquals(RedirectView.class, mv.getView().getClass());
    RedirectView redirectView = (RedirectView) mv.getView();
    MockHttpServletResponse response = new MockHttpServletResponse();
    redirectView.render(Collections.emptyMap(), request, response);
    assertEquals("/new?a=b", response.getRedirectedUrl());
    assertEquals(308, response.getStatus());
    request = new MockHttpServletRequest("GET", "/bad");
    chain = mapping2.getHandler(request);
    response = new MockHttpServletResponse();
    mv = adapter.handle(request, response, chain.getHandler());
    assertNull(mv);
    assertEquals(404, response.getStatus());
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

BeanNameUrlHandlerMapping (org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping)6 Test (org.junit.Test)5 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 RedirectView (org.springframework.web.servlet.view.RedirectView)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)2 ParameterizableViewController (org.springframework.web.servlet.mvc.ParameterizableViewController)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 ElementType (java.lang.annotation.ElementType)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 Method (java.lang.reflect.Method)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1