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.

TemporaryBufferTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (C) 2008-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;
  44. import static org.eclipse.jgit.junit.JGitTestUtil.getName;
  45. import static org.junit.Assert.assertArrayEquals;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.fail;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import org.eclipse.jgit.junit.TestRng;
  54. import org.eclipse.jgit.util.TemporaryBuffer.Block;
  55. import org.junit.Test;
  56. public class TemporaryBufferTest {
  57. @Test
  58. public void testEmpty() throws IOException {
  59. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  60. try {
  61. b.close();
  62. assertEquals(0, b.length());
  63. final byte[] r = b.toByteArray();
  64. assertNotNull(r);
  65. assertEquals(0, r.length);
  66. } finally {
  67. b.destroy();
  68. }
  69. }
  70. @Test
  71. public void testOneByte() throws IOException {
  72. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  73. final byte test = (byte) new TestRng(getName()).nextInt();
  74. try {
  75. b.write(test);
  76. b.close();
  77. assertEquals(1, b.length());
  78. {
  79. final byte[] r = b.toByteArray();
  80. assertNotNull(r);
  81. assertEquals(1, r.length);
  82. assertEquals(test, r[0]);
  83. }
  84. {
  85. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  86. b.writeTo(o, null);
  87. o.close();
  88. final byte[] r = o.toByteArray();
  89. assertEquals(1, r.length);
  90. assertEquals(test, r[0]);
  91. }
  92. } finally {
  93. b.destroy();
  94. }
  95. }
  96. @Test
  97. public void testOneBlock_BulkWrite() throws IOException {
  98. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  99. final byte[] test = new TestRng(getName())
  100. .nextBytes(TemporaryBuffer.Block.SZ);
  101. try {
  102. b.write(test, 0, 2);
  103. b.write(test, 2, 4);
  104. b.write(test, 6, test.length - 6 - 2);
  105. b.write(test, test.length - 2, 2);
  106. b.close();
  107. assertEquals(test.length, b.length());
  108. {
  109. final byte[] r = b.toByteArray();
  110. assertNotNull(r);
  111. assertEquals(test.length, r.length);
  112. assertArrayEquals(test, r);
  113. }
  114. {
  115. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  116. b.writeTo(o, null);
  117. o.close();
  118. final byte[] r = o.toByteArray();
  119. assertEquals(test.length, r.length);
  120. assertArrayEquals(test, r);
  121. }
  122. } finally {
  123. b.destroy();
  124. }
  125. }
  126. @Test
  127. public void testOneBlockAndHalf_BulkWrite() throws IOException {
  128. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  129. final byte[] test = new TestRng(getName())
  130. .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
  131. try {
  132. b.write(test, 0, 2);
  133. b.write(test, 2, 4);
  134. b.write(test, 6, test.length - 6 - 2);
  135. b.write(test, test.length - 2, 2);
  136. b.close();
  137. assertEquals(test.length, b.length());
  138. {
  139. final byte[] r = b.toByteArray();
  140. assertNotNull(r);
  141. assertEquals(test.length, r.length);
  142. assertArrayEquals(test, r);
  143. }
  144. {
  145. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  146. b.writeTo(o, null);
  147. o.close();
  148. final byte[] r = o.toByteArray();
  149. assertEquals(test.length, r.length);
  150. assertArrayEquals(test, r);
  151. }
  152. } finally {
  153. b.destroy();
  154. }
  155. }
  156. @Test
  157. public void testOneBlockAndHalf_SingleWrite() throws IOException {
  158. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  159. final byte[] test = new TestRng(getName())
  160. .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
  161. try {
  162. for (int i = 0; i < test.length; i++)
  163. b.write(test[i]);
  164. b.close();
  165. assertEquals(test.length, b.length());
  166. {
  167. final byte[] r = b.toByteArray();
  168. assertNotNull(r);
  169. assertEquals(test.length, r.length);
  170. assertArrayEquals(test, r);
  171. }
  172. {
  173. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  174. b.writeTo(o, null);
  175. o.close();
  176. final byte[] r = o.toByteArray();
  177. assertEquals(test.length, r.length);
  178. assertArrayEquals(test, r);
  179. }
  180. } finally {
  181. b.destroy();
  182. }
  183. }
  184. @Test
  185. public void testOneBlockAndHalf_Copy() throws IOException {
  186. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  187. final byte[] test = new TestRng(getName())
  188. .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
  189. try {
  190. final ByteArrayInputStream in = new ByteArrayInputStream(test);
  191. b.write(in.read());
  192. b.copy(in);
  193. b.close();
  194. assertEquals(test.length, b.length());
  195. {
  196. final byte[] r = b.toByteArray();
  197. assertNotNull(r);
  198. assertEquals(test.length, r.length);
  199. assertArrayEquals(test, r);
  200. }
  201. {
  202. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  203. b.writeTo(o, null);
  204. o.close();
  205. final byte[] r = o.toByteArray();
  206. assertEquals(test.length, r.length);
  207. assertArrayEquals(test, r);
  208. }
  209. } finally {
  210. b.destroy();
  211. }
  212. }
  213. @Test
  214. public void testLarge_SingleWrite() throws IOException {
  215. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  216. final byte[] test = new TestRng(getName())
  217. .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
  218. try {
  219. b.write(test);
  220. b.close();
  221. assertEquals(test.length, b.length());
  222. {
  223. final byte[] r = b.toByteArray();
  224. assertNotNull(r);
  225. assertEquals(test.length, r.length);
  226. assertArrayEquals(test, r);
  227. }
  228. {
  229. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  230. b.writeTo(o, null);
  231. o.close();
  232. final byte[] r = o.toByteArray();
  233. assertEquals(test.length, r.length);
  234. assertArrayEquals(test, r);
  235. }
  236. } finally {
  237. b.destroy();
  238. }
  239. }
  240. @Test
  241. public void testInCoreInputStream() throws IOException {
  242. final int cnt = 256;
  243. final byte[] test = new TestRng(getName()).nextBytes(cnt);
  244. final TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4);
  245. b.write(test);
  246. b.close();
  247. InputStream in = b.openInputStream();
  248. byte[] act = new byte[cnt];
  249. IO.readFully(in, act, 0, cnt);
  250. assertArrayEquals(test, act);
  251. }
  252. @Test
  253. public void testInCoreLimit_SwitchOnAppendByte() throws IOException {
  254. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  255. final byte[] test = new TestRng(getName())
  256. .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT + 1);
  257. try {
  258. b.write(test, 0, test.length - 1);
  259. b.write(test[test.length - 1]);
  260. b.close();
  261. assertEquals(test.length, b.length());
  262. {
  263. final byte[] r = b.toByteArray();
  264. assertNotNull(r);
  265. assertEquals(test.length, r.length);
  266. assertArrayEquals(test, r);
  267. }
  268. {
  269. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  270. b.writeTo(o, null);
  271. o.close();
  272. final byte[] r = o.toByteArray();
  273. assertEquals(test.length, r.length);
  274. assertArrayEquals(test, r);
  275. }
  276. } finally {
  277. b.destroy();
  278. }
  279. }
  280. @Test
  281. public void testInCoreLimit_SwitchBeforeAppendByte() throws IOException {
  282. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  283. final byte[] test = new TestRng(getName())
  284. .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
  285. try {
  286. b.write(test, 0, test.length - 1);
  287. b.write(test[test.length - 1]);
  288. b.close();
  289. assertEquals(test.length, b.length());
  290. {
  291. final byte[] r = b.toByteArray();
  292. assertNotNull(r);
  293. assertEquals(test.length, r.length);
  294. assertArrayEquals(test, r);
  295. }
  296. {
  297. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  298. b.writeTo(o, null);
  299. o.close();
  300. final byte[] r = o.toByteArray();
  301. assertEquals(test.length, r.length);
  302. assertArrayEquals(test, r);
  303. }
  304. } finally {
  305. b.destroy();
  306. }
  307. }
  308. @Test
  309. public void testInCoreLimit_SwitchOnCopy() throws IOException {
  310. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  311. final byte[] test = new TestRng(getName())
  312. .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2);
  313. try {
  314. final ByteArrayInputStream in = new ByteArrayInputStream(test,
  315. TemporaryBuffer.DEFAULT_IN_CORE_LIMIT, test.length
  316. - TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
  317. b.write(test, 0, TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
  318. b.copy(in);
  319. b.close();
  320. assertEquals(test.length, b.length());
  321. {
  322. final byte[] r = b.toByteArray();
  323. assertNotNull(r);
  324. assertEquals(test.length, r.length);
  325. assertArrayEquals(test, r);
  326. }
  327. {
  328. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  329. b.writeTo(o, null);
  330. o.close();
  331. final byte[] r = o.toByteArray();
  332. assertEquals(test.length, r.length);
  333. assertArrayEquals(test, r);
  334. }
  335. } finally {
  336. b.destroy();
  337. }
  338. }
  339. @Test
  340. public void testDestroyWhileOpen() throws IOException {
  341. @SuppressWarnings("resource" /* java 7 */)
  342. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  343. try {
  344. b.write(new TestRng(getName())
  345. .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2));
  346. } finally {
  347. b.destroy();
  348. }
  349. }
  350. @Test
  351. public void testRandomWrites() throws IOException {
  352. final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
  353. final TestRng rng = new TestRng(getName());
  354. final int max = TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2;
  355. final byte[] expect = new byte[max];
  356. try {
  357. int written = 0;
  358. boolean onebyte = true;
  359. while (written < max) {
  360. if (onebyte) {
  361. final byte v = (byte) rng.nextInt();
  362. b.write(v);
  363. expect[written++] = v;
  364. } else {
  365. final int len = Math
  366. .min(rng.nextInt() & 127, max - written);
  367. final byte[] tmp = rng.nextBytes(len);
  368. b.write(tmp, 0, len);
  369. System.arraycopy(tmp, 0, expect, written, len);
  370. written += len;
  371. }
  372. onebyte = !onebyte;
  373. }
  374. assertEquals(expect.length, written);
  375. b.close();
  376. assertEquals(expect.length, b.length());
  377. {
  378. final byte[] r = b.toByteArray();
  379. assertNotNull(r);
  380. assertEquals(expect.length, r.length);
  381. assertArrayEquals(expect, r);
  382. }
  383. {
  384. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  385. b.writeTo(o, null);
  386. o.close();
  387. final byte[] r = o.toByteArray();
  388. assertEquals(expect.length, r.length);
  389. assertArrayEquals(expect, r);
  390. }
  391. } finally {
  392. b.destroy();
  393. }
  394. }
  395. @Test
  396. public void testHeap() throws IOException {
  397. @SuppressWarnings("resource" /* java 7 */)
  398. final TemporaryBuffer b = new TemporaryBuffer.Heap(2 * 8 * 1024);
  399. final byte[] r = new byte[8 * 1024];
  400. b.write(r);
  401. b.write(r);
  402. try {
  403. b.write(1);
  404. fail("accepted too many bytes of data");
  405. } catch (IOException e) {
  406. assertEquals("In-memory buffer limit exceeded", e.getMessage());
  407. }
  408. }
  409. @Test
  410. public void testHeapWithEstimatedSize() throws IOException {
  411. int sz = 2 * Block.SZ;
  412. try (TemporaryBuffer b = new TemporaryBuffer.Heap(sz / 2, sz)) {
  413. for (int i = 0; i < sz; i++) {
  414. b.write('x');
  415. }
  416. try {
  417. b.write(1);
  418. fail("accepted too many bytes of data");
  419. } catch (IOException e) {
  420. assertEquals("In-memory buffer limit exceeded", e.getMessage());
  421. }
  422. try (InputStream in = b.openInputStream()) {
  423. for (int i = 0; i < sz; i++) {
  424. assertEquals('x', in.read());
  425. }
  426. assertEquals(-1, in.read());
  427. }
  428. }
  429. }
  430. }