Search in sources :

Example 11 with TestEvent

use of org.springframework.context.event.test.TestEvent in project spring-framework by spring-projects.

the class AnnotationDrivenEventListenerTests method eventListenerWorksWithAnnotatedInterfaceProxy.

@Test
public void eventListenerWorksWithAnnotatedInterfaceProxy() throws Exception {
    load(AnnotatedProxyTestBean.class);
    AnnotatedSimpleService proxy = this.context.getBean(AnnotatedSimpleService.class);
    assertTrue("bean should be a proxy", proxy instanceof Advised);
    this.eventCollector.assertNoEventReceived(proxy.getId());
    this.context.publishEvent(new ContextRefreshedEvent(this.context));
    this.eventCollector.assertNoEventReceived(proxy.getId());
    TestEvent event = new TestEvent();
    this.context.publishEvent(event);
    this.eventCollector.assertEvent(proxy.getId(), event);
    this.eventCollector.assertTotalEventsCount(1);
}
Also used : Advised(org.springframework.aop.framework.Advised) TestEvent(org.springframework.context.event.test.TestEvent) AnotherTestEvent(org.springframework.context.event.test.AnotherTestEvent) Test(org.junit.Test)

Example 12 with TestEvent

use of org.springframework.context.event.test.TestEvent in project spring-framework by spring-projects.

the class AnnotationDrivenEventListenerTests method eventListenerWorksWithCustomScope.

@Test
public void eventListenerWorksWithCustomScope() throws Exception {
    load(CustomScopeTestBean.class);
    CustomScope customScope = new CustomScope();
    this.context.getBeanFactory().registerScope("custom", customScope);
    CustomScopeTestBean proxy = this.context.getBean(CustomScopeTestBean.class);
    assertTrue("bean should be a cglib proxy", AopUtils.isCglibProxy(proxy));
    this.eventCollector.assertNoEventReceived(proxy.getId());
    this.context.publishEvent(new ContextRefreshedEvent(this.context));
    this.eventCollector.assertNoEventReceived(proxy.getId());
    customScope.active = false;
    this.context.publishEvent(new ContextRefreshedEvent(this.context));
    customScope.active = true;
    this.eventCollector.assertNoEventReceived(proxy.getId());
    TestEvent event = new TestEvent();
    this.context.publishEvent(event);
    this.eventCollector.assertEvent(proxy.getId(), event);
    this.eventCollector.assertTotalEventsCount(1);
    try {
        customScope.active = false;
        this.context.publishEvent(new TestEvent());
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) TestEvent(org.springframework.context.event.test.TestEvent) AnotherTestEvent(org.springframework.context.event.test.AnotherTestEvent) Test(org.junit.Test)

Aggregations

TestEvent (org.springframework.context.event.test.TestEvent)12 Test (org.junit.Test)11 AnotherTestEvent (org.springframework.context.event.test.AnotherTestEvent)11 Advised (org.springframework.aop.framework.Advised)2 Test (org.junit.jupiter.api.Test)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1 TestApplicationListener (org.springframework.context.testfixture.beans.TestApplicationListener)1