INTSAMPLES-79 - Update TCP Samples to Use S-I-IP TestingUtilities
* Also: Don't use hard-coded port in *tcp-client-server-multiplex* sample For reference: https://jira.springsource.org/browse/INTSAMPLES-79
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* 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 java.net.ServerSocket;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
public final class SocketUtils {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,8 +22,8 @@ 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.ip.util.TestingUtilities;
|
||||
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;
|
||||
@@ -40,6 +40,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* Strings.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
// This one uses transformers
|
||||
@@ -56,7 +57,7 @@ public class TcpClientServerDemoTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ServerUtils.waitListening(this.crLfServer);
|
||||
TestingUtilities.waitListening(this.crLfServer, 10000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -22,8 +22,8 @@ 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.ip.util.TestingUtilities;
|
||||
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;
|
||||
@@ -41,6 +41,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* Strings.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration(loader=CustomTestContextLoader.class, locations={"/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml"})
|
||||
@@ -56,7 +57,7 @@ public class TcpClientServerDemoWithConversionServiceTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ServerUtils.waitListening(this.crLfServer);
|
||||
TestingUtilities.waitListening(this.crLfServer, 10000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -15,6 +15,10 @@
|
||||
*/
|
||||
package org.springframework.integration.samples.tcpclientserver;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -26,16 +30,12 @@ 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.ip.util.TestingUtilities;
|
||||
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;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Shows an example of using the Stx/Etx stream framing serializers that are included with
|
||||
* Spring Integration. We can be confident that the streams are properly handled because we
|
||||
@@ -45,6 +45,7 @@ import static org.junit.Assert.assertEquals;
|
||||
* we create and attach to the incomingServerChannel), does not have any of the Stx/Etx bytes.
|
||||
*
|
||||
* @author: ceposta
|
||||
* @author Gunnar Hillert
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader=CustomTestContextLoader.class,
|
||||
@@ -52,62 +53,62 @@ import static org.junit.Assert.assertEquals;
|
||||
@DirtiesContext
|
||||
public class TcpServerConnectionDeserializeTest {
|
||||
|
||||
@Autowired
|
||||
@Autowired
|
||||
SimpleGateway gw;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("incomingServerChannel")
|
||||
MessageChannel incomingServerChannel;
|
||||
@Autowired
|
||||
@Qualifier("incomingServerChannel")
|
||||
MessageChannel incomingServerChannel;
|
||||
|
||||
@Autowired
|
||||
AbstractServerConnectionFactory crLfServer;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ServerUtils.waitListening(this.crLfServer);
|
||||
TestingUtilities.waitListening(this.crLfServer, 10000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHappyPath() {
|
||||
@Test
|
||||
public void testHappyPath() {
|
||||
|
||||
// add a listener to this channel, otherwise there is not one defined
|
||||
// the reason we use a listener here is so we can assert truths on the
|
||||
// message and/or payload
|
||||
SubscribableChannel channel = (SubscribableChannel) incomingServerChannel;
|
||||
channel.subscribe(new AbstractReplyProducingMessageHandler(){
|
||||
// add a listener to this channel, otherwise there is not one defined
|
||||
// the reason we use a listener here is so we can assert truths on the
|
||||
// message and/or payload
|
||||
SubscribableChannel channel = (SubscribableChannel) incomingServerChannel;
|
||||
channel.subscribe(new AbstractReplyProducingMessageHandler(){
|
||||
|
||||
@Override
|
||||
protected Object handleRequestMessage(Message<?> requestMessage) {
|
||||
byte[] payload = (byte[]) requestMessage.getPayload();
|
||||
@Override
|
||||
protected Object handleRequestMessage(Message<?> requestMessage) {
|
||||
byte[] payload = (byte[]) requestMessage.getPayload();
|
||||
|
||||
// we assert during the processing of the messaging that the
|
||||
// payload is just the content we wanted to send without the
|
||||
// framing bytes (STX/ETX)
|
||||
assertEquals("Hello World!", new String(payload));
|
||||
return requestMessage;
|
||||
}
|
||||
});
|
||||
// we assert during the processing of the messaging that the
|
||||
// payload is just the content we wanted to send without the
|
||||
// framing bytes (STX/ETX)
|
||||
assertEquals("Hello World!", new String(payload));
|
||||
return requestMessage;
|
||||
}
|
||||
});
|
||||
|
||||
String sourceMessage = wrapWithStxEtx("Hello World!");
|
||||
String result = gw.send(sourceMessage);
|
||||
System.out.println(result);
|
||||
assertEquals("Hello World!", result);
|
||||
}
|
||||
String sourceMessage = wrapWithStxEtx("Hello World!");
|
||||
String result = gw.send(sourceMessage);
|
||||
System.out.println(result);
|
||||
assertEquals("Hello World!", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show, explicitly, how the stream would look if you had to manually create it.
|
||||
*
|
||||
* See more about TCP synchronous communication for more about framing the stream
|
||||
* with STX/ETX: http://en.wikipedia.org/wiki/Binary_Synchronous_Communications
|
||||
*
|
||||
* @param content
|
||||
* @return a string that is wrapped with the STX/ETX framing bytes
|
||||
*/
|
||||
private String wrapWithStxEtx(String content) {
|
||||
StringWriter writer = new StringWriter();
|
||||
writer.write(ByteArrayStxEtxSerializer.STX);
|
||||
writer.write(content);
|
||||
writer.write(ByteArrayStxEtxSerializer.ETX);
|
||||
return writer.toString();
|
||||
}
|
||||
/**
|
||||
* Show, explicitly, how the stream would look if you had to manually create it.
|
||||
*
|
||||
* See more about TCP synchronous communication for more about framing the stream
|
||||
* with STX/ETX: http://en.wikipedia.org/wiki/Binary_Synchronous_Communications
|
||||
*
|
||||
* @param content
|
||||
* @return a string that is wrapped with the STX/ETX framing bytes
|
||||
*/
|
||||
private String wrapWithStxEtx(String content) {
|
||||
StringWriter writer = new StringWriter();
|
||||
writer.write(ByteArrayStxEtxSerializer.STX);
|
||||
writer.write(content);
|
||||
writer.write(ByteArrayStxEtxSerializer.ETX);
|
||||
return writer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ 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.util.TestingUtilities;
|
||||
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;
|
||||
@@ -74,7 +74,7 @@ public class TcpServerCustomSerializerTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ServerUtils.waitListening(serverConnectionFactory);
|
||||
TestingUtilities.waitListening(this.serverConnectionFactory, 10000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Map;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.integration.test.util.SocketUtils;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.support.GenericXmlContextLoader;
|
||||
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user