Migrate event module tests to JUnit 5
* Use `@RecordApplicationEvents` feature from SF
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,8 +22,7 @@ import java.util.Date;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -41,7 +40,7 @@ import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
/**
|
||||
@@ -53,7 +52,7 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringJUnitConfig
|
||||
@ContextConfiguration
|
||||
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
|
||||
public class EventInboundChannelAdapterParserTests {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,21 +18,22 @@ package org.springframework.integration.event.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.PayloadApplicationEvent;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.event.core.MessagingEvent;
|
||||
import org.springframework.integration.event.outbound.ApplicationEventPublishingMessageHandler;
|
||||
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
@@ -40,26 +41,36 @@ import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.event.ApplicationEvents;
|
||||
import org.springframework.test.context.event.RecordApplicationEvents;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Artem Bilan
|
||||
* @author Gary Russell
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringJUnitConfig
|
||||
@RecordApplicationEvents
|
||||
@ContextConfiguration
|
||||
public class EventOutboundChannelAdapterParserTests {
|
||||
|
||||
@Autowired
|
||||
private volatile ConfigurableApplicationContext context;
|
||||
|
||||
private volatile boolean receivedEvent;
|
||||
@Autowired
|
||||
private ApplicationEvents applicationEvents;
|
||||
|
||||
private static volatile int adviceCalled;
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
this.applicationEvents.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateEventParser() {
|
||||
EventDrivenConsumer adapter = this.context.getBean("eventAdapter", EventDrivenConsumer.class);
|
||||
@@ -73,88 +84,54 @@ public class EventOutboundChannelAdapterParserTests {
|
||||
|
||||
@Test
|
||||
public void validateUsage() {
|
||||
ApplicationListener<?> listener = event -> {
|
||||
if (event instanceof PayloadApplicationEvent) {
|
||||
String payload = (String) ((PayloadApplicationEvent<?>) event).getPayload();
|
||||
if (payload.equals("hello")) {
|
||||
receivedEvent = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
this.context.addApplicationListener(listener);
|
||||
DirectChannel channel = context.getBean("input", DirectChannel.class);
|
||||
channel.send(new GenericMessage<String>("hello"));
|
||||
assertThat(this.receivedEvent).isTrue();
|
||||
channel.send(new GenericMessage<>("hello"));
|
||||
assertThat(this.applicationEvents.stream(String.class)).containsOnly("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withAdvice() {
|
||||
this.receivedEvent = false;
|
||||
ApplicationListener<?> listener = event -> {
|
||||
Object source = event.getSource();
|
||||
if (source instanceof Message) {
|
||||
String payload = (String) ((Message<?>) source).getPayload();
|
||||
if (payload.equals("hello")) {
|
||||
receivedEvent = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
context.addApplicationListener(listener);
|
||||
DirectChannel channel = context.getBean("inputAdvice", DirectChannel.class);
|
||||
channel.send(new GenericMessage<String>("hello"));
|
||||
assertThat(this.receivedEvent).isTrue();
|
||||
channel.send(new GenericMessage<>("hello"));
|
||||
assertThat(this.applicationEvents.stream(MessagingEvent.class))
|
||||
.hasSize(1)
|
||||
.satisfiesExactly(event -> assertThat(event.getMessage().getPayload()).isEqualTo("hello"));
|
||||
assertThat(adviceCalled).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test //INT-2275
|
||||
@Test
|
||||
public void testInsideChain() {
|
||||
this.receivedEvent = false;
|
||||
ApplicationListener<?> listener = event -> {
|
||||
Object source = event.getSource();
|
||||
if (source instanceof Message) {
|
||||
String payload = (String) ((Message<?>) source).getPayload();
|
||||
if (payload.equals("foobar")) {
|
||||
receivedEvent = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
this.context.addApplicationListener(listener);
|
||||
DirectChannel channel = context.getBean("inputChain", DirectChannel.class);
|
||||
channel.send(new GenericMessage<String>("foo"));
|
||||
assertThat(this.receivedEvent).isTrue();
|
||||
channel.send(new GenericMessage<>("foo"));
|
||||
assertThat(this.applicationEvents.stream(MessagingEvent.class))
|
||||
.hasSize(1)
|
||||
.satisfiesExactly(event -> assertThat(event.getMessage().getPayload()).isEqualTo("foobar"));
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
public void validateUsageWithPollableChannel() throws Exception {
|
||||
this.receivedEvent = false;
|
||||
@Test
|
||||
@Timeout(10000)
|
||||
public void validateUsageWithPollableChannel() throws InterruptedException {
|
||||
ConfigurableApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("EventOutboundChannelAdapterParserTestsWithPollable-context.xml",
|
||||
EventOutboundChannelAdapterParserTests.class);
|
||||
final CyclicBarrier barrier = new CyclicBarrier(2);
|
||||
@SuppressWarnings("resource")
|
||||
|
||||
CountDownLatch eventLatch = new CountDownLatch(1);
|
||||
|
||||
ApplicationListener<?> listener = event -> {
|
||||
Object source = event.getSource();
|
||||
if (source instanceof Message) {
|
||||
String payload = (String) ((Message<?>) source).getPayload();
|
||||
if (payload.equals("hello")) {
|
||||
receivedEvent = true;
|
||||
try {
|
||||
barrier.await();
|
||||
}
|
||||
catch (InterruptedException e1) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
catch (BrokenBarrierException e2) {
|
||||
throw new IllegalStateException("broken barrier", e2);
|
||||
}
|
||||
eventLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
context.addApplicationListener(listener);
|
||||
QueueChannel channel = context.getBean("input", QueueChannel.class);
|
||||
channel.send(new GenericMessage<String>("hello"));
|
||||
barrier.await();
|
||||
assertThat(this.receivedEvent).isTrue();
|
||||
channel.send(new GenericMessage<>("hello"));
|
||||
|
||||
assertThat(eventLatch.await(10, TimeUnit.SECONDS)).isTrue();
|
||||
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.integration.event.dsl;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -46,6 +44,8 @@ import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.event.ApplicationEvents;
|
||||
import org.springframework.test.context.event.RecordApplicationEvents;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
/**
|
||||
@@ -54,6 +54,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
* @since 5.0
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@RecordApplicationEvents
|
||||
@DirtiesContext
|
||||
public class IntegrationFlowEventsTests {
|
||||
|
||||
@@ -81,14 +82,15 @@ public class IntegrationFlowEventsTests {
|
||||
private MessageChannel flow3Input;
|
||||
|
||||
@Autowired
|
||||
private AtomicReference<Object> eventHolder;
|
||||
private ApplicationEvents applicationEvents;
|
||||
|
||||
@Test
|
||||
public void testEventsFlow() {
|
||||
assertThat(this.eventHolder.get()).isNull();
|
||||
assertThat(this.applicationEvents.stream(MessagingEvent.class)).isEmpty();
|
||||
this.flow3Input.send(new GenericMessage<>("2"));
|
||||
assertThat(this.eventHolder.get()).isNotNull();
|
||||
assertThat(this.eventHolder.get()).isEqualTo(4);
|
||||
assertThat(this.applicationEvents.stream(MessagingEvent.class))
|
||||
.hasSize(1)
|
||||
.satisfiesExactly(event -> assertThat(event.getMessage().getPayload()).isEqualTo(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,16 +120,6 @@ public class IntegrationFlowEventsTests {
|
||||
@EnableIntegration
|
||||
public static class ContextConfiguration {
|
||||
|
||||
@Bean
|
||||
public AtomicReference<Object> eventHolder() {
|
||||
return new AtomicReference<>();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicationListener<MessagingEvent> eventListener() {
|
||||
return event -> eventHolder().set(event.getMessage().getPayload());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IntegrationFlow flow3() {
|
||||
return IntegrationFlow.from("flow3Input")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,12 +17,13 @@
|
||||
package org.springframework.integration.event.inbound;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
@@ -193,7 +194,7 @@ public class ApplicationEventListeningMessageProducerTests {
|
||||
assertThat(message2.getPayload()).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test(expected = MessageHandlingException.class)
|
||||
@Test
|
||||
public void anyApplicationEventCausesExceptionWithErrorHandling() {
|
||||
DirectChannel channel = new DirectChannel();
|
||||
channel.subscribe(new AbstractReplyProducingMessageHandler() {
|
||||
@@ -213,11 +214,12 @@ public class ApplicationEventListeningMessageProducerTests {
|
||||
assertThat(message).isNotNull();
|
||||
assertThat(((Exception) message.getPayload()).getCause().getMessage()).isEqualTo("Failed");
|
||||
adapter.setErrorChannel(null);
|
||||
adapter.onApplicationEvent(new TestApplicationEvent1());
|
||||
assertThatExceptionOfType(MessageHandlingException.class)
|
||||
.isThrownBy(() -> adapter.onApplicationEvent(new TestApplicationEvent1()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "serial" })
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testInt2935CheckRetrieverCache() {
|
||||
GenericApplicationContext ctx = TestUtils.createTestApplicationContext();
|
||||
ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,7 +18,7 @@ package org.springframework.integration.event.outbound;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
@@ -28,16 +28,17 @@ import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class ApplicationEventPublishingMessageHandlerTests {
|
||||
|
||||
@Test
|
||||
public void messagingEvent() throws InterruptedException {
|
||||
public void messagingEvent() {
|
||||
TestApplicationEventPublisher publisher = new TestApplicationEventPublisher();
|
||||
ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler();
|
||||
handler.setApplicationEventPublisher(publisher);
|
||||
assertThat(publisher.getLastEvent()).isNull();
|
||||
Message<?> message = new GenericMessage<String>("testing");
|
||||
Message<?> message = new GenericMessage<>("testing");
|
||||
handler.handleMessage(message);
|
||||
ApplicationEvent event = publisher.getLastEvent();
|
||||
assertThat(event.getClass()).isEqualTo(MessagingEvent.class);
|
||||
@@ -50,11 +51,11 @@ public class ApplicationEventPublishingMessageHandlerTests {
|
||||
ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler();
|
||||
handler.setApplicationEventPublisher(publisher);
|
||||
assertThat(publisher.getLastEvent()).isNull();
|
||||
Message<?> message = new GenericMessage<TestEvent>(new TestEvent("foo"));
|
||||
Message<?> message = new GenericMessage<>(new TestEvent("foo"));
|
||||
handler.handleMessage(message);
|
||||
ApplicationEvent event = publisher.getLastEvent();
|
||||
assertThat(event.getClass()).isEqualTo(TestEvent.class);
|
||||
assertThat(((TestEvent) event).getSource()).isEqualTo("foo");
|
||||
assertThat((event).getSource()).isEqualTo("foo");
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +86,7 @@ public class ApplicationEventPublishingMessageHandlerTests {
|
||||
TestEvent(String text) {
|
||||
super(text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="org.springframework.integration" level="warn"/>
|
||||
<Logger name="org.springframework.integration.event" level="info"/>
|
||||
<Logger name="org.springframework.integration.event" level="warn"/>
|
||||
<Root level="warn">
|
||||
<AppenderRef ref="STDOUT" />
|
||||
</Root>
|
||||
|
||||
Reference in New Issue
Block a user