You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnionInputStreamTest.java 8.2KB

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