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