Search in sources :

Example 11 with MockServletContext

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

the class ServletContextAwareProcessorTests method servletConfigAwareWithServletConfig.

@Test
public void servletConfigAwareWithServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletConfig);
    ServletConfigAwareBean bean = new ServletConfigAwareBean();
    assertNull(bean.getServletConfig());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletConfig should have been set", bean.getServletConfig());
    assertEquals(servletConfig, bean.getServletConfig());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 12 with MockServletContext

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

the class ServletContextAwareProcessorTests method servletContextAwareWithServletContextAndServletConfig.

@Test
public void servletContextAwareWithServletContextAndServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, servletConfig);
    ServletContextAwareBean bean = new ServletContextAwareBean();
    assertNull(bean.getServletContext());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletContext should have been set", bean.getServletContext());
    assertEquals(servletContext, bean.getServletContext());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 13 with MockServletContext

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

the class ServletContextAwareProcessorTests method servletConfigAwareWithServletContextOnly.

@Test
public void servletConfigAwareWithServletContextOnly() {
    ServletContext servletContext = new MockServletContext();
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext);
    ServletConfigAwareBean bean = new ServletConfigAwareBean();
    assertNull(bean.getServletConfig());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNull(bean.getServletConfig());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 14 with MockServletContext

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

the class ServletContextSupportTests method testServletContextResourcePatternResolverWithUnboundedPatternPath.

@Test
public void testServletContextResourcePatternResolverWithUnboundedPatternPath() throws IOException {
    final Set<String> dirs = new HashSet<>();
    dirs.add("/WEB-INF/mydir1/");
    dirs.add("/WEB-INF/mydir2/");
    final Set<String> paths = new HashSet<>();
    paths.add("/WEB-INF/mydir2/context2.xml");
    paths.add("/WEB-INF/mydir2/mydir3/");
    MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return dirs;
            }
            if ("/WEB-INF/mydir1/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir1/context1.xml");
            }
            if ("/WEB-INF/mydir2/".equals(path)) {
                return paths;
            }
            if ("/WEB-INF/mydir2/mydir3/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir2/mydir3/context3.xml");
            }
            return null;
        }
    };
    ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
    Resource[] found = rpr.getResources("/WEB-INF/**/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertEquals(3, foundPaths.size());
    assertTrue(foundPaths.contains("/WEB-INF/mydir1/context1.xml"));
    assertTrue(foundPaths.contains("/WEB-INF/mydir2/context2.xml"));
    assertTrue(foundPaths.contains("/WEB-INF/mydir2/mydir3/context3.xml"));
}
Also used : Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.mock.web.test.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with MockServletContext

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

the class WebApplicationObjectSupportTests method testWebApplicationObjectSupport.

@Test
public void testWebApplicationObjectSupport() {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    File tempDir = new File("");
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, tempDir);
    wac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class));
    wac.refresh();
    WebApplicationObjectSupport wao = (WebApplicationObjectSupport) wac.getBean("test");
    assertEquals(wao.getServletContext(), wac.getServletContext());
    assertEquals(wao.getTempDir(), tempDir);
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) File(java.io.File) 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