您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TemporaryBufferTest.java 12KB

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