diff --git a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java index 48f6e488..da807e65 100644 --- a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java @@ -17,10 +17,13 @@ package org.springframework.integration.samples.tcpclientserver; import static org.junit.Assert.assertEquals; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader; +import org.springframework.integration.samples.tcpclientserver.support.ServerUtils; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -48,6 +51,14 @@ public class TcpClientServerDemoTest { @Autowired SimpleGateway gw; + @Autowired + AbstractServerConnectionFactory crLfServer; + + @Before + public void setup() { + ServerUtils.waitListening(this.crLfServer); + } + @Test public void testHappyDay() { String result = gw.send("Hello world!"); diff --git a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoWithConversionServiceTest.java b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoWithConversionServiceTest.java index 440efd0f..b54f2dd2 100644 --- a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoWithConversionServiceTest.java +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoWithConversionServiceTest.java @@ -17,10 +17,13 @@ package org.springframework.integration.samples.tcpclientserver; import static org.junit.Assert.assertEquals; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader; +import org.springframework.integration.samples.tcpclientserver.support.ServerUtils; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -48,6 +51,14 @@ public class TcpClientServerDemoWithConversionServiceTest { @Autowired SimpleGateway gw; + @Autowired + AbstractServerConnectionFactory crLfServer; + + @Before + public void setup() { + ServerUtils.waitListening(this.crLfServer); + } + @Test public void testHappyDay() { String result = gw.send("Hello world!"); diff --git a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerConnectionDeserializeTest.java b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerConnectionDeserializeTest.java index ebd6dbe0..21daec4a 100644 --- a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerConnectionDeserializeTest.java +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerConnectionDeserializeTest.java @@ -15,6 +15,7 @@ */ package org.springframework.integration.samples.tcpclientserver; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -23,8 +24,10 @@ import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.core.SubscribableChannel; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; +import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer; import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader; +import org.springframework.integration.samples.tcpclientserver.support.ServerUtils; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -56,6 +59,14 @@ public class TcpServerConnectionDeserializeTest { @Qualifier("incomingServerChannel") MessageChannel incomingServerChannel; + @Autowired + AbstractServerConnectionFactory crLfServer; + + @Before + public void setup() { + ServerUtils.waitListening(this.crLfServer); + } + @Test public void testHappyPath() { diff --git a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerCustomSerializerTest.java b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerCustomSerializerTest.java index ab917f90..a6afc136 100644 --- a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerCustomSerializerTest.java +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpServerCustomSerializerTest.java @@ -27,6 +27,7 @@ import java.io.Writer; import java.net.Socket; import org.apache.log4j.Logger; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -38,6 +39,7 @@ import org.springframework.integration.core.SubscribableChannel; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader; +import org.springframework.integration.samples.tcpclientserver.support.ServerUtils; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -70,6 +72,11 @@ public class TcpServerCustomSerializerTest { @Autowired AbstractServerConnectionFactory serverConnectionFactory; + @Before + public void setup() { + ServerUtils.waitListening(serverConnectionFactory); + } + @Test public void testHappyPath() { @@ -100,20 +107,6 @@ public class TcpServerCustomSerializerTest { Writer out = null; BufferedReader in = null; - int n = 0; - while (!serverConnectionFactory.isListening()) { - - try { - Thread.sleep(100); - } catch (InterruptedException e1) { - throw new IllegalStateException(e1); - } - - if (n++ > 100) { - fail("Server didn't begin listening."); - } - } - try { socket = new Socket("localhost", availableServerSocket); out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); @@ -146,4 +139,5 @@ public class TcpServerCustomSerializerTest { } } + } diff --git a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/support/ServerUtils.java b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/support/ServerUtils.java new file mode 100644 index 00000000..18ca7ca2 --- /dev/null +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/support/ServerUtils.java @@ -0,0 +1,44 @@ +/* + * 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.samples.tcpclientserver.support; + +import static org.junit.Assert.fail; + +import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; + +/** + * @author Gary Russell + * + */ +public class ServerUtils { + + public static void waitListening(AbstractServerConnectionFactory serverConnectionFactory) { + int n = 0; + while (!serverConnectionFactory.isListening()) { + + try { + Thread.sleep(100); + } catch (InterruptedException e1) { + throw new IllegalStateException(e1); + } + + if (n++ > 100) { + fail("Server didn't begin listening."); + } + } + } + +}