Skip to content

Commit c13e40d

Browse files
committed
Miscellaneous tests and Sonar fixes
* Fix complexity in the `DefaultJmsHeaderMapper` according Sonar report * Optimize all JMS tests to rely on a shared `ActiveMQConnectionFactory` resource and disable JMX & statistics for embedded ActiveMQ broker * Increase timeout for some sporadically failing tests
1 parent c811da6 commit c13e40d

File tree

9 files changed

+248
-196
lines changed

9 files changed

+248
-196
lines changed

spring-integration-core/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

22-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.integration.channel.QueueChannel;
2525
import org.springframework.integration.support.MessageBuilder;
@@ -47,8 +47,9 @@ public void simpleBridge() {
4747
Message<?> request = new GenericMessage<>("test");
4848
this.handler.handleMessage(request);
4949
Message<?> reply = outputChannel.receive(0);
50-
assertThat(reply).isNotNull();
51-
assertThat(reply).matches(new MessagePredicate(request));
50+
assertThat(reply)
51+
.isNotNull()
52+
.matches(new MessagePredicate(request));
5253
}
5354

5455
@Test
@@ -60,12 +61,14 @@ public void missingOutputChannelVerifiedAtRuntime() {
6061
.withCauseInstanceOf(DestinationResolutionException.class);
6162
}
6263

63-
@Test(timeout = 1000)
64+
@Test
6465
public void missingOutputChannelAllowedForReplyChannelMessages() {
6566
PollableChannel replyChannel = new QueueChannel();
6667
Message<String> request = MessageBuilder.withPayload("tst").setReplyChannel(replyChannel).build();
6768
this.handler.handleMessage(request);
68-
assertThat(replyChannel.receive()).matches(new MessagePredicate(request));
69+
assertThat(replyChannel.receive(10_000))
70+
.isNotNull()
71+
.matches(new MessagePredicate(request));
6972
}
7073

7174
}

spring-integration-core/src/test/java/org/springframework/integration/handler/MethodInvokingMessageProcessorAnnotationTests.java

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.handler;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2021
import static org.mockito.Mockito.mock;
2122

2223
import java.lang.reflect.Method;
@@ -32,7 +33,7 @@
3233

3334
import org.apache.commons.logging.Log;
3435
import org.apache.commons.logging.LogFactory;
35-
import org.junit.Test;
36+
import org.junit.jupiter.api.Test;
3637

3738
import org.springframework.beans.factory.BeanFactory;
3839
import org.springframework.integration.support.MessageBuilder;
@@ -70,16 +71,10 @@ public void multiThreadsUUIDToStringConversion() throws Exception {
7071
ExecutorService exec = Executors.newFixedThreadPool(100);
7172
processor.processMessage(new GenericMessage<>("foo"));
7273
for (int i = 0; i < 100; i++) {
73-
exec.execute(new Runnable() {
74-
75-
public void run() {
76-
Object result = processor.processMessage(new GenericMessage<>("foo"));
77-
assertThat(result).isNotNull();
78-
}
79-
});
74+
exec.execute(() -> assertThat(processor.processMessage(new GenericMessage<>("foo"))).isNotNull());
8075
}
8176
exec.shutdown();
82-
assertThat(exec.awaitTermination(10, TimeUnit.SECONDS)).isTrue();
77+
assertThat(exec.awaitTermination(20, TimeUnit.SECONDS)).isTrue();
8378
assertThat(concurrencyFailures).isEqualTo(0);
8479
}
8580

@@ -92,15 +87,16 @@ public void optionalHeader() throws Exception {
9287
assertThat(result).isNull();
9388
}
9489

95-
@Test(expected = MessageHandlingException.class)
90+
@Test
9691
public void requiredHeaderNotProvided() throws Exception {
9792
Method method = TestService.class.getMethod("requiredHeader", Integer.class);
9893
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(testService, method);
9994
processor.setBeanFactory(mock(BeanFactory.class));
100-
processor.processMessage(new GenericMessage<>("foo"));
95+
assertThatExceptionOfType(MessageHandlingException.class)
96+
.isThrownBy(() -> processor.processMessage(new GenericMessage<>("foo")));
10197
}
10298

103-
@Test(expected = MessageHandlingException.class)
99+
@Test
104100
public void requiredHeaderNotProvidedOnSecondMessage() throws Exception {
105101
Method method = TestService.class.getMethod("requiredHeader", Integer.class);
106102
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(testService, method);
@@ -110,7 +106,8 @@ public void requiredHeaderNotProvidedOnSecondMessage() throws Exception {
110106
GenericMessage<String> messageWithoutHeader = new GenericMessage<>("foo");
111107

112108
processor.processMessage(messageWithHeader);
113-
processor.processMessage(messageWithoutHeader);
109+
assertThatExceptionOfType(MessageHandlingException.class)
110+
.isThrownBy(() -> processor.processMessage(messageWithoutHeader));
114111
}
115112

116113
@Test
@@ -124,14 +121,16 @@ public void fromMessageWithRequiredHeaderProvided() throws Exception {
124121
assertThat(result).isEqualTo(123);
125122
}
126123

127-
@Test(expected = MessageHandlingException.class)
124+
@Test
128125
public void fromMessageWithOptionalAndRequiredHeaderAndOnlyOptionalHeaderProvided() throws Exception {
129126
Method method = TestService.class.getMethod("optionalAndRequiredHeader", String.class, Integer.class);
130127
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(testService, method);
131128
processor.setBeanFactory(mock(BeanFactory.class));
132129
Message<String> message = MessageBuilder.withPayload("foo")
133130
.setHeader("prop", "bar").build();
134-
processor.processMessage(message);
131+
132+
assertThatExceptionOfType(MessageHandlingException.class)
133+
.isThrownBy(() -> processor.processMessage(message));
135134
}
136135

137136
@Test
@@ -342,13 +341,11 @@ public void fromMessageToPayloadArgsHeaderArgs() throws Exception {
342341
assertThat(result).isEqualTo("olegmonday");
343342
}
344343

345-
@Test(expected = MessagingException.class)
344+
@Test
346345
public void fromMessageInvalidMethodWithMultipleMappingAnnotations() throws Exception {
347346
Method method = MultipleMappingAnnotationTestBean.class.getMethod("test", String.class);
348-
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(testService, method);
349-
processor.setBeanFactory(mock(BeanFactory.class));
350-
Message<?> message = MessageBuilder.withPayload("payload").setHeader("foo", "bar").build();
351-
processor.processMessage(message);
347+
assertThatExceptionOfType(MessagingException.class)
348+
.isThrownBy(() -> new MethodInvokingMessageProcessor(testService, method));
352349
}
353350

354351
@Test
@@ -468,7 +465,7 @@ public Object[] multipleAnnotatedArguments(@Header("day") String argA,
468465
@Payload Employee payloadArg,
469466
@Payload("fname") String value,
470467
@Headers Map<?, ?> headers) {
471-
return new Object[] { argA, argB, payloadArg, value, headers };
468+
return new Object[]{ argA, argB, payloadArg, value, headers };
472469
}
473470

474471
public String irrelevantAnnotation(@BogusAnnotation String value) {
@@ -479,7 +476,7 @@ public String headerNameWithHyphen(@Header("foo-bar") String foobar) {
479476
return foobar.toUpperCase();
480477
}
481478

482-
Set<String> ids = Collections.synchronizedSet(new HashSet<String>());
479+
Set<String> ids = Collections.synchronizedSet(new HashSet<>());
483480

484481
public String headerId(String payload, @Header("id") String id) {
485482
logger.debug(id);

0 commit comments

Comments
 (0)