Search in sources :

Example 16 with ApplicationEvent

use of cn.taketoday.context.ApplicationEvent in project today-framework 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);
}
Also used : ApplicationEvent(cn.taketoday.context.ApplicationEvent) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 17 with ApplicationEvent

use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.

the class ApplicationContextEventTests method simpleApplicationEventMulticasterWithException.

@Test
public void simpleApplicationEventMulticasterWithException() {
    @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
    ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
    SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
    smc.addApplicationListener(listener);
    RuntimeException thrown = new RuntimeException();
    willThrow(thrown).given(listener).onApplicationEvent(evt);
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> smc.multicastEvent(evt)).satisfies(ex -> assertThat(ex).isSameAs(thrown));
}
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 18 with ApplicationEvent

use of cn.taketoday.context.ApplicationEvent in project today-framework by TAKETODAY.

the class ApplicationContextEventTests method lambdaAsListenerWithJava9StyleClassCastMessage.

@Test
public void lambdaAsListenerWithJava9StyleClassCastMessage() {
    StaticApplicationContext context = new StaticApplicationContext();
    ApplicationListener<ApplicationEvent> listener = event -> {
        throw new ClassCastException("spring.context/" + event.getClass().getName());
    };
    context.addApplicationListener(listener);
    context.refresh();
    context.publishEvent(new MyEvent(context));
    context.close();
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) ApplicationEvent(cn.taketoday.context.ApplicationEvent) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference) ApplicationContextAware(cn.taketoday.context.aware.ApplicationContextAware) ApplicationEventPublisherAware(cn.taketoday.context.aware.ApplicationEventPublisherAware) Ordered(cn.taketoday.core.Ordered) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BeanThatBroadcasts(cn.taketoday.context.testfixture.beans.BeanThatBroadcasts) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ApplicationListener(cn.taketoday.context.ApplicationListener) BDDMockito.given(org.mockito.BDDMockito.given) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) BeanThatListens(cn.taketoday.context.testfixture.beans.BeanThatListens) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) BeansException(cn.taketoday.beans.BeansException) Order(cn.taketoday.core.annotation.Order) InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) StaticMessageSource(cn.taketoday.context.support.StaticMessageSource) ApplicationContext(cn.taketoday.context.ApplicationContext) ApplicationEventPublisher(cn.taketoday.context.ApplicationEventPublisher) AbstractApplicationContext(cn.taketoday.context.support.AbstractApplicationContext) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) ResolvableType(cn.taketoday.core.ResolvableType) Set(java.util.Set) ReflectionUtils(cn.taketoday.util.ReflectionUtils) PayloadApplicationEvent(cn.taketoday.context.PayloadApplicationEvent) Mockito.times(org.mockito.Mockito.times) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) TaskUtils(cn.taketoday.scheduling.support.TaskUtils) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) InitializingBean(cn.taketoday.beans.factory.InitializingBean) Mockito.mock(org.mockito.Mockito.mock) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) ApplicationEvent(cn.taketoday.context.ApplicationEvent) PayloadApplicationEvent(cn.taketoday.context.PayloadApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 19 with ApplicationEvent

use of cn.taketoday.context.ApplicationEvent in project today-framework 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();
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) ApplicationEvent(cn.taketoday.context.ApplicationEvent) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference) ApplicationContextAware(cn.taketoday.context.aware.ApplicationContextAware) ApplicationEventPublisherAware(cn.taketoday.context.aware.ApplicationEventPublisherAware) Ordered(cn.taketoday.core.Ordered) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BeanThatBroadcasts(cn.taketoday.context.testfixture.beans.BeanThatBroadcasts) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ApplicationListener(cn.taketoday.context.ApplicationListener) BDDMockito.given(org.mockito.BDDMockito.given) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) BeanThatListens(cn.taketoday.context.testfixture.beans.BeanThatListens) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) BeansException(cn.taketoday.beans.BeansException) Order(cn.taketoday.core.annotation.Order) InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) StaticMessageSource(cn.taketoday.context.support.StaticMessageSource) ApplicationContext(cn.taketoday.context.ApplicationContext) ApplicationEventPublisher(cn.taketoday.context.ApplicationEventPublisher) AbstractApplicationContext(cn.taketoday.context.support.AbstractApplicationContext) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) ResolvableType(cn.taketoday.core.ResolvableType) Set(java.util.Set) ReflectionUtils(cn.taketoday.util.ReflectionUtils) PayloadApplicationEvent(cn.taketoday.context.PayloadApplicationEvent) Mockito.times(org.mockito.Mockito.times) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) TaskUtils(cn.taketoday.scheduling.support.TaskUtils) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) InitializingBean(cn.taketoday.beans.factory.InitializingBean) Mockito.mock(org.mockito.Mockito.mock) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) ApplicationEvent(cn.taketoday.context.ApplicationEvent) PayloadApplicationEvent(cn.taketoday.context.PayloadApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 20 with ApplicationEvent

use of cn.taketoday.context.ApplicationEvent in project today-framework 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)

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