use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class CompositeFilterTests method testCompositeFilter.
@Test
public void testCompositeFilter() throws ServletException, IOException {
ServletContext sc = new MockServletContext();
MockFilter targetFilter = new MockFilter();
MockFilterConfig proxyConfig = new MockFilterConfig(sc);
CompositeFilter filterProxy = new CompositeFilter();
filterProxy.setFilters(Arrays.asList(targetFilter));
filterProxy.init(proxyConfig);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
filterProxy.doFilter(request, response, null);
assertThat(targetFilter.filterConfig).isNotNull();
assertThat(request.getAttribute("called")).isEqualTo(Boolean.TRUE);
filterProxy.destroy();
assertThat(targetFilter.filterConfig).isNull();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class Spr8510Tests method customAbstractRefreshableWAC_fallsBackToInitParam.
/**
* Ensure that any custom default locations are still respected.
*/
@Test
public void customAbstractRefreshableWAC_fallsBackToInitParam() {
XmlWebApplicationContext ctx = new XmlWebApplicationContext() {
@Override
protected String[] getDefaultConfigLocations() {
return new String[] { "/WEB-INF/custom.xml" };
}
};
// ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
ContextLoaderListener cll = new ContextLoaderListener(ctx);
MockServletContext sc = new MockServletContext();
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> cll.contextInitialized(new ServletContextEvent(sc))).withMessageEndingWith("Could not open ServletContext resource [/from-init-param.xml]");
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class Spr8510Tests method abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations.
/**
* If a contextConfigLocation init-param has been specified for the ContextLoaderListener,
* then it should take precedence. This is generally not a recommended practice, but
* when it does happen, the init-param should be considered more specific than the
* programmatic configuration, given that it still quite possibly externalized in
* hybrid web.xml + WebApplicationInitializer cases.
*/
@Test
public void abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations() {
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
ctx.setConfigLocation("programmatic.xml");
ContextLoaderListener cll = new ContextLoaderListener(ctx);
MockServletContext sc = new MockServletContext();
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> cll.contextInitialized(new ServletContextEvent(sc))).withMessageEndingWith("Could not open ServletContext resource [/from-init-param.xml]");
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class Spr8510Tests method genericWAC.
/**
* Ensure that ContextLoaderListener and GenericWebApplicationContext interact nicely.
*/
@Test
public void genericWAC() {
GenericWebApplicationContext ctx = new GenericWebApplicationContext();
ContextLoaderListener cll = new ContextLoaderListener(ctx);
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx);
scanner.scan("bogus.pkg");
cll.contextInitialized(new ServletContextEvent(new MockServletContext()));
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class Spr8510Tests method abstractRefreshableWAC_fallsBackToConventionBasedNaming.
/**
* If context config locations have been specified neither against the application
* context nor the context loader listener, then fall back to default values.
*/
@Test
public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() {
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
// ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
ContextLoaderListener cll = new ContextLoaderListener(ctx);
MockServletContext sc = new MockServletContext();
// no init-param set
// sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> cll.contextInitialized(new ServletContextEvent(sc))).withMessageEndingWith("Could not open ServletContext resource [/WEB-INF/applicationContext.xml]");
}
Aggregations