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:
@@ -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