summaryrefslogtreecommitdiff
path: root/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java
diff options
context:
space:
mode:
Diffstat (limited to 'spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java')
-rw-r--r--spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java100
1 files changed, 79 insertions, 21 deletions
diff --git a/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java b/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java
index 3e52c58b..89738660 100644
--- a/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java
+++ b/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java
@@ -54,6 +54,7 @@ import org.springframework.context.event.test.GenericEventPojo;
import org.springframework.context.event.test.Identifiable;
import org.springframework.context.event.test.TestEvent;
import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
@@ -116,7 +117,7 @@ public class AnnotationDrivenEventListenerTests {
public void metaAnnotationIsDiscovered() {
load(MetaAnnotationListenerTestBean.class);
- MetaAnnotationListenerTestBean bean = context.getBean(MetaAnnotationListenerTestBean.class);
+ MetaAnnotationListenerTestBean bean = this.context.getBean(MetaAnnotationListenerTestBean.class);
this.eventCollector.assertNoEventReceived(bean);
TestEvent event = new TestEvent();
@@ -148,9 +149,9 @@ public class AnnotationDrivenEventListenerTests {
failingContext.register(BasicConfiguration.class,
InvalidMethodSignatureEventListener.class);
- thrown.expect(BeanInitializationException.class);
- thrown.expectMessage(InvalidMethodSignatureEventListener.class.getName());
- thrown.expectMessage("cannotBeCalled");
+ this.thrown.expect(BeanInitializationException.class);
+ this.thrown.expectMessage(InvalidMethodSignatureEventListener.class.getName());
+ this.thrown.expectMessage("cannotBeCalled");
failingContext.refresh();
}
@@ -286,6 +287,17 @@ public class AnnotationDrivenEventListenerTests {
}
@Test
+ public void privateMethodOnCglibProxyFails() throws Exception {
+ try {
+ load(CglibProxyWithPrivateMethod.class);
+ fail("Should have thrown BeanInitializationException");
+ }
+ catch (BeanInitializationException ex) {
+ assertTrue(ex.getCause() instanceof IllegalStateException);
+ }
+ }
+
+ @Test
public void eventListenerWorksWithCustomScope() throws Exception {
load(CustomScopeTestBean.class);
CustomScope customScope = new CustomScope();
@@ -329,7 +341,7 @@ public class AnnotationDrivenEventListenerTests {
this.eventCollector.assertNoEventReceived(listener);
this.context.publishEvent(event);
- countDownLatch.await(2, TimeUnit.SECONDS);
+ this.countDownLatch.await(2, TimeUnit.SECONDS);
this.eventCollector.assertEvent(listener, event);
this.eventCollector.assertTotalEventsCount(1);
}
@@ -344,7 +356,7 @@ public class AnnotationDrivenEventListenerTests {
this.eventCollector.assertNoEventReceived(listener);
this.context.publishEvent(event);
- countDownLatch.await(2, TimeUnit.SECONDS);
+ this.countDownLatch.await(2, TimeUnit.SECONDS);
this.eventCollector.assertEvent(listener, event);
this.eventCollector.assertTotalEventsCount(1);
}
@@ -359,7 +371,7 @@ public class AnnotationDrivenEventListenerTests {
this.eventCollector.assertNoEventReceived(listener);
this.context.publishEvent(event);
- countDownLatch.await(2, TimeUnit.SECONDS);
+ this.countDownLatch.await(2, TimeUnit.SECONDS);
this.eventCollector.assertEvent(listener, event);
this.eventCollector.assertTotalEventsCount(1);
}
@@ -389,7 +401,7 @@ public class AnnotationDrivenEventListenerTests {
this.eventCollector.assertNoEventReceived(listener);
this.context.publishEvent(event);
- countDownLatch.await(2, TimeUnit.SECONDS);
+ this.countDownLatch.await(2, TimeUnit.SECONDS);
this.eventCollector.assertEvent(listener, event);
this.eventCollector.assertTotalEventsCount(1);
@@ -487,6 +499,10 @@ public class AnnotationDrivenEventListenerTests {
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);
}
@Test
@@ -508,6 +524,10 @@ public class AnnotationDrivenEventListenerTests {
this.context.publishEvent(maxLong);
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertTotalEventsCount(0);
+
+ this.context.publishEvent(24d);
+ this.eventCollector.assertNoEventReceived(listener);
+ this.eventCollector.assertTotalEventsCount(0);
}
@Test
@@ -559,6 +579,18 @@ public class AnnotationDrivenEventListenerTests {
public CountDownLatch testCountDownLatch() {
return new CountDownLatch(1);
}
+
+ @Bean
+ public TestConditionEvaluator conditionEvaluator() {
+ return new TestConditionEvaluator();
+ }
+
+ static class TestConditionEvaluator {
+
+ public boolean valid(Double ratio) {
+ return new Double(42).equals(ratio);
+ }
+ }
}
@@ -667,7 +699,7 @@ public class AnnotationDrivenEventListenerTests {
public void handleAsync(AnotherTestEvent event) {
collectEvent(event);
if ("fail".equals(event.content)) {
- countDownLatch.countDown();
+ this.countDownLatch.countDown();
throw new IllegalStateException("Test exception");
}
}
@@ -685,7 +717,7 @@ public class AnnotationDrivenEventListenerTests {
public void handleAsync(AnotherTestEvent event) {
assertTrue(!Thread.currentThread().getName().equals(event.content));
collectEvent(event);
- countDownLatch.countDown();
+ this.countDownLatch.countDown();
}
}
@@ -724,15 +756,15 @@ public class AnnotationDrivenEventListenerTests {
@EventListener
@Override
public void handleIt(TestEvent event) {
- eventCollector.addEvent(this, event);
+ this.eventCollector.addEvent(this, event);
}
@EventListener
@Async
public void handleAsync(AnotherTestEvent event) {
assertTrue(!Thread.currentThread().getName().equals(event.content));
- eventCollector.addEvent(this, event);
- countDownLatch.countDown();
+ this.eventCollector.addEvent(this, event);
+ this.countDownLatch.countDown();
}
}
@@ -750,15 +782,15 @@ public class AnnotationDrivenEventListenerTests {
@EventListener
@Override
public void handleIt(TestEvent event) {
- eventCollector.addEvent(this, event);
+ this.eventCollector.addEvent(this, event);
}
@EventListener
@Async
public void handleAsync(AnotherTestEvent event) {
assertTrue(!Thread.currentThread().getName().equals(event.content));
- eventCollector.addEvent(this, event);
- countDownLatch.countDown();
+ this.eventCollector.addEvent(this, event);
+ this.countDownLatch.countDown();
}
}
@@ -779,7 +811,7 @@ public class AnnotationDrivenEventListenerTests {
@Override
public void handleIt(TestEvent event) {
- eventCollector.addEvent(this, event);
+ this.eventCollector.addEvent(this, event);
}
}
@@ -796,6 +828,17 @@ public class AnnotationDrivenEventListenerTests {
@Component
+ @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
+ static class CglibProxyWithPrivateMethod extends AbstractTestEventListener {
+
+ @EventListener
+ private void handleIt(TestEvent event) {
+ collectEvent(event);
+ }
+ }
+
+
+ @Component
@Scope(scopeName = "custom", proxyMode = ScopedProxyMode.TARGET_CLASS)
static class CustomScopeTestBean extends AbstractTestEventListener {
@@ -826,6 +869,16 @@ public class AnnotationDrivenEventListenerTests {
}
+
+ @EventListener
+ @Retention(RetentionPolicy.RUNTIME)
+ public @interface ConditionalEvent {
+
+ @AliasFor(annotation = EventListener.class, attribute = "condition")
+ String value();
+ }
+
+
@Component
static class ConditionalEventListener extends TestEventListener {
@@ -841,10 +894,15 @@ public class AnnotationDrivenEventListenerTests {
super.handleString(payload);
}
- @EventListener(condition = "#root.event.timestamp > #p0")
+ @ConditionalEvent("#root.event.timestamp > #p0")
public void handleTimestamp(Long timestamp) {
collectEvent(timestamp);
}
+
+ @ConditionalEvent("@conditionEvaluator.valid(#p0)")
+ public void handleRatio(Double ratio) {
+ collectEvent(ratio);
+ }
}
@@ -856,18 +914,18 @@ public class AnnotationDrivenEventListenerTests {
@EventListener
@Order(50)
public void handleThird(String payload) {
- order.add("third");
+ this.order.add("third");
}
@EventListener
@Order(-50)
public void handleFirst(String payload) {
- order.add("first");
+ this.order.add("first");
}
@EventListener
public void handleSecond(String payload) {
- order.add("second");
+ this.order.add("second");
}
}