Support for reactive result values from event listener methods

Closes gh-21831
This commit is contained in:
Juergen Hoeller
2019-07-05 16:19:23 +02:00
parent 97d020c509
commit 7bfe01a028
6 changed files with 169 additions and 31 deletions

View File

@@ -25,6 +25,7 @@ import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
@@ -32,6 +33,8 @@ import javax.annotation.PostConstruct;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
@@ -61,6 +64,7 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.concurrent.SettableListenableFuture;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
@@ -243,7 +247,69 @@ public class AnnotationDrivenEventListenerTests {
}
@Test
public void eventListenerWorksWithSimpleInterfaceProxy() throws Exception {
public void listenableFutureReply() {
load(TestEventListener.class, ReplyEventListener.class);
SettableListenableFuture<String> future = new SettableListenableFuture<>();
future.set("dummy");
AnotherTestEvent event = new AnotherTestEvent(this, future);
ReplyEventListener replyEventListener = this.context.getBean(ReplyEventListener.class);
TestEventListener listener = this.context.getBean(TestEventListener.class);
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertNoEventReceived(replyEventListener);
this.context.publishEvent(event);
this.eventCollector.assertEvent(replyEventListener, event);
this.eventCollector.assertEvent(listener, "dummy"); // reply
this.eventCollector.assertTotalEventsCount(2);
}
@Test
public void completableFutureReply() {
load(TestEventListener.class, ReplyEventListener.class);
AnotherTestEvent event = new AnotherTestEvent(this, CompletableFuture.completedFuture("dummy"));
ReplyEventListener replyEventListener = this.context.getBean(ReplyEventListener.class);
TestEventListener listener = this.context.getBean(TestEventListener.class);
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertNoEventReceived(replyEventListener);
this.context.publishEvent(event);
this.eventCollector.assertEvent(replyEventListener, event);
this.eventCollector.assertEvent(listener, "dummy"); // reply
this.eventCollector.assertTotalEventsCount(2);
}
@Test
public void monoReply() {
load(TestEventListener.class, ReplyEventListener.class);
AnotherTestEvent event = new AnotherTestEvent(this, Mono.just("dummy"));
ReplyEventListener replyEventListener = this.context.getBean(ReplyEventListener.class);
TestEventListener listener = this.context.getBean(TestEventListener.class);
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertNoEventReceived(replyEventListener);
this.context.publishEvent(event);
this.eventCollector.assertEvent(replyEventListener, event);
this.eventCollector.assertEvent(listener, "dummy"); // reply
this.eventCollector.assertTotalEventsCount(2);
}
@Test
public void fluxReply() {
load(TestEventListener.class, ReplyEventListener.class);
AnotherTestEvent event = new AnotherTestEvent(this, Flux.just("dummy1", "dummy2"));
ReplyEventListener replyEventListener = this.context.getBean(ReplyEventListener.class);
TestEventListener listener = this.context.getBean(TestEventListener.class);
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertNoEventReceived(replyEventListener);
this.context.publishEvent(event);
this.eventCollector.assertEvent(replyEventListener, event);
this.eventCollector.assertEvent(listener, "dummy1", "dummy2"); // reply
this.eventCollector.assertTotalEventsCount(3);
}
@Test
public void eventListenerWorksWithSimpleInterfaceProxy() {
load(ScopedProxyTestBean.class);
SimpleService proxy = this.context.getBean(SimpleService.class);
@@ -260,7 +326,7 @@ public class AnnotationDrivenEventListenerTests {
}
@Test
public void eventListenerWorksWithAnnotatedInterfaceProxy() throws Exception {
public void eventListenerWorksWithAnnotatedInterfaceProxy() {
load(AnnotatedProxyTestBean.class);
AnnotatedSimpleService proxy = this.context.getBean(AnnotatedSimpleService.class);
@@ -277,7 +343,7 @@ public class AnnotationDrivenEventListenerTests {
}
@Test
public void eventListenerWorksWithCglibProxy() throws Exception {
public void eventListenerWorksWithCglibProxy() {
load(CglibProxyTestBean.class);
CglibProxyTestBean proxy = this.context.getBean(CglibProxyTestBean.class);
@@ -294,14 +360,14 @@ public class AnnotationDrivenEventListenerTests {
}
@Test
public void privateMethodOnCglibProxyFails() throws Exception {
public void privateMethodOnCglibProxyFails() {
assertThatExceptionOfType(BeanInitializationException.class).isThrownBy(() ->
load(CglibProxyWithPrivateMethod.class))
.withCauseInstanceOf(IllegalStateException.class);
}
@Test
public void eventListenerWorksWithCustomScope() throws Exception {
public void eventListenerWorksWithCustomScope() {
load(CustomScopeTestBean.class);
CustomScope customScope = new CustomScope();
this.context.getBeanFactory().registerScope("custom", customScope);

View File

@@ -99,8 +99,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
@Test
public void listenerWithSubTypeSeveralGenerics() {
Method method = ReflectionUtils.findMethod(SampleEvents.class,
"handleString", String.class);
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
supportsEventType(true, method, ResolvableType.forClass(PayloadTestEvent.class));
}
@@ -141,23 +140,20 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
public void listenerWithTooManyParameters() {
Method method = ReflectionUtils.findMethod(
SampleEvents.class, "tooManyParameters", String.class, String.class);
assertThatIllegalStateException().isThrownBy(() ->
createTestInstance(method));
assertThatIllegalStateException().isThrownBy(() -> createTestInstance(method));
}
@Test
public void listenerWithNoParameter() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, "noParameter");
assertThatIllegalStateException().isThrownBy(() ->
createTestInstance(method));
assertThatIllegalStateException().isThrownBy(() -> createTestInstance(method));
}
@Test
public void listenerWithMoreThanOneParameter() {
Method method = ReflectionUtils.findMethod(
SampleEvents.class, "moreThanOneParameter", String.class, Integer.class);
assertThatIllegalStateException().isThrownBy(() ->
createTestInstance(method));
assertThatIllegalStateException().isThrownBy(() -> createTestInstance(method));
}
@Test
@@ -331,7 +327,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
private void supportsEventType(boolean match, Method method, ResolvableType eventType) {
ApplicationListenerMethodAdapter adapter = createTestInstance(method);
assertThat(adapter.supportsEventType(eventType)).as("Wrong match for event '" + eventType + "' on " + method).isEqualTo(match);
assertThat(adapter.supportsEventType(eventType))
.as("Wrong match for event '" + eventType + "' on " + method).isEqualTo(match);
}
private void invokeListener(Method method, ApplicationEvent event) {

View File

@@ -16,7 +16,6 @@
package org.springframework.context.event;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
@@ -42,16 +41,11 @@ import static org.mockito.Mockito.mock;
*/
public class EventPublicationInterceptorTests {
private ApplicationEventPublisher publisher;
private final ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
@Before
public void setUp() {
this.publisher = mock(ApplicationEventPublisher.class);
}
@Test
public void testWithNoApplicationEventClassSupplied() throws Exception {
public void testWithNoApplicationEventClassSupplied() {
EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
interceptor.setApplicationEventPublisher(this.publisher);
assertThatIllegalArgumentException().isThrownBy(
@@ -59,7 +53,7 @@ public class EventPublicationInterceptorTests {
}
@Test
public void testWithNonApplicationEventClassSupplied() throws Exception {
public void testWithNonApplicationEventClassSupplied() {
EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
interceptor.setApplicationEventPublisher(this.publisher);
assertThatIllegalArgumentException().isThrownBy(() -> {
@@ -69,7 +63,7 @@ public class EventPublicationInterceptorTests {
}
@Test
public void testWithAbstractStraightApplicationEventClassSupplied() throws Exception {
public void testWithAbstractStraightApplicationEventClassSupplied() {
EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
interceptor.setApplicationEventPublisher(this.publisher);
assertThatIllegalArgumentException().isThrownBy(() -> {
@@ -79,7 +73,7 @@ public class EventPublicationInterceptorTests {
}
@Test
public void testWithApplicationEventClassThatDoesntExposeAValidCtor() throws Exception {
public void testWithApplicationEventClassThatDoesntExposeAValidCtor() {
EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
interceptor.setApplicationEventPublisher(this.publisher);
assertThatIllegalArgumentException().isThrownBy(() -> {
@@ -89,7 +83,7 @@ public class EventPublicationInterceptorTests {
}
@Test
public void testExpectedBehavior() throws Exception {
public void testExpectedBehavior() {
TestBean target = new TestBean();
final TestListener listener = new TestListener();