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:
@@ -33,6 +33,11 @@
|
||||
<artifactId>spring-integration-ip</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-test</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,11 @@
|
||||
<artifactId>spring-integration-ip</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-test</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
|
||||
@@ -3,14 +3,18 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:ip="http://www.springframework.org/schema/integration/ip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
|
||||
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">
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<beans:description>
|
||||
Uses conversion service and collaborating channel adapters.
|
||||
</beans:description>
|
||||
|
||||
<context:property-placeholder />
|
||||
|
||||
<converter>
|
||||
<beans:bean class="org.springframework.integration.samples.tcpclientserver.ByteArrayToStringConverter" />
|
||||
</converter>
|
||||
@@ -31,7 +35,7 @@
|
||||
<ip:tcp-connection-factory id="client"
|
||||
type="client"
|
||||
host="localhost"
|
||||
port="11111"
|
||||
port="${availableServerSocket}"
|
||||
single-use="false"
|
||||
serializer="fastestWireFormatSerializer"
|
||||
deserializer="fastestWireFormatSerializer"
|
||||
@@ -71,7 +75,7 @@
|
||||
|
||||
<ip:tcp-connection-factory id="server"
|
||||
type="server"
|
||||
port="11111"
|
||||
port="${availableServerSocket}"
|
||||
using-nio="true"
|
||||
serializer="fastestWireFormatSerializer"
|
||||
deserializer="fastestWireFormatSerializer" />
|
||||
|
||||
@@ -30,7 +30,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
|
||||
import org.springframework.integration.samples.tcpclientserver.support.ServerUtils;
|
||||
import org.springframework.integration.ip.util.TestingUtilities;
|
||||
import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -46,8 +48,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @since 2.1
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration("/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml")
|
||||
@ContextConfiguration(loader=CustomTestContextLoader.class, locations={"/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
public class TcpClientServerDemoTest {
|
||||
|
||||
@Autowired
|
||||
@@ -58,7 +61,7 @@ public class TcpClientServerDemoTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ServerUtils.waitListening(this.crLfServer);
|
||||
TestingUtilities.waitListening(this.crLfServer, 10000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.util.HashMap;
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
public class CustomTestContextLoader extends GenericXmlContextLoader {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(CustomTestContextLoader.class);
|
||||
|
||||
@Override
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context,
|
||||
MergedContextConfiguration mergedConfig) {
|
||||
|
||||
int availableServerSocket = SocketUtils.findAvailableServerSocket(5678);
|
||||
|
||||
final Map<String, Object> sockets = new HashMap<String, Object>();
|
||||
sockets.put("availableServerSocket", availableServerSocket);
|
||||
|
||||
if (LOGGER.isInfoEnabled()) {
|
||||
LOGGER.info("Available Server Socket: " + availableServerSocket);
|
||||
}
|
||||
|
||||
final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);
|
||||
|
||||
context.getEnvironment().getPropertySources().addLast(propertySource);
|
||||
super.loadBeanDefinitions(context, mergedConfig);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +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) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(e1);
|
||||
}
|
||||
|
||||
if (n++ > 100) {
|
||||
fail("Server didn't begin listening.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user