Search in sources :

Example 41 with MockServletContext

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();
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 42 with MockServletContext

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"));
}
Also used : Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 43 with MockServletContext

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();
}
Also used : Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 44 with MockServletContext

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);
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 45 with MockServletContext

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);
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)138 Test (org.junit.jupiter.api.Test)112 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)38 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)24 ServletContext (jakarta.servlet.ServletContext)22 ServletContextEvent (jakarta.servlet.ServletContextEvent)21 BeforeEach (org.junit.jupiter.api.BeforeEach)17 WebApplicationContext (org.springframework.web.context.WebApplicationContext)17 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)14 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13 Resource (org.springframework.core.io.Resource)12 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 HashMap (java.util.HashMap)11 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7