@Test public void testSocketBind() throws Exception { final InetAddress localAddress = InetAddress.getByAddress(new byte[] {127, 0, 0, 1}); final int localPort = 8888; final InetAddress remoteAddress = InetAddress.getByAddress(new byte[] {10, 0, 0, 2}); final int remotePort = 80; final InetSocketAddress localSockAddress = new InetSocketAddress(localAddress, localPort); final InetSocketAddress remoteSockAddress = new InetSocketAddress(remoteAddress, remotePort); Mockito.when(socket.getLocalSocketAddress()).thenReturn(localSockAddress); Mockito.when(socket.getRemoteSocketAddress()).thenReturn(remoteSockAddress); Mockito.when(socket.getLocalAddress()).thenReturn(localAddress); Mockito.when(socket.getLocalPort()).thenReturn(localPort); Mockito.when(socket.getInetAddress()).thenReturn(remoteAddress); Mockito.when(socket.getPort()).thenReturn(remotePort); conn.bind(socket); Assert.assertEquals("127.0.0.1:8888<->10.0.0.2:80", conn.toString()); Assert.assertTrue(conn.isOpen()); Assert.assertEquals(8888, conn.getLocalPort()); Assert.assertEquals(80, conn.getRemotePort()); Assert.assertEquals( InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), conn.getLocalAddress()); Assert.assertEquals( InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), conn.getRemoteAddress()); }
@Test public void testBasics() throws Exception { Assert.assertFalse(conn.isOpen()); Assert.assertEquals(-1, conn.getLocalPort()); Assert.assertEquals(-1, conn.getRemotePort()); Assert.assertEquals(null, conn.getLocalAddress()); Assert.assertEquals(null, conn.getRemoteAddress()); Assert.assertEquals("[Not bound]", conn.toString()); }