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 11KB

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