use of javax.servlet.ServletContextEvent in project roboguice by roboguice.
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 spring-boot by spring-projects.
the class SpringBootServletInitializer method onStartup.
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Logger initialization is deferred in case a ordered
// LogServletContextInitializer is being used
this.logger = LogFactory.getLog(getClass());
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
if (rootAppContext != null) {
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
@Override
public void contextInitialized(ServletContextEvent event) {
// no-op because the application context is already initialized
}
});
} else {
this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not " + "return an application context");
}
}
use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class Spr8510Tests method abstractRefreshableWAC_respectsProgrammaticConfigLocations.
@Test
public void abstractRefreshableWAC_respectsProgrammaticConfigLocations() {
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
ctx.setConfigLocation("programmatic.xml");
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
assertTrue(t.getMessage(), t.getMessage().endsWith("Could not open ServletContext resource [/programmatic.xml]"));
}
}
use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderInitializerTests method register.
@Test
public void register() throws ServletException {
initializer.onStartup(servletContext);
assertTrue(eventListener instanceof ContextLoaderListener);
ContextLoaderListener cll = (ContextLoaderListener) eventListener;
cll.contextInitialized(new ServletContextEvent(servletContext));
WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
assertTrue(applicationContext.containsBean(BEAN_NAME));
assertTrue(applicationContext.getBean(BEAN_NAME) instanceof MyBean);
}
use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderWithDefaultLocation.
@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
MockServletContext sc = new MockServletContext("");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
try {
listener.contextInitialized(event);
fail("Should have thrown BeanDefinitionStoreException");
} catch (BeanDefinitionStoreException ex) {
// expected
assertTrue(ex.getCause() instanceof IOException);
assertTrue(ex.getCause().getMessage().contains("/WEB-INF/applicationContext.xml"));
}
}
Aggregations