|
| 1 | +package com.sap.hcp.cf.logging.servlet.filter; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.is; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | +import static org.mockito.Mockito.times; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | + |
| 8 | +import javax.servlet.ServletOutputStream; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.runners.MockitoJUnitRunner; |
| 15 | + |
| 16 | +@RunWith(MockitoJUnitRunner.class) |
| 17 | +public class WrappedOutputStreamTest { |
| 18 | + |
| 19 | + @Mock |
| 20 | + private ServletOutputStream out; |
| 21 | + |
| 22 | + @InjectMocks |
| 23 | + private WrappedOutputStream wrapper; |
| 24 | + |
| 25 | + @Test |
| 26 | + public void delegatesClose() throws Exception { |
| 27 | + wrapper.close(); |
| 28 | + verify(out).close(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void delegatesFlush() throws Exception { |
| 33 | + wrapper.flush(); |
| 34 | + verify(out).flush(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void emptyStreamReportsMinusOneBytes() throws Exception { |
| 39 | + assertThat(wrapper.getContentLength(), is(-1L)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void writeOneByteIncreasesByteCountByOne() throws Exception { |
| 44 | + wrapper.write(0); |
| 45 | + assertThat(wrapper.getContentLength(), is(1L)); |
| 46 | + verify(out).write(0); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void repeatedlyWritingSingleBytesIncreasesByteCountCorrectly() throws Exception { |
| 51 | + for (int i = 0; i < 10; i++) { |
| 52 | + wrapper.write(0); |
| 53 | + } |
| 54 | + assertThat(wrapper.getContentLength(), is(10L)); |
| 55 | + verify(out, times(10)).write(0); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void writingBufferAddsLengthToByteCount() throws Exception { |
| 60 | + byte[] b = new byte[13]; |
| 61 | + wrapper.write(b); |
| 62 | + assertThat(wrapper.getContentLength(), is(13L)); |
| 63 | + verify(out).write(b); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void writingBufferWithOffsetAddsLengthToByteCount() throws Exception { |
| 68 | + byte[] b = new byte[13]; |
| 69 | + wrapper.write(b, 3, 10); |
| 70 | + assertThat(wrapper.getContentLength(), is(10L)); |
| 71 | + verify(out).write(b, 3, 10); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void printingAsciiStringAddsLengthToByteCount() throws Exception { |
| 76 | + wrapper.print("Testing"); |
| 77 | + assertThat(wrapper.getContentLength(), is(7L)); |
| 78 | + } |
| 79 | +} |
0 commit comments