Search in sources :

Example 6 with TestEvent

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

the class EventPublicationInterceptorTests method testExpectedBehavior.

@Test
public void testExpectedBehavior() {
    TestBean target = new TestBean();
    final TestApplicationListener listener = new TestApplicationListener();
    class TestContext extends StaticApplicationContext {

        @Override
        protected void onRefresh() throws BeansException {
            addApplicationListener(listener);
        }
    }
    StaticApplicationContext ctx = new TestContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("applicationEventClass", TestEvent.class.getName());
    // should automatically receive applicationEventPublisher reference
    ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs);
    ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
    ctx.refresh();
    EventPublicationInterceptor interceptor = (EventPublicationInterceptor) ctx.getBean("publisher");
    ProxyFactory factory = new ProxyFactory(target);
    factory.addAdvice(0, interceptor);
    ITestBean testBean = (ITestBean) factory.getProxy();
    // invoke any method on the advised proxy to see if the interceptor has been invoked
    testBean.getAge();
    // two events: ContextRefreshedEvent and TestEvent
    assertThat(listener.getEventCount() == 2).as("Interceptor must have published 2 events").isTrue();
    TestApplicationListener otherListener = (TestApplicationListener) ctx.getBean("&otherListener");
    assertThat(otherListener.getEventCount() == 2).as("Interceptor must have published 2 events").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestApplicationListener(org.springframework.context.testfixture.beans.TestApplicationListener) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestEvent(org.springframework.context.event.test.TestEvent) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.jupiter.api.Test)

Example 7 with TestEvent

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

the class AnnotationDrivenEventListenerTests method metaAnnotationIsDiscovered.

@Test
public void metaAnnotationIsDiscovered() {
    load(MetaAnnotationListenerTestBean.class);
    MetaAnnotationListenerTestBean bean = this.context.getBean(MetaAnnotationListenerTestBean.class);
    this.eventCollector.assertNoEventReceived(bean);
    TestEvent event = new TestEvent();
    this.context.publishEvent(event);
    this.eventCollector.assertEvent(bean, event);
    this.eventCollector.assertTotalEventsCount(1);
}
Also used : TestEvent(org.springframework.context.event.test.TestEvent) AnotherTestEvent(org.springframework.context.event.test.AnotherTestEvent) Test(org.junit.Test)

Example 8 with TestEvent

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

the class AnnotationDrivenEventListenerTests method conditionDoesNotMatch.

@Test
public void conditionDoesNotMatch() {
    long maxLong = Long.MAX_VALUE;
    load(ConditionalEventListener.class);
    TestEvent event = new TestEvent(this, "KO");
    TestEventListener listener = this.context.getBean(ConditionalEventListener.class);
    this.eventCollector.assertNoEventReceived(listener);
    this.context.publishEvent(event);
    this.eventCollector.assertNoEventReceived(listener);
    this.eventCollector.assertTotalEventsCount(0);
    this.context.publishEvent("KO");
    this.eventCollector.assertNoEventReceived(listener);
    this.eventCollector.assertTotalEventsCount(0);
    this.context.publishEvent(maxLong);
    this.eventCollector.assertNoEventReceived(listener);
    this.eventCollector.assertTotalEventsCount(0);
    this.context.publishEvent(24d);
    this.eventCollector.assertNoEventReceived(listener);
    this.eventCollector.assertTotalEventsCount(0);
}
Also used : TestEvent(org.springframework.context.event.test.TestEvent) AnotherTestEvent(org.springframework.context.event.test.AnotherTestEvent) Test(org.junit.Test)

Example 9 with TestEvent

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

the class AnnotationDrivenEventListenerTests method eventListenerWorksWithSimpleInterfaceProxy.

@Test
public void eventListenerWorksWithSimpleInterfaceProxy() throws Exception {
    load(ScopedProxyTestBean.class);
    SimpleService proxy = this.context.getBean(SimpleService.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 10 with TestEvent

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

the class AnnotationDrivenEventListenerTests method conditionMatch.

@Test
public void conditionMatch() {
    long timestamp = System.currentTimeMillis();
    load(ConditionalEventListener.class);
    TestEvent event = new TestEvent(this, "OK");
    TestEventListener listener = this.context.getBean(ConditionalEventListener.class);
    this.eventCollector.assertNoEventReceived(listener);
    this.context.publishEvent(event);
    this.eventCollector.assertEvent(listener, event);
    this.eventCollector.assertTotalEventsCount(1);
    this.context.publishEvent("OK");
    this.eventCollector.assertEvent(listener, event, "OK");
    this.eventCollector.assertTotalEventsCount(2);
    this.context.publishEvent(timestamp);
    this.eventCollector.assertEvent(listener, event, "OK", timestamp);
    this.eventCollector.assertTotalEventsCount(3);
    this.context.publishEvent(42d);
    this.eventCollector.assertEvent(listener, event, "OK", timestamp, 42d);
    this.eventCollector.assertTotalEventsCount(4);
}
Also used : 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