View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 				// This is only used in tests and is thread-safe
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") // We are testing the close() method
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 }