### Question:
LimitedOutputStream extends ByteArrayOutputStream { @SuppressWarnings("sync-override") @Override public void write(byte[] b, int off, int len) { int nextSize = size() + len; if (limit >= nextSize) { super.write(b, off, len); } else { int remainingSize = limit - size(); super.write(b, off, remainingSize); } } LimitedOutputStream(int limit); @SuppressWarnings("sync-override") @Override void write(byte[] b, int off, int len); @SuppressWarnings("sync-override") @Override void write(int b);  }### Answer:
@Test public void streamShouldHoldAllDataWhenWriteByteDontReachTheLimit() { int size = LIMIT -1; for (int index = 0 ; index < size ; index++) { testee.write(0x0); } assertThat(testee.size()).isEqualTo(size); assertThat(testee.toByteArray()).isEqualTo(new byte[size]); }
@Test public void streamShouldHoldAllDataWhenWriteByteJustReachTheLimit() { for (int index = 0 ; index < LIMIT ; index++) { testee.write(0x0); } assertThat(testee.size()).isEqualTo(LIMIT); assertThat(testee.toByteArray()).isEqualTo(new byte[LIMIT]); }
@Test public void streamShouldDiscardsExceedingDataWhenWriteByteGoBeyondTheLimit() { for (int index = 0 ; index < LIMIT +1 ; index++) { testee.write(0x0); } assertThat(testee.size()).isEqualTo(LIMIT); assertThat(testee.toByteArray()).isEqualTo(new byte[LIMIT]); }
@Test public void streamShouldHoldAllDataWhenWriteByteArrayDontReachTheLimit() throws IOException { int size = LIMIT -1; testee.write(new byte[size]); assertThat(testee.size()).isEqualTo(size); assertThat(testee.toByteArray()).isEqualTo(new byte[size]); }
@Test public void streamShouldHoldAllDataWhenWriteByteArrayJustReachTheLimit() throws IOException { testee.write(new byte[LIMIT]); assertThat(testee.size()).isEqualTo(LIMIT); assertThat(testee.toByteArray()).isEqualTo(new byte[LIMIT]); }
@Test public void streamShouldDiscardsExceedingDataWhenWriteArrayByteGoBeyondTheLimit() throws IOException { testee.write(new byte[LIMIT +1]); assertThat(testee.size()).isEqualTo(LIMIT); assertThat(testee.toByteArray()).isEqualTo(new byte[LIMIT]); }
@Test public void streamShouldHoldAllDataWhenWriteByRangeDontReachTheLimit() { int count = LIMIT-1; int offset = 2; testee.write(new byte[LIMIT+100], offset, count); assertThat(testee.size()).isEqualTo(count); assertThat(testee.toByteArray()).isEqualTo(new byte[count]); }
@Test public void streamShouldHoldAllDataWhenWriteByRangeJustReachTheLimit() { int count = LIMIT; int offset = 2; testee.write(new byte[LIMIT+100], offset, count); assertThat(testee.size()).isEqualTo(LIMIT); assertThat(testee.toByteArray()).isEqualTo(new byte[LIMIT]); }
@Test public void streamShouldDiscardsExceedingDataWhenWriteByRangeGoBeyondTheLimit() { int count = LIMIT +1; int offset = 2; testee.write(new byte[LIMIT+100], offset, count); assertThat(testee.size()).isEqualTo(LIMIT); assertThat(testee.toByteArray()).isEqualTo(new byte[LIMIT]); }
@Test public void streamShouldDiscardsExceedingDataWhenDifferentWritesAreUsed() throws IOException { int count = 1; int offset = 0; testee.write(new byte[LIMIT+100], offset, count); testee.write(0x0); testee.write(new byte[2]); testee.write(0x0); testee.write(new byte[LIMIT+100], offset, count); assertThat(testee.size()).isEqualTo(LIMIT); assertThat(testee.toByteArray()).isEqualTo(new byte[LIMIT]); }