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);
}
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");
}
Aggregations