Skip to content

Commit 034cf06

Browse files
KarstenSchnitterGitHub Enterprise
authored andcommitted
Merge pull request #4 from perfx/StreamFix
Fix WrappedOutputStream
2 parents 3d1ce1e + 4ddd4e0 commit 034cf06

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/WrappedOutputStream.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sap.hcp.cf.logging.servlet.filter;
22

33
import java.io.IOException;
4+
import java.io.OutputStream;
45

56
import javax.servlet.ServletOutputStream;
67

@@ -30,4 +31,28 @@ private void incrContentLength(int i) {
3031
contentLength += i;
3132
}
3233
}
34+
35+
@Override
36+
public void write(byte[] b) throws IOException {
37+
wrappedStream.write(b);
38+
incrContentLength(b.length);
39+
}
40+
41+
@Override
42+
public void write(byte[] b, int off, int len) throws IOException {
43+
wrappedStream.write(b, off, len);
44+
incrContentLength(len);
45+
}
46+
47+
@Override
48+
public void close() throws IOException {
49+
wrappedStream.close();
50+
}
51+
52+
@Override
53+
public void flush() throws IOException {
54+
try (OutputStream ostream = wrappedStream) {
55+
wrappedStream.flush();
56+
}
57+
}
3358
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)