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 @SuppressWarnings("resource" )
73 final UnionInputStream u = new UnionInputStream();
74
75 assertTrue(u.isEmpty());
76 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
77 u.add(new ByteArrayInputStream(new byte[] { 3 }));
78 u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
79
80 assertFalse(u.isEmpty());
81 assertEquals(3, u.available());
82 assertEquals(1, u.read());
83 assertEquals(0, u.read());
84 assertEquals(2, u.read());
85 assertEquals(0, u.available());
86
87 assertEquals(3, u.read());
88 assertEquals(0, u.available());
89
90 assertEquals(4, u.read());
91 assertEquals(1, u.available());
92 assertEquals(5, u.read());
93 assertEquals(0, u.available());
94 assertEquals(-1, u.read());
95
96 assertTrue(u.isEmpty());
97 u.add(new ByteArrayInputStream(new byte[] { (byte) 255 }));
98 assertEquals(255, u.read());
99 assertEquals(-1, u.read());
100 assertTrue(u.isEmpty());
101 }
102
103 @Test
104 public void testReadByteBlocks() throws IOException {
105 @SuppressWarnings("resource" )
106 final UnionInputStream u = new UnionInputStream();
107 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
108 u.add(new ByteArrayInputStream(new byte[] { 3 }));
109 u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
110
111 final byte[] r = new byte[5];
112 assertEquals(3, u.read(r, 0, 5));
113 assertTrue(Arrays.equals(new byte[] { 1, 0, 2, }, slice(r, 3)));
114 assertEquals(1, u.read(r, 0, 5));
115 assertEquals(3, r[0]);
116 assertEquals(2, u.read(r, 0, 5));
117 assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
118 assertEquals(-1, u.read(r, 0, 5));
119 }
120
121 private static byte[] slice(byte[] in, int len) {
122 byte[] r = new byte[len];
123 System.arraycopy(in, 0, r, 0, len);
124 return r;
125 }
126
127 @Test
128 public void testArrayConstructor() throws IOException {
129 @SuppressWarnings("resource" )
130 final UnionInputStream u = new UnionInputStream(
131 new ByteArrayInputStream(new byte[] { 1, 0, 2 }),
132 new ByteArrayInputStream(new byte[] { 3 }),
133 new ByteArrayInputStream(new byte[] { 4, 5 }));
134
135 final byte[] r = new byte[5];
136 assertEquals(3, u.read(r, 0, 5));
137 assertTrue(Arrays.equals(new byte[] { 1, 0, 2, }, slice(r, 3)));
138 assertEquals(1, u.read(r, 0, 5));
139 assertEquals(3, r[0]);
140 assertEquals(2, u.read(r, 0, 5));
141 assertTrue(Arrays.equals(new byte[] { 4, 5, }, slice(r, 2)));
142 assertEquals(-1, u.read(r, 0, 5));
143 }
144
145 @Test
146 public void testMarkSupported() {
147 @SuppressWarnings("resource" )
148 final UnionInputStream u = new UnionInputStream();
149 assertFalse(u.markSupported());
150 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
151 assertFalse(u.markSupported());
152 }
153
154 @Test
155 public void testSkip() throws IOException {
156 @SuppressWarnings("resource" )
157 final UnionInputStream u = new UnionInputStream();
158 u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
159 u.add(new ByteArrayInputStream(new byte[] { 3 }));
160 u.add(new ByteArrayInputStream(new byte[] { 4, 5 }));
161 assertEquals(0, u.skip(0));
162 assertEquals(3, u.skip(3));
163 assertEquals(3, u.read());
164 assertEquals(2, u.skip(5));
165 assertEquals(0, u.skip(5));
166 assertEquals(-1, u.read());
167
168 u.add(new ByteArrayInputStream(new byte[] { 20, 30 }) {
169 @Override
170 public long skip(long n) {
171 return 0;
172 }
173 });
174 assertEquals(2, u.skip(8));
175 assertEquals(-1, u.read());
176 }
177
178 @Test
179 public void testAutoCloseDuringRead() throws IOException {
180 @SuppressWarnings("resource" )
181 final UnionInputStream u = new UnionInputStream();
182 final boolean closed[] = new boolean[2];
183 u.add(new ByteArrayInputStream(new byte[] { 1 }) {
184 @Override
185 public void close() {
186 closed[0] = true;
187 }
188 });
189 u.add(new ByteArrayInputStream(new byte[] { 2 }) {
190 @Override
191 public void close() {
192 closed[1] = true;
193 }
194 });
195
196 assertFalse(closed[0]);
197 assertFalse(closed[1]);
198
199 assertEquals(1, u.read());
200 assertFalse(closed[0]);
201 assertFalse(closed[1]);
202
203 assertEquals(2, u.read());
204 assertTrue(closed[0]);
205 assertFalse(closed[1]);
206
207 assertEquals(-1, u.read());
208 assertTrue(closed[0]);
209 assertTrue(closed[1]);
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 @SuppressWarnings("resource" )
264 final UnionInputStream u = new UnionInputStream(
265 new ByteArrayInputStream(new byte[]{1,2,3}),
266 errorReadStream);
267 byte buf[] = new byte[10];
268 assertEquals(3, u.read(buf, 0, 10));
269 assertTrue(Arrays.equals(new byte[] {1,2,3}, slice(buf, 3)));
270 try {
271 u.read(buf, 0, 1);
272 fail("Expected exception from errorReadStream");
273 } catch (IOException e) {
274 assertEquals("Expected", e.getMessage());
275 }
276 }
277 }