Search in sources :

Example 16 with ServletContextListener

use of javax.servlet.ServletContextListener in project jetty.project by eclipse.

the class ServletContextHandlerTest method testReplaceHandler.

@Test
public void testReplaceHandler() throws Exception {
    ServletContextHandler servletContextHandler = new ServletContextHandler();
    ServletHolder sh = new ServletHolder(new TestServlet());
    servletContextHandler.addServlet(sh, "/foo");
    final AtomicBoolean contextInit = new AtomicBoolean(false);
    final AtomicBoolean contextDestroy = new AtomicBoolean(false);
    servletContextHandler.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            if (sce.getServletContext() != null)
                contextInit.set(true);
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            if (sce.getServletContext() != null)
                contextDestroy.set(true);
        }
    });
    ServletHandler shandler = servletContextHandler.getServletHandler();
    ResourceHandler rh = new ResourceHandler();
    servletContextHandler.insertHandler(rh);
    assertEquals(shandler, servletContextHandler.getServletHandler());
    assertEquals(rh, servletContextHandler.getHandler());
    assertEquals(rh.getHandler(), shandler);
    _server.setHandler(servletContextHandler);
    _server.start();
    assertTrue(contextInit.get());
    _server.stop();
    assertTrue(contextDestroy.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletContextListener(javax.servlet.ServletContextListener) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 17 with ServletContextListener

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

the class ContextLoaderTests method testContextLoaderListenerWithCustomizedContextLoader.

/**
	 * Addresses the issues raised in <a
	 * href="http://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
public void testContextLoaderListenerWithCustomizedContextLoader() {
    final StringBuffer buffer = new StringBuffer();
    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) {
            assertNotNull("The ServletContext should not be null.", sc);
            assertEquals("Verifying that we received the expected ServletContext.", sc, sc);
            assertFalse("The ApplicationContext should not yet have been refreshed.", wac.isActive());
            buffer.append(expectedContents);
        }
    };
    listener.contextInitialized(new ServletContextEvent(sc));
    assertEquals("customizeContext() should have been called.", expectedContents, buffer.toString());
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 18 with ServletContextListener

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

the class ContextLoaderTests method testContextLoaderWithInvalidContext.

@Test
public void testContextLoaderWithInvalidContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.context.support.InvalidWebApplicationContext");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    try {
        listener.contextInitialized(event);
        fail("Should have thrown ApplicationContextException");
    } catch (ApplicationContextException ex) {
        // expected
        assertTrue(ex.getCause() instanceof ClassNotFoundException);
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) ApplicationContextException(org.springframework.context.ApplicationContextException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 19 with ServletContextListener

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

the class ContextLoaderTests method testContextLoaderListenerWithDefaultContext.

@Test
public void testContextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
    assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertTrue("Has father", context.containsBean("father"));
    assertTrue("Has rod", context.containsBean("rod"));
    assertTrue("Has kerry", context.containsBean("kerry"));
    assertTrue("Not destroyed", !lb.isDestroyed());
    assertFalse(context.containsBean("beans1.bean1"));
    assertFalse(context.containsBean("beans1.bean2"));
    listener.contextDestroyed(event);
    assertTrue("Destroyed", lb.isDestroyed());
    assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) ServletContextListener(javax.servlet.ServletContextListener) LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) 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 20 with ServletContextListener

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

the class ServletWebServerServletContextListenerTests method servletContextListenerBeanIsCalled.

private void servletContextListenerBeanIsCalled(Class<?> configuration) {
    AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(ServletContextListenerBeanConfiguration.class, configuration);
    ServletContextListener servletContextListener = context.getBean("servletContextListener", ServletContextListener.class);
    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)

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