use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.
the class ServletWebServerApplicationContextTests method ServletWebServerInitializedEventPublished.
@Test
void ServletWebServerInitializedEventPublished() {
addWebServerFactoryBean();
this.context.registerBeanDefinition("listener", new RootBeanDefinition(TestApplicationListener.class));
this.context.refresh();
List<ApplicationEvent> events = this.context.getBean(TestApplicationListener.class).receivedEvents();
assertThat(events).hasSize(2).extracting("class").containsExactly(ServletWebServerInitializedEvent.class, ContextRefreshedEvent.class);
ServletWebServerInitializedEvent initializedEvent = (ServletWebServerInitializedEvent) events.get(0);
assertThat(initializedEvent.getSource().getPort() >= 0).isTrue();
assertThat(initializedEvent.getApplicationContext()).isEqualTo(this.context);
}
use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.
the class ApplicationContextEventTests method simpleApplicationEventMulticasterWithException.
@Test
public void simpleApplicationEventMulticasterWithException() {
@SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(listener);
RuntimeException thrown = new RuntimeException();
willThrow(thrown).given(listener).onApplicationEvent(evt);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> smc.multicastEvent(evt)).satisfies(ex -> assertThat(ex).isSameAs(thrown));
}
use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.
the class ApplicationContextEventTests method lambdaAsListenerWithJava9StyleClassCastMessage.
@Test
public void lambdaAsListenerWithJava9StyleClassCastMessage() {
StaticApplicationContext context = new StaticApplicationContext();
ApplicationListener<ApplicationEvent> listener = event -> {
throw new ClassCastException("spring.context/" + event.getClass().getName());
};
context.addApplicationListener(listener);
context.refresh();
context.publishEvent(new MyEvent(context));
context.close();
}
use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.
the class ApplicationContextEventTests method lambdaAsListenerWithJava8StyleClassCastMessage.
@Test
public void lambdaAsListenerWithJava8StyleClassCastMessage() {
StaticApplicationContext context = new StaticApplicationContext();
ApplicationListener<ApplicationEvent> listener = event -> {
throw new ClassCastException(event.getClass().getName());
};
context.addApplicationListener(listener);
context.refresh();
context.publishEvent(new MyEvent(context));
context.close();
}
use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.
the class ApplicationTests method deregistersShutdownHookForFailedApplicationContext.
@Test
void deregistersShutdownHookForFailedApplicationContext() {
Application application = new Application(BrokenPostConstructConfig.class);
List<ApplicationEvent> events = new ArrayList<>();
application.addListeners(events::add);
application.setApplicationType(ApplicationType.NONE_WEB);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(application::run);
assertThat(events).hasAtLeastOneElementOfType(ApplicationFailedEvent.class);
ApplicationFailedEvent failure = events.stream().filter((event) -> event instanceof ApplicationFailedEvent).map(ApplicationFailedEvent.class::cast).findFirst().get();
assertThat(ApplicationShutdownHookInstance.get()).didNotRegisterApplicationContext(failure.getApplicationContext());
}
Aggregations