INT-4364 Deprecate SocketUtils and remove all use

JIRA: https://jira.spring.io/browse/INT-4364

Always let the OS choose the ports for tests.

Just one test remains (RMI) because there appears to be no way to obtain
the port after creating a registry with a 0 port.

This test has been switched to use the Spring utility.

Polishing - PR Comments
This commit is contained in:
Gary Russell
2017-11-13 12:49:49 -05:00
committed by Artem Bilan
parent 8c89d0ef98
commit 4207f36a17
25 changed files with 256 additions and 267 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -28,15 +28,20 @@ import javax.net.ServerSocketFactory;
import org.springframework.util.Assert;
/**
* Contains several socket-specific utility methods. For example, you may have
* test cases that require an open port. Rather than hard-coding the relevant port,
* it will be better to use methods from this utility class to automatically select
* an open port, therefore improving the portability of your test-cases across
* systems.
* Contains several socket-specific utility methods. For example, you may have test cases
* that require an open port. Rather than hard-coding the relevant port, it will be better
* to use methods from this utility class to automatically select an open port, therefore
* improving the portability of your test-cases across systems.
*
* @deprecated - it's generally better to set the server port to 0; let the operating
* system choose a port; wait until the server starts (see TestingUtilities in the ip
* module for an example), then set the port on the client factory.
*
* @author Gunnar Hillert
* @author Gary Russell
* @since 2.2
*/
@Deprecated
public final class SocketUtils {
public static final int DEFAULT_PORT_RANGE_MIN = 10000;

View File

@@ -1,83 +0,0 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.test.util;
import static org.junit.Assert.assertNotEquals;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import javax.net.ServerSocketFactory;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Gunnar Hillert
* @author Gary Russell
*/
public class SocketUtilsTests {
@Test
public void testFindAvailableServerSocketWithNegativeSeedPort() {
try {
SocketUtils.findAvailableServerSocket(-500);
}
catch (IllegalArgumentException e) {
Assert.assertEquals("'seed' must not be negative", e.getMessage());
return;
}
Assert.fail("Expected an IllegalArgumentException to be thrown.");
}
@Test
public void testFindAvailableUdpSocketWithNegativeSeedPort() {
try {
SocketUtils.findAvailableUdpSocket(-500);
}
catch (IllegalArgumentException e) {
Assert.assertEquals("'seed' must not be negative", e.getMessage());
return;
}
Assert.fail("Expected an IllegalArgumentException to be thrown.");
}
@Test
public void testTcpLocalhost() throws Exception {
int available = SocketUtils.findAvailableServerSocket();
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(available, 1,
InetAddress.getByName("localhost"));
assertNotEquals(available, SocketUtils.findAvailableServerSocket(available));
ss.close();
}
@Test
public void testUdpLocalhost() throws Exception {
int available = SocketUtils.findAvailableUdpSocket(2000);
DatagramSocket dgs = new DatagramSocket(available, InetAddress.getByName("localhost"));
assertNotEquals(available, SocketUtils.findAvailableUdpSocket(available));
dgs.close();
}
}