Search in sources :

Example 6 with ServletContextListener

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);
}
Also used : ServletContextListener(jakarta.servlet.ServletContextListener) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Example 7 with ServletContextListener

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);
}
Also used : ServletContextListener(jakarta.servlet.ServletContextListener) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Example 8 with ServletContextListener

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");
}
Also used : ServletContextListener(jakarta.servlet.ServletContextListener) IOException(java.io.IOException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Aggregations

ServletContextEvent (jakarta.servlet.ServletContextEvent)8 ServletContextListener (jakarta.servlet.ServletContextListener)8 Test (org.junit.jupiter.api.Test)6 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)2 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)2 ServletContext (jakarta.servlet.ServletContext)1 ServletContextAttributeListener (jakarta.servlet.ServletContextAttributeListener)1 ServletRequestAttributeListener (jakarta.servlet.ServletRequestAttributeListener)1 ServletRequestListener (jakarta.servlet.ServletRequestListener)1 HttpSessionAttributeListener (jakarta.servlet.http.HttpSessionAttributeListener)1 HttpSessionIdListener (jakarta.servlet.http.HttpSessionIdListener)1 HttpSessionListener (jakarta.servlet.http.HttpSessionListener)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 LifecycleBean (org.springframework.beans.testfixture.beans.LifecycleBean)1