1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
*/
public class AbstractContentOutputStreamTestCase {
@Test
public void requireThatStreamCanBeWrittenTo() throws IOException {
MyOutputStream out = new MyOutputStream();
int len = 2 * AbstractContentOutputStream.BUFFERSIZE;
for (int i = 0; i < len; ++i) {
out.write(69);
out.write(new byte[] { });
out.write(new byte[] { 6, 9 });
out.write(new byte[] { 6, 69, 9 }, 1, 0); // zero length
out.write(new byte[] { 6, 69, 9 }, 1, 1);
}
out.close();
InputStream in = out.toInputStream();
for (int i = 0; i < len; ++i) {
assertEquals(69, in.read());
assertEquals(6, in.read());
assertEquals(9, in.read());
assertEquals(69, in.read());
}
assertEquals(-1, in.read());
assertTrue(out.closed);
}
@Test
public void requireThatBigBuffersAreWrittenInOrder() throws IOException {
MyOutputStream out = new MyOutputStream();
out.write(6);
out.write(new byte[2 * AbstractContentOutputStream.BUFFERSIZE]);
out.write(9);
out.close();
InputStream in = out.toInputStream();
assertEquals(6, in.read());
for (int i = 0, len = 2 * AbstractContentOutputStream.BUFFERSIZE; i < len; ++i) {
assertEquals(0, in.read());
}
assertEquals(9, in.read());
assertEquals(-1, in.read());
assertTrue(out.closed);
}
@Test
public void requireThatEmptyBuffersAreNotFlushed() throws Exception {
MyOutputStream out = new MyOutputStream();
out.close();
assertTrue(out.writes.isEmpty());
assertTrue(out.closed);
}
@Test
public void requireThatNoExcessiveBytesAreWritten() throws Exception {
MyOutputStream out = new MyOutputStream();
out.write(new byte[] { 6, 9 });
out.close();
InputStream in = out.toInputStream();
assertEquals(2, in.available());
assertEquals(6, in.read());
assertEquals(9, in.read());
assertEquals(0, in.available());
assertEquals(-1, in.read());
assertTrue(out.closed);
}
@Test
public void requireThatWrittenArraysAreCopied() throws Exception {
MyOutputStream out = new MyOutputStream();
byte[] buf = new byte[1];
for (byte b = 0; b < 127; ++b) {
buf[0] = b;
out.write(buf);
}
out.close();
InputStream in = out.toInputStream();
for (byte b = 0; b < 127; ++b) {
assertEquals(b, in.read());
}
}
private static class MyOutputStream extends AbstractContentOutputStream {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final List<ByteBuffer> writes = new ArrayList<>();
boolean closed;
@Override
protected void doFlush(ByteBuffer buf) {
writes.add(buf);
buf = buf.slice();
while (buf.hasRemaining()) {
out.write(buf.get());
}
}
@Override
protected void doClose() {
closed = true;
}
InputStream toInputStream() {
return new ByteArrayInputStream(out.toByteArray());
}
}
}
|