Search in sources :

Example 6 with ServletContextListener

use of javax.servlet.ServletContextListener in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method servletContextListenerBeans.

@Test
public void servletContextListenerBeans() throws Exception {
    addWebServerFactoryBean();
    ServletContextListener initializer = mock(ServletContextListener.class);
    this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    verify(servletContext).addListener(initializer);
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 7 with ServletContextListener

use of javax.servlet.ServletContextListener in project spring-boot by spring-projects.

the class ServletWebServerServletContextListenerTests method registeredServletContextListenerBeanIsCalled.

private void registeredServletContextListenerBeanIsCalled(Class<?> configuration) {
    AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(ServletListenerRegistrationBeanConfiguration.class, configuration);
    ServletContextListener servletContextListener = (ServletContextListener) context.getBean("registration", ServletListenerRegistrationBean.class).getListener();
    verify(servletContextListener).contextInitialized(any(ServletContextEvent.class));
    context.close();
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 8 with ServletContextListener

use of javax.servlet.ServletContextListener in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithDefaultLocation.

@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    try {
        listener.contextInitialized(event);
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/applicationContext.xml"));
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 9 with ServletContextListener

use of javax.servlet.ServletContextListener in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithCustomContext.

@Test
public void testContextLoaderWithCustomContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.servlet.SimpleWebApplicationContext");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct WebApplicationContext exposed in ServletContext", wc instanceof SimpleWebApplicationContext);
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.Test)

Example 10 with ServletContextListener

use of javax.servlet.ServletContextListener in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithInvalidLocation.

@Test
public void testContextLoaderWithInvalidLocation() 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);
    try {
        listener.contextInitialized(event);
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof FileNotFoundException);
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) FileNotFoundException(java.io.FileNotFoundException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Aggregations

ServletContextListener (javax.servlet.ServletContextListener)21 ServletContextEvent (javax.servlet.ServletContextEvent)19 Test (org.junit.Test)11 MockServletContext (org.springframework.mock.web.test.MockServletContext)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 ServletContext (javax.servlet.ServletContext)3 HandlerWrapper (io.undertow.server.HandlerWrapper)2 HttpHandler (io.undertow.server.HttpHandler)2 Servlet (javax.servlet.Servlet)2 ServletException (javax.servlet.ServletException)2 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 PathTemplateHandler (io.undertow.server.handlers.PathTemplateHandler)1 PredicatedHandler (io.undertow.server.handlers.builder.PredicatedHandler)1 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)1 FileResourceManager (io.undertow.server.handlers.resource.FileResourceManager)1 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)1 ServerSentEventConnectionCallback (io.undertow.server.handlers.sse.ServerSentEventConnectionCallback)1 ServerSentEventHandler (io.undertow.server.handlers.sse.ServerSentEventHandler)1