Search in sources :

Example 21 with ServletContextEvent

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

Example 22 with ServletContextEvent

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);
}
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 23 with ServletContextEvent

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

Example 24 with ServletContextEvent

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

Example 25 with ServletContextEvent

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

Aggregations

ServletContextEvent (jakarta.servlet.ServletContextEvent)28 Test (org.junit.jupiter.api.Test)25 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 ServletContextListener (jakarta.servlet.ServletContextListener)10 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)8 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)8 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 ContextLoaderListener (org.springframework.web.context.ContextLoaderListener)6 ServletContext (jakarta.servlet.ServletContext)3 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2 ServletContextAttributeListener (jakarta.servlet.ServletContextAttributeListener)1 Dynamic (jakarta.servlet.ServletRegistration.Dynamic)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 Method (java.lang.reflect.Method)1 InetAddress (java.net.InetAddress)1