Search in sources :

Example 1 with SimpleControllerHandlerAdapter

use of org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewControllers.

@Test
public void testViewControllers() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml");
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    mapping.setDefaultHandler(handlerMethod);
    BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
    assertThat(beanNameMapping).isNotNull();
    assertThat(beanNameMapping.getOrder()).isEqualTo(2);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(3);
    assertThat(chain.getInterceptorList().get(0) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(1) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof ThemeChangeInterceptor).isTrue();
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertThat(mapping2).isNotNull();
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    assertThat(adapter).isNotNull();
    request = new MockHttpServletRequest("GET", "/foo");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    ModelAndView mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat((Object) mv.getViewName()).isNull();
    request = new MockHttpServletRequest("GET", "/myapp/app/bar");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv.getViewName()).isEqualTo("baz");
    request = new MockHttpServletRequest("GET", "/myapp/app/");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv.getViewName()).isEqualTo("root");
    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());
    assertThat(mv.getView()).isNotNull();
    assertThat(mv.getView().getClass()).isEqualTo(RedirectView.class);
    RedirectView redirectView = (RedirectView) mv.getView();
    MockHttpServletResponse response = new MockHttpServletResponse();
    redirectView.render(Collections.emptyMap(), request, response);
    assertThat(response.getRedirectedUrl()).isEqualTo("/new?a=b");
    assertThat(response.getStatus()).isEqualTo(308);
    request = new MockHttpServletRequest("GET", "/bad");
    chain = mapping2.getHandler(request);
    response = new MockHttpServletResponse();
    mv = adapter.handle(request, response, chain.getHandler());
    assertThat(mv).isNull();
    assertThat(response.getStatus()).isEqualTo(404);
}
Also used : BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) RedirectView(org.springframework.web.servlet.view.RedirectView) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 2 with SimpleControllerHandlerAdapter

use of org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewControllersOnWebSphere.

/**
 * WebSphere gives trailing servlet path slashes by default!!
 */
@Test
public void testViewControllersOnWebSphere() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml");
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/myapp/app/bar");
    request.setContextPath("/myapp");
    request.setServletPath("/app/");
    request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
    HandlerExecutionChain chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv2.getViewName()).isEqualTo("baz");
    request.setRequestURI("/myapp/app/");
    request.setContextPath("/myapp");
    request.setServletPath("/app/");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv3.getViewName()).isEqualTo("root");
    request.setRequestURI("/myapp/");
    request.setContextPath("/myapp");
    request.setServletPath("/");
    chain = mapping2.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(1) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof ThemeChangeInterceptor).isTrue();
    mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mv3.getViewName()).isEqualTo("root");
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)2 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)2 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)2 SimpleControllerHandlerAdapter (org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter)2 ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 BeanNameUrlHandlerMapping (org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping)1 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1