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());
}
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());
}
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());
}
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());
}
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);
}
}
Aggregations