Skip to content

Commit c661d79

Browse files
artembilangaryrussell
authored andcommitted
Remove SocketUtils usage
* Remove usage of non-stable `org.springframework.util.SocketUtils` * Replace it with `0` for those tests where it is possible to select OS port * Remove the mentioning of the `SocketUtils` from the `testing.adoc` * Use `TransportConstants.DEFAULT_STOMP_PORT` for `StompServerIntegrationTests`. We may disable this test in the future for CI if it is not going to be stable * Introduce `Supplier<String> connectUrl` variants for `ZeroMqMessageHandler` to let it defer connection evaluation until subscription to the socket `Mono` in the `ZeroMqMessageHandler`. * Move connection logic in the `ZeroMqMessageHandler` to `Lifecycle.start()` Related to spring-projects/spring-framework#28054 **Cherry-pick to `5.4.x`**
1 parent 9ac0b65 commit c661d79

File tree

12 files changed

+188
-148
lines changed

12 files changed

+188
-148
lines changed

spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/MulticastRule.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2020 the original author or authors.
2+
* Copyright 2015-2022 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.
@@ -29,7 +29,6 @@
2929
import org.springframework.integration.ip.util.SocketTestUtils;
3030
import org.springframework.lang.Nullable;
3131
import org.springframework.util.Assert;
32-
import org.springframework.util.SocketUtils;
3332

3433
/**
3534
* @author Artem Bilan
@@ -76,7 +75,7 @@ private NetworkInterface checkMulticast() throws Exception {
7675
}
7776
try {
7877
MulticastSocket socket = new MulticastSocket();
79-
socket.joinGroup(new InetSocketAddress(this.group, SocketUtils.findAvailableUdpPort()), nic);
78+
socket.joinGroup(new InetSocketAddress(this.group, 0), nic);
8079
socket.close();
8180
}
8281
catch (Exception e) {

spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/UdpChannelAdapterTests.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -50,7 +50,6 @@
5050
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
5151
import org.springframework.messaging.Message;
5252
import org.springframework.messaging.SubscribableChannel;
53-
import org.springframework.util.SocketUtils;
5453

5554
/**
5655
*
@@ -284,8 +283,7 @@ public void testMulticastReceiver() throws Exception {
284283
}
285284

286285
}
287-
DatagramSocket datagramSocket =
288-
new DatagramSocket(SocketUtils.findAvailableUdpPort(), inetAddress);
286+
DatagramSocket datagramSocket = new DatagramSocket(0, inetAddress);
289287
datagramSocket.send(packet);
290288
datagramSocket.close();
291289

spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2015-2022 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.
@@ -21,6 +21,7 @@
2121

2222
import org.apache.activemq.artemis.api.core.SimpleString;
2323
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
24+
import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
2425
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
2526
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
2627
import org.junit.jupiter.api.AfterAll;
@@ -57,7 +58,6 @@
5758
import org.springframework.messaging.simp.stomp.StompCommand;
5859
import org.springframework.messaging.support.GenericMessage;
5960
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
60-
import org.springframework.util.SocketUtils;
6161

6262
/**
6363
* @author Artem Bilan
@@ -73,22 +73,21 @@ public class StompServerIntegrationTests {
7373

7474
@BeforeAll
7575
public static void setup() throws Exception {
76-
int port = SocketUtils.findAvailableTcpPort(61613);
7776
ConfigurationImpl configuration =
7877
new ConfigurationImpl()
7978
.setName("embedded-server")
8079
.setPersistenceEnabled(false)
8180
.setSecurityEnabled(false)
8281
.setJMXManagementEnabled(false)
8382
.setJournalDatasync(false)
84-
.addAcceptorConfiguration("stomp", "tcp://127.0.0.1:" + port)
83+
.addAcceptorConfiguration("stomp", "tcp://127.0.0.1:" + TransportConstants.DEFAULT_STOMP_PORT)
8584
.addAddressesSetting("#",
8685
new AddressSettings()
8786
.setDeadLetterAddress(SimpleString.toSimpleString("dla"))
8887
.setExpiryAddress(SimpleString.toSimpleString("expiry")));
8988
broker.setConfiguration(configuration).start();
9089

91-
stompClient = new ReactorNettyTcpStompClient("127.0.0.1", port);
90+
stompClient = new ReactorNettyTcpStompClient("127.0.0.1", TransportConstants.DEFAULT_STOMP_PORT);
9291
stompClient.setMessageConverter(new PassThruMessageConverter());
9392
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
9493
taskScheduler.afterPropertiesSet();

spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests-context.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
1010
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
1111

12-
<bean id="socketUtils" class="org.springframework.util.SocketUtils" />
13-
14-
<int-syslog:inbound-channel-adapter id="foo" port="#{socketUtils.findAvailableUdpPort(1514)}" />
12+
<int-syslog:inbound-channel-adapter id="foo" port="0" />
1513

1614
<int-syslog:inbound-channel-adapter id="foobar" channel="foo" port="1514" auto-startup="false" />
1715

@@ -42,7 +40,7 @@
4240
<bean id="converter"
4341
class="org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterParserTests$PassThruConverter" />
4442

45-
<int-syslog:inbound-channel-adapter id="bar" protocol="tcp" port="#{socketUtils.findAvailableTcpPort(1514)}" />
43+
<int-syslog:inbound-channel-adapter id="bar" protocol="tcp" port="0" />
4644

4745
<int:channel id="bar">
4846
<int:queue/>

spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests.java

+47-57
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-2022 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,17 +17,18 @@
1717
package org.springframework.integration.syslog.config;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

22+
import java.lang.reflect.Method;
2223
import java.net.DatagramPacket;
2324
import java.net.DatagramSocket;
2425
import java.net.InetSocketAddress;
2526
import java.net.Socket;
27+
import java.nio.charset.StandardCharsets;
2628

2729
import javax.net.SocketFactory;
2830

29-
import org.junit.Test;
30-
import org.junit.runner.RunWith;
31+
import org.junit.jupiter.api.Test;
3132

3233
import org.springframework.beans.factory.BeanCreationException;
3334
import org.springframework.beans.factory.annotation.Autowired;
@@ -42,19 +43,21 @@
4243
import org.springframework.integration.test.util.TestUtils;
4344
import org.springframework.messaging.Message;
4445
import org.springframework.messaging.PollableChannel;
45-
import org.springframework.test.context.ContextConfiguration;
46-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
47+
import org.springframework.util.ReflectionUtils;
4748

4849
/**
4950
* @author Gary Russell
51+
* @author Artem Bilan
52+
*
5053
* @since 3.0
5154
*
5255
*/
53-
@ContextConfiguration
54-
@RunWith(SpringJUnit4ClassRunner.class)
56+
@SpringJUnitConfig
5557
public class SyslogReceivingChannelAdapterParserTests {
5658

57-
@Autowired @Qualifier("foo.adapter")
59+
@Autowired
60+
@Qualifier("foo.adapter")
5861
private UdpSyslogReceivingChannelAdapter adapter1;
5962

6063
@Autowired
@@ -63,7 +66,8 @@ public class SyslogReceivingChannelAdapterParserTests {
6366
@Autowired
6467
private PollableChannel foo;
6568

66-
@Autowired @Qualifier("explicitUdp.adapter")
69+
@Autowired
70+
@Qualifier("explicitUdp.adapter")
6771
private UdpSyslogReceivingChannelAdapter explicitUdpAdapter;
6872

6973
@Autowired
@@ -81,7 +85,8 @@ public class SyslogReceivingChannelAdapterParserTests {
8185
@Autowired
8286
private RFC5424MessageConverter rfc5424;
8387

84-
@Autowired @Qualifier("bar.adapter")
88+
@Autowired
89+
@Qualifier("bar.adapter")
8590
private TcpSyslogReceivingChannelAdapter adapter2;
8691

8792
@Autowired
@@ -95,8 +100,10 @@ public class SyslogReceivingChannelAdapterParserTests {
95100

96101
@Test
97102
public void testSimplestUdp() throws Exception {
98-
int port = TestUtils.getPropertyValue(adapter1, "udpAdapter.port", Integer.class);
99-
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes("UTF-8");
103+
Method getPort = ReflectionUtils.findMethod(UdpSyslogReceivingChannelAdapter.class, "getPort");
104+
ReflectionUtils.makeAccessible(getPort);
105+
int port = (int) ReflectionUtils.invokeMethod(getPort, this.adapter1);
106+
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes(StandardCharsets.UTF_8);
100107
DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", port));
101108
DatagramSocket socket = new DatagramSocket();
102109
Thread.sleep(1000);
@@ -108,13 +115,13 @@ public void testSimplestUdp() throws Exception {
108115
}
109116

110117
@Test
111-
public void testExplicitChannelUdp() throws Exception {
118+
public void testExplicitChannelUdp() {
112119
assertThat(TestUtils.getPropertyValue(foobar, "udpAdapter.port")).isEqualTo(1514);
113120
assertThat(TestUtils.getPropertyValue(foobar, "outputChannel")).isSameAs(foo);
114121
}
115122

116123
@Test
117-
public void testExplicitUdp() throws Exception {
124+
public void testExplicitUdp() {
118125
assertThat(TestUtils.getPropertyValue(explicitUdpAdapter, "outputChannel")).isSameAs(explicitUdp);
119126
}
120127

@@ -133,9 +140,9 @@ public void testFullBoatUdp() {
133140
public void testSimplestTcp() throws Exception {
134141
AbstractServerConnectionFactory connectionFactory = TestUtils.getPropertyValue(adapter2, "connectionFactory",
135142
AbstractServerConnectionFactory.class);
136-
int port = connectionFactory.getPort();
137143
waitListening(connectionFactory, 10000L);
138-
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes("UTF-8");
144+
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes(StandardCharsets.UTF_8);
145+
int port = connectionFactory.getPort();
139146
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
140147
Thread.sleep(1000);
141148
socket.getOutputStream().write(buf);
@@ -159,57 +166,40 @@ public void testFullBoatTcp() {
159166

160167
@Test
161168
public void testPortOnUdpChild() {
162-
try {
163-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail1-context.xml", this.getClass())
164-
.close();
165-
fail("Expected exception");
166-
}
167-
catch (BeanDefinitionParsingException e) {
168-
assertThat(e.getMessage().startsWith(
169-
"Configuration problem: When child element 'udp-attributes' is present, 'port' must be defined " +
170-
"there"))
171-
.isTrue();
172-
}
169+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
170+
.isThrownBy(() ->
171+
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail1-context.xml",
172+
getClass()))
173+
.withMessageStartingWith(
174+
"Configuration problem: " +
175+
"When child element 'udp-attributes' is present, 'port' must be defined there");
173176
}
174177

175178
@Test
176179
public void testPortWithTCPFactory() {
177-
try {
178-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail2-context.xml", this.getClass())
179-
.close();
180-
fail("Expected exception");
181-
}
182-
catch (BeanCreationException e) {
183-
assertThat(e.getCause().getMessage()).isEqualTo("Cannot specify both 'port' and 'connectionFactory'");
184-
}
180+
assertThatExceptionOfType(BeanCreationException.class)
181+
.isThrownBy(() ->
182+
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail2-context.xml",
183+
getClass()))
184+
.withStackTraceContaining("Cannot specify both 'port' and 'connectionFactory'");
185185
}
186186

187187
@Test
188188
public void testUdpChildWithTcp() {
189-
try {
190-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail3-context.xml", this.getClass())
191-
.close();
192-
fail("Expected exception");
193-
}
194-
catch (BeanCreationException e) {
195-
e.printStackTrace();
196-
197-
assertThat(e.getCause().getMessage())
198-
.isEqualTo("Cannot specify 'udp-attributes' when the protocol is 'tcp'");
199-
}
189+
assertThatExceptionOfType(BeanCreationException.class)
190+
.isThrownBy(() ->
191+
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail3-context.xml",
192+
getClass()))
193+
.withStackTraceContaining("Cannot specify 'udp-attributes' when the protocol is 'tcp'");
200194
}
201195

202196
@Test
203197
public void testUDPWithTCPFactory() {
204-
try {
205-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail4-context.xml", this.getClass())
206-
.close();
207-
fail("Expected exception");
208-
}
209-
catch (BeanCreationException e) {
210-
assertThat(e.getCause().getMessage())
211-
.isEqualTo("Cannot specify 'connection-factory' unless the protocol is 'tcp'");
212-
}
198+
assertThatExceptionOfType(BeanCreationException.class)
199+
.isThrownBy(() ->
200+
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail4-context.xml",
201+
getClass()))
202+
.withStackTraceContaining("Cannot specify 'connection-factory' unless the protocol is 'tcp'");
213203
}
214204

215205
public static class PassThruConverter implements MessageConverter {
@@ -229,7 +219,7 @@ public Message<?> fromSyslog(Message<?> syslog) {
229219
* @throws IllegalStateException
230220
*/
231221
private void waitListening(AbstractServerConnectionFactory serverConnectionFactory, Long delay)
232-
throws IllegalStateException {
222+
throws IllegalStateException {
233223
if (delay == null) {
234224
delay = 100L;
235225
}

spring-integration-zeromq/src/main/java/org/springframework/integration/zeromq/ZeroMqProxy.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 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.
@@ -212,7 +212,7 @@ public int getBackendPort() {
212212

213213
/**
214214
* Return the address an {@code inproc} control socket is bound or null if this proxy has not been started yet.
215-
* @return the the address for control socket or null
215+
* @return the address for control socket or null
216216
*/
217217
@Nullable
218218
public String getControlAddress() {
@@ -222,7 +222,7 @@ public String getControlAddress() {
222222
/**
223223
* Return the address an {@code inproc} capture socket is bound or null if this proxy has not been started yet
224224
* or {@link #captureAddress} is false.
225-
* @return the the address for capture socket or null
225+
* @return the address for capture socket or null
226226
*/
227227
@Nullable
228228
public String getCaptureAddress() {

0 commit comments

Comments
 (0)