Search in sources :

Example 26 with ServletContextEvent

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

the class ContextLoaderTests method registeredContextInitializerCanAccessServletContextParamsViaEnvironment.

@Test
void registeredContextInitializerCanAccessServletContextParamsViaEnvironment() {
    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("someProperty", "someValue");
    sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, EnvApplicationContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));
}
Also used : MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Example 27 with ServletContextEvent

use of jakarta.servlet.ServletContextEvent 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)

Example 28 with ServletContextEvent

use of jakarta.servlet.ServletContextEvent in project metrics by dropwizard.

the class InstrumentedFilterContextListenerTest method injectsTheMetricRegistryIntoTheServletContext.

@Test
public void injectsTheMetricRegistryIntoTheServletContext() {
    final ServletContext context = mock(ServletContext.class);
    final ServletContextEvent event = mock(ServletContextEvent.class);
    when(event.getServletContext()).thenReturn(context);
    listener.contextInitialized(event);
    verify(context).setAttribute("io.dropwizard.metrics.servlet.InstrumentedFilter.registry", registry);
}
Also used : ServletContext(jakarta.servlet.ServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.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