Search in sources :

Example 26 with ApplicationEvent

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

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

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

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