use of jakarta.servlet.ServletContextEvent 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]");
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class Spr8510Tests method annotationConfigWAC.
/**
* Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely.
*/
@Test
public void annotationConfigWAC() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.scan("does.not.matter");
ContextLoaderListener cll = new ContextLoaderListener(ctx);
cll.contextInitialized(new ServletContextEvent(new MockServletContext()));
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class Spr8510Tests method abstractRefreshableWAC_fallsBackToInitParam.
/**
* If setConfigLocation has not been called explicitly against the application context,
* then fall back to the ContextLoaderListener init-param if present.
*/
@Test
public void abstractRefreshableWAC_fallsBackToInitParam() {
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
// 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 jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class WebApplicationContextScopeTests method testApplicationScope.
@Test
public void testApplicationScope() {
WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
assertThat(ac.getServletContext().getAttribute(NAME)).isNull();
DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
assertThat(ac.getServletContext().getAttribute(NAME)).isSameAs(bean);
assertThat(ac.getBean(NAME)).isSameAs(bean);
new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
assertThat(bean.wasDestroyed()).isTrue();
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithGlobalContextInitializers.
@Test
void contextLoaderListenerWithGlobalContextInitializers() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(new Object[] { TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName() }));
ContextLoaderListener listener = new ContextLoaderListener();
listener.contextInitialized(new ServletContextEvent(sc));
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
TestBean testBean = wac.getBean(TestBean.class);
assertThat(testBean.getName()).isEqualTo("testName");
assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
Aggregations