use of javax.servlet.ServletContextEvent in project jetty.project by eclipse.
the class WebAppContextTest method testServletContextListener.
@Test
public void testServletContextListener() throws Exception {
Server server = new Server();
HotSwapHandler swap = new HotSwapHandler();
server.setHandler(swap);
server.start();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(System.getProperty("java.io.tmpdir"));
final List<String> history = new ArrayList<>();
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I0");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D0");
}
});
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I1");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D1");
throw new RuntimeException("Listener1 destroy broken");
}
});
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I2");
throw new RuntimeException("Listener2 init broken");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D2");
}
});
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I3");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D3");
}
});
try {
swap.setHandler(context);
context.start();
} catch (Exception e) {
history.add(e.getMessage());
} finally {
try {
swap.setHandler(null);
} catch (Exception e) {
while (e.getCause() instanceof Exception) e = (Exception) e.getCause();
history.add(e.getMessage());
} finally {
}
}
Assert.assertThat(history, Matchers.contains("I0", "I1", "I2", "Listener2 init broken", "D1", "D0", "Listener1 destroy broken"));
server.stop();
}
use of javax.servlet.ServletContextEvent 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.ServletContextEvent in project guice by google.
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);
}
use of javax.servlet.ServletContextEvent in project hudson-2.x by hudson.
the class HudsonTestCase method setUp.
@Override
protected void setUp() throws Exception {
//System.setProperty("hudson.PluginStrategy", "hudson.ClassicPluginStrategy");
env.pin();
recipe();
AbstractProject.WORKSPACE.toString();
User.clear();
// Bootstrap the container with details about our testing classes, so it can figure out what to scan/include
// force-reset, some tests may not properly hit the tear-down to reset so do it again here
SmoothieUtil.reset();
new SmoothieContainerBootstrap().bootstrap(getClass().getClassLoader(), Hudson.class, Smoothie.class, HudsonTestCase.class, getClass());
try {
hudson = newHudson();
} catch (Exception e) {
// if Hudson instance fails to initialize, it leaves the instance field non-empty and break all the rest of the tests, so clean that up.
Field f = Hudson.class.getDeclaredField("theInstance");
f.setAccessible(true);
f.set(null, null);
throw e;
}
// collecting usage stats from tests are pointless.
hudson.setNoUsageStatistics(true);
hudson.setCrumbIssuer(new TestCrumbIssuer());
final WebAppController controller = WebAppController.get();
try {
controller.setContext(hudson.servletContext);
} catch (IllegalStateException e) {
// handle tests which run several times inside the same JVM
Field f = WebAppController.class.getDeclaredField("context");
f.setAccessible(true);
f.set(controller, hudson.servletContext);
}
try {
controller.setInstallStrategy(new DefaultInstallStrategy());
} catch (IllegalStateException e) {
// strategy already set ignore
}
controller.install(hudson);
hudson.servletContext.setAttribute("version", "?");
WebAppMain.installExpressionFactory(new ServletContextEvent(hudson.servletContext));
// set a default JDK to be the one that the harness is using.
hudson.getJDKs().add(new JDK("default", System.getProperty("java.home")));
configureUpdateCenter();
// expose the test instance as a part of URL tree.
// this allows tests to use a part of the URL space for itself.
hudson.getActions().add(this);
// cause all the descriptors to reload.
// ideally we'd like to reset them to properly emulate the behavior, but that's not possible.
Mailer.descriptor().setHudsonUrl(null);
for (Descriptor d : hudson.getExtensionList(Descriptor.class)) d.load();
}
use of javax.servlet.ServletContextEvent in project jersey by jersey.
the class WebAppContextListener method contextInitialized.
@Override
public void contextInitialized(final ServletContextEvent sce) {
if (getEa() != null) {
final String contextPath = sce.getServletContext().getContextPath();
getEa().sendEvent(new Event("jersey/test/DEPLOYED", new HashMap<String, String>() {
{
put("context-path", contextPath);
}
}));
}
}
Aggregations