Search in sources :

Example 71 with MockServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithFrameworkServletContext.

@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    proxyConfig.addInitParameter("targetBeanName", "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) 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)

Example 72 with MockServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetBeanName.

@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
    filterProxy.init(new MockFilterConfig(sc));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) 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)

Example 73 with MockServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithLazyContextStartup.

@Test
public void testDelegatingFilterProxyWithLazyContextStartup() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    proxyConfig.addInitParameter("targetBeanName", "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) 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)

Example 74 with MockServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyNotInjectedWithRootPreferred.

@Test
public void testDelegatingFilterProxyNotInjectedWithRootPreferred() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.refresh();
    sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
    sc.setAttribute("another", wac);
    StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
    wacToUse.setServletContext(sc);
    String beanName = "targetFilter";
    wacToUse.registerSingleton(beanName, MockFilter.class);
    wacToUse.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wacToUse);
    MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
    filterProxy.setServletContext(sc);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 75 with MockServletContext

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

the class DelegatingFilterProxyTests method testDelegatingFilterProxyAndCustomContextAttribute.

@Test
public void testDelegatingFilterProxyAndCustomContextAttribute() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute("CUSTOM_ATTR", wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    proxyConfig.addInitParameter("targetBeanName", "targetFilter");
    proxyConfig.addInitParameter("contextAttribute", "CUSTOM_ATTR");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) 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