use of cn.taketoday.context.ApplicationEvent in project today-infrastructure 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-infrastructure 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-infrastructure by TAKETODAY.
the class AvailabilityChangeEventTests method publishPublishesEvent.
@Test
void publishPublishesEvent() {
ApplicationContext context = mock(ApplicationContext.class);
AvailabilityState state = LivenessState.CORRECT;
AvailabilityChangeEvent.publish(context, state);
ArgumentCaptor<ApplicationEvent> captor = ArgumentCaptor.forClass(ApplicationEvent.class);
then(context).should().publishEvent(captor.capture());
AvailabilityChangeEvent<?> event = (AvailabilityChangeEvent<?>) captor.getValue();
assertThat(event.getSource()).isEqualTo(context);
assertThat(event.getState()).isEqualTo(state);
}
use of cn.taketoday.context.ApplicationEvent in project today-infrastructure by TAKETODAY.
the class EventPublishingTestExecutionListenerTests method assertEvent.
private void assertEvent(Class<? extends TestContextEvent> eventClass, Consumer<TestContext> callback) {
callback.accept(testContext);
// The listener attempted to publish the event...
verify(testContext, times(1)).publishEvent(eventFactory.capture());
// The listener successfully published the event...
verify(applicationContext, times(1)).publishEvent(any());
// Verify the type of event that was published.
ApplicationEvent event = eventFactory.getValue().apply(testContext);
assertThat(event).isInstanceOf(eventClass);
assertThat(event.getSource()).isEqualTo(testContext);
}
use of cn.taketoday.context.ApplicationEvent in project today-infrastructure by TAKETODAY.
the class EventPublishingTestExecutionListenerTests method assertNoEvent.
private void assertNoEvent(Class<? extends TestContextEvent> eventClass, Consumer<TestContext> callback) {
callback.accept(testContext);
// The listener attempted to publish the event...
verify(testContext, times(1)).publishEvent(eventFactory.capture());
// But the event was not actually published since the ApplicationContext
// was not available.
verify(applicationContext, never()).publishEvent(any());
// In any case, we can still verify the type of event that would have
// been published.
ApplicationEvent event = eventFactory.getValue().apply(testContext);
assertThat(event).isInstanceOf(eventClass);
assertThat(event.getSource()).isEqualTo(testContext);
}
Aggregations