@Test public void testCallsReadArrayWithOffsetAndCountOnWrapped() throws IOException { int offset = 5; int count = 100; byte[] buffer = new byte[105]; when(wrapped.read(eq(buffer), eq(offset), eq(count))).thenReturn(count); assertEquals(count, is.read(buffer, offset, count)); }
@Test public void testCanObtainNewStreamFromPool() throws IOException { InputStream fromPool = ExceptionCatchingInputStream.obtain(wrapped); when(wrapped.read()).thenReturn(1); int read = fromPool.read(); assertEquals(1, read); } }
@Test public void testCallsReadByteArrayOnWrapped() throws IOException { byte[] buffer = new byte[100]; when(wrapped.read(eq(buffer))).thenReturn(buffer.length); assertEquals(buffer.length, is.read(buffer)); }
@Test public void testCatchesExceptionOnReadBufferWithOffsetAndCount() throws IOException { IOException exception = new SocketTimeoutException(); when(wrapped.read(any(byte[].class), anyInt(), anyInt())).thenThrow(exception); int read = is.read(new byte[0], 10, 100); assertEquals(-1, read); assertEquals(exception, is.getException()); }
@Test(expected = IOException.class) public void testReadThrowsIfStreamIsClosed() throws IOException { stream.close(); stream.read(); }
@Test(expected = IOException.class) public void testReadBulkThrowsIfStreamIsClosed() throws IOException { stream.close(); stream.read(new byte[1], 0, 1); }
@Test public void testCatchesExceptionOnRead() throws IOException { IOException expected = new SocketTimeoutException(); when(wrapped.read()).thenThrow(expected); int read = is.read(); assertEquals(-1, read); assertEquals(expected, is.getException()); }
@SuppressWarnings("ResultOfMethodCallIgnored") @Test public void testResetsExceptionToNullOnRelease() throws IOException { IOException exception = new SocketTimeoutException(); when(wrapped.read()).thenThrow(exception); is.read(); is.release(); assertNull(is.getException()); }
@Test public void testCanReadBytesInBulkLargerThanBufferSize() throws IOException { byte[] buffer = new byte[DATA_SIZE]; assertEquals(DATA_SIZE, stream.read(buffer, 0, DATA_SIZE)); for (int i = 0; i < DATA_SIZE; i++) { assertEquals(i, buffer[i]); } }
@Test public void testCanReadBytesInBulkSmallerThanBufferSize() throws IOException { int toRead = BUFFER_SIZE / 2; byte[] buffer = new byte[toRead]; assertEquals(toRead, stream.read(buffer, 0, toRead)); for (int i = 0; i < toRead; i++) { assertEquals(i, buffer[i]); } }
@Test public void testAvailableIncludesDataInBufferAndWrappedAvailableIfBytesRead() throws IOException { stream.read(); assertEquals(DATA_SIZE - 1, stream.available()); }
@Test public void testCanReadIntoBufferLargerThanDataSize() throws IOException { int toRead = DATA_SIZE * 2; byte[] buffer = new byte[toRead]; assertEquals(DATA_SIZE, stream.read(buffer, 0, toRead)); for (int i = 0; i < DATA_SIZE; i++) { assertEquals(i, buffer[i]); } for (int i = DATA_SIZE; i < toRead; i++) { assertEquals(0, buffer[i]); } }
@Test public void testSkipReturnsZeroIfSkipByteCountIsZero() throws IOException { assertEquals(0, stream.skip(0)); assertEquals(0, stream.read()); }
@Test public void testSkipReturnsZeroIfSkipByteCountIsNegative() throws IOException { assertEquals(0, stream.skip(-13)); assertEquals(0, stream.read()); }
@Test public void testCanResetRepeatedlyAfterMarking() throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; stream.mark(BUFFER_SIZE); for (int repeat = 0; repeat < 10; repeat++) { assertEquals(BUFFER_SIZE, stream.read(buffer, 0, BUFFER_SIZE)); for (int i = 0; i < BUFFER_SIZE; i++) { assertEquals(i, buffer[i]); } stream.reset(); } }