@Override public void bind(final Socket socket) throws IOException { super.bind(socket); }
@Override public void bind(final Socket socket) throws IOException { super.bind(socket); }
@Override public void bind(final Socket socket) throws IOException { super.bind(socket); }
@Override public void bind(final Socket socket) throws IOException { super.bind(socket); }
/** * Binds this connection to the given {@link Socket}. This socket will be * used by the connection to send and receive data. * <p> * After this method's execution the connection status will be reported * as open and the {@link #isOpen()} will return {@code true}. * * @param socket the socket. * @throws IOException in case of an I/O error. */ protected void bind(final Socket socket) throws IOException { Args.notNull(socket, "Socket"); bind(new SocketHolder(socket)); }
/** * Binds this connection to the given {@link Socket}. This socket will be * used by the connection to send and receive data. * <p> * After this method's execution the connection status will be reported * as open and the {@link #isOpen()} will return {@code true}. * * @param socket the socket. * @throws IOException in case of an I/O error. */ protected void bind(final Socket socket) throws IOException { Args.notNull(socket, "Socket"); bind(new SocketHolder(socket)); }
@Test public void testSetSocketTimeoutException() throws Exception { conn.bind(socket); Mockito.doThrow(new SocketException()).when(socket).setSoTimeout(ArgumentMatchers.anyInt()); conn.setSocketTimeout(Timeout.ofMilliseconds(123)); Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123); }
@Test public void testAwaitInputInSocket() throws Exception { final ByteArrayInputStream inStream = Mockito.spy(new ByteArrayInputStream( new byte[] {1, 2, 3, 4, 5})); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(socket.getSoTimeout()).thenReturn(345); conn.bind(socket); conn.ensureOpen(); Assert.assertTrue(conn.awaitInput(432)); Mockito.verify(socket, Mockito.times(1)).setSoTimeout(432); Mockito.verify(socket, Mockito.times(1)).setSoTimeout(345); Mockito.verify(inStream, Mockito.times(1)).read( ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); }
@Test public void testSetSocketTimeout() throws Exception { conn.bind(socket); conn.setSocketTimeout(Timeout.ofMilliseconds(123)); Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123); }
@Test public void testStaleWhenEndOfStream() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) .thenReturn(-1); conn.bind(socket); conn.ensureOpen(); Assert.assertTrue(conn.isStale()); }
@Test public void testNotStaleWhenHasData() throws Exception { final ByteArrayInputStream inStream = Mockito.spy(new ByteArrayInputStream( new byte[] {1, 2, 3, 4, 5})); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); conn.ensureOpen(); Assert.assertFalse(conn.isStale()); }
@Test public void testNotStaleWhenTimeout() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) .thenThrow(new SocketTimeoutException()); conn.bind(socket); conn.ensureOpen(); Assert.assertFalse(conn.isStale()); }
@Test public void testStaleWhenIOError() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) .thenThrow(new SocketException()); conn.bind(socket); conn.ensureOpen(); Assert.assertTrue(conn.isStale()); }
@Test public void testAwaitInputNoData() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) .thenReturn(-1); conn.bind(socket); conn.ensureOpen(); Assert.assertFalse(conn.awaitInput(432)); }
@Test public void testAwaitInputInBuffer() throws Exception { final ByteArrayInputStream inStream = Mockito.spy(new ByteArrayInputStream( new byte[] {1, 2, 3, 4, 5})); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); conn.ensureOpen(); conn.inBuffer.read(inStream); Assert.assertTrue(conn.awaitInput(432)); Mockito.verify(socket, Mockito.never()).setSoTimeout(ArgumentMatchers.anyInt()); Mockito.verify(inStream, Mockito.times(1)).read( ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); }
@Test public void testGetSocketTimeoutException() throws Exception { Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout()); Mockito.when(socket.getSoTimeout()).thenThrow(new SocketException()); conn.bind(socket); Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout()); }
@Test public void testStaleWhenClosed() throws Exception { final OutputStream outStream = Mockito.mock(OutputStream.class); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); conn.ensureOpen(); conn.close(); Assert.assertTrue(conn.isStale()); }
@Test public void testConnectionClose() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final OutputStream outStream = Mockito.mock(OutputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); conn.ensureOpen(); conn.outbuffer.write(0, outStream); Assert.assertTrue(conn.isOpen()); conn.close(); Assert.assertFalse(conn.isOpen()); Mockito.verify(outStream, Mockito.times(1)).write( ArgumentMatchers.<byte[]>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); Mockito.verify(socket, Mockito.times(1)).shutdownInput(); Mockito.verify(socket, Mockito.times(1)).shutdownOutput(); Mockito.verify(socket, Mockito.times(1)).close(); conn.close(); Mockito.verify(socket, Mockito.times(1)).close(); Mockito.verify(outStream, Mockito.times(1)).write( ArgumentMatchers.<byte[]>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); }
@Test public void testConnectionShutdown() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final OutputStream outStream = Mockito.mock(OutputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); conn.ensureOpen(); conn.outbuffer.write(0, outStream); Assert.assertTrue(conn.isOpen()); conn.close(CloseMode.GRACEFUL); Assert.assertFalse(conn.isOpen()); Mockito.verify(outStream, Mockito.never()).write( ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); Mockito.verify(socket, Mockito.never()).shutdownInput(); Mockito.verify(socket, Mockito.never()).shutdownOutput(); Mockito.verify(socket, Mockito.times(1)).close(); conn.close(); Mockito.verify(socket, Mockito.times(1)).close(); conn.close(CloseMode.GRACEFUL); Mockito.verify(socket, Mockito.times(1)).close(); }
@Test public void testGetSocketTimeout() throws Exception { Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout()); Mockito.when(socket.getSoTimeout()).thenReturn(345); conn.bind(socket); Assert.assertEquals(Timeout.ofMilliseconds(345), conn.getSocketTimeout()); }