use of cn.taketoday.context.ApplicationEvent in project today-infrastructure by TAKETODAY.
the class ApplicationTests method eventsArePublishedInExpectedOrder.
@Test
@SuppressWarnings("unchecked")
void eventsArePublishedInExpectedOrder() {
Application application = new Application(ExampleConfig.class);
application.setApplicationType(ApplicationType.NONE_WEB);
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
application.addListeners(listener);
this.context = application.run();
InOrder inOrder = Mockito.inOrder(listener);
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationStartingEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationEnvironmentPreparedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationContextInitializedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationPreparedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ContextRefreshedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationStartedEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(LivenessState.CORRECT)));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationReadyEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(ReadinessState.ACCEPTING_TRAFFIC)));
inOrder.verifyNoMoreInteractions();
}
use of cn.taketoday.context.ApplicationEvent in project today-infrastructure 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));
}
use of cn.taketoday.context.ApplicationEvent in project today-infrastructure 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());
}
use of cn.taketoday.context.ApplicationEvent in project today-infrastructure by TAKETODAY.
the class ApplicationTests method verifyRegisteredListenerSuccessEvents.
@SuppressWarnings("unchecked")
private void verifyRegisteredListenerSuccessEvents() {
ApplicationListener<ApplicationEvent> listener = this.context.getBean("testApplicationListener", ApplicationListener.class);
InOrder inOrder = Mockito.inOrder(listener);
then(listener).should(inOrder).onApplicationEvent(isA(ContextRefreshedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationStartedEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(LivenessState.CORRECT)));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationReadyEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(ReadinessState.ACCEPTING_TRAFFIC)));
inOrder.verifyNoMoreInteractions();
}
use of cn.taketoday.context.ApplicationEvent in project today-infrastructure 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);
}
Aggregations