use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class Spr8510Tests method abstractRefreshableWAC_respectsProgrammaticConfigLocations.
@Test
public void abstractRefreshableWAC_respectsProgrammaticConfigLocations() {
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
ctx.setConfigLocation("programmatic.xml");
ContextLoaderListener cll = new ContextLoaderListener(ctx);
MockServletContext sc = new MockServletContext();
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> cll.contextInitialized(new ServletContextEvent(sc))).withMessageEndingWith("Could not open ServletContext resource [/programmatic.xml]");
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderInitializerTests method register.
@Test
public void register() throws ServletException {
initializer.onStartup(servletContext);
boolean condition1 = eventListener instanceof ContextLoaderListener;
assertThat(condition1).isTrue();
ContextLoaderListener cll = (ContextLoaderListener) eventListener;
cll.contextInitialized(new ServletContextEvent(servletContext));
WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
assertThat(applicationContext.containsBean(BEAN_NAME)).isTrue();
boolean condition = applicationContext.getBean(BEAN_NAME) instanceof MyBean;
assertThat(condition).isTrue();
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithProgrammaticAndGlobalInitializers.
@Test
void contextLoaderListenerWithProgrammaticAndGlobalInitializers() {
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, TestWebContextInitializer.class.getName());
ContextLoaderListener listener = new ContextLoaderListener();
listener.setContextInitializers(new TestContextInitializer());
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();
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithProgrammaticInitializers.
@Test
void contextLoaderListenerWithProgrammaticInitializers() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
ContextLoaderListener listener = new ContextLoaderListener();
listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer());
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();
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderWithCustomContext.
@Test
void contextLoaderWithCustomContext() throws Exception {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.servlet.SimpleWebApplicationContext");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(contextAttr);
boolean condition = wc instanceof SimpleWebApplicationContext;
assertThat(condition).as("Correct WebApplicationContext exposed in ServletContext").isTrue();
}
Aggregations