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 final 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 u.close();
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 public long skip(long n) {
170 return 0;
171 }
172 });
173 assertEquals(2, u.skip(8));
174 assertEquals(-1, u.read());
175 }
176
177 @Test
178 public void testAutoCloseDuringRead() throws IOException {
179 @SuppressWarnings("resource" )
180 final UnionInputStream u = new UnionInputStream();
181 final boolean closed[] = new boolean[2];
182 u.add(new ByteArrayInputStream(new byte[] { 1 }) {
183 public void close() {
184 closed[0] = true;
185 }
186 });
187 u.add(new ByteArrayInputStream(new byte[] { 2 }) {
188 public void close() {
189 closed[1] = true;
190 }
191 });
192
193 assertFalse(closed[0]);
194 assertFalse(closed[1]);
195
196 assertEquals(1, u.read());
197 assertFalse(closed[0]);
198 assertFalse(closed[1]);
199
200 assertEquals(2, u.read());
201 assertTrue(closed[0]);
202 assertFalse(closed[1]);
203
204 assertEquals(-1, u.read());
205 assertTrue(closed[0]);
206 assertTrue(closed[1]);
207 }
208
209 @Test
210 public void testCloseDuringClose() throws IOException {
211 final UnionInputStream u = new UnionInputStream();
212 final boolean closed[] = new boolean[2];
213 u.add(new ByteArrayInputStream(new byte[] { 1 }) {
214 public void close() {
215 closed[0] = true;
216 }
217 });
218 u.add(new ByteArrayInputStream(new byte[] { 2 }) {
219 public void close() {
220 closed[1] = true;
221 }
222 });
223
224 assertFalse(closed[0]);
225 assertFalse(closed[1]);
226
227 u.close();
228
229 assertTrue(closed[0]);
230 assertTrue(closed[1]);
231 }
232
233 @Test
234 public void testExceptionDuringClose() {
235 final UnionInputStream u = new UnionInputStream();
236 u.add(new ByteArrayInputStream(new byte[] { 1 }) {
237 public void close() throws IOException {
238 throw new IOException("I AM A TEST");
239 }
240 });
241 try {
242 u.close();
243 fail("close ignored inner stream exception");
244 } catch (IOException e) {
245 assertEquals("I AM A TEST", e.getMessage());
246 }
247 }
248
249 @Test
250 public void testNonBlockingPartialRead() throws Exception {
251 InputStream errorReadStream = new InputStream() {
252 @Override
253 public int read() throws IOException {
254 throw new IOException("Expected");
255 }
256 };
257 @SuppressWarnings("resource" )
258 final UnionInputStream u = new UnionInputStream(
259 new ByteArrayInputStream(new byte[]{1,2,3}),
260 errorReadStream);
261 byte buf[] = new byte[10];
262 assertEquals(3, u.read(buf, 0, 10));
263 assertTrue(Arrays.equals(new byte[] {1,2,3}, slice(buf, 3)));
264 try {
265 u.read(buf, 0, 1);
266 fail("Expected exception from errorReadStream");
267 } catch (IOException e) {
268 assertEquals("Expected", e.getMessage());
269 }
270 }
271 }