use of cn.taketoday.context.ApplicationEvent in project today-framework 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);
}
use of cn.taketoday.context.ApplicationEvent in project today-framework 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-framework by TAKETODAY.
the class ApplicationContextEventTests method simpleApplicationEventMulticasterWithErrorHandler.
@Test
public void simpleApplicationEventMulticasterWithErrorHandler() {
@SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.setErrorHandler(TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER);
smc.addApplicationListener(listener);
willThrow(new RuntimeException()).given(listener).onApplicationEvent(evt);
smc.multicastEvent(evt);
}
use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.
the class ApplicationContextEventTests method simpleApplicationEventMulticasterWithTaskExecutor.
@Test
public void simpleApplicationEventMulticasterWithTaskExecutor() {
@SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.setTaskExecutor(command -> {
command.run();
command.run();
});
smc.addApplicationListener(listener);
smc.multicastEvent(evt);
verify(listener, times(2)).onApplicationEvent(evt);
}
use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.
the class ApplicationTests method applicationRunnerFailureCausesApplicationFailedEventToBePublished.
@Test
void applicationRunnerFailureCausesApplicationFailedEventToBePublished() throws Exception {
Application application = new Application(ExampleConfig.class);
application.setApplicationType(ApplicationType.NONE_WEB);
@SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
application.addListeners(listener);
ApplicationRunner runner = mock(ApplicationRunner.class);
Exception failure = new Exception();
willThrow(failure).given(runner).run(isA(ApplicationArguments.class));
application.addInitializers((context) -> context.getBeanFactory().registerSingleton("runner", runner));
assertThatIllegalStateException().isThrownBy(application::run).withCause(failure);
then(listener).should().onApplicationEvent(isA(ApplicationStartedEvent.class));
then(listener).should().onApplicationEvent(isA(ApplicationFailedEvent.class));
then(listener).should(never()).onApplicationEvent(isA(ApplicationReadyEvent.class));
}
Aggregations