Search in sources :

Example 61 with ServletContextEvent

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

the class WebApplicationContextScopeTests method testApplicationScope.

@Test
public void testApplicationScope() {
    WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
    assertNull(ac.getServletContext().getAttribute(NAME));
    DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
    assertSame(bean, ac.getServletContext().getAttribute(NAME));
    assertSame(bean, ac.getBean(NAME));
    new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
    assertTrue(bean.wasDestroyed());
}
Also used : DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ContextCleanupListener(org.springframework.web.context.ContextCleanupListener) ServletContextEvent(javax.servlet.ServletContextEvent) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 62 with ServletContextEvent

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

the class ContextLoaderTests method testContextLoaderListenerWithProgrammaticInitializers.

@Test
public void testContextLoaderListenerWithProgrammaticInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestContextInitializer(), 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 63 with ServletContextEvent

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

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

the class ContextLoaderTests method testContextLoaderListenerWithGlobalContextInitializers.

@Test
public void testContextLoaderListenerWithGlobalContextInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(new Object[] { TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName() }));
    ContextLoaderListener listener = new ContextLoaderListener();
    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 65 with ServletContextEvent

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

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