Search in sources :

Example 16 with ServletContextEvent

use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class Spr8510Tests method abstractRefreshableWAC_fallsBackToConventionBasedNaming.

/**
 * If context config locations have been specified neither against the application
 * context nor the context loader listener, then fall back to default values.
 */
@Test
public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    // ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    // no init-param set
    // sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> cll.contextInitialized(new ServletContextEvent(sc))).withMessageEndingWith("Could not open ServletContext resource [/WEB-INF/applicationContext.xml]");
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Example 17 with ServletContextEvent

use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class Spr8510Tests method annotationConfigWAC.

/**
 * Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely.
 */
@Test
public void annotationConfigWAC() {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.scan("does.not.matter");
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    cll.contextInitialized(new ServletContextEvent(new MockServletContext()));
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextEvent(jakarta.servlet.ServletContextEvent) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 18 with ServletContextEvent

use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class Spr8510Tests method abstractRefreshableWAC_fallsBackToInitParam.

/**
 * If setConfigLocation has not been called explicitly against the application context,
 * then fall back to the ContextLoaderListener init-param if present.
 */
@Test
public void abstractRefreshableWAC_fallsBackToInitParam() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    // ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> cll.contextInitialized(new ServletContextEvent(sc))).withMessageEndingWith("Could not open ServletContext resource [/from-init-param.xml]");
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Example 19 with ServletContextEvent

use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class WebApplicationContextScopeTests method testApplicationScope.

@Test
public void testApplicationScope() {
    WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
    assertThat(ac.getServletContext().getAttribute(NAME)).isNull();
    DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
    assertThat(ac.getServletContext().getAttribute(NAME)).isSameAs(bean);
    assertThat(ac.getBean(NAME)).isSameAs(bean);
    new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
    assertThat(bean.wasDestroyed()).isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) ContextCleanupListener(org.springframework.web.context.ContextCleanupListener) ServletContextEvent(jakarta.servlet.ServletContextEvent) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 20 with ServletContextEvent

use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class ContextLoaderTests method contextLoaderListenerWithGlobalContextInitializers.

@Test
void contextLoaderListenerWithGlobalContextInitializers() {
    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, 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)

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