ContentLengthVerifyingInputStream extends InputStream { @Override public int read() throws IOException { final int byteRead = delegate.read(); final int bytesRead = byteRead != -1 ? 1 : -1; processBytesRead(bytesRead); return byteRead; }  @Override int read(); @Override int read(byte b[]); @Override int read(byte b[], int off, int len); @Override long skip(long n); @Override synchronized void mark(int readlimit); @Override synchronized void reset(); @Override int available(); @Override void close(); @Override boolean markSupported();  }
@Test public void readByte_validInputStream() throws IOException { final InputStream inputStream = createInputStream(TEST_BUFFER); while (inputStream.read() != -1) {} }
@Test public void readBuffer_validInputStream() throws IOException { final InputStream inputStream = createInputStream(TEST_BUFFER); byte[] buffer = new byte[8]; while (inputStream.read(buffer) != -1) {} }
@Test public void readOffsetBuffer_validInputStream() throws IOException { final InputStream inputStream = createInputStream(TEST_BUFFER); byte[] buffer = new byte[1000]; int offset = 0; int bytesRead; while ((bytesRead = inputStream.read(buffer, offset, 2)) != -1) { offset += bytesRead; } }
@Test(expected = IOException.class) public void readBuffer_inputStreamLongerThanExpectedLength_shouldFail() throws IOException { final InputStream inputStream = createInputStream(TEST_BUFFER, 2); byte[] buffer = new byte[10]; while (inputStream.read(buffer) != -1) {} }
@Test(expected = IOException.class) public void readBuffer_inputStreamShorterThanExpectedLength_shouldFail() throws IOException { final InputStream inputStream = createInputStream(TEST_BUFFER, 2000); byte[] buffer = new byte[10]; while (inputStream.read(buffer) != -1) {} }