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