@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 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 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()); }