Search in sources :

Example 81 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method testFrameworkServletWithDefaultLocation.

@Test
public void testFrameworkServletWithDefaultLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextClass(XmlWebApplicationContext.class);
    try {
        servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/test-servlet.xml"));
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 82 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderListenerWithProgrammaticAndLocalInitializers.

@Test
public void testContextLoaderListenerWithProgrammaticAndLocalInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestWebContextInitializer());
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName(), equalTo("testName"));
    assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.Test)

Example 83 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class SimpleUrlHandlerMappingTests method checkMappings.

private void checkMappings(String beanName) throws Exception {
    MockServletContext sc = new MockServletContext("");
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setServletContext(sc);
    wac.setConfigLocations(new String[] { "/org/springframework/web/servlet/handler/map2.xml" });
    wac.refresh();
    Object bean = wac.getBean("mainController");
    Object otherBean = wac.getBean("otherController");
    Object defaultBean = wac.getBean("starController");
    HandlerMapping hm = (HandlerMapping) wac.getBean(beanName);
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
    HandlerExecutionChain hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome.x");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/");
    req.setServletPath("/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/welcome.html");
    req.setContextPath("/app");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-welcome.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-show.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-bookseats.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/somePath");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("/somePath", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext)

Example 84 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ResourceUrlProviderTests method setUp.

@Before
public void setUp() throws Exception {
    this.locations.add(new ClassPathResource("test/", getClass()));
    this.locations.add(new ClassPathResource("testalternatepath/", getClass()));
    this.handler.setServletContext(new MockServletContext());
    this.handler.setLocations(locations);
    this.handler.afterPropertiesSet();
    this.handlerMap.put("/resources/**", this.handler);
    this.urlProvider.setHandlerMap(this.handlerMap);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Before(org.junit.Before)

Example 85 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ResourceUrlProviderTests method initializeOnce.

// SPR-12592
@Test
public void initializeOnce() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(HandlerMappingConfiguration.class);
    context.refresh();
    ResourceUrlProvider urlProviderBean = context.getBean(ResourceUrlProvider.class);
    assertThat(urlProviderBean.getHandlerMap(), Matchers.hasKey("/resources/**"));
    assertFalse(urlProviderBean.isAutodetect());
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9