use of javax.servlet.ServletContextListener in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method servletContextListenerBeans.
@Test
public void servletContextListenerBeans() throws Exception {
addWebServerFactoryBean();
ServletContextListener initializer = mock(ServletContextListener.class);
this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
this.context.refresh();
ServletContext servletContext = getWebServerFactory().getServletContext();
verify(servletContext).addListener(initializer);
}
use of javax.servlet.ServletContextListener in project spring-boot by spring-projects.
the class ServletWebServerServletContextListenerTests method registeredServletContextListenerBeanIsCalled.
private void registeredServletContextListenerBeanIsCalled(Class<?> configuration) {
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(ServletListenerRegistrationBeanConfiguration.class, configuration);
ServletContextListener servletContextListener = (ServletContextListener) context.getBean("registration", ServletListenerRegistrationBean.class).getListener();
verify(servletContextListener).contextInitialized(any(ServletContextEvent.class));
context.close();
}
use of javax.servlet.ServletContextListener 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"));
}
}
use of javax.servlet.ServletContextListener in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderWithCustomContext.
@Test
public void testContextLoaderWithCustomContext() throws Exception {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.servlet.SimpleWebApplicationContext");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
assertTrue("Correct WebApplicationContext exposed in ServletContext", wc instanceof SimpleWebApplicationContext);
}
use of javax.servlet.ServletContextListener in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderWithInvalidLocation.
@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
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 FileNotFoundException);
}
}
Aggregations