use of jakarta.servlet.ServletContextListener 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.ServletContextListener 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.ServletContextListener in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderWithDefaultLocation.
@Test
void contextLoaderWithDefaultLocation() throws Exception {
MockServletContext sc = new MockServletContext("");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> listener.contextInitialized(event)).havingCause().isInstanceOf(IOException.class).withMessageContaining("/WEB-INF/applicationContext.xml");
}
Aggregations