Search in sources :

Example 11 with ApplicationEvent

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);
}
Also used : ApplicationEvent(cn.taketoday.context.ApplicationEvent)

Example 12 with ApplicationEvent

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);
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) ApplicationEvent(cn.taketoday.context.ApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 13 with ApplicationEvent

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);
}
Also used : StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) ApplicationEvent(cn.taketoday.context.ApplicationEvent) PayloadApplicationEvent(cn.taketoday.context.PayloadApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 14 with ApplicationEvent

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);
}
Also used : StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) ApplicationEvent(cn.taketoday.context.ApplicationEvent) PayloadApplicationEvent(cn.taketoday.context.PayloadApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 15 with ApplicationEvent

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));
}
Also used : ApplicationFailedEvent(cn.taketoday.framework.context.event.ApplicationFailedEvent) ApplicationReadyEvent(cn.taketoday.framework.context.event.ApplicationReadyEvent) ApplicationEvent(cn.taketoday.context.ApplicationEvent) ApplicationStartedEvent(cn.taketoday.framework.context.event.ApplicationStartedEvent) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) BeanDefinitionOverrideException(cn.taketoday.beans.factory.support.BeanDefinitionOverrideException) BeanCurrentlyInCreationException(cn.taketoday.beans.factory.BeanCurrentlyInCreationException) ApplicationContextException(cn.taketoday.context.ApplicationContextException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) BeanCreationException(cn.taketoday.beans.factory.BeanCreationException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationEvent (cn.taketoday.context.ApplicationEvent)28 Test (org.junit.jupiter.api.Test)22 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)12 PayloadApplicationEvent (cn.taketoday.context.PayloadApplicationEvent)10 ApplicationReadyEvent (cn.taketoday.framework.context.event.ApplicationReadyEvent)10 ApplicationStartedEvent (cn.taketoday.framework.context.event.ApplicationStartedEvent)10 ApplicationContext (cn.taketoday.context.ApplicationContext)8 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)6 BeanCurrentlyInCreationException (cn.taketoday.beans.factory.BeanCurrentlyInCreationException)6 UnsatisfiedDependencyException (cn.taketoday.beans.factory.UnsatisfiedDependencyException)6 BeanDefinitionOverrideException (cn.taketoday.beans.factory.support.BeanDefinitionOverrideException)6 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)6 ApplicationContextException (cn.taketoday.context.ApplicationContextException)6 ApplicationListener (cn.taketoday.context.ApplicationListener)6 ApplicationContextAware (cn.taketoday.context.aware.ApplicationContextAware)6 ContextRefreshedEvent (cn.taketoday.context.event.ContextRefreshedEvent)6 AbstractApplicationContext (cn.taketoday.context.support.AbstractApplicationContext)6 Ordered (cn.taketoday.core.Ordered)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6