PageRenderTime 115ms CodeModel.GetById 2ms app.highlight 103ms RepoModel.GetById 2ms app.codeStats 0ms

/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingMessageHandlerTests.java

https://github.com/nickspacek/spring-integration
Java | 1128 lines | 1077 code | 31 blank | 20 comment | 55 complexity | 7546dfeaebb48faff557ea39fdff64d6 MD5 | raw file
   1/*
   2 * Copyright 2002-2012 the original author or authors.
   3 *
   4 * Licensed under the Apache License, Version 2.0 (the "License");
   5 * you may not use this file except in compliance with the License.
   6 * You may obtain a copy of the License at
   7 *
   8 *      http://www.apache.org/licenses/LICENSE-2.0
   9 *
  10 * Unless required by applicable law or agreed to in writing, software
  11 * distributed under the License is distributed on an "AS IS" BASIS,
  12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 * See the License for the specific language governing permissions and
  14 * limitations under the License.
  15 */
  16
  17package org.springframework.integration.ip.tcp;
  18
  19import static org.junit.Assert.assertEquals;
  20import static org.junit.Assert.assertNotNull;
  21import static org.junit.Assert.assertTrue;
  22import static org.junit.Assert.fail;
  23import static org.mockito.Mockito.mock;
  24
  25import java.io.IOException;
  26import java.io.InputStream;
  27import java.io.ObjectInputStream;
  28import java.io.ObjectOutputStream;
  29import java.net.ServerSocket;
  30import java.net.Socket;
  31import java.net.SocketException;
  32import java.util.ArrayList;
  33import java.util.HashSet;
  34import java.util.List;
  35import java.util.Set;
  36import java.util.TreeSet;
  37import java.util.concurrent.CountDownLatch;
  38import java.util.concurrent.Executors;
  39import java.util.concurrent.Semaphore;
  40import java.util.concurrent.TimeUnit;
  41import java.util.concurrent.atomic.AtomicBoolean;
  42
  43import javax.net.ServerSocketFactory;
  44
  45import org.apache.commons.logging.Log;
  46import org.apache.commons.logging.LogFactory;
  47import org.junit.Test;
  48import org.mockito.Mockito;
  49import org.mockito.invocation.InvocationOnMock;
  50import org.mockito.stubbing.Answer;
  51import org.springframework.context.ApplicationContext;
  52import org.springframework.context.support.ClassPathXmlApplicationContext;
  53import org.springframework.core.serializer.DefaultDeserializer;
  54import org.springframework.core.serializer.DefaultSerializer;
  55import org.springframework.integration.Message;
  56import org.springframework.integration.MessageChannel;
  57import org.springframework.integration.MessagingException;
  58import org.springframework.integration.channel.QueueChannel;
  59import org.springframework.integration.core.PollableChannel;
  60import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
  61import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory;
  62import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
  63import org.springframework.integration.ip.tcp.connection.HelloWorldInterceptorFactory;
  64import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactory;
  65import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain;
  66import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory;
  67import org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory;
  68import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer;
  69import org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer;
  70import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer;
  71import org.springframework.integration.ip.util.TestingUtilities;
  72import org.springframework.integration.message.GenericMessage;
  73import org.springframework.integration.support.MessageBuilder;
  74import org.springframework.integration.test.util.SocketUtils;
  75import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  76
  77
  78/**
  79 * @author Gary Russell
  80 * @author Artem Bilan
  81 * @since 2.0
  82 */
  83public class TcpSendingMessageHandlerTests {
  84
  85	private static final Log logger = LogFactory.getLog(TcpSendingMessageHandlerTests.class);
  86
  87
  88	private void readFully(InputStream is, byte[] buff) throws IOException {
  89		for (int i = 0; i < buff.length; i++) {
  90			buff[i] = (byte) is.read();
  91		}
  92	}
  93
  94	@Test
  95	public void testNetCrLf() throws Exception {
  96		final int port = SocketUtils.findAvailableServerSocket();
  97		final CountDownLatch latch = new CountDownLatch(1);
  98		final AtomicBoolean done = new AtomicBoolean();
  99		Executors.newSingleThreadExecutor().execute(new Runnable() {
 100			public void run() {
 101				try {
 102					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 103					latch.countDown();
 104					Socket socket = server.accept();
 105					int i = 0;
 106					while (true) {
 107						byte[] b = new byte[6];
 108						readFully(socket.getInputStream(), b);
 109						b = ("Reply" + (++i) + "\r\n").getBytes();
 110						socket.getOutputStream().write(b);
 111					}
 112				}
 113				catch (Exception e) {
 114					if (!done.get()) {
 115						e.printStackTrace();
 116					}
 117				}
 118			}
 119		});
 120		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 121		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 122		ccf.setSerializer(serializer);
 123		ccf.setDeserializer(serializer);
 124		ccf.setSoTimeout(10000);
 125		ccf.start();
 126		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 127		handler.setConnectionFactory(ccf);
 128		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 129		adapter.setConnectionFactory(ccf);
 130		QueueChannel channel = new QueueChannel();
 131		adapter.setOutputChannel(channel);
 132		assertTrue(latch.await(10, TimeUnit.SECONDS));
 133		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 134		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 135		Message<?> mOut = channel.receive(10000);
 136		assertNotNull(mOut);
 137		assertEquals("Reply1", new String((byte[]) mOut.getPayload()));
 138		mOut = channel.receive(10000);
 139		assertNotNull(mOut);
 140		assertEquals("Reply2", new String((byte[]) mOut.getPayload()));
 141		done.set(true);
 142		ccf.stop();
 143	}
 144
 145	@Test
 146	public void testNetCrLfClientMode() throws Exception {
 147		final int port = SocketUtils.findAvailableServerSocket();
 148		final CountDownLatch latch = new CountDownLatch(1);
 149		final AtomicBoolean done = new AtomicBoolean();
 150		Executors.newSingleThreadExecutor().execute(new Runnable() {
 151			public void run() {
 152				try {
 153					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 154					latch.countDown();
 155					Socket socket = server.accept();
 156					int i = 0;
 157					while (true) {
 158						byte[] b = new byte[6];
 159						readFully(socket.getInputStream(), b);
 160						b = ("Reply" + (++i) + "\r\n").getBytes();
 161						socket.getOutputStream().write(b);
 162					}
 163				}
 164				catch (Exception e) {
 165					if (!done.get()) {
 166						e.printStackTrace();
 167					}
 168				}
 169			}
 170		});
 171		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 172		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 173		ccf.setSerializer(serializer);
 174		ccf.setDeserializer(serializer);
 175		ccf.setSoTimeout(Integer.MAX_VALUE);
 176		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 177		handler.setConnectionFactory(ccf);
 178		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 179		adapter.setConnectionFactory(ccf);
 180		QueueChannel channel = new QueueChannel();
 181		adapter.setOutputChannel(channel);
 182		assertTrue(latch.await(10, TimeUnit.SECONDS));
 183		handler.setClientMode(true);
 184		handler.setRetryInterval(10000);
 185		handler.afterPropertiesSet();
 186		ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
 187		taskScheduler.setPoolSize(1);
 188		taskScheduler.initialize();
 189		handler.setTaskScheduler(taskScheduler);
 190		handler.start();
 191		adapter.start();
 192		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 193		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 194		Message<?> mOut = channel.receive(10000);
 195		assertNotNull(mOut);
 196		assertEquals("Reply1", new String((byte[]) mOut.getPayload()));
 197		mOut = channel.receive(10000);
 198		assertNotNull(mOut);
 199		assertEquals("Reply2", new String((byte[]) mOut.getPayload()));
 200		done.set(true);
 201		handler.stop();
 202		handler.start();
 203		handler.stop();
 204	}
 205
 206	@Test
 207	public void testNioCrLf() throws Exception {
 208		final int port = SocketUtils.findAvailableServerSocket();
 209		final CountDownLatch latch = new CountDownLatch(1);
 210		final AtomicBoolean done = new AtomicBoolean();
 211		Executors.newSingleThreadExecutor().execute(new Runnable() {
 212			public void run() {
 213				try {
 214					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 215					latch.countDown();
 216					Socket socket = server.accept();
 217					int i = 0;
 218					while (true) {
 219						byte[] b = new byte[6];
 220						readFully(socket.getInputStream(), b);
 221						b = ("Reply" + (++i) + "\r\n").getBytes();
 222						socket.getOutputStream().write(b);
 223					}
 224				} catch (Exception e) {
 225					if (!done.get()) {
 226						e.printStackTrace();
 227					}
 228				}
 229			}
 230		});
 231		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 232		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 233		ccf.setSerializer(serializer);
 234		ccf.setDeserializer(serializer);
 235		ccf.setSoTimeout(10000);
 236		ccf.start();
 237		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 238		handler.setConnectionFactory(ccf);
 239		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 240		adapter.setConnectionFactory(ccf);
 241		QueueChannel channel = new QueueChannel();
 242		adapter.setOutputChannel(channel);
 243		assertTrue(latch.await(10, TimeUnit.SECONDS));
 244		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 245		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 246		Set<String> results = new HashSet<String>();
 247		Message<?> mOut = channel.receive(10000);
 248		assertNotNull(mOut);
 249		results.add(new String((byte[]) mOut.getPayload()));
 250		mOut = channel.receive(10000);
 251		assertNotNull(mOut);
 252		results.add(new String((byte[]) mOut.getPayload()));
 253		assertTrue(results.remove("Reply1"));
 254		assertTrue(results.remove("Reply2"));
 255		done.set(true);
 256		ccf.stop();
 257	}
 258
 259	@Test
 260	public void testNetStxEtx() throws Exception {
 261		final int port = SocketUtils.findAvailableServerSocket();
 262		final CountDownLatch latch = new CountDownLatch(1);
 263		final AtomicBoolean done = new AtomicBoolean();
 264		Executors.newSingleThreadExecutor().execute(new Runnable() {
 265			public void run() {
 266				try {
 267					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 268					latch.countDown();
 269					Socket socket = server.accept();
 270					int i = 0;
 271					while (true) {
 272						byte[] b = new byte[6];
 273						readFully(socket.getInputStream(), b);
 274						b = ("\u0002Reply" + (++i) + "\u0003").getBytes();
 275						socket.getOutputStream().write(b);
 276					}
 277				} catch (Exception e) {
 278					if (!done.get()) {
 279						e.printStackTrace();
 280					}
 281				}
 282			}
 283		});
 284		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 285		ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
 286		ccf.setSerializer(serializer);
 287		ccf.setDeserializer(serializer);
 288		ccf.setSoTimeout(10000);
 289		ccf.start();
 290		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 291		handler.setConnectionFactory(ccf);
 292		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 293		adapter.setConnectionFactory(ccf);
 294		QueueChannel channel = new QueueChannel();
 295		adapter.setOutputChannel(channel);
 296		assertTrue(latch.await(10, TimeUnit.SECONDS));
 297		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 298		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 299		Message<?> mOut = channel.receive(10000);
 300		assertNotNull(mOut);
 301		assertEquals("Reply1", new String((byte[]) mOut.getPayload()));
 302		mOut = channel.receive(10000);
 303		assertNotNull(mOut);
 304		assertEquals("Reply2", new String((byte[]) mOut.getPayload()));
 305		done.set(true);
 306		ccf.stop();
 307	}
 308
 309	@Test
 310	public void testNioStxEtx() throws Exception {
 311		final int port = SocketUtils.findAvailableServerSocket();
 312		final CountDownLatch latch = new CountDownLatch(1);
 313		final AtomicBoolean done = new AtomicBoolean();
 314		Executors.newSingleThreadExecutor().execute(new Runnable() {
 315			public void run() {
 316				try {
 317					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 318					latch.countDown();
 319					Socket socket = server.accept();
 320					int i = 0;
 321					while (true) {
 322						byte[] b = new byte[6];
 323						readFully(socket.getInputStream(), b);
 324						b = ("\u0002Reply" + (++i) + "\u0003").getBytes();
 325						socket.getOutputStream().write(b);
 326					}
 327				} catch (Exception e) {
 328					if (!done.get()) {
 329						e.printStackTrace();
 330					}
 331				}
 332			}
 333		});
 334		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 335		ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer();
 336		ccf.setSerializer(serializer);
 337		ccf.setDeserializer(serializer);
 338		ccf.setSoTimeout(10000);
 339		ccf.start();
 340		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 341		handler.setConnectionFactory(ccf);
 342		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 343		adapter.setConnectionFactory(ccf);
 344		QueueChannel channel = new QueueChannel();
 345		adapter.setOutputChannel(channel);
 346		assertTrue(latch.await(10, TimeUnit.SECONDS));
 347		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 348		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 349		Set<String> results = new HashSet<String>();
 350		Message<?> mOut = channel.receive(10000);
 351		assertNotNull(mOut);
 352		results.add(new String((byte[]) mOut.getPayload()));
 353		mOut = channel.receive(10000);
 354		assertNotNull(mOut);
 355		results.add(new String((byte[]) mOut.getPayload()));
 356		assertTrue(results.remove("Reply1"));
 357		assertTrue(results.remove("Reply2"));
 358		done.set(true);
 359		ccf.stop();
 360	}
 361
 362	@Test
 363	public void testNetLength() throws Exception {
 364		final int port = SocketUtils.findAvailableServerSocket();
 365		final CountDownLatch latch = new CountDownLatch(1);
 366		final AtomicBoolean done = new AtomicBoolean();
 367		Executors.newSingleThreadExecutor().execute(new Runnable() {
 368			public void run() {
 369				try {
 370					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 371					latch.countDown();
 372					Socket socket = server.accept();
 373					int i = 0;
 374					while (true) {
 375						byte[] b = new byte[8];
 376						readFully(socket.getInputStream(), b);
 377						if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
 378							throw new RuntimeException("Bad Data");
 379						}
 380						b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
 381						socket.getOutputStream().write(b);
 382					}
 383				} catch (Exception e) {
 384					if (!done.get()) {
 385						e.printStackTrace();
 386					}
 387				}
 388			}
 389		});
 390		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 391		ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
 392		ccf.setSerializer(serializer);
 393		ccf.setDeserializer(serializer);
 394		ccf.setSoTimeout(10000);
 395		ccf.start();
 396		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 397		handler.setConnectionFactory(ccf);
 398		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 399		adapter.setConnectionFactory(ccf);
 400		QueueChannel channel = new QueueChannel();
 401		adapter.setOutputChannel(channel);
 402		assertTrue(latch.await(10, TimeUnit.SECONDS));
 403		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 404		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 405		Message<?> mOut = channel.receive(10000);
 406		assertNotNull(mOut);
 407		assertEquals("Reply1", new String((byte[]) mOut.getPayload()));
 408		mOut = channel.receive(10000);
 409		assertNotNull(mOut);
 410		assertEquals("Reply2", new String((byte[]) mOut.getPayload()));
 411		done.set(true);
 412		ccf.stop();
 413	}
 414
 415	@Test
 416	public void testNioLength() throws Exception {
 417		final int port = SocketUtils.findAvailableServerSocket();
 418		final CountDownLatch latch = new CountDownLatch(1);
 419		final AtomicBoolean done = new AtomicBoolean();
 420		Executors.newSingleThreadExecutor().execute(new Runnable() {
 421			public void run() {
 422				try {
 423					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 424					latch.countDown();
 425					Socket socket = server.accept();
 426					int i = 0;
 427					while (true) {
 428						byte[] b = new byte[8];
 429						readFully(socket.getInputStream(), b);
 430						if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
 431							throw new RuntimeException("Bad Data");
 432						}
 433						b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
 434						socket.getOutputStream().write(b);
 435					}
 436				} catch (Exception e) {
 437					if (!done.get()) {
 438						e.printStackTrace();
 439					}
 440				}
 441			}
 442		});
 443		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 444		ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
 445		ccf.setSerializer(serializer);
 446		ccf.setDeserializer(serializer);
 447		ccf.setSoTimeout(10000);
 448		ccf.start();
 449		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 450		handler.setConnectionFactory(ccf);
 451		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 452		adapter.setConnectionFactory(ccf);
 453		QueueChannel channel = new QueueChannel();
 454		adapter.setOutputChannel(channel);
 455		assertTrue(latch.await(10, TimeUnit.SECONDS));
 456		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 457		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 458		Set<String> results = new HashSet<String>();
 459		Message<?> mOut = channel.receive(10000);
 460		assertNotNull(mOut);
 461		results.add(new String((byte[]) mOut.getPayload()));
 462		mOut = channel.receive(10000);
 463		assertNotNull(mOut);
 464		results.add(new String((byte[]) mOut.getPayload()));
 465		assertTrue(results.remove("Reply1"));
 466		assertTrue(results.remove("Reply2"));
 467		done.set(true);
 468		ccf.stop();
 469	}
 470
 471	@Test
 472	public void testNetSerial() throws Exception {
 473		final int port = SocketUtils.findAvailableServerSocket();
 474		final CountDownLatch latch = new CountDownLatch(1);
 475		final AtomicBoolean done = new AtomicBoolean();
 476		Executors.newSingleThreadExecutor().execute(new Runnable() {
 477			public void run() {
 478				try {
 479					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 480					latch.countDown();
 481					Socket socket = server.accept();
 482					int i = 0;
 483					while (true) {
 484						ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 485						ois.readObject();
 486						ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
 487						oos.writeObject("Reply" + (++i));
 488					}
 489				} catch (Exception e) {
 490					if (!done.get()) {
 491						e.printStackTrace();
 492					}
 493				}
 494			}
 495		});
 496		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 497		ccf.setSerializer(new DefaultSerializer());
 498		ccf.setDeserializer(new DefaultDeserializer());
 499		ccf.setSoTimeout(10000);
 500		ccf.start();
 501		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 502		handler.setConnectionFactory(ccf);
 503		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 504		adapter.setConnectionFactory(ccf);
 505		QueueChannel channel = new QueueChannel();
 506		adapter.setOutputChannel(channel);
 507		assertTrue(latch.await(10, TimeUnit.SECONDS));
 508		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 509		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 510		Message<?> mOut = channel.receive(10000);
 511		assertNotNull(mOut);
 512		assertEquals("Reply1", mOut.getPayload());
 513		mOut = channel.receive(10000);
 514		assertNotNull(mOut);
 515		assertEquals("Reply2", mOut.getPayload());
 516		done.set(true);
 517		ccf.stop();
 518	}
 519
 520	@Test
 521	public void testNioSerial() throws Exception {
 522		final int port = SocketUtils.findAvailableServerSocket();
 523		final CountDownLatch latch = new CountDownLatch(1);
 524		final AtomicBoolean done = new AtomicBoolean();
 525		Executors.newSingleThreadExecutor().execute(new Runnable() {
 526			public void run() {
 527				try {
 528					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 529					latch.countDown();
 530					Socket socket = server.accept();
 531					int i = 0;
 532					while (true) {
 533						ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 534						ois.readObject();
 535						ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
 536						oos.writeObject("Reply" + (++i));
 537					}
 538				} catch (Exception e) {
 539					if (!done.get()) {
 540						e.printStackTrace();
 541					}
 542				}
 543			}
 544		});
 545		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 546		ccf.setSerializer(new DefaultSerializer());
 547		ccf.setDeserializer(new DefaultDeserializer());
 548		ccf.setSoTimeout(10000);
 549		ccf.start();
 550		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 551		handler.setConnectionFactory(ccf);
 552		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 553		adapter.setConnectionFactory(ccf);
 554		QueueChannel channel = new QueueChannel();
 555		adapter.setOutputChannel(channel);
 556		assertTrue(latch.await(10, TimeUnit.SECONDS));
 557		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 558		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 559		Set<String> results = new HashSet<String>();
 560		Message<?> mOut = channel.receive(10000);
 561		assertNotNull(mOut);
 562		results.add((String) mOut.getPayload());
 563		mOut = channel.receive(10000);
 564		assertNotNull(mOut);
 565		results.add((String) mOut.getPayload());
 566		assertTrue(results.remove("Reply1"));
 567		assertTrue(results.remove("Reply2"));
 568		done.set(true);
 569		ccf.stop();
 570	}
 571
 572	@Test
 573	public void testNetSingleUseNoInbound() throws Exception  {
 574		final int port = SocketUtils.findAvailableServerSocket();
 575		final CountDownLatch latch = new CountDownLatch(1);
 576		final Semaphore semaphore = new Semaphore(0);
 577		final AtomicBoolean done = new AtomicBoolean();
 578		Executors.newSingleThreadExecutor().execute(new Runnable() {
 579			public void run() {
 580				try {
 581					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 582					latch.countDown();
 583					for (int i = 0; i < 2; i++) {
 584						Socket socket = server.accept();
 585						semaphore.release();
 586						byte[] b = new byte[6];
 587						readFully(socket.getInputStream(), b);
 588						semaphore.release();
 589						socket.close();
 590					}
 591					server.close();
 592				} catch (Exception e) {
 593					if (!done.get()) {
 594						e.printStackTrace();
 595					}
 596				}
 597			}
 598		});
 599		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 600		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 601		ccf.setSerializer(serializer);
 602		ccf.setDeserializer(serializer);
 603		ccf.setSoTimeout(10000);
 604		ccf.start();
 605		ccf.setSingleUse(true);
 606		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 607		handler.setConnectionFactory(ccf);
 608		assertTrue(latch.await(10, TimeUnit.SECONDS));
 609		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 610		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 611		assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
 612		done.set(true);
 613		ccf.stop();
 614	}
 615
 616	@Test
 617	public void testNioSingleUseNoInbound() throws Exception  {
 618		final int port = SocketUtils.findAvailableServerSocket();
 619		final CountDownLatch latch = new CountDownLatch(1);
 620		final Semaphore semaphore = new Semaphore(0);
 621		final AtomicBoolean done = new AtomicBoolean();
 622		Executors.newSingleThreadExecutor().execute(new Runnable() {
 623			public void run() {
 624				try {
 625					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 626					latch.countDown();
 627					for (int i = 0; i < 2; i++) {
 628						Socket socket = server.accept();
 629						semaphore.release();
 630						byte[] b = new byte[8];
 631						readFully(socket.getInputStream(), b);
 632						semaphore.release();
 633						socket.close();
 634					}
 635					server.close();
 636				} catch (Exception e) {
 637					if (!done.get()) {
 638						e.printStackTrace();
 639					}
 640				}
 641			}
 642		});
 643		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 644		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 645		ccf.setSerializer(serializer);
 646		ccf.setDeserializer(serializer);
 647		ccf.setSoTimeout(5000);
 648		ccf.start();
 649		ccf.setSingleUse(true);
 650		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 651		handler.setConnectionFactory(ccf);
 652		assertTrue(latch.await(10, TimeUnit.SECONDS));
 653		handler.handleMessage(MessageBuilder.withPayload("Test.1").build());
 654		handler.handleMessage(MessageBuilder.withPayload("Test.2").build());
 655		assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
 656		done.set(true);
 657		ccf.stop();
 658	}
 659
 660	@Test
 661	public void testNetSingleUseWithInbound() throws Exception  {
 662		final int port = SocketUtils.findAvailableServerSocket();
 663		final CountDownLatch latch = new CountDownLatch(1);
 664		final Semaphore semaphore = new Semaphore(0);
 665		final AtomicBoolean done = new AtomicBoolean();
 666		Executors.newSingleThreadExecutor().execute(new Runnable() {
 667			public void run() {
 668				try {
 669					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 670					latch.countDown();
 671					for (int i = 1; i < 3; i++) {
 672						Socket socket = server.accept();
 673						semaphore.release();
 674						byte[] b = new byte[6];
 675						readFully(socket.getInputStream(), b);
 676						b = ("Reply" + i + "\r\n").getBytes();
 677						socket.getOutputStream().write(b);
 678						socket.close();
 679					}
 680					server.close();
 681				} catch (Exception e) {
 682					if (!done.get()) {
 683						e.printStackTrace();
 684					}
 685				}
 686			}
 687		});
 688		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 689		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 690		ccf.setSerializer(serializer);
 691		ccf.setDeserializer(serializer);
 692		ccf.setSoTimeout(10000);
 693		ccf.start();
 694		ccf.setSingleUse(true);
 695		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 696		handler.setConnectionFactory(ccf);
 697		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 698		adapter.setConnectionFactory(ccf);
 699		QueueChannel channel = new QueueChannel();
 700		adapter.setOutputChannel(channel);
 701		assertTrue(latch.await(10, TimeUnit.SECONDS));
 702		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 703		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 704		assertTrue(semaphore.tryAcquire(2, 10000, TimeUnit.MILLISECONDS));
 705		Set<String> replies = new HashSet<String>();
 706		for (int i = 0; i < 2; i++) {
 707			Message<?> mOut = channel.receive(10000);
 708			assertNotNull(mOut);
 709			replies.add(new String((byte[])mOut.getPayload()));
 710		}
 711		assertTrue(replies.remove("Reply1"));
 712		assertTrue(replies.remove("Reply2"));
 713		done.set(true);
 714		ccf.stop();
 715	}
 716
 717	@Test
 718	public void testNioSingleUseWithInbound() throws Exception  {
 719		final int port = SocketUtils.findAvailableServerSocket();
 720		final CountDownLatch latch = new CountDownLatch(1);
 721		final Semaphore semaphore = new Semaphore(0);
 722		final AtomicBoolean done = new AtomicBoolean();
 723		Executors.newSingleThreadExecutor().execute(new Runnable() {
 724			public void run() {
 725				try {
 726					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 727					latch.countDown();
 728					for (int i = 1; i < 3; i++) {
 729						Socket socket = server.accept();
 730						semaphore.release();
 731						byte[] b = new byte[6];
 732						readFully(socket.getInputStream(), b);
 733						b = ("Reply" + i + "\r\n").getBytes();
 734						socket.getOutputStream().write(b);
 735						socket.close();
 736					}
 737					server.close();
 738				} catch (Exception e) {
 739					if (!done.get()) {
 740						e.printStackTrace();
 741					}
 742				}
 743			}
 744		});
 745		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 746		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 747		ccf.setSerializer(serializer);
 748		ccf.setDeserializer(serializer);
 749		ccf.setSoTimeout(10000);
 750		ccf.start();
 751		ccf.setSingleUse(true);
 752		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 753		handler.setConnectionFactory(ccf);
 754		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 755		adapter.setConnectionFactory(ccf);
 756		QueueChannel channel = new QueueChannel();
 757		adapter.setOutputChannel(channel);
 758		assertTrue(latch.await(10, TimeUnit.SECONDS));
 759		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 760		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 761		assertTrue(semaphore.tryAcquire(2, 10000, TimeUnit.MILLISECONDS));
 762		Set<String> replies = new HashSet<String>();
 763		for (int i = 0; i < 2; i++) {
 764			Message<?> mOut = channel.receive(10000);
 765			assertNotNull(mOut);
 766			replies.add(new String((byte[])mOut.getPayload()));
 767		}
 768		assertTrue(replies.remove("Reply1"));
 769		assertTrue(replies.remove("Reply2"));
 770		done.set(true);
 771		ccf.stop();
 772	}
 773
 774	@Test
 775	public void testNioSingleUseWithInboundMany() throws Exception  {
 776		final int port = SocketUtils.findAvailableServerSocket();
 777		final CountDownLatch latch = new CountDownLatch(1);
 778		final Semaphore semaphore = new Semaphore(0);
 779		final AtomicBoolean done = new AtomicBoolean();
 780		final List<Socket> serverSockets = new ArrayList<Socket>();
 781		Executors.newSingleThreadExecutor().execute(new Runnable() {
 782			public void run() {
 783				try {
 784					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port, 100);
 785					latch.countDown();
 786					for (int i = 0; i < 100; i++) {
 787						Socket socket = server.accept();
 788						serverSockets.add(socket);
 789						semaphore.release();
 790						byte[] b = new byte[9];
 791						readFully(socket.getInputStream(), b);
 792						b = ("Reply" + i + "\r\n").getBytes();
 793						socket.getOutputStream().write(b);
 794						socket.close();
 795					}
 796					server.close();
 797				} catch (Exception e) {
 798					if (!done.get()) {
 799						e.printStackTrace();
 800					}
 801				}
 802			}
 803		});
 804		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 805		ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
 806		ccf.setSerializer(serializer);
 807		ccf.setDeserializer(serializer);
 808		ccf.setSoTimeout(10000);
 809		ccf.setSingleUse(true);
 810		ccf.setTaskExecutor(Executors.newFixedThreadPool(100));
 811		ccf.start();
 812		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 813		handler.setConnectionFactory(ccf);
 814		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 815		adapter.setConnectionFactory(ccf);
 816		QueueChannel channel = new QueueChannel();
 817		adapter.setOutputChannel(channel);
 818		assertTrue(latch.await(10, TimeUnit.SECONDS));
 819		int i = 0;
 820		try {
 821			for (i = 100; i < 200; i++) {
 822				handler.handleMessage(MessageBuilder.withPayload("Test" + i).build());
 823			}
 824		} catch (Exception e) {
 825			e.printStackTrace();
 826			fail("Exception at " + i);
 827		}
 828		assertTrue(semaphore.tryAcquire(100, 20000, TimeUnit.MILLISECONDS));
 829		Set<String> replies = new HashSet<String>();
 830		for (i = 100; i < 200; i++) {
 831			Message<?> mOut = channel.receive(20000);
 832			assertNotNull(mOut);
 833			replies.add(new String((byte[])mOut.getPayload()));
 834		}
 835		for (i = 0; i < 100; i++) {
 836			assertTrue("Reply" + i + " missing", replies.remove("Reply" + i));
 837		}
 838		done.set(true);
 839		ccf.stop();
 840	}
 841
 842	@Test
 843	public void testNetNegotiate() throws Exception {
 844		final int port = SocketUtils.findAvailableServerSocket();
 845		final CountDownLatch latch = new CountDownLatch(1);
 846		final AtomicBoolean done = new AtomicBoolean();
 847		Executors.newSingleThreadExecutor().execute(new Runnable() {
 848			public void run() {
 849				try {
 850					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 851					latch.countDown();
 852					Socket socket = server.accept();
 853					int i = 0;
 854					while (true) {
 855						ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 856						Object in = null;
 857						ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
 858						if (i == 0) {
 859							in = ois.readObject();
 860							logger.debug("read object: " + in);
 861							oos.writeObject("world!");
 862							ois = new ObjectInputStream(socket.getInputStream());
 863							oos = new ObjectOutputStream(socket.getOutputStream());
 864							in = ois.readObject();
 865							logger.debug("read object: " + in);
 866							oos.writeObject("world!");
 867							ois = new ObjectInputStream(socket.getInputStream());
 868							oos = new ObjectOutputStream(socket.getOutputStream());
 869						}
 870						in = ois.readObject();
 871						oos.writeObject("Reply" + (++i));
 872					}
 873				}
 874				catch (Exception e) {
 875					if (!done.get()) {
 876						e.printStackTrace();
 877					}
 878				}
 879			}
 880		});
 881		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
 882		ccf.setSerializer(new DefaultSerializer());
 883		ccf.setDeserializer(new DefaultDeserializer());
 884		ccf.setSoTimeout(10000);
 885		TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
 886		fc.setInterceptors(new TcpConnectionInterceptorFactory[]
 887		     {new HelloWorldInterceptorFactory(),
 888		      new HelloWorldInterceptorFactory()});
 889		ccf.setInterceptorFactoryChain(fc);
 890		ccf.start();
 891		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 892		handler.setConnectionFactory(ccf);
 893		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 894		adapter.setConnectionFactory(ccf);
 895		QueueChannel channel = new QueueChannel();
 896		adapter.setOutputChannel(channel);
 897		assertTrue(latch.await(10, TimeUnit.SECONDS));
 898		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 899		handler.handleMessage(MessageBuilder.withPayload("Test").build());
 900		Message<?> mOut = channel.receive(10000);
 901		assertNotNull(mOut);
 902		assertEquals("Reply1", mOut.getPayload());
 903		mOut = channel.receive(10000);
 904		assertNotNull(mOut);
 905		assertEquals("Reply2", mOut.getPayload());
 906		done.set(true);
 907		ccf.stop();
 908	}
 909
 910	@Test
 911	public void testNioNegotiate() throws Exception {
 912		final int port = SocketUtils.findAvailableServerSocket();
 913		final CountDownLatch latch = new CountDownLatch(1);
 914		final AtomicBoolean done = new AtomicBoolean();
 915		Executors.newSingleThreadExecutor().execute(new Runnable() {
 916			public void run() {
 917				try {
 918					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 919					latch.countDown();
 920					Socket socket = server.accept();
 921					int i = 100;
 922					while (true) {
 923						ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 924						Object in;
 925						ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
 926						if (i == 100) {
 927							in = ois.readObject();
 928							logger.debug("read object: " + in);
 929							oos.writeObject("world!");
 930							ois = new ObjectInputStream(socket.getInputStream());
 931							oos = new ObjectOutputStream(socket.getOutputStream());
 932							Thread.sleep(500);
 933						}
 934						in = ois.readObject();
 935						oos.writeObject("Reply" + (i++));
 936					}
 937				} catch (Exception e) {
 938					if (!done.get()) {
 939						e.printStackTrace();
 940					}
 941				}
 942			}
 943		});
 944		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
 945		ccf.setSerializer(new DefaultSerializer());
 946		ccf.setDeserializer(new DefaultDeserializer());
 947		ccf.setSoTimeout(10000);
 948		TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
 949		fc.setInterceptors(new TcpConnectionInterceptorFactory[] {new HelloWorldInterceptorFactory()});
 950		ccf.setInterceptorFactoryChain(fc);
 951		ccf.start();
 952		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
 953		handler.setConnectionFactory(ccf);
 954		TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
 955		adapter.setConnectionFactory(ccf);
 956		QueueChannel channel = new QueueChannel();
 957		adapter.setOutputChannel(channel);
 958		assertTrue(latch.await(10, TimeUnit.SECONDS));
 959		for (int i = 0; i < 1000; i++) {
 960			handler.handleMessage(MessageBuilder.withPayload("Test").build());
 961		}
 962		Set<String> results = new TreeSet<String>();
 963		for (int i = 0; i < 1000; i++) {
 964			Message<?> mOut = channel.receive(10000);
 965			assertNotNull(mOut);
 966			results.add((String) mOut.getPayload());
 967		}
 968		logger.debug("results: " + results);
 969		for (int i = 100; i < 1100; i++) {
 970			assertTrue("Missing Reply" + i, results.remove("Reply" + i));
 971		}
 972		done.set(true);
 973		ccf.stop();
 974	}
 975
 976	@Test
 977	public void testNetNegotiateSingleNoListen() throws Exception {
 978		final int port = SocketUtils.findAvailableServerSocket();
 979		final CountDownLatch latch = new CountDownLatch(1);
 980		final AtomicBoolean done = new AtomicBoolean();
 981		Executors.newSingleThreadExecutor().execute(new Runnable() {
 982			public void run() {
 983				try {
 984					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
 985					latch.countDown();
 986					Socket socket = server.accept();
 987					int i = 0;
 988					ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 989					Object in = null;
 990					ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
 991					if (i == 0) {
 992						in = ois.readObject();
 993						logger.debug("read object: " + in);
 994						oos.writeObject("world!");
 995						ois = new ObjectInputStream(socket.getInputStream());
 996						oos = new ObjectOutputStream(socket.getOutputStream());
 997						in = ois.readObject();
 998						logger.debug("read object: " + in);
 999						oos.writeObject("world!");
1000						ois = new ObjectInputStream(socket.getInputStream());
1001						oos = new ObjectOutputStream(socket.getOutputStream());
1002					}
1003					in = ois.readObject();
1004					oos.writeObject("Reply" + (++i));
1005					socket.close();
1006					server.close();
1007				}
1008				catch (Exception e) {
1009					if (!done.get()) {
1010						e.printStackTrace();
1011					}
1012				}
1013			}
1014		});
1015		AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
1016		ccf.setSerializer(new DefaultSerializer());
1017		ccf.setDeserializer(new DefaultDeserializer());
1018		ccf.setSoTimeout(10000);
1019		TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
1020		fc.setInterceptors(new TcpConnectionInterceptorFactory[]
1021            		     {new HelloWorldInterceptorFactory(),
1022               		      new HelloWorldInterceptorFactory()});
1023		ccf.setInterceptorFactoryChain(fc);
1024		ccf.setSingleUse(true);
1025		ccf.start();
1026		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
1027		handler.setConnectionFactory(ccf);
1028		assertTrue(latch.await(10, TimeUnit.SECONDS));
1029		handler.handleMessage(MessageBuilder.withPayload("Test").build());
1030		done.set(true);
1031		ccf.stop();
1032	}
1033
1034	@Test
1035	public void testNioNegotiateSingleNoListen() throws Exception {
1036		final int port = SocketUtils.findAvailableServerSocket();
1037		final CountDownLatch latch = new CountDownLatch(1);
1038		final AtomicBoolean done = new AtomicBoolean();
1039		Executors.newSingleThreadExecutor().execute(new Runnable() {
1040			public void run() {
1041				int i = 0;
1042				try {
1043					ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
1044					latch.countDown();
1045					Socket socket = server.accept();
1046					ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
1047					Object in = null;
1048					ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
1049					if (i == 0) {
1050						in = ois.readObject();
1051						logger.debug("read object: " + in);
1052						oos.writeObject("world!");
1053						ois = new ObjectInputStream(socket.getInputStream());
1054						oos = new ObjectOutputStream(socket.getOutputStream());
1055						in = ois.readObject();
1056						logger.debug("read object: " + in);
1057						oos.writeObject("world!");
1058						ois = new ObjectInputStream(socket.getInputStream());
1059						oos = new ObjectOutputStream(socket.getOutputStream());
1060					}
1061					in = ois.readObject();
1062					oos.writeObject("Reply" + (++i));
1063					socket.close();
1064					server.close();
1065				} catch (Exception e) {
1066					if (i == 0) {
1067						e.printStackTrace();
1068					}
1069				}
1070			}
1071		});
1072		AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
1073		ccf.setSerializer(new DefaultSerializer());
1074		ccf.setDeserializer(new DefaultDeserializer());
1075		ccf.setSoTimeout(10000);
1076		TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
1077		fc.setInterceptors(new TcpConnectionInterceptorFactory[]
1078            		     {new HelloWorldInterceptorFactory(),
1079               		      new HelloWorldInterceptorFactory()});
1080		ccf.setInterceptorFactoryChain(fc);
1081		ccf.setSingleUse(true);
1082		ccf.start();
1083		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
1084		handler.setConnectionFactory(ccf);
1085		assertTrue(latch.await(10, TimeUnit.SECONDS));
1086		handler.handleMessage(MessageBuilder.withPayload("Test").build());
1087		done.set(true);
1088		ccf.stop();
1089	}
1090
1091	@Test
1092	public void testOutboundChannelAdapterWithinChain() throws Exception {
1093		ApplicationContext ctx = new ClassPathXmlApplicationContext(
1094				"TcpOutboundChannelAdapterWithinChainTests-context.xml", this.getClass());
1095		AbstractServerConnectionFactory scf = ctx.getBean(AbstractServerConnectionFactory.class);
1096		TestingUtilities.waitListening(scf, null);
1097		MessageChannel channelAdapterWithinChain = ctx.getBean("tcpOutboundChannelAdapterWithinChain", MessageChannel.class);
1098		PollableChannel inbound = ctx.getBean("inbound", PollableChannel.class);
1099		String testPayload = "Hello, world!";
1100		channelAdapterWithinChain.send(new GenericMessage<String>(testPayload));
1101		Message<?> m = inbound.receive(1000);
1102		assertNotNull(m);
1103		assertEquals(testPayload, new String((byte[]) m.getPayload()));
1104	}
1105
1106	@Test
1107	public void testConnectionException() throws Exception {
1108		TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
1109		AbstractConnectionFactory mockCcf = mock(AbstractClientConnectionFactory.class);
1110		Mockito.doAnswer(new Answer<Object>() {
1111
1112			public Object answer(InvocationOnMock invocation) throws Throwable {
1113				throw new SocketException("Failed to connect");
1114			}
1115		}).when(mockCcf).getConnection();
1116		handler.setConnectionFactory(mockCcf);
1117		try {
1118			handler.handleMessage(new GenericMessage<String>("foo"));
1119			fail("Expected exception");
1120		}
1121		catch (Exception e) {
1122			assertTrue(e instanceof MessagingException);
1123			assertTrue(e.getCause() != null);
1124			assertTrue(e.getCause() instanceof SocketException);
1125			assertEquals("Failed to connect", e.getCause().getMessage());
1126		}
1127	}
1128}