Search in sources :

Example 56 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class WebApplicationContextScopeTests method initApplicationContext.

private WebApplicationContext initApplicationContext(String scope) {
    MockServletContext sc = new MockServletContext();
    GenericWebApplicationContext ac = new GenericWebApplicationContext(sc);
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(DerivedTestBean.class);
    bd.setScope(scope);
    ac.registerBeanDefinition(NAME, bd);
    ac.refresh();
    return ac;
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext)

Example 57 with MockServletContext

use of org.springframework.mock.web.test.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");
    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 58 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext 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 59 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class RequestContextFilterTests method testFilterInvocation.

private void testFilterInvocation(final ServletException sex) throws Exception {
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setAttribute("myAttr", "myValue");
    final MockHttpServletResponse resp = new MockHttpServletResponse();
    // Expect one invocation by the filter being tested
    class DummyFilterChain implements FilterChain {

        public int invocations = 0;

        @Override
        public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
            ++invocations;
            if (invocations == 1) {
                assertSame("myValue", RequestContextHolder.currentRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST));
                if (sex != null) {
                    throw sex;
                }
            } else {
                throw new IllegalStateException("Too many invocations");
            }
        }
    }
    ;
    DummyFilterChain fc = new DummyFilterChain();
    MockFilterConfig mfc = new MockFilterConfig(new MockServletContext(), "foo");
    RequestContextFilter rbf = new RequestContextFilter();
    rbf.init(mfc);
    try {
        rbf.doFilter(req, resp, fc);
        if (sex != null) {
            fail();
        }
    } catch (ServletException ex) {
        assertNotNull(sex);
    }
    try {
        RequestContextHolder.currentRequestAttributes();
        fail();
    } catch (IllegalStateException ex) {
    // Ok
    }
    assertEquals(1, fc.invocations);
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig)

Example 60 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetBeanNameAndNoApplicationContext.

@Test(expected = IllegalStateException.class)
public void testDelegatingFilterProxyWithTargetBeanNameAndNoApplicationContext() throws ServletException, IOException {
    MockServletContext sc = new MockServletContext();
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", null);
    filterProxy.init(new MockFilterConfig(sc));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    // throws
    filterProxy.doFilter(request, response, null);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9