use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderWithInvalidLocation.
@Test
void contextLoaderWithInvalidLocation() throws Exception {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> listener.contextInitialized(event)).withCauseInstanceOf(FileNotFoundException.class);
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithCustomizedContextLoader.
/**
* Addresses the issues raised in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-4008"
* target="_blank">SPR-4008</a>: <em>Supply an opportunity to customize
* context before calling refresh in ContextLoaders</em>.
*/
@Test
void contextLoaderListenerWithCustomizedContextLoader() {
final StringBuilder builder = new StringBuilder();
final String expectedContents = "customizeContext() was called";
final MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml");
ServletContextListener listener = new ContextLoaderListener() {
@Override
protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) {
assertThat(sc).as("The ServletContext should not be null.").isNotNull();
assertThat(sc).as("Verifying that we received the expected ServletContext.").isEqualTo(sc);
assertThat(wac.isActive()).as("The ApplicationContext should not yet have been refreshed.").isFalse();
builder.append(expectedContents);
}
};
listener.contextInitialized(new ServletContextEvent(sc));
assertThat(builder.toString()).as("customizeContext() should have been called.").isEqualTo(expectedContents);
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithLocalContextInitializers.
@Test
void contextLoaderListenerWithLocalContextInitializers() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
sc.addInitParameter(ContextLoader.CONTEXT_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();
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithProgrammaticAndLocalInitializers.
@Test
void contextLoaderListenerWithProgrammaticAndLocalInitializers() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
ContextLoaderListener listener = new ContextLoaderListener();
listener.setContextInitializers(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 contextLoaderListenerWithUnknownContextInitializer.
@Test
void contextLoaderListenerWithUnknownContextInitializer() {
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();
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> listener.contextInitialized(new ServletContextEvent(sc))).withMessageContaining("not assignable");
}
Aggregations