ClientTcpTransportHandler extends TransportHandler { @Override public void initialize() throws IOException { socket = new Socket(); socket.connect(new InetSocketAddress(hostname, port), (int) connectionTimeout); if (!socket.isConnected()) { throw new IOException("Could not connect to " + hostname + ":" + "port"); } setStreams(new PushbackInputStream(socket.getInputStream()), socket.getOutputStream()); socket.setSoTimeout(1); } ClientTcpTransportHandler(Connection connection);  ClientTcpTransportHandler(long timeout, String hostname, int port);  ClientTcpTransportHandler(long connectionTimeout, long timeout, String hostname, int port); @Override void closeConnection(); @Override void initialize(); @Override boolean isClosed(); @Override void closeClientConnection(); SocketState getSocketState();  }
@Test public void testInitialize() throws IOException { ServerSocketChannel serverSocketChannel = null; try { serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(0)); serverSocketChannel.configureBlocking(false); handler = new ClientTcpTransportHandler(100, "localhost", serverSocketChannel.socket().getLocalPort()); handler.initialize(); SocketChannel acceptChannel = serverSocketChannel.accept(); assertNotNull(acceptChannel); assertTrue(handler.isInitialized()); } finally { if (serverSocketChannel != null) { try { serverSocketChannel.close(); } catch (IOException ex) { } } } }
@Test public void fullTest() throws IOException { ServerSocketChannel serverSocketChannel = null; Socket s = null; try { serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(0)); serverSocketChannel.configureBlocking(false); handler = new ClientTcpTransportHandler(100, "localhost", serverSocketChannel.socket().getLocalPort()); handler.initialize(); SocketChannel acceptChannel = serverSocketChannel.accept(); assertNotNull(acceptChannel); s = acceptChannel.socket(); handler.sendData(new byte[] { 1, 2, 3 }); byte[] receive = new byte[3]; s.getInputStream().read(receive); assertArrayEquals(new byte[] { 1, 2, 3 }, receive); s.getOutputStream().write(new byte[] { 6, 6, 6 }); byte[] fetchData = handler.fetchData(); assertArrayEquals(new byte[] { 6, 6, 6 }, fetchData); } finally { if (serverSocketChannel != null) { try { serverSocketChannel.close(); } catch (IOException ex) { } } } }