use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method relativeMethodPathDispatchingController.
@Test
public void relativeMethodPathDispatchingController() throws Exception {
initServletWithControllers(MyRelativeMethodPathDispatchingController.class);
getServlet().init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/yourApp/myOther");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myOtherView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/hisApp/myLang");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myLangView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/herApp/surprise.do");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("mySurpriseView", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method detectAllHandlerAdapters.
@Test
public void detectAllHandlerAdapters() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertEquals("body", response.getContentAsString());
request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do");
response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method detectHandlerMappingFromParent.
@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
// create a parent context that includes a mapping
StaticWebApplicationContext parent = new StaticWebApplicationContext();
parent.setServletContext(getServletContext());
parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
parent.refresh();
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
// will have parent
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
ServletConfig config = new MockServletConfig(getServletContext(), "complex");
config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
complexDispatcherServlet.init(config);
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(), response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method dispatcherServletRefresh.
@Test
public void dispatcherServletRefresh() throws ServletException {
MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
DispatcherServlet servlet = new DispatcherServlet();
servlet.init(new MockServletConfig(servletContext, "empty"));
ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
assertSame(servletContext, contextBean.getServletContext());
assertSame(servlet.getServletConfig(), configBean.getServletConfig());
MultipartResolver multipartResolver = servlet.getMultipartResolver();
assertNotNull(multipartResolver);
servlet.refresh();
ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
assertSame(servletContext, contextBean2.getServletContext());
assertSame(servlet.getServletConfig(), configBean2.getServletConfig());
assertNotSame(contextBean, contextBean2);
assertNotSame(configBean, configBean2);
MultipartResolver multipartResolver2 = servlet.getMultipartResolver();
assertNotSame(multipartResolver, multipartResolver2);
servlet.destroy();
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method setUp.
@Before
public void setUp() throws ServletException {
MockServletConfig complexConfig = new MockServletConfig(getServletContext(), "complex");
complexConfig.addInitParameter("publishContext", "false");
complexConfig.addInitParameter("class", "notWritable");
complexConfig.addInitParameter("unknownParam", "someValue");
simpleDispatcherServlet = new DispatcherServlet();
simpleDispatcherServlet.setContextClass(SimpleWebApplicationContext.class);
simpleDispatcherServlet.init(servletConfig);
complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.addRequiredProperty("publishContext");
complexDispatcherServlet.init(complexConfig);
}
Aggregations