Search in sources :

Example 11 with ServletContextEvent

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

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 12 with ServletContextEvent

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

the class SpringBootServletInitializer method onStartup.

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Logger initialization is deferred in case a ordered
    // LogServletContextInitializer is being used
    this.logger = LogFactory.getLog(getClass());
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        servletContext.addListener(new ContextLoaderListener(rootAppContext) {

            @Override
            public void contextInitialized(ServletContextEvent event) {
            // no-op because the application context is already initialized
            }
        });
    } else {
        this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not " + "return an application context");
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextEvent(javax.servlet.ServletContextEvent) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 13 with ServletContextEvent

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

the class Spr8510Tests method abstractRefreshableWAC_respectsProgrammaticConfigLocations.

@Test
public void abstractRefreshableWAC_respectsProgrammaticConfigLocations() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocation("programmatic.xml");
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    try {
        cll.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (Throwable t) {
        // assert that an attempt was made to load the correct XML
        assertTrue(t.getMessage(), t.getMessage().endsWith("Could not open ServletContext resource [/programmatic.xml]"));
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 14 with ServletContextEvent

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

the class ContextLoaderInitializerTests method register.

@Test
public void register() throws ServletException {
    initializer.onStartup(servletContext);
    assertTrue(eventListener instanceof ContextLoaderListener);
    ContextLoaderListener cll = (ContextLoaderListener) eventListener;
    cll.contextInitialized(new ServletContextEvent(servletContext));
    WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    assertTrue(applicationContext.containsBean(BEAN_NAME));
    assertTrue(applicationContext.getBean(BEAN_NAME) instanceof MyBean);
}
Also used : ServletContextEvent(javax.servlet.ServletContextEvent) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 15 with ServletContextEvent

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

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