use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class DelegatingFilterProxyTests method testDelegatingFilterProxy.
@Test
public void testDelegatingFilterProxy() throws ServletException, IOException {
ServletContext 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");
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);
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class DelegatingFilterProxyTests method testDelegatingFilterProxyInjectedPreferred.
@Test
public void testDelegatingFilterProxyInjectedPreferred() 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);
StaticWebApplicationContext injectedWac = new StaticWebApplicationContext();
injectedWac.setServletContext(sc);
String beanName = "targetFilter";
injectedWac.registerSingleton(beanName, MockFilter.class);
injectedWac.refresh();
MockFilter targetFilter = (MockFilter) injectedWac.getBean(beanName);
DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName, injectedWac);
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);
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ResourceUrlProviderTests method initializeOnce.
// SPR-12592
@Test
public void initializeOnce() throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(HandlerMappingConfiguration.class);
context.refresh();
ResourceUrlProvider urlProviderBean = context.getBean(ResourceUrlProvider.class);
assertThat(urlProviderBean.getHandlerMap(), Matchers.hasKey("/resources/**"));
assertFalse(urlProviderBean.isAutodetect());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderListenerWithDefaultContext.
@Test
public void testContextLoaderListenerWithDefaultContext() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
assertTrue("Has father", context.containsBean("father"));
assertTrue("Has rod", context.containsBean("rod"));
assertTrue("Has kerry", context.containsBean("kerry"));
assertTrue("Not destroyed", !lb.isDestroyed());
assertFalse(context.containsBean("beans1.bean1"));
assertFalse(context.containsBean("beans1.bean2"));
listener.contextDestroyed(event);
assertTrue("Destroyed", lb.isDestroyed());
assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderListenerWithUnknownContextInitializer.
@Test
public void testContextLoaderListenerWithUnknownContextInitializer() {
MockServletContext sc = new MockServletContext("");
// config file doesn't matter. just a placeholder
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/empty-context.xml");
sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(new Object[] { UnknownContextInitializer.class.getName() }));
ContextLoaderListener listener = new ContextLoaderListener();
try {
listener.contextInitialized(new ServletContextEvent(sc));
fail("expected exception");
} catch (ApplicationContextException ex) {
assertTrue(ex.getMessage().contains("not assignable"));
}
}
Aggregations