use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderWithInvalidContext.
@Test
void contextLoaderWithInvalidContext() 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);
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> listener.contextInitialized(event)).withCauseInstanceOf(ClassNotFoundException.class);
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithDefaultContext.
@Test
void contextLoaderListenerWithDefaultContext() {
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);
String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr);
boolean condition1 = context instanceof XmlWebApplicationContext;
assertThat(condition1).as("Correct WebApplicationContext exposed in ServletContext").isTrue();
assertThat(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext).isTrue();
LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
assertThat(context.containsBean("father")).as("Has father").isTrue();
assertThat(context.containsBean("rod")).as("Has rod").isTrue();
assertThat(context.containsBean("kerry")).as("Has kerry").isTrue();
boolean condition = !lb.isDestroyed();
assertThat(condition).as("Not destroyed").isTrue();
assertThat(context.containsBean("beans1.bean1")).isFalse();
assertThat(context.containsBean("beans1.bean2")).isFalse();
listener.contextDestroyed(event);
assertThat(lb.isDestroyed()).as("Destroyed").isTrue();
assertThat(sc.getAttribute(contextAttr)).isNull();
assertThat(WebApplicationContextUtils.getWebApplicationContext(sc)).isNull();
}
use of jakarta.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithMixedContextInitializers.
@Test
void contextLoaderListenerWithMixedContextInitializers() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, 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()).isEqualTo("testName");
assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
use of jakarta.servlet.ServletContextEvent in project spring-boot by spring-projects.
the class JettyServletWebServerFactoryTests method faultyListenerCausesStartFailure.
@Test
void faultyListenerCausesStartFailure() {
JettyServletWebServerFactory factory = getFactory();
factory.addServerCustomizers((JettyServerCustomizer) (server) -> {
Collection<WebAppContext> contexts = server.getBeans(WebAppContext.class);
EventListener eventListener = new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent event) {
throw new RuntimeException();
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
};
WebAppContext context = contexts.iterator().next();
try {
context.addEventListener(eventListener);
} catch (NoSuchMethodError ex) {
Method addEventListener = ReflectionUtils.findMethod(context.getClass(), "addEventListener", EventListener.class);
ReflectionUtils.invokeMethod(addEventListener, context, eventListener);
}
});
assertThatExceptionOfType(WebServerException.class).isThrownBy(() -> {
JettyWebServer jettyWebServer = (JettyWebServer) factory.getWebServer();
try {
jettyWebServer.start();
} finally {
QueuedThreadPool threadPool = (QueuedThreadPool) jettyWebServer.getServer().getThreadPool();
assertThat(threadPool.isRunning()).isFalse();
}
});
}
use of jakarta.servlet.ServletContextEvent in project spring-boot by spring-projects.
the class SpringBootServletInitializerTests method whenServletContextIsDestroyedThenJdbcDriversAreDeregistered.
@Test
void whenServletContextIsDestroyedThenJdbcDriversAreDeregistered() throws ServletException {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.getInitParameterNames()).willReturn(new Vector<String>().elements());
given(servletContext.getAttributeNames()).willReturn(new Vector<String>().elements());
AtomicBoolean driversDeregistered = new AtomicBoolean();
new SpringBootServletInitializer() {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Config.class);
}
@Override
protected void deregisterJdbcDrivers(ServletContext servletContext) {
driversDeregistered.set(true);
}
}.onStartup(servletContext);
ArgumentCaptor<ServletContextListener> captor = ArgumentCaptor.forClass(ServletContextListener.class);
then(servletContext).should().addListener(captor.capture());
captor.getValue().contextDestroyed(new ServletContextEvent(servletContext));
assertThat(driversDeregistered).isTrue();
}
Aggregations