Search in sources :

Example 51 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project undertow by undertow-io.

the class ApplicationListeners method contextInitialized.

public void contextInitialized() {
    if (!started) {
        return;
    }
    //new listeners can be added here, so we don't use an iterator
    final ServletContextEvent event = new ServletContextEvent(servletContext);
    for (int i = 0; i < servletContextListeners.length; ++i) {
        ManagedListener listener = servletContextListeners[i];
        IN_PROGRAMATIC_SC_LISTENER_INVOCATION.set(listener.isProgramatic() ? PROGRAMATIC_LISTENER : DECLARED_LISTENER);
        try {
            this.<ServletContextListener>get(listener).contextInitialized(event);
        } finally {
            IN_PROGRAMATIC_SC_LISTENER_INVOCATION.remove();
        }
    }
}
Also used : ServletContextEvent(javax.servlet.ServletContextEvent)

Example 52 with ServletContextEvent

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

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

the class ContextLoaderTests method testContextLoaderListenerWithUnknownContextInitializer.

@Test
public void testContextLoaderListenerWithUnknownContextInitializer() {
    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(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(new Object[] { UnknownContextInitializer.class.getName() }));
    ContextLoaderListener listener = new ContextLoaderListener();
    try {
        listener.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (ApplicationContextException ex) {
        assertTrue(ex.getMessage().contains("not assignable"));
    }
}
Also used : ApplicationContextException(org.springframework.context.ApplicationContextException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 54 with ServletContextEvent

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

the class ContextLoaderTests method testContextLoaderListenerWithProgrammaticAndLocalInitializers.

@Test
public void testContextLoaderListenerWithProgrammaticAndLocalInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestWebContextInitializer());
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName(), equalTo("testName"));
    assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) 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 55 with ServletContextEvent

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

the class Spr8510Tests method abstractRefreshableWAC_fallsBackToConventionBasedNaming.

/**
	 * If context config locations have been specified neither against the application
	 * context nor the context loader listener, then fall back to default values.
	 */
@Test
public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
    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
        System.out.println(t.getMessage());
        assertTrue(t.getMessage().endsWith("Could not open ServletContext resource [/WEB-INF/applicationContext.xml]"));
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Aggregations

ServletContextEvent (javax.servlet.ServletContextEvent)74 Test (org.junit.Test)43 MockServletContext (org.springframework.mock.web.test.MockServletContext)21 ServletContextListener (javax.servlet.ServletContextListener)19 ServletContext (javax.servlet.ServletContext)18 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