Search in sources :

Example 36 with ServletContextEvent

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

the class WebAppContextTest method testServletContextListener.

@Test
public void testServletContextListener() throws Exception {
    Server server = new Server();
    HotSwapHandler swap = new HotSwapHandler();
    server.setHandler(swap);
    server.start();
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(System.getProperty("java.io.tmpdir"));
    final List<String> history = new ArrayList<>();
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I0");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D0");
        }
    });
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I1");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D1");
            throw new RuntimeException("Listener1 destroy broken");
        }
    });
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I2");
            throw new RuntimeException("Listener2 init broken");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D2");
        }
    });
    context.addEventListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            history.add("I3");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            history.add("D3");
        }
    });
    try {
        swap.setHandler(context);
        context.start();
    } catch (Exception e) {
        history.add(e.getMessage());
    } finally {
        try {
            swap.setHandler(null);
        } catch (Exception e) {
            while (e.getCause() instanceof Exception) e = (Exception) e.getCause();
            history.add(e.getMessage());
        } finally {
        }
    }
    Assert.assertThat(history, Matchers.contains("I0", "I1", "I2", "Listener2 init broken", "D1", "D0", "Listener1 destroy broken"));
    server.stop();
}
Also used : Server(org.eclipse.jetty.server.Server) HotSwapHandler(org.eclipse.jetty.server.handler.HotSwapHandler) ServletContextListener(javax.servlet.ServletContextListener) ArrayList(java.util.ArrayList) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletContextEvent(javax.servlet.ServletContextEvent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with ServletContextEvent

use of javax.servlet.ServletContextEvent 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 38 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project guice by google.

the class MultipleServletInjectorsTest method testTwoInjectors.

public final void testTwoInjectors() {
    ServletContext fakeContextOne = createMock(ServletContext.class);
    ServletContext fakeContextTwo = createMock(ServletContext.class);
    fakeContextOne.setAttribute(eq(INJECTOR_NAME), isA(Injector.class));
    expectLastCall().once();
    fakeContextTwo.setAttribute(eq(INJECTOR_NAME), isA(Injector.class));
    expectLastCall().once();
    replay(fakeContextOne);
    // Simulate the start of a servlet container.
    new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            // Cache this injector in the test for later testing...
            return injectorOne = Guice.createInjector(new ServletModule() {

                @Override
                protected void configureServlets() {
                    // This creates a ManagedFilterPipeline internally...
                    serve("/*").with(DummyServlet.class);
                }
            });
        }
    }.contextInitialized(new ServletContextEvent(fakeContextOne));
    ServletContext contextOne = injectorOne.getInstance(ServletContext.class);
    assertNotNull(contextOne);
    // Now simulate a second injector with a slightly different config.
    replay(fakeContextTwo);
    new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return injectorTwo = Guice.createInjector(new ServletModule() {

                @Override
                protected void configureServlets() {
                    // This creates a ManagedFilterPipeline internally...
                    filter("/8").through(DummyFilterImpl.class);
                    serve("/*").with(HttpServlet.class);
                }
            });
        }
    }.contextInitialized(new ServletContextEvent(fakeContextTwo));
    ServletContext contextTwo = injectorTwo.getInstance(ServletContext.class);
    // Make sure they are different.
    assertNotNull(contextTwo);
    assertNotSame(contextOne, contextTwo);
    // Make sure they are as expected
    assertSame(fakeContextOne, contextOne);
    assertSame(fakeContextTwo, contextTwo);
    // Make sure they are consistent.
    assertSame(contextOne, injectorOne.getInstance(ServletContext.class));
    assertSame(contextTwo, injectorTwo.getInstance(ServletContext.class));
    verify(fakeContextOne, fakeContextTwo);
}
Also used : Injector(com.google.inject.Injector) HttpServlet(javax.servlet.http.HttpServlet) ServletContext(javax.servlet.ServletContext) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 39 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project hudson-2.x by hudson.

the class HudsonTestCase method setUp.

@Override
protected void setUp() throws Exception {
    //System.setProperty("hudson.PluginStrategy", "hudson.ClassicPluginStrategy");
    env.pin();
    recipe();
    AbstractProject.WORKSPACE.toString();
    User.clear();
    // Bootstrap the container with details about our testing classes, so it can figure out what to scan/include
    // force-reset, some tests may not properly hit the tear-down to reset so do it again here
    SmoothieUtil.reset();
    new SmoothieContainerBootstrap().bootstrap(getClass().getClassLoader(), Hudson.class, Smoothie.class, HudsonTestCase.class, getClass());
    try {
        hudson = newHudson();
    } catch (Exception e) {
        // if Hudson instance fails to initialize, it leaves the instance field non-empty and break all the rest of the tests, so clean that up.
        Field f = Hudson.class.getDeclaredField("theInstance");
        f.setAccessible(true);
        f.set(null, null);
        throw e;
    }
    // collecting usage stats from tests are pointless.
    hudson.setNoUsageStatistics(true);
    hudson.setCrumbIssuer(new TestCrumbIssuer());
    final WebAppController controller = WebAppController.get();
    try {
        controller.setContext(hudson.servletContext);
    } catch (IllegalStateException e) {
        // handle tests which run several times inside the same JVM
        Field f = WebAppController.class.getDeclaredField("context");
        f.setAccessible(true);
        f.set(controller, hudson.servletContext);
    }
    try {
        controller.setInstallStrategy(new DefaultInstallStrategy());
    } catch (IllegalStateException e) {
    // strategy already set ignore
    }
    controller.install(hudson);
    hudson.servletContext.setAttribute("version", "?");
    WebAppMain.installExpressionFactory(new ServletContextEvent(hudson.servletContext));
    // set a default JDK to be the one that the harness is using.
    hudson.getJDKs().add(new JDK("default", System.getProperty("java.home")));
    configureUpdateCenter();
    // expose the test instance as a part of URL tree.
    // this allows tests to use a part of the URL space for itself.
    hudson.getActions().add(this);
    // cause all the descriptors to reload.
    // ideally we'd like to reset them to properly emulate the behavior, but that's not possible.
    Mailer.descriptor().setHudsonUrl(null);
    for (Descriptor d : hudson.getExtensionList(Descriptor.class)) d.load();
}
Also used : Field(java.lang.reflect.Field) DefaultInstallStrategy(hudson.stapler.WebAppController.DefaultInstallStrategy) ClassDescriptor(org.kohsuke.stapler.ClassDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) SmoothieContainerBootstrap(org.hudsonci.inject.internal.SmoothieContainerBootstrap) WebAppController(hudson.stapler.WebAppController) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) BadCredentialsException(org.acegisecurity.BadCredentialsException) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CSSParseException(org.w3c.css.sac.CSSParseException) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) AuthenticationException(org.acegisecurity.AuthenticationException) SAXException(org.xml.sax.SAXException) DataAccessException(org.springframework.dao.DataAccessException) CSSException(org.w3c.css.sac.CSSException) MalformedURLException(java.net.MalformedURLException) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 40 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project jersey by jersey.

the class WebAppContextListener method contextInitialized.

@Override
public void contextInitialized(final ServletContextEvent sce) {
    if (getEa() != null) {
        final String contextPath = sce.getServletContext().getContextPath();
        getEa().sendEvent(new Event("jersey/test/DEPLOYED", new HashMap<String, String>() {

            {
                put("context-path", contextPath);
            }
        }));
    }
}
Also used : HashMap(java.util.HashMap) ServletContextEvent(javax.servlet.ServletContextEvent) Event(org.osgi.service.event.Event)

Aggregations

ServletContextEvent (javax.servlet.ServletContextEvent)70 Test (org.junit.Test)42 MockServletContext (org.springframework.mock.web.test.MockServletContext)21 ServletContext (javax.servlet.ServletContext)18 ServletContextListener (javax.servlet.ServletContextListener)17 Injector (com.google.inject.Injector)10 ContextLoaderListener (org.springframework.web.context.ContextLoaderListener)10 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)8 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)8 Test (org.testng.annotations.Test)8 BrokerService (org.apache.activemq.broker.BrokerService)7 IOException (java.io.IOException)6 Properties (java.util.Properties)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 ArrayList (java.util.ArrayList)5 SystemPropertiesHelper.overrideSystemProperties (org.eclipse.che.commons.test.SystemPropertiesHelper.overrideSystemProperties)5 File (java.io.File)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3