Search in sources :

Example 1 with ApplicationEvent

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();
}
Also used : InOrder(org.mockito.InOrder) ApplicationReadyEvent(cn.taketoday.framework.context.event.ApplicationReadyEvent) ApplicationEvent(cn.taketoday.context.ApplicationEvent) ApplicationPreparedEvent(cn.taketoday.framework.context.event.ApplicationPreparedEvent) ApplicationStartedEvent(cn.taketoday.framework.context.event.ApplicationStartedEvent) ApplicationStartingEvent(cn.taketoday.framework.context.event.ApplicationStartingEvent) ApplicationContextInitializedEvent(cn.taketoday.framework.context.event.ApplicationContextInitializedEvent) ApplicationEnvironmentPreparedEvent(cn.taketoday.framework.context.event.ApplicationEnvironmentPreparedEvent) ContextRefreshedEvent(cn.taketoday.context.event.ContextRefreshedEvent) Test(org.junit.jupiter.api.Test)

Example 2 with ApplicationEvent

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));
}
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)

Example 3 with ApplicationEvent

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());
}
Also used : ApplicationFailedEvent(cn.taketoday.framework.context.event.ApplicationFailedEvent) BeforeEach(org.junit.jupiter.api.BeforeEach) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Resource(cn.taketoday.core.io.Resource) ApplicationEvent(cn.taketoday.context.ApplicationEvent) PostConstruct(jakarta.annotation.PostConstruct) Ordered(cn.taketoday.core.Ordered) PropertySource(cn.taketoday.core.env.PropertySource) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) ApplicationContextInitializedEvent(cn.taketoday.framework.context.event.ApplicationContextInitializedEvent) ConfigurableWebEnvironment(cn.taketoday.web.context.ConfigurableWebEnvironment) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) Map(java.util.Map) ApplicationBuilder(cn.taketoday.framework.builder.ApplicationBuilder) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) BeanDefinitionOverrideException(cn.taketoday.beans.factory.support.BeanDefinitionOverrideException) ApplicationEnvironmentPreparedEvent(cn.taketoday.framework.context.event.ApplicationEnvironmentPreparedEvent) AbstractApplicationContext(cn.taketoday.context.support.AbstractApplicationContext) BeanNameGenerator(cn.taketoday.beans.factory.support.BeanNameGenerator) Profiles(cn.taketoday.core.env.Profiles) BeanCurrentlyInCreationException(cn.taketoday.beans.factory.BeanCurrentlyInCreationException) ObjectProvider(cn.taketoday.beans.factory.ObjectProvider) Bean(cn.taketoday.context.annotation.Bean) MockEnvironment(cn.taketoday.mock.env.MockEnvironment) Environment(cn.taketoday.core.env.Environment) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Set(java.util.Set) ContextRefreshedEvent(cn.taketoday.context.event.ContextRefreshedEvent) ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) ApplicationFailedEvent(cn.taketoday.framework.context.event.ApplicationFailedEvent) ApplicationContextException(cn.taketoday.context.ApplicationContextException) CapturedOutput(cn.taketoday.framework.test.system.CapturedOutput) CachedIntrospectionResults(cn.taketoday.beans.CachedIntrospectionResults) TestPropertySourceUtils(cn.taketoday.test.context.support.TestPropertySourceUtils) DefaultBeanNameGenerator(cn.taketoday.beans.factory.support.DefaultBeanNameGenerator) CommandLinePropertySource(cn.taketoday.core.env.CommandLinePropertySource) Mockito.mock(org.mockito.Mockito.mock) AvailabilityChangeEvent(cn.taketoday.framework.availability.AvailabilityChangeEvent) HttpHandler(cn.taketoday.http.server.reactive.HttpHandler) StringUtils(cn.taketoday.util.StringUtils) ConfigurableBeanFactory(cn.taketoday.beans.factory.config.ConfigurableBeanFactory) InstanceSupplier(cn.taketoday.framework.BootstrapRegistry.InstanceSupplier) DefaultResourceLoader(cn.taketoday.core.io.DefaultResourceLoader) Mockito.spy(org.mockito.Mockito.spy) ApplicationReadyEvent(cn.taketoday.framework.context.event.ApplicationReadyEvent) ArrayList(java.util.ArrayList) WebApplicationContext(cn.taketoday.web.WebApplicationContext) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MultiValueMap(cn.taketoday.core.MultiValueMap) LinkedHashSet(java.util.LinkedHashSet) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) ApplicationPreparedEvent(cn.taketoday.framework.context.event.ApplicationPreparedEvent) Mono(reactor.core.publisher.Mono) AnnotationConfigReactiveWebServerApplicationContext(cn.taketoday.framework.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) Condition(org.assertj.core.api.Condition) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) AnnotationConfigServletWebServerApplicationContext(cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Configuration(cn.taketoday.context.annotation.Configuration) NettyReactiveWebServerFactory(cn.taketoday.framework.web.embedded.netty.NettyReactiveWebServerFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReadinessState(cn.taketoday.framework.availability.ReadinessState) ApplicationStartingEvent(cn.taketoday.framework.context.event.ApplicationStartingEvent) SimpleApplicationEventMulticaster(cn.taketoday.context.event.SimpleApplicationEventMulticaster) ArgumentMatcher(org.mockito.ArgumentMatcher) ApplicationEventMulticaster(cn.taketoday.context.event.ApplicationEventMulticaster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClassPathResource(cn.taketoday.core.io.ClassPathResource) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) BeanCreationException(cn.taketoday.beans.factory.BeanCreationException) Lazy(cn.taketoday.context.annotation.Lazy) ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer) ApplicationContext(cn.taketoday.context.ApplicationContext) CompositePropertySource(cn.taketoday.core.env.CompositePropertySource) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) ApplicationConversionService(cn.taketoday.format.support.ApplicationConversionService) Autowired(cn.taketoday.beans.factory.annotation.Autowired) AnnotationConfigUtils(cn.taketoday.context.annotation.AnnotationConfigUtils) ResourceLoader(cn.taketoday.core.io.ResourceLoader) SmartApplicationListener(cn.taketoday.context.event.SmartApplicationListener) Test(org.junit.jupiter.api.Test) List(java.util.List) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) HashMap(java.util.HashMap) ReactiveWebApplicationContext(cn.taketoday.framework.web.reactive.context.ReactiveWebApplicationContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationContextAware(cn.taketoday.context.aware.ApplicationContextAware) OutputCaptureExtension(cn.taketoday.framework.test.system.OutputCaptureExtension) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationListener(cn.taketoday.context.ApplicationListener) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) InOrder(org.mockito.InOrder) Iterator(java.util.Iterator) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) BDDMockito.then(org.mockito.BDDMockito.then) TomcatServletWebServerFactory(cn.taketoday.framework.web.embedded.tomcat.TomcatServletWebServerFactory) Mockito(org.mockito.Mockito) AvailabilityState(cn.taketoday.framework.availability.AvailabilityState) LivenessState(cn.taketoday.framework.availability.LivenessState) Collections(java.util.Collections) ApplicationStartedEvent(cn.taketoday.framework.context.event.ApplicationStartedEvent) BeanCreationException(cn.taketoday.beans.factory.BeanCreationException) ApplicationEvent(cn.taketoday.context.ApplicationEvent) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 4 with ApplicationEvent

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();
}
Also used : InOrder(org.mockito.InOrder) ApplicationReadyEvent(cn.taketoday.framework.context.event.ApplicationReadyEvent) ApplicationEvent(cn.taketoday.context.ApplicationEvent) ApplicationStartedEvent(cn.taketoday.framework.context.event.ApplicationStartedEvent) ContextRefreshedEvent(cn.taketoday.context.event.ContextRefreshedEvent)

Example 5 with ApplicationEvent

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