Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 the original author or authors.
* Copyright 2015-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.
Expand Down Expand Up @@ -29,7 +29,6 @@
import org.springframework.integration.ip.util.SocketTestUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.SocketUtils;

/**
* @author Artem Bilan
Expand Down Expand Up @@ -76,7 +75,7 @@ private NetworkInterface checkMulticast() throws Exception {
}
try {
MulticastSocket socket = new MulticastSocket();
socket.joinGroup(new InetSocketAddress(this.group, SocketUtils.findAvailableUdpPort()), nic);
socket.joinGroup(new InetSocketAddress(this.group, 0), nic);
socket.close();
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -50,7 +50,6 @@
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
import org.springframework.messaging.Message;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.util.SocketUtils;

/**
*
Expand Down Expand Up @@ -284,8 +283,7 @@ public void testMulticastReceiver() throws Exception {
}

}
DatagramSocket datagramSocket =
new DatagramSocket(SocketUtils.findAvailableUdpPort(), inetAddress);
DatagramSocket datagramSocket = new DatagramSocket(0, inetAddress);
datagramSocket.send(packet);
datagramSocket.close();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* Copyright 2015-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.
Expand All @@ -21,6 +21,7 @@

import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -57,7 +58,6 @@
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.SocketUtils;

/**
* @author Artem Bilan
Expand All @@ -73,22 +73,21 @@ public class StompServerIntegrationTests {

@BeforeAll
public static void setup() throws Exception {
int port = SocketUtils.findAvailableTcpPort(61613);
ConfigurationImpl configuration =
new ConfigurationImpl()
.setName("embedded-server")
.setPersistenceEnabled(false)
.setSecurityEnabled(false)
.setJMXManagementEnabled(false)
.setJournalDatasync(false)
.addAcceptorConfiguration("stomp", "tcp://127.0.0.1:" + port)
.addAcceptorConfiguration("stomp", "tcp://127.0.0.1:" + TransportConstants.DEFAULT_STOMP_PORT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use a flaky SocketUtils rather than hard coding to a single port if the component doesn't support 0.

I'd prefer disabling (per your comment), if you want to remove it completely.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no such a class in SF 6.0 any more.
So, I wouldn't mind to watch this until it fails with the port conflict 😄

.addAddressesSetting("#",
new AddressSettings()
.setDeadLetterAddress(SimpleString.toSimpleString("dla"))
.setExpiryAddress(SimpleString.toSimpleString("expiry")));
broker.setConfiguration(configuration).start();

stompClient = new ReactorNettyTcpStompClient("127.0.0.1", port);
stompClient = new ReactorNettyTcpStompClient("127.0.0.1", TransportConstants.DEFAULT_STOMP_PORT);
stompClient.setMessageConverter(new PassThruMessageConverter());
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="socketUtils" class="org.springframework.util.SocketUtils" />

<int-syslog:inbound-channel-adapter id="foo" port="#{socketUtils.findAvailableUdpPort(1514)}" />
<int-syslog:inbound-channel-adapter id="foo" port="0" />

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

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

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

<int:channel id="bar">
<int:queue/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,17 +17,18 @@
package org.springframework.integration.syslog.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.lang.reflect.Method;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import javax.net.SocketFactory;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -42,19 +43,21 @@
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.PollableChannel;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.util.ReflectionUtils;

/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 3.0
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
public class SyslogReceivingChannelAdapterParserTests {

@Autowired @Qualifier("foo.adapter")
@Autowired
@Qualifier("foo.adapter")
private UdpSyslogReceivingChannelAdapter adapter1;

@Autowired
Expand All @@ -63,7 +66,8 @@ public class SyslogReceivingChannelAdapterParserTests {
@Autowired
private PollableChannel foo;

@Autowired @Qualifier("explicitUdp.adapter")
@Autowired
@Qualifier("explicitUdp.adapter")
private UdpSyslogReceivingChannelAdapter explicitUdpAdapter;

@Autowired
Expand All @@ -81,7 +85,8 @@ public class SyslogReceivingChannelAdapterParserTests {
@Autowired
private RFC5424MessageConverter rfc5424;

@Autowired @Qualifier("bar.adapter")
@Autowired
@Qualifier("bar.adapter")
private TcpSyslogReceivingChannelAdapter adapter2;

@Autowired
Expand All @@ -95,8 +100,10 @@ public class SyslogReceivingChannelAdapterParserTests {

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

@Test
public void testExplicitChannelUdp() throws Exception {
public void testExplicitChannelUdp() {
assertThat(TestUtils.getPropertyValue(foobar, "udpAdapter.port")).isEqualTo(1514);
assertThat(TestUtils.getPropertyValue(foobar, "outputChannel")).isSameAs(foo);
}

@Test
public void testExplicitUdp() throws Exception {
public void testExplicitUdp() {
assertThat(TestUtils.getPropertyValue(explicitUdpAdapter, "outputChannel")).isSameAs(explicitUdp);
}

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

@Test
public void testPortOnUdpChild() {
try {
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail1-context.xml", this.getClass())
.close();
fail("Expected exception");
}
catch (BeanDefinitionParsingException e) {
assertThat(e.getMessage().startsWith(
"Configuration problem: When child element 'udp-attributes' is present, 'port' must be defined " +
"there"))
.isTrue();
}
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail1-context.xml",
getClass()))
.withMessageStartingWith(
"Configuration problem: " +
"When child element 'udp-attributes' is present, 'port' must be defined there");
}

@Test
public void testPortWithTCPFactory() {
try {
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail2-context.xml", this.getClass())
.close();
fail("Expected exception");
}
catch (BeanCreationException e) {
assertThat(e.getCause().getMessage()).isEqualTo("Cannot specify both 'port' and 'connectionFactory'");
}
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail2-context.xml",
getClass()))
.withStackTraceContaining("Cannot specify both 'port' and 'connectionFactory'");
}

@Test
public void testUdpChildWithTcp() {
try {
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail3-context.xml", this.getClass())
.close();
fail("Expected exception");
}
catch (BeanCreationException e) {
e.printStackTrace();

assertThat(e.getCause().getMessage())
.isEqualTo("Cannot specify 'udp-attributes' when the protocol is 'tcp'");
}
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail3-context.xml",
getClass()))
.withStackTraceContaining("Cannot specify 'udp-attributes' when the protocol is 'tcp'");
}

@Test
public void testUDPWithTCPFactory() {
try {
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail4-context.xml", this.getClass())
.close();
fail("Expected exception");
}
catch (BeanCreationException e) {
assertThat(e.getCause().getMessage())
.isEqualTo("Cannot specify 'connection-factory' unless the protocol is 'tcp'");
}
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail4-context.xml",
getClass()))
.withStackTraceContaining("Cannot specify 'connection-factory' unless the protocol is 'tcp'");
}

public static class PassThruConverter implements MessageConverter {
Expand All @@ -229,7 +219,7 @@ public Message<?> fromSyslog(Message<?> syslog) {
* @throws IllegalStateException
*/
private void waitListening(AbstractServerConnectionFactory serverConnectionFactory, Long delay)
throws IllegalStateException {
throws IllegalStateException {
if (delay == null) {
delay = 100L;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-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.
Expand Down Expand Up @@ -212,7 +212,7 @@ public int getBackendPort() {

/**
* Return the address an {@code inproc} control socket is bound or null if this proxy has not been started yet.
* @return the the address for control socket or null
* @return the address for control socket or null
*/
@Nullable
public String getControlAddress() {
Expand All @@ -222,7 +222,7 @@ public String getControlAddress() {
/**
* Return the address an {@code inproc} capture socket is bound or null if this proxy has not been started yet
* or {@link #captureAddress} is false.
* @return the the address for capture socket or null
* @return the address for capture socket or null
*/
@Nullable
public String getCaptureAddress() {
Expand Down
Loading