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  		@SuppressWarnings("resource" /* java 7 */)
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" /* java 7 */)
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" /* java 7 */)
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" /* java 7 */)
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" /* java 7 */)
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" /* java 7 */)
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") // 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 		@SuppressWarnings("resource" /* java 7 */)
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 }