use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class RequestContextListenerTests method requestContextListenerWithDifferentThread.
@Test
public void requestContextListenerWithDifferentThread() {
final RequestContextListener listener = new RequestContextListener();
final MockServletContext context = new MockServletContext();
final MockHttpServletRequest request = new MockHttpServletRequest(context);
request.setAttribute("test", "value");
assertThat(RequestContextHolder.getRequestAttributes()).isNull();
listener.requestInitialized(new ServletRequestEvent(context, request));
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();
assertThat(RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST)).isEqualTo("value");
MockRunnable runnable = new MockRunnable();
RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
// Execute requestDestroyed callback in different thread.
Thread thread = new Thread() {
@Override
public void run() {
listener.requestDestroyed(new ServletRequestEvent(context, request));
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException ex) {
}
// Still bound to original thread, but at least completed.
assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();
assertThat(runnable.wasExecuted()).isTrue();
// Check that a repeated execution in the same thread works and performs cleanup.
listener.requestInitialized(new ServletRequestEvent(context, request));
listener.requestDestroyed(new ServletRequestEvent(context, request));
assertThat(RequestContextHolder.getRequestAttributes()).isNull();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceTests method testServletContextResourceWithRelativePath.
@Test
public void testServletContextResourceWithRelativePath() throws IOException {
MockServletContext sc = new MockServletContext();
Resource resource = new ServletContextResource(sc, "dir/");
Resource relative = resource.createRelative("subdir");
assertThat(relative).isEqualTo(new ServletContextResource(sc, "dir/subdir"));
}
use of org.springframework.web.testfixture.servlet.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());
}
assertThat(foundPaths.size()).isEqualTo(3);
assertThat(foundPaths.contains("/WEB-INF/mydir1/context1.xml")).isTrue();
assertThat(foundPaths.contains("/WEB-INF/mydir2/context2.xml")).isTrue();
assertThat(foundPaths.contains("/WEB-INF/mydir2/mydir3/context3.xml")).isTrue();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletContextAwareWithServletContext.
@Test
public void servletContextAwareWithServletContext() {
ServletContext servletContext = new MockServletContext();
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext);
ServletContextAwareBean bean = new ServletContextAwareBean();
assertThat(bean.getServletContext()).isNull();
processor.postProcessBeforeInitialization(bean, "testBean");
assertThat(bean.getServletContext()).as("ServletContext should have been set").isNotNull();
assertThat(bean.getServletContext()).isEqualTo(servletContext);
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletContextAwareWithNonNullServletContextAndNullServletConfig.
@Test
public void servletContextAwareWithNonNullServletContextAndNullServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, null);
ServletContextAwareBean bean = new ServletContextAwareBean();
assertThat(bean.getServletContext()).isNull();
processor.postProcessBeforeInitialization(bean, "testBean");
assertThat(bean.getServletContext()).as("ServletContext should have been set").isNotNull();
assertThat(bean.getServletContext()).isEqualTo(servletContext);
}
Aggregations