diff --git a/build.gradle b/build.gradle index 185246e859..70c72b5639 100644 --- a/build.gradle +++ b/build.gradle @@ -782,6 +782,7 @@ project('spring-integration-test') { 'org.apache.tools.ant.*;version="[1.7.0, 2.0.0)"', 'org.springframework.*;version="[3.1.1, 4.0.0)"', 'org.springframework.integration.*;version="[2.2.0, 2.2.1)"', + 'javax.net.*;version="0"', 'junit.framework.*;version="[4.6.0, 4.7.0)"', 'org.junit.*;version="[4.6.0, 4.7.0)"', 'org.mockito.*;version="[1.6.0, 2.0.0)"', diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/config/ParserUnitTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/config/ParserUnitTests-context.xml index 0016770c7f..411ec92291 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/config/ParserUnitTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/config/ParserUnitTests-context.xml @@ -13,7 +13,7 @@ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/AutoStartTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/AutoStartTests-context.xml index 8a5f843866..9d3877d173 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/AutoStartTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/AutoStartTests-context.xml @@ -7,7 +7,7 @@ http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - + - + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/ConnectionToConnectionTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/ConnectionToConnectionTests-context.xml index ff6e03197d..ed8ed9b0d4 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/ConnectionToConnectionTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/ConnectionToConnectionTests-context.xml @@ -11,7 +11,7 @@ - + - + - + - + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/FactoryStopStartTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/FactoryStopStartTests.java index db2932cecc..8bfb9b63cd 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/FactoryStopStartTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/FactoryStopStartTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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.ip.tcp; import org.junit.Test; import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory; -import org.springframework.integration.ip.util.SocketTestUtils; +import org.springframework.integration.test.util.SocketUtils; /** @@ -27,10 +27,10 @@ import org.springframework.integration.ip.util.SocketTestUtils; * */ public class FactoryStopStartTests { - + @Test public void testRestart() { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); AbstractServerConnectionFactory factory = new TcpNetServerConnectionFactory(port); factory.setSoTimeout(10000); factory.start(); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml index 661f07442c..92972060e1 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml @@ -7,12 +7,12 @@ http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - - + + - + @@ -24,7 +24,7 @@ - + - + - + @@ -56,15 +56,15 @@ - + - + connection-factory="client"/> + - + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/SharedConnectionTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/SharedConnectionTests-context.xml index 9b0951a4f1..e921583adb 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/SharedConnectionTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/SharedConnectionTests-context.xml @@ -9,7 +9,7 @@ - + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpInboundGatewayTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpInboundGatewayTests.java index e08902dc18..b393c41624 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpInboundGatewayTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpInboundGatewayTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -48,16 +48,16 @@ import org.springframework.integration.ip.tcp.connection.AbstractServerConnectio import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory; import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory; import org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory; -import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.ip.util.TestingUtilities; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.support.channel.ChannelResolver; +import org.springframework.integration.test.util.SocketUtils; public class TcpInboundGatewayTests { @Test public void testNetSingle() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port); scf.setSingleUse(true); TcpInboundGateway gateway = new TcpInboundGateway(); @@ -87,7 +87,7 @@ public class TcpInboundGatewayTests { @Test public void testNetNotSingle() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port); scf.setSingleUse(false); TcpInboundGateway gateway = new TcpInboundGateway(); @@ -111,7 +111,7 @@ public class TcpInboundGatewayTests { @Test public void testNetClientMode() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port); ccf.setSingleUse(false); TcpInboundGateway gateway = new TcpInboundGateway(); @@ -166,7 +166,7 @@ public class TcpInboundGatewayTests { @Test public void testNioSingle() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(port); scf.setSingleUse(true); TcpInboundGateway gateway = new TcpInboundGateway(); @@ -196,7 +196,7 @@ public class TcpInboundGatewayTests { @Test public void testNioNotSingle() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(port); scf.setSingleUse(false); TcpInboundGateway gateway = new TcpInboundGateway(); @@ -223,7 +223,7 @@ public class TcpInboundGatewayTests { @Test public void testErrorFlow() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port); scf.setSingleUse(true); TcpInboundGateway gateway = new TcpInboundGateway(); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundChannelAdapterWithinChainTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundChannelAdapterWithinChainTests-context.xml index abe18b3580..f990373f16 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundChannelAdapterWithinChainTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundChannelAdapterWithinChainTests-context.xml @@ -7,7 +7,7 @@ http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - + - + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java index cb1ae913e1..b5f64fe0e5 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java @@ -21,8 +21,8 @@ import java.net.Socket; import org.junit.Test; import org.springframework.integration.Message; -import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.ip.util.TestingUtilities; +import org.springframework.integration.test.util.SocketUtils; import org.springframework.integration.test.util.TestUtils; /** @@ -34,7 +34,7 @@ public class DefaultTimeoutTests { @Test public void test() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(port); server.registerListener(new TcpListener() { public boolean onMessage(Message message) { diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java index d6efb60c0b..fd607c6470 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java @@ -49,9 +49,9 @@ import org.springframework.integration.core.SubscribableChannel; import org.springframework.integration.ip.IpHeaders; import org.springframework.integration.ip.tcp.TcpInboundGateway; import org.springframework.integration.ip.tcp.TcpOutboundGateway; -import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.ip.util.TestingUtilities; import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.test.util.SocketUtils; import org.springframework.integration.test.util.TestUtils; /** @@ -226,8 +226,11 @@ public class FailoverClientConnectionFactoryTests { @Test public void testRealNet() throws Exception { - int port1 = SocketTestUtils.findAvailableServerSocket(); - int port2 = SocketTestUtils.findAvailableServerSocket(port1 + 1); + + final List openPorts = SocketUtils.findAvailableServerSockets(SocketUtils.getRandomSeedPort(), 2); + + int port1 = openPorts.get(0); + int port2 = openPorts.get(1); AbstractClientConnectionFactory client1 = new TcpNetClientConnectionFactory("localhost", port1); AbstractClientConnectionFactory client2 = new TcpNetClientConnectionFactory("localhost", port2); AbstractServerConnectionFactory server1 = new TcpNetServerConnectionFactory(port1); @@ -237,8 +240,12 @@ public class FailoverClientConnectionFactoryTests { @Test public void testRealNio() throws Exception { - int port1 = SocketTestUtils.findAvailableServerSocket(); - int port2 = SocketTestUtils.findAvailableServerSocket(port1 + 1); + + final List openPorts = SocketUtils.findAvailableServerSockets(SocketUtils.getRandomSeedPort(), 2); + + int port1 = openPorts.get(0); + int port2 = openPorts.get(1); + AbstractClientConnectionFactory client1 = new TcpNioClientConnectionFactory("localhost", port1); AbstractClientConnectionFactory client2 = new TcpNioClientConnectionFactory("localhost", port2); AbstractServerConnectionFactory server1 = new TcpNioServerConnectionFactory(port1); @@ -248,8 +255,12 @@ public class FailoverClientConnectionFactoryTests { @Test public void testRealNetSingleUse() throws Exception { - int port1 = SocketTestUtils.findAvailableServerSocket(); - int port2 = SocketTestUtils.findAvailableServerSocket(port1 + 1); + + final List openPorts = SocketUtils.findAvailableServerSockets(SocketUtils.getRandomSeedPort(), 2); + + int port1 = openPorts.get(0); + int port2 = openPorts.get(1); + AbstractClientConnectionFactory client1 = new TcpNetClientConnectionFactory("localhost", port1); AbstractClientConnectionFactory client2 = new TcpNetClientConnectionFactory("localhost", port2); AbstractServerConnectionFactory server1 = new TcpNetServerConnectionFactory(port1); @@ -261,8 +272,12 @@ public class FailoverClientConnectionFactoryTests { @Test public void testRealNioSingleUse() throws Exception { - int port1 = SocketTestUtils.findAvailableServerSocket(); - int port2 = SocketTestUtils.findAvailableServerSocket(port1 + 1); + + final List openPorts = SocketUtils.findAvailableServerSockets(SocketUtils.getRandomSeedPort(), 2); + + int port1 = openPorts.get(0); + int port2 = openPorts.get(1); + AbstractClientConnectionFactory client1 = new TcpNioClientConnectionFactory("localhost", port1); AbstractClientConnectionFactory client2 = new TcpNioClientConnectionFactory("localhost", port2); AbstractServerConnectionFactory server1 = new TcpNioServerConnectionFactory(port1); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SOLingerTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SOLingerTests-context.xml index 384d160d31..559ad2ef0d 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SOLingerTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SOLingerTests-context.xml @@ -7,18 +7,18 @@ http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd"> - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SocketSupportTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SocketSupportTests.java index 246f45c462..f800f6ea01 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SocketSupportTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/SocketSupportTests.java @@ -47,9 +47,9 @@ import org.springframework.integration.ip.tcp.connection.support.TcpSSLContextSu import org.springframework.integration.ip.tcp.connection.support.TcpSocketFactorySupport; import org.springframework.integration.ip.tcp.connection.support.TcpSocketSupport; import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer; -import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.ip.util.TestingUtilities; import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.test.util.SocketUtils; import org.springframework.integration.test.util.TestUtils; /** @@ -122,7 +122,7 @@ public class SocketSupportTests { @Test public void testNioClientAndServer() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); TcpNioClientConnectionFactory clientConnectionFactory = new TcpNioClientConnectionFactory("localhost", port); final AtomicInteger ppSocketCountClient = new AtomicInteger(); final AtomicInteger ppServerSocketCountClient = new AtomicInteger(); @@ -264,7 +264,7 @@ Certificate fingerprints: @Test public void testNetClientAndServerSSL() throws Exception { System.setProperty("javax.net.debug", "all"); // SSL activity in the console - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(port); TcpSSLContextSupport sslContextSupport = new DefaultTcpSSLContextSupport("test.ks", "test.truststore.ks", "secret", "secret"); @@ -296,7 +296,7 @@ Certificate fingerprints: @Test public void testNetClientAndServerSSLDifferentContexts() throws Exception { System.setProperty("javax.net.debug", "all"); // SSL activity in the console - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(port); TcpSSLContextSupport serverSslContextSupport = new DefaultTcpSSLContextSupport("server.ks", "server.truststore.ks", "secret", "secret"); @@ -332,7 +332,7 @@ Certificate fingerprints: @Test public void testNioClientAndServerSSL() throws Exception { System.setProperty("javax.net.debug", "all"); // SSL activity in the console - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); TcpNioServerConnectionFactory server = new TcpNioServerConnectionFactory(port); DefaultTcpSSLContextSupport sslContextSupport = new DefaultTcpSSLContextSupport("test.ks", "test.truststore.ks", "secret", "secret"); @@ -372,7 +372,7 @@ Certificate fingerprints: @Test public void testNioClientAndServerSSLDifferentContextsLargeDataWithReply() throws Exception { System.setProperty("javax.net.debug", "all"); // SSL activity in the console - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); TcpNioServerConnectionFactory server = new TcpNioServerConnectionFactory(port); TcpSSLContextSupport serverSslContextSupport = new DefaultTcpSSLContextSupport("server.ks", "server.truststore.ks", "secret", "secret"); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java index 5c068bff37..063a2bcd5c 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionReadTests.java @@ -39,6 +39,7 @@ import org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSe import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer; import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.ip.util.TestingUtilities; +import org.springframework.integration.test.util.SocketUtils; /** * @author Gary Russell @@ -73,7 +74,7 @@ public class TcpNioConnectionReadTests { @SuppressWarnings("unchecked") @Test public void testReadLength() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer(); final List> responses = new ArrayList>(); final Semaphore semaphore = new Semaphore(0); @@ -104,7 +105,7 @@ public class TcpNioConnectionReadTests { @SuppressWarnings("unchecked") @Test public void testFragmented() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer(); final List> responses = new ArrayList>(); final Semaphore semaphore = new Semaphore(0); @@ -138,7 +139,7 @@ public class TcpNioConnectionReadTests { @SuppressWarnings("unchecked") @Test public void testReadStxEtx() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer(); final List> responses = new ArrayList>(); final Semaphore semaphore = new Semaphore(0); @@ -170,7 +171,7 @@ public class TcpNioConnectionReadTests { @SuppressWarnings("unchecked") @Test public void testReadCrLf() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer(); final List> responses = new ArrayList>(); final Semaphore semaphore = new Semaphore(0); @@ -201,7 +202,7 @@ public class TcpNioConnectionReadTests { */ @Test public void testReadLengthOverflow() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer(); final List> responses = new ArrayList>(); final Semaphore semaphore = new Semaphore(0); @@ -239,7 +240,7 @@ public class TcpNioConnectionReadTests { */ @Test public void testReadStxEtxOverflow() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer(); serializer.setMaxMessageSize(1024); final List> responses = new ArrayList>(); @@ -278,7 +279,7 @@ public class TcpNioConnectionReadTests { */ @Test public void testReadCrLfOverflow() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer(); serializer.setMaxMessageSize(1024); final List> responses = new ArrayList>(); @@ -319,7 +320,7 @@ public class TcpNioConnectionReadTests { */ @Test public void testCloseCleanupNoData() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer(); serializer.setMaxMessageSize(1024); final List> responses = new ArrayList>(); @@ -358,7 +359,7 @@ public class TcpNioConnectionReadTests { */ @Test public void testCloseCleanupPartialData() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer(); serializer.setMaxMessageSize(1024); final List> responses = new ArrayList>(); @@ -429,7 +430,7 @@ public class TcpNioConnectionReadTests { private void testClosureMidMessageGuts(AbstractByteArraySerializer serializer, String shortMessage) throws Exception, IOException, UnknownHostException, InterruptedException { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final List> responses = new ArrayList>(); final Semaphore semaphore = new Semaphore(0); final List added = new ArrayList(); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionTests.java index 03d5f82c28..63ddeef49e 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionTests.java @@ -56,8 +56,8 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.springframework.integration.Message; import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer; -import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.util.SocketUtils; import org.springframework.integration.test.util.TestUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.FieldCallback; @@ -75,7 +75,7 @@ public class TcpNioConnectionTests { @Test public void testWriteTimeout() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost", port); factory.setSoTimeout(1000); factory.start(); @@ -106,7 +106,7 @@ public class TcpNioConnectionTests { @Test public void testReadTimeout() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost", port); factory.setSoTimeout(1000); factory.start(); @@ -145,7 +145,7 @@ public class TcpNioConnectionTests { @Test public void testMemoryLeak() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost", port); factory.setNioHarvestInterval(100); factory.start(); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionWriteTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionWriteTests.java index 199e389340..5aaaf763db 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionWriteTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionWriteTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -27,17 +27,16 @@ import java.nio.ByteBuffer; import javax.net.ServerSocketFactory; import org.junit.Test; - import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer; import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer; import org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer; import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer; -import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.util.SocketUtils; /** * @author Gary Russell - * @since 2.0 + * @since 2.0 */ public class TcpNioConnectionWriteTests { @@ -51,10 +50,10 @@ public class TcpNioConnectionWriteTests { ccf.start(); return ccf; } - + @Test public void testWriteLengthHeader() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault() .createServerSocket(port); @@ -87,7 +86,7 @@ public class TcpNioConnectionWriteTests { @Test public void testWriteStxEtx() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault() .createServerSocket(port); @@ -120,7 +119,7 @@ public class TcpNioConnectionWriteTests { @Test public void testWriteCrLf() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault() .createServerSocket(port); @@ -153,7 +152,7 @@ public class TcpNioConnectionWriteTests { @Test public void testWriteLengthHeaderDirect() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault() .createServerSocket(port); @@ -186,7 +185,7 @@ public class TcpNioConnectionWriteTests { @Test public void testWriteStxEtxDirect() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault() .createServerSocket(port); @@ -220,7 +219,7 @@ public class TcpNioConnectionWriteTests { @Test public void testWriteCrLfDirect() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault() .createServerSocket(port); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/DeserializationTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/DeserializationTests.java index 4d5dab20b1..7f9f85ae61 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/DeserializationTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/DeserializationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -26,9 +26,9 @@ import java.net.Socket; import javax.net.ServerSocketFactory; import org.junit.Test; - import org.springframework.core.serializer.DefaultDeserializer; import org.springframework.integration.ip.util.SocketTestUtils; +import org.springframework.integration.test.util.SocketUtils; /** * @author Gary Russell @@ -38,7 +38,7 @@ public class DeserializationTests { @Test public void testReadLength() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendLength(port, null); @@ -46,17 +46,17 @@ public class DeserializationTests { socket.setSoTimeout(5000); ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer(); byte[] out = serializer.deserialize(socket.getInputStream()); - assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, + assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(out)); out = serializer.deserialize(socket.getInputStream()); - assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, + assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(out)); server.close(); } - + @Test public void testReadStxEtx() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendStxEtx(port, null); @@ -64,17 +64,17 @@ public class DeserializationTests { socket.setSoTimeout(5000); ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer(); byte[] out = serializer.deserialize(socket.getInputStream()); - assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, + assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(out)); out = serializer.deserialize(socket.getInputStream()); - assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, + assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(out)); server.close(); } @Test public void testReadCrLf() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendCrLf(port, null); @@ -82,17 +82,17 @@ public class DeserializationTests { socket.setSoTimeout(5000); ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer(); byte[] out = serializer.deserialize(socket.getInputStream()); - assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, + assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(out)); out = serializer.deserialize(socket.getInputStream()); - assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, + assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(out)); server.close(); } @Test public void testReadRaw() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendRaw(port); @@ -100,14 +100,14 @@ public class DeserializationTests { socket.setSoTimeout(5000); ByteArrayRawSerializer serializer = new ByteArrayRawSerializer(); byte[] out = serializer.deserialize(socket.getInputStream()); - assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, + assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(out)); server.close(); } @Test public void testReadSerialized() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendSerialized(port); @@ -123,7 +123,7 @@ public class DeserializationTests { @Test public void testReadLengthOverflow() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendLengthOverflow(port); @@ -144,7 +144,7 @@ public class DeserializationTests { @Test public void testReadStxEtxTimeout() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendStxEtxOverflow(port); @@ -165,7 +165,7 @@ public class DeserializationTests { @Test public void testReadStxEtxOverflow() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendStxEtxOverflow(port); @@ -187,7 +187,7 @@ public class DeserializationTests { @Test public void testReadCrLfTimeout() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendCrLfOverflow(port); @@ -208,7 +208,7 @@ public class DeserializationTests { @Test public void testReadCrLfOverflow() throws Exception { - int port = SocketTestUtils.findAvailableServerSocket(); + int port = SocketUtils.findAvailableServerSocket(); ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); SocketTestUtils.testSendCrLfOverflow(port); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/SerializationTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/SerializationTests.java index 1ff12564c0..e05374ee10 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/SerializationTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/SerializationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2012 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. @@ -29,9 +29,8 @@ import javax.net.ServerSocketFactory; import javax.net.SocketFactory; import org.junit.Test; - import org.springframework.core.serializer.DefaultSerializer; -import org.springframework.integration.ip.util.SocketTestUtils; +import org.springframework.integration.test.util.SocketUtils; /** * @author Gary Russell @@ -41,7 +40,7 @@ public class SerializationTests { @Test public void testWriteLengthHeader() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); @@ -74,7 +73,7 @@ public class SerializationTests { @Test public void testWriteStxEtx() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); @@ -107,7 +106,7 @@ public class SerializationTests { @Test public void testWriteCrLf() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); @@ -137,10 +136,10 @@ public class SerializationTests { assertEquals('\n', buff[testString.length() + 1]); server.close(); } - + @Test public void testWriteRaw() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); @@ -170,10 +169,10 @@ public class SerializationTests { assertEquals(-1, buff[testString.length()]); server.close(); } - + @Test public void testWriteSerialized() throws Exception { - final int port = SocketTestUtils.findAvailableServerSocket(); + final int port = SocketUtils.findAvailableServerSocket(); final String testString = "abcdef"; ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port); server.setSoTimeout(10000); @@ -201,7 +200,7 @@ public class SerializationTests { assertEquals(testString, ois.readObject()); server.close(); } - + /** * @param is * @param buff diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/DatagramPacketSendingHandlerTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/DatagramPacketSendingHandlerTests.java index 280e3ae8f7..47aa20c135 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/DatagramPacketSendingHandlerTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/DatagramPacketSendingHandlerTests.java @@ -24,6 +24,7 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.MulticastSocket; +import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -34,8 +35,8 @@ import org.junit.Ignore; import org.junit.Test; import org.springframework.integration.Message; import org.springframework.integration.ip.IpHeaders; -import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.util.SocketUtils; /** * @author Mark Fisher @@ -45,10 +46,10 @@ import org.springframework.integration.support.MessageBuilder; public class DatagramPacketSendingHandlerTests { private boolean noMulticast; - + @Test public void verifySend() throws Exception { - final int testPort = SocketTestUtils.findAvailableUdpSocket(); + final int testPort = SocketUtils.findAvailableUdpSocket(); byte[] buffer = new byte[8]; final DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length); final CountDownLatch latch = new CountDownLatch(1); @@ -66,7 +67,7 @@ public class DatagramPacketSendingHandlerTests { } }); Thread.sleep(1000); - UnicastSendingMessageHandler handler = + UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("localhost", testPort); String payload = "foo"; handler.handleMessage(MessageBuilder.withPayload(payload).build()); @@ -82,14 +83,18 @@ public class DatagramPacketSendingHandlerTests { @Test public void verifySendWithAck() throws Exception { - final int testPort = SocketTestUtils.findAvailableUdpSocket(); - final int ackPort = SocketTestUtils.findAvailableUdpSocket(testPort + 1); + + final List openPorts = SocketUtils.findAvailableUdpSockets(SocketUtils.getRandomSeedPort(), 2); + + final int testPort = openPorts.get(0); + final int ackPort = openPorts.get(1); + byte[] buffer = new byte[1000]; final DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length); - final CountDownLatch latch1 = new CountDownLatch(1); + final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); - UnicastSendingMessageHandler handler = - new UnicastSendingMessageHandler("localhost", testPort, true, + UnicastSendingMessageHandler handler = + new UnicastSendingMessageHandler("localhost", testPort, true, true, "localhost", ackPort, 5000); handler.afterPropertiesSet(); Executors.newSingleThreadExecutor().execute(new Runnable() { @@ -105,7 +110,7 @@ public class DatagramPacketSendingHandlerTests { Message message = mapper.toMessage(receivedPacket); Object id = message.getHeaders().get(IpHeaders.ACK_ID); byte[] ack = id.toString().getBytes(); - DatagramPacket ackPack = new DatagramPacket(ack, ack.length, + DatagramPacket ackPack = new DatagramPacket(ack, ack.length, new InetSocketAddress("localHost", ackPort)); DatagramSocket out = new DatagramSocket(); out.send(ackPack); @@ -133,7 +138,7 @@ public class DatagramPacketSendingHandlerTests { @Test @Ignore public void verifySendMulticast() throws Exception { - final int testPort = SocketTestUtils.findAvailableUdpSocket(); + final int testPort = SocketUtils.findAvailableUdpSocket(); final String multicastAddress = "225.6.7.8"; final String payload = "foo"; final CountDownLatch latch1 = new CountDownLatch(2); @@ -184,8 +189,12 @@ public class DatagramPacketSendingHandlerTests { @Test @Ignore public void verifySendMulticastWithAcks() throws Exception { - final int testPort = SocketTestUtils.findAvailableUdpSocket(); - final int ackPort = SocketTestUtils.findAvailableUdpSocket(testPort + 1); + + final List openPorts = SocketUtils.findAvailableUdpSockets(SocketUtils.getRandomSeedPort(), 2); + + final int testPort = openPorts.get(0); + final int ackPort = openPorts.get(1); + final String multicastAddress = "225.6.7.8"; final String payload = "foobar"; final CountDownLatch latch1 = new CountDownLatch(2); @@ -215,7 +224,7 @@ public class DatagramPacketSendingHandlerTests { Message message = mapper.toMessage(receivedPacket); Object id = message.getHeaders().get(IpHeaders.ACK_ID); byte[] ack = id.toString().getBytes(); - DatagramPacket ackPack = new DatagramPacket(ack, ack.length, + DatagramPacket ackPack = new DatagramPacket(ack, ack.length, new InetSocketAddress("localHost", ackPort)); DatagramSocket out = new DatagramSocket(); out.send(ackPack); @@ -236,13 +245,13 @@ public class DatagramPacketSendingHandlerTests { if (noMulticast) { return; } - MulticastSendingMessageHandler handler = - new MulticastSendingMessageHandler(multicastAddress, testPort, true, + MulticastSendingMessageHandler handler = + new MulticastSendingMessageHandler(multicastAddress, testPort, true, true, "localhost", ackPort, 500000); handler.setMinAcksForSuccess(2); handler.handleMessage(MessageBuilder.withPayload(payload).build()); assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS)); handler.shutDown(); } - + } diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/MultiClientTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/MultiClientTests.java index 3c2cd764c7..3adac3dd10 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/MultiClientTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/MultiClientTests.java @@ -24,11 +24,12 @@ import org.springframework.integration.Message; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.util.SocketUtils; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; /** - * + * * For both .net. and .nio. adapters, creates a single server and 10 clients * and sends 3 messages from each client to the associated server. * Ensures that all messages are correctly assembled and received ok. @@ -37,7 +38,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; * will cause messages to be lost. * Even with this restriction, we are still testing the receiving adapter's * ability to handle multiple requests from multiple clients. - * + * * @author Gary Russell * */ @@ -46,9 +47,9 @@ public class MultiClientTests { @SuppressWarnings("unchecked") @Test @Ignore public void testNoAck() throws Exception { - final String payload = largePayload(1000); - final UnicastReceivingChannelAdapter adapter = - new UnicastReceivingChannelAdapter(SocketTestUtils.findAvailableUdpSocket()); + final String payload = largePayload(1000); + final UnicastReceivingChannelAdapter adapter = + new UnicastReceivingChannelAdapter(SocketUtils.findAvailableUdpSocket()); int drivers = 10; adapter.setPoolSize(drivers); QueueChannel queue = new QueueChannel(drivers * 3); @@ -87,9 +88,9 @@ public class MultiClientTests { @Test @Ignore public void testAck() throws Exception { Thread.sleep(1000); - final String payload = largePayload(1000); - final UnicastReceivingChannelAdapter adapter = - new UnicastReceivingChannelAdapter(SocketTestUtils.findAvailableUdpSocket(), false); + final String payload = largePayload(1000); + final UnicastReceivingChannelAdapter adapter = + new UnicastReceivingChannelAdapter(SocketUtils.findAvailableUdpSocket(), false); int drivers = 5; adapter.setPoolSize(drivers); QueueChannel queue = new QueueChannel(drivers * 3); @@ -107,7 +108,7 @@ public class MultiClientTests { UnicastSendingMessageHandler sender = new UnicastSendingMessageHandler( "localhost", adapter.getPort(), false, true, "localhost", - SocketTestUtils.findAvailableUdpSocket(adapter.getPort() + j + 1000), + SocketUtils.findAvailableUdpSocket(adapter.getPort() + j + 1000), 10000); while (true) { Message message = queueIn.receive(); @@ -132,11 +133,11 @@ public class MultiClientTests { @Test @Ignore public void testAckWithLength() throws Exception { Thread.sleep(1000); - final String payload = largePayload(1000); - final UnicastReceivingChannelAdapter adapter = - new UnicastReceivingChannelAdapter(SocketTestUtils.findAvailableUdpSocket(), true); + final String payload = largePayload(1000); + final UnicastReceivingChannelAdapter adapter = + new UnicastReceivingChannelAdapter(SocketUtils.findAvailableUdpSocket(), true); int drivers = 10; - adapter.setPoolSize(drivers); + adapter.setPoolSize(drivers); QueueChannel queue = new QueueChannel(drivers * 3); adapter.setOutputChannel(queue); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); @@ -152,7 +153,7 @@ public class MultiClientTests { UnicastSendingMessageHandler sender = new UnicastSendingMessageHandler( "localhost", adapter.getPort(), true, true, "localhost", - SocketTestUtils.findAvailableUdpSocket(adapter.getPort() + j + 1100), + SocketUtils.findAvailableUdpSocket(adapter.getPort() + j + 1100), 10000); while (true) { Message message = queueIn.receive(); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/UdpChannelAdapterTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/UdpChannelAdapterTests.java index 3a164c7006..6954fe15ef 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/UdpChannelAdapterTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/UdpChannelAdapterTests.java @@ -18,6 +18,7 @@ import org.springframework.integration.core.SubscribableChannel; import org.springframework.integration.handler.ServiceActivatingHandler; import org.springframework.integration.ip.util.SocketTestUtils; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.util.SocketUtils; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @@ -27,7 +28,7 @@ public class UdpChannelAdapterTests { @Test public void testUnicastReceiver() throws Exception { QueueChannel channel = new QueueChannel(2); - int port = SocketTestUtils.findAvailableUdpSocket(); + int port = SocketUtils.findAvailableUdpSocket(); UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(port); adapter.setOutputChannel(channel); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); @@ -36,21 +37,21 @@ public class UdpChannelAdapterTests { // SocketUtils.setLocalNicIfPossible(adapter); adapter.start(); SocketTestUtils.waitListening(adapter); - + Message message = MessageBuilder.withPayload("ABCD".getBytes()).build(); DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper(); DatagramPacket packet = mapper.fromMessage(message); packet.setSocketAddress(new InetSocketAddress("localhost", port)); - new DatagramSocket(SocketTestUtils.findAvailableUdpSocket()).send(packet); + new DatagramSocket(SocketUtils.findAvailableUdpSocket()).send(packet); Message receivedMessage = (Message) channel.receive(2000); assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload())); } - + @SuppressWarnings("unchecked") @Test public void testUnicastSender() throws Exception { QueueChannel channel = new QueueChannel(2); - int port = SocketTestUtils.findAvailableUdpSocket(); + int port = SocketUtils.findAvailableUdpSocket(); UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(port); adapter.setOutputChannel(channel); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); @@ -64,8 +65,8 @@ public class UdpChannelAdapterTests { UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler( "localhost", port, false, true, "localhost", -// whichNic, - SocketTestUtils.findAvailableUdpSocket(), 5000); +// whichNic, + SocketUtils.findAvailableUdpSocket(), 5000); // handler.setLocalAddress(whichNic); handler.afterPropertiesSet(); Message message = MessageBuilder.withPayload("ABCD".getBytes()).build(); @@ -78,7 +79,7 @@ public class UdpChannelAdapterTests { @Test @Ignore public void testMulticastReceiver() throws Exception { QueueChannel channel = new QueueChannel(2); - int port = SocketTestUtils.findAvailableUdpSocket(); + int port = SocketUtils.findAvailableUdpSocket(); MulticastReceivingChannelAdapter adapter = new MulticastReceivingChannelAdapter("225.6.7.8", port); adapter.setOutputChannel(channel); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); @@ -92,7 +93,7 @@ public class UdpChannelAdapterTests { adapter.setLocalAddress(nic); adapter.start(); SocketTestUtils.waitListening(adapter); - + Message message = MessageBuilder.withPayload("ABCD".getBytes()).build(); DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper(); DatagramPacket packet = mapper.fromMessage(message); @@ -108,7 +109,7 @@ public class UdpChannelAdapterTests { @Test @Ignore public void testMulticastSender() throws Exception { QueueChannel channel = new QueueChannel(2); - int port = SocketTestUtils.findAvailableUdpSocket(); + int port = SocketUtils.findAvailableUdpSocket(); UnicastReceivingChannelAdapter adapter = new MulticastReceivingChannelAdapter("225.6.7.9", port); adapter.setOutputChannel(channel); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); @@ -122,12 +123,12 @@ public class UdpChannelAdapterTests { adapter.setLocalAddress(nic); adapter.start(); SocketTestUtils.waitListening(adapter); - + MulticastSendingMessageHandler handler = new MulticastSendingMessageHandler("225.6.7.9", port); handler.setLocalAddress(nic); Message message = MessageBuilder.withPayload("ABCD".getBytes()).build(); handler.handleMessage(message); - + Message receivedMessage = (Message) channel.receive(2000); assertNotNull(receivedMessage); assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload())); @@ -136,7 +137,7 @@ public class UdpChannelAdapterTests { @Test public void testUnicastReceiverException() throws Exception { SubscribableChannel channel = new DirectChannel(); - int port = SocketTestUtils.findAvailableUdpSocket(); + int port = SocketUtils.findAvailableUdpSocket(); UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(port); adapter.setOutputChannel(channel); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); @@ -150,17 +151,17 @@ public class UdpChannelAdapterTests { adapter.setErrorChannel(errorChannel); adapter.start(); SocketTestUtils.waitListening(adapter); - + Message message = MessageBuilder.withPayload("ABCD".getBytes()).build(); DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper(); DatagramPacket packet = mapper.fromMessage(message); packet.setSocketAddress(new InetSocketAddress("localhost", port)); - new DatagramSocket(SocketTestUtils.findAvailableUdpSocket()).send(packet); + new DatagramSocket(SocketUtils.findAvailableUdpSocket()).send(packet); Message receivedMessage = errorChannel.receive(2000); assertNotNull(receivedMessage); assertEquals("Failed", ((Exception) receivedMessage.getPayload()).getCause().getMessage()); } - + private class FailingService { @SuppressWarnings("unused") public String serviceMethod(byte[] bytes) { diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/util/SocketTestUtils.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/util/SocketTestUtils.java index f89e8bcf07..8efdca0271 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/util/SocketTestUtils.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/util/SocketTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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,16 +18,12 @@ package org.springframework.integration.ip.util; import java.io.ObjectOutputStream; import java.io.OutputStream; -import java.net.DatagramSocket; import java.net.InetAddress; -import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.util.concurrent.CountDownLatch; -import javax.net.ServerSocketFactory; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,8 +31,9 @@ import org.springframework.integration.ip.AbstractInternetProtocolReceivingChann /** * TCP/IP Test utilities. - * + * * @author Gary Russell + * @author Gunnar Hillert * */ public class SocketTestUtils { @@ -44,7 +41,7 @@ public class SocketTestUtils { public static final String TEST_STRING = "TestMessage"; private static final Log logger = LogFactory.getLog(SocketTestUtils.class); - + /** * Sends a message in two chunks with a preceding length. Two such messages are sent. * @param latch If not null, await until counted down before sending second chunk. @@ -137,7 +134,7 @@ public class SocketTestUtils { } Thread.sleep(500); } - + /** * Sends a STX/ETX message in two chunks. Two such messages are sent. * @param latch If not null, await until counted down before sending second chunk. @@ -170,7 +167,7 @@ public class SocketTestUtils { thread.setDaemon(true); thread.start(); } - + /** * Sends a large STX/ETX message with no ETX */ @@ -191,7 +188,7 @@ public class SocketTestUtils { thread.setDaemon(true); thread.start(); } - + /** * Sends a message +CRLF in two chunks. Two such messages are sent. * @param latch If not null, await until counted down before sending second chunk. @@ -317,37 +314,6 @@ public class SocketTestUtils { thread.setDaemon(true); thread.start(); } - - public static int findAvailableServerSocket(int seed) { - for (int i = seed; i < seed+200; i++) { - try { - ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(i); - sock.close(); - return i; - } catch (Exception e) { } - } - throw new RuntimeException("Cannot find a free server socket"); - } - - public static int findAvailableServerSocket() { - return findAvailableServerSocket(5678); - } - - public static int findAvailableUdpSocket(int seed) { - for (int i = seed; i < seed+200; i++) { - try { - DatagramSocket sock = new DatagramSocket(i); - sock.close(); - Thread.sleep(100); - return i; - } catch (Exception e) { } - } - throw new RuntimeException("Cannot find a free server socket"); - } - - public static int findAvailableUdpSocket() { - return findAvailableUdpSocket(9876); - } public static void setLocalNicIfPossible( AbstractInternetProtocolReceivingChannelAdapter adapter) @@ -359,22 +325,22 @@ public class SocketTestUtils { adapter.setLocalAddress(loopBack); } } - + public static String chooseANic(boolean multicast) throws Exception { -// Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); +// Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); // while (interfaces.hasMoreElements()) { // NetworkInterface intface = interfaces.nextElement(); // if (intface.isLoopback() || (multicast && !intface.supportsMulticast())) // continue; // Enumeration inet = intface.getInetAddresses(); -// if (!inet.hasMoreElements()) +// if (!inet.hasMoreElements()) // continue; // String address = inet.nextElement().getHostAddress(); // return address; // } return null; } - + public static void waitListening(AbstractInternetProtocolReceivingChannelAdapter adapter) throws Exception { int n = 0; while (!adapter.isListening()) { @@ -383,9 +349,7 @@ public class SocketTestUtils { throw new Exception("Gateway failed to listen"); } } - - } - + } } diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/util/SocketUtils.java b/spring-integration-test/src/main/java/org/springframework/integration/test/util/SocketUtils.java new file mode 100644 index 0000000000..e54f27cbc4 --- /dev/null +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/util/SocketUtils.java @@ -0,0 +1,214 @@ +/* + * Copyright 2002-2012 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 java.net.DatagramSocket; +import java.net.ServerSocket; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +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. + * + * @author Gunnar Hillert + * @since 2.2 + * + */ +public final class SocketUtils { + + public static final int DEFAULT_PORT_RANGE_MIN = 10000; + public static final int DEFAULT_PORT_RANGE_MAX = 60000; + + /** + * The constructor is intentionally public. In several test cases you may have + * the need to use the methods of this class multiple times from within your + * Spring Application Context XML file using SpEL. Of course you can do: + * + *
+	 * {@code
+	 * ...port="#{T(org.springframework.integration.test.util.SocketUtils).findAvailableServerSocket(12000)}"
+	 * }
+	 * 
+ * + * But unfortunately, you would need to repeat the package for each usage. + * This will be acceptable for single use, but if you need to invoke the + * methods numerous time, you may instead want to do this: + * + *
+	 * {@code
+	 * 
+	 *
+	 * ...port="#{tcpIpUtils.findAvailableServerSocket(12000)}"
+	 * }
+	 * 
+ * + */ + public SocketUtils() { } + + /** + * Determines a free available server socket (port) using the 'seed' value as + * the starting port. The utility methods will probe for 200 sockets but will + * return as soon an open port is found. + * + * @param seed The starting port, which must not be negative. + * @return An available port number + * + * @throws IllegalStateException when no open port was found. + */ + public static int findAvailableServerSocket(int seed) { + final List openPorts = findAvailableServerSockets(seed, 1); + return openPorts.get(0); + } + + /** + * Determines a free available server socket (port) using the 'seed' value as + * the starting port. The utility methods will probe for 200 sockets but will + * return as soon an open port is found. + * + * @param seed The starting port, which must not be negative. + * @param numberOfRequestedPorts How many open ports shall be retrieved? + * @return A list containing the requested number of open ports + * + * @throws IllegalStateException when no open port was found. + */ + public static List findAvailableServerSockets(int seed, int numberOfRequestedPorts) { + + Assert.isTrue(seed >= 0, "'seed' must not be negative"); + Assert.isTrue(numberOfRequestedPorts > 0, "'numberOfRequestedPorts' must not be negative"); + + final List openPorts = new ArrayList(numberOfRequestedPorts); + + for (int i = seed; i < seed+200; i++) { + try { + ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(i); + sock.close(); + openPorts.add(i); + + if (openPorts.size() == numberOfRequestedPorts) { + return openPorts; + } + + } catch (Exception e) { } + } + + throw new IllegalStateException(String.format("Cannot find a free server socket (%s requested)", numberOfRequestedPorts)); + } + + /** + * Determines a free available server socket (port) using an automatically + * chosen start seed port. + * + * @return An available port number + * + * @throws IllegalStateException when no open port was found. + */ + public static int findAvailableServerSocket() { + int seed = getRandomSeedPort(); + return findAvailableServerSocket(seed); + } + + /** + * Determines a free available Udp socket (port) using the 'seed' value as + * the starting port. The utility methods will probe for 200 sockets but will + * return as soon an open port is found. + * + * @param seed The starting port, which must not be negative. + * @return An available port number + * + * @throws IllegalStateException when no open port was found. + * + */ + public static int findAvailableUdpSocket(int seed) { + final List openPorts = findAvailableUdpSockets(seed, 1); + return openPorts.get(0); + } + + /** + * Determines free available udp socket(s) (port) using the 'seed' value as + * the starting port. The utility methods will probe for 200 sockets but will + * return as soon an open port is found. + * + * @param seed The starting port, which must not be negative. + * @param numberOfRequestedPorts How many open ports shall be retrieved? + * @return A list containing the requested number of open ports + * + * @throws IllegalStateException when no open port was found. + */ + public static List findAvailableUdpSockets(int seed, int numberOfRequestedPorts) { + + Assert.isTrue(seed >= 0, "'seed' must not be negative"); + Assert.isTrue(numberOfRequestedPorts > 0, "'numberOfRequestedPorts' must not be negative"); + + final List openPorts = new ArrayList(numberOfRequestedPorts); + + for (int i = seed; i < seed+200; i++) { + try { + DatagramSocket sock = new DatagramSocket(i); + sock.close(); + Thread.sleep(100); + + openPorts.add(i); + + if (openPorts.size() == numberOfRequestedPorts) { + return openPorts; + } + + } catch (Exception e) { } + } + + throw new IllegalStateException(String.format("Cannot find a free server socket (%s requested)", numberOfRequestedPorts)); + + } + + /** + * Determines a free available Udp socket using an automatically + * chosen start seed port. + * + * @return An available port number + * + * @throws IllegalStateException when no open port was found. + */ + public static int findAvailableUdpSocket() { + int seed = getRandomSeedPort(); + return findAvailableUdpSocket(seed); + } + + /** + * Determines a random seed port number within the port range + * {@value #DEFAULT_PORT_RANGE_MIN} and {@value #DEFAULT_PORT_RANGE_MAX}. + * + * @return A number with the the specified range + */ + public static int getRandomSeedPort() { + + final Random random = new Random(); + + int randomNumber = random.nextInt(DEFAULT_PORT_RANGE_MAX - DEFAULT_PORT_RANGE_MIN + 1) + DEFAULT_PORT_RANGE_MIN; + + return randomNumber; + } + +} diff --git a/spring-integration-test/src/test/java/org/springframework/integration/test/util/SocketUtilsTests.java b/spring-integration-test/src/test/java/org/springframework/integration/test/util/SocketUtilsTests.java new file mode 100644 index 0000000000..508354ac18 --- /dev/null +++ b/spring-integration-test/src/test/java/org/springframework/integration/test/util/SocketUtilsTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 2002-2012 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 junit.framework.Assert; + +import org.junit.Test; + +/** + * @author Gunnar Hillert + */ +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."); + } + +}