MatchingOutputStream extends OutputStream { @Override public void write(int b) throws IOException { int expected = expectedBytesStream.read(); if (expected == -1) { throw new MismatchException("EOF reached in expectedBytesStream"); } if (expected != b) { throw new MismatchException("Data does not match"); } } MatchingOutputStream(InputStream expectedBytesStream, int matchBufferSize); @Override void write(int b); @Override void write(byte[] b); @Override void write(byte[] dataToWrite, int offset, int length); @Override void close(); void expectEof();  }
@Test public void testWrite_OneByte() throws IOException { for (int x = 0; x < data.length; x++) { outputStream.write(data[x] & 0xff); } }
@Test public void testWrite_WholeBuffer() throws IOException { outputStream.write(data); }
@Test public void testWrite_WholeBuffer_RealisticCopyBuffer() throws IOException { outputStream = new MatchingOutputStream(inputStream, 32768); outputStream.write(data); }
@Test public void testWrite_PartialBuffer() throws IOException { for (int x = 0; x < data.length; x++) { outputStream.write(data, x, 1); } }
@Test(expected = MismatchException.class) public void testWrite_OneByte_MatchFail() throws IOException { outputStream.write(0); outputStream.write(77); }
@Test(expected = MismatchException.class) public void testWrite_OneByte_StreamFail() throws IOException { for (int x = 0; x <= data.length; x++) { outputStream.write(x); } }
@Test(expected = MismatchException.class) public void testWrite_WholeBuffer_Fail() throws IOException { byte[] tweaked = new byte[] {0, 1, 2, 3, 4, 55, 6, 7, 8, 9}; outputStream.write(tweaked); }
@Test(expected = MismatchException.class) public void testWrite_PartialBuffer_Fail() throws IOException { byte[] tweaked = new byte[] {0, 1, 2, 3, 4, 55, 6, 7, 8, 9}; outputStream.write(tweaked, 0, 8); }
@Test(expected = MismatchException.class) public void testWrite_PastEndOfMatchStream() throws IOException { outputStream.write(data); outputStream.write(data); }