Search in sources :

Example 6 with ApplicationEvent

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

use of cn.taketoday.context.ApplicationEvent in project today-infrastructure 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 8 with ApplicationEvent

use of cn.taketoday.context.ApplicationEvent in project today-infrastructure 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 9 with ApplicationEvent

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

the class EventPublishingTestExecutionListenerTests method assertEvent.

private void assertEvent(Class<? extends TestContextEvent> eventClass, Consumer<TestContext> callback) {
    callback.accept(testContext);
    // The listener attempted to publish the event...
    verify(testContext, times(1)).publishEvent(eventFactory.capture());
    // The listener successfully published the event...
    verify(applicationContext, times(1)).publishEvent(any());
    // Verify the type of event that was published.
    ApplicationEvent event = eventFactory.getValue().apply(testContext);
    assertThat(event).isInstanceOf(eventClass);
    assertThat(event.getSource()).isEqualTo(testContext);
}
Also used : ApplicationEvent(cn.taketoday.context.ApplicationEvent)

Example 10 with ApplicationEvent

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

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