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 package org.eclipse.jgit.util.io;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertTrue;
49 import static org.junit.Assert.fail;
50
51 import java.io.ByteArrayInputStream;
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.util.Arrays;
55
56 import org.junit.Test;
57
58 public class UnionInputStreamTest {
59 @Test
60 public void testEmptyStream() throws IOException {
61 try (UnionInputStream u = new UnionInputStream()) {
62 assertTrue(u.isEmpty());
63 assertEquals(-1, u.read());
64 assertEquals(-1, u.read(new byte[1], 0, 1));
65 assertEquals(0, u.available());
66 assertEquals(0, u.skip(1));
67 }
68 }
69
70 @Test
71 public void testReadSingleBytes() throws IOException {
72 try (UnionInputStream u = new UnionInputStream()) {
73 assertTrue(u.isEmpty());
74 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
75 u.add(new ByteArrayInputStream(new byte[] { 3 }));
76 u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
77
78 assertFalse(u.isEmpty());
79 assertEquals(3, u.available());
80 assertEquals(1, u.read());
81 assertEquals(0, u.read());
82 assertEquals(2, u.read());
83 assertEquals(0, u.available());
84
85 assertEquals(3, u.read());
86 assertEquals(0, u.available());
87
88 assertEquals(4, u.read());
89 assertEquals(1, u.available());
90 assertEquals(5, u.read());
91 assertEquals(0, u.available());
92 assertEquals(-1, u.read());
93
94 assertTrue(u.isEmpty());
95 u.add(new ByteArrayInputStream(new byte[] { (byte) 255 }));
96 assertEquals(255, u.read());
97 assertEquals(-1, u.read());
98 assertTrue(u.isEmpty());
99 }
100 }
101
102 @Test
103 public void testReadByteBlocks() throws IOException {
104 try (UnionInputStream u = new UnionInputStream()) {
105 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
106 u.add(new ByteArrayInputStream(new byte[] { 3 }));
107 u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
108
109 final byte[] r = new byte[5];
110 assertEquals(3, u.read(r, 0, 5));
111 assertTrue(Arrays.equals(new byte[] { 1, 0, 2, }, slice(r, 3)));
112 assertEquals(1, u.read(r, 0, 5));
113 assertEquals(3, r[0]);
114 assertEquals(2, u.read(r, 0, 5));
115 assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
116 assertEquals(-1, u.read(r, 0, 5));
117 }
118 }
119
120 private static byte[] slice(byte[] in, int len) {
121 byte[] r = new byte[len];
122 System.arraycopy(in, 0, r, 0, len);
123 return r;
124 }
125
126 @Test
127 public void testArrayConstructor() throws IOException {
128 try (UnionInputStream u = new UnionInputStream(
129 new ByteArrayInputStream(new byte[] { 1, 0, 2 }),
130 new ByteArrayInputStream(new byte[] { 3 }),
131 new ByteArrayInputStream(new byte[] { 4, 5 }))) {
132 final byte[] r = new byte[5];
133 assertEquals(3, u.read(r, 0, 5));
134 assertTrue(Arrays.equals(new byte[] { 1, 0, 2, }, slice(r, 3)));
135 assertEquals(1, u.read(r, 0, 5));
136 assertEquals(3, r[0]);
137 assertEquals(2, u.read(r, 0, 5));
138 assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
139 assertEquals(-1, u.read(r, 0, 5));
140 }
141 }
142
143 @Test
144 public void testMarkSupported() throws IOException {
145 try (UnionInputStream u = new UnionInputStream()) {
146 assertFalse(u.markSupported());
147 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
148 assertFalse(u.markSupported());
149 }
150 }
151
152 @Test
153 public void testSkip() throws IOException {
154 try (UnionInputStream u = new UnionInputStream()) {
155 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
156 u.add(new ByteArrayInputStream(new byte[] { 3 }));
157 u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
158 assertEquals(0, u.skip(0));
159 assertEquals(3, u.skip(3));
160 assertEquals(3, u.read());
161 assertEquals(2, u.skip(5));
162 assertEquals(0, u.skip(5));
163 assertEquals(-1, u.read());
164
165 u.add(new ByteArrayInputStream(new byte[] { 20, 30 }) {
166 @Override
167 @SuppressWarnings("UnsynchronizedOverridesSynchronized")
168
169 public long skip(long n) {
170 return 0;
171 }
172 });
173 assertEquals(2, u.skip(8));
174 assertEquals(-1, u.read());
175 }
176 }
177
178 @Test
179 public void testAutoCloseDuringRead() throws IOException {
180 try (UnionInputStream u = new UnionInputStream()) {
181 final boolean closed[] = new boolean[2];
182 u.add(new ByteArrayInputStream(new byte[] { 1 }) {
183 @Override
184 public void close() {
185 closed[0] = true;
186 }
187 });
188 u.add(new ByteArrayInputStream(new byte[] { 2 }) {
189 @Override
190 public void close() {
191 closed[1] = true;
192 }
193 });
194
195 assertFalse(closed[0]);
196 assertFalse(closed[1]);
197
198 assertEquals(1, u.read());
199 assertFalse(closed[0]);
200 assertFalse(closed[1]);
201
202 assertEquals(2, u.read());
203 assertTrue(closed[0]);
204 assertFalse(closed[1]);
205
206 assertEquals(-1, u.read());
207 assertTrue(closed[0]);
208 assertTrue(closed[1]);
209 }
210 }
211
212 @Test
213 public void testCloseDuringClose() throws IOException {
214 final boolean closed[] = new boolean[2];
215 try (UnionInputStream u = new UnionInputStream()) {
216 u.add(new ByteArrayInputStream(new byte[] { 1 }) {
217 @Override
218 public void close() {
219 closed[0] = true;
220 }
221 });
222 u.add(new ByteArrayInputStream(new byte[] { 2 }) {
223 @Override
224 public void close() {
225 closed[1] = true;
226 }
227 });
228
229 assertFalse(closed[0]);
230 assertFalse(closed[1]);
231 }
232
233 assertTrue(closed[0]);
234 assertTrue(closed[1]);
235 }
236
237 @Test
238 public void testExceptionDuringClose() {
239 @SuppressWarnings("resource")
240 final UnionInputStream u = new UnionInputStream();
241 u.add(new ByteArrayInputStream(new byte[] { 1 }) {
242 @Override
243 public void close() throws IOException {
244 throw new IOException("I AM A TEST");
245 }
246 });
247 try {
248 u.close();
249 fail("close ignored inner stream exception");
250 } catch (IOException e) {
251 assertEquals("I AM A TEST", e.getMessage());
252 }
253 }
254
255 @Test
256 public void testNonBlockingPartialRead() throws Exception {
257 InputStream errorReadStream = new InputStream() {
258 @Override
259 public int read() throws IOException {
260 throw new IOException("Expected");
261 }
262
263 @Override
264 public int read(byte b[], int off, int len) throws IOException {
265 throw new IOException("Expected");
266 }
267 };
268 try (UnionInputStream u = new UnionInputStream(
269 new ByteArrayInputStream(new byte[] { 1, 2, 3 }),
270 errorReadStream)) {
271 byte buf[] = new byte[10];
272 assertEquals(3, u.read(buf, 0, 10));
273 assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, slice(buf, 3)));
274 try {
275 u.read(buf, 0, 1);
276 fail("Expected exception from errorReadStream");
277 } catch (IOException e) {
278 assertEquals("Expected", e.getMessage());
279 }
280 }
281 }
282 }