use of javax.servlet.ServletContextListener 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());
}
use of javax.servlet.ServletContextListener 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.ServletContextListener 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);
}
}
use of javax.servlet.ServletContextListener 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));
}
use of javax.servlet.ServletContextListener in project spring-boot by spring-projects.
the class ServletWebServerServletContextListenerTests method servletContextListenerBeanIsCalled.
private void servletContextListenerBeanIsCalled(Class<?> configuration) {
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(ServletContextListenerBeanConfiguration.class, configuration);
ServletContextListener servletContextListener = context.getBean("servletContextListener", ServletContextListener.class);
verify(servletContextListener).contextInitialized(any(ServletContextEvent.class));
context.close();
}
Aggregations