Search in sources :

Example 1 with MockRunnable

use of org.springframework.core.task.MockRunnable 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");
    assertNull(RequestContextHolder.getRequestAttributes());
    listener.requestInitialized(new ServletRequestEvent(context, request));
    assertNotNull(RequestContextHolder.getRequestAttributes());
    assertEquals("value", RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
    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.
    assertNotNull(RequestContextHolder.getRequestAttributes());
    assertTrue(runnable.wasExecuted());
    // 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));
    assertNull(RequestContextHolder.getRequestAttributes());
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletRequestEvent(javax.servlet.ServletRequestEvent) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 2 with MockRunnable

use of org.springframework.core.task.MockRunnable in project spring-framework by spring-projects.

the class RequestContextListenerTests method requestContextListenerWithSameThread.

@Test
public void requestContextListenerWithSameThread() {
    RequestContextListener listener = new RequestContextListener();
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    request.setAttribute("test", "value");
    assertNull(RequestContextHolder.getRequestAttributes());
    listener.requestInitialized(new ServletRequestEvent(context, request));
    assertNotNull(RequestContextHolder.getRequestAttributes());
    assertEquals("value", RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
    MockRunnable runnable = new MockRunnable();
    RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
    listener.requestDestroyed(new ServletRequestEvent(context, request));
    assertNull(RequestContextHolder.getRequestAttributes());
    assertTrue(runnable.wasExecuted());
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletRequestEvent(javax.servlet.ServletRequestEvent) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 3 with MockRunnable

use of org.springframework.core.task.MockRunnable in project spring-framework by spring-projects.

the class RequestContextListenerTests method requestContextListenerWithSameThreadAndAttributesGone.

@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
    RequestContextListener listener = new RequestContextListener();
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    request.setAttribute("test", "value");
    assertNull(RequestContextHolder.getRequestAttributes());
    listener.requestInitialized(new ServletRequestEvent(context, request));
    assertNotNull(RequestContextHolder.getRequestAttributes());
    assertEquals("value", RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
    MockRunnable runnable = new MockRunnable();
    RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
    request.clearAttributes();
    listener.requestDestroyed(new ServletRequestEvent(context, request));
    assertNull(RequestContextHolder.getRequestAttributes());
    assertTrue(runnable.wasExecuted());
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletRequestEvent(javax.servlet.ServletRequestEvent) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

ServletRequestEvent (javax.servlet.ServletRequestEvent)3 Test (org.junit.Test)3 MockRunnable (org.springframework.core.task.MockRunnable)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 MockServletContext (org.springframework.mock.web.test.MockServletContext)3