Search in sources :

Example 96 with MockServletContext

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

the class ServletContextSupportTests method testServletContextAttributeExporter.

@Test
public void testServletContextAttributeExporter() {
    TestBean tb = new TestBean();
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("attr1", "value1");
    attributes.put("attr2", tb);
    MockServletContext sc = new MockServletContext();
    ServletContextAttributeExporter exporter = new ServletContextAttributeExporter();
    exporter.setAttributes(attributes);
    exporter.setServletContext(sc);
    assertEquals("value1", sc.getAttribute("attr1"));
    assertSame(tb, sc.getAttribute("attr2"));
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) HashMap(java.util.HashMap) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 97 with MockServletContext

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

the class ServletContextSupportTests method testServletContextResourcePatternResolver.

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

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return paths;
            }
            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(2, foundPaths.size());
    assertTrue(foundPaths.contains("/WEB-INF/context1.xml"));
    assertTrue(foundPaths.contains("/WEB-INF/context2.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 98 with MockServletContext

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

the class ServletContextSupportTests method testServletContextAttributeFactoryBeanWithAttributeNotFound.

@Test
public void testServletContextAttributeFactoryBeanWithAttributeNotFound() {
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("attributeName", "myAttr");
    wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
    try {
        wac.refresh();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IllegalStateException);
        assertTrue(ex.getCause().getMessage().contains("myAttr"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 99 with MockServletContext

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

the class ServletContextSupportTests method testServletContextResourcePatternResolverWithAbsolutePaths.

@Test
public void testServletContextResourcePatternResolverWithAbsolutePaths() throws IOException {
    final Set<String> paths = new HashSet<>();
    paths.add("C:/webroot/WEB-INF/context1.xml");
    paths.add("C:/webroot/WEB-INF/context2.xml");
    paths.add("C:/webroot/someOtherDirThatDoesntContainPath");
    MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return paths;
            }
            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(2, foundPaths.size());
    assertTrue(foundPaths.contains("/WEB-INF/context1.xml"));
    assertTrue(foundPaths.contains("/WEB-INF/context2.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 100 with MockServletContext

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

the class ServletContextSupportTests method testServletContextParameterFactoryBean.

@Test
public void testServletContextParameterFactoryBean() {
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter("myParam", "myValue");
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("initParamName", "myParam");
    wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
    wac.refresh();
    Object value = wac.getBean("importedParam");
    assertEquals("myValue", value);
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) 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