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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations