123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- /*
- * Copyright (C) 2008-2009, Google Inc.
- * and other copyright owners as documented in the project's IP log.
- *
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
- package org.eclipse.jgit.util;
-
- import static org.eclipse.jgit.junit.JGitTestUtil.getName;
- import static org.junit.Assert.assertArrayEquals;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.fail;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import org.eclipse.jgit.junit.TestRng;
- import org.junit.Test;
-
- public class TemporaryBufferTest {
- @Test
- public void testEmpty() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- try {
- b.close();
- assertEquals(0, b.length());
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(0, r.length);
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testOneByte() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte test = (byte) new TestRng(getName()).nextInt();
- try {
- b.write(test);
- b.close();
- assertEquals(1, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(1, r.length);
- assertEquals(test, r[0]);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(1, r.length);
- assertEquals(test, r[0]);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testOneBlock_BulkWrite() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.Block.SZ);
- try {
- b.write(test, 0, 2);
- b.write(test, 2, 4);
- b.write(test, 6, test.length - 6 - 2);
- b.write(test, test.length - 2, 2);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testOneBlockAndHalf_BulkWrite() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
- try {
- b.write(test, 0, 2);
- b.write(test, 2, 4);
- b.write(test, 6, test.length - 6 - 2);
- b.write(test, test.length - 2, 2);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testOneBlockAndHalf_SingleWrite() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
- try {
- for (int i = 0; i < test.length; i++)
- b.write(test[i]);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testOneBlockAndHalf_Copy() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
- try {
- final ByteArrayInputStream in = new ByteArrayInputStream(test);
- b.write(in.read());
- b.copy(in);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testLarge_SingleWrite() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
- try {
- b.write(test);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testInCoreInputStream() throws IOException {
- final int cnt = 256;
- final byte[] test = new TestRng(getName()).nextBytes(cnt);
- final TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4);
- b.write(test);
- b.close();
-
- InputStream in = b.openInputStream();
- byte[] act = new byte[cnt];
- IO.readFully(in, act, 0, cnt);
- assertArrayEquals(test, act);
- }
-
- @Test
- public void testInCoreLimit_SwitchOnAppendByte() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT + 1);
- try {
- b.write(test, 0, test.length - 1);
- b.write(test[test.length - 1]);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testInCoreLimit_SwitchBeforeAppendByte() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
- try {
- b.write(test, 0, test.length - 1);
- b.write(test[test.length - 1]);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testInCoreLimit_SwitchOnCopy() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final byte[] test = new TestRng(getName())
- .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2);
- try {
- final ByteArrayInputStream in = new ByteArrayInputStream(test,
- TemporaryBuffer.DEFAULT_IN_CORE_LIMIT, test.length
- - TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
- b.write(test, 0, TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
- b.copy(in);
- b.close();
- assertEquals(test.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(test.length, r.length);
- assertArrayEquals(test, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testDestroyWhileOpen() throws IOException {
- @SuppressWarnings("resource" /* java 7 */)
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- try {
- b.write(new TestRng(getName())
- .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2));
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testRandomWrites() throws IOException {
- final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null);
- final TestRng rng = new TestRng(getName());
- final int max = TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2;
- final byte[] expect = new byte[max];
- try {
- int written = 0;
- boolean onebyte = true;
- while (written < max) {
- if (onebyte) {
- final byte v = (byte) rng.nextInt();
- b.write(v);
- expect[written++] = v;
- } else {
- final int len = Math
- .min(rng.nextInt() & 127, max - written);
- final byte[] tmp = rng.nextBytes(len);
- b.write(tmp, 0, len);
- System.arraycopy(tmp, 0, expect, written, len);
- written += len;
- }
- onebyte = !onebyte;
- }
- assertEquals(expect.length, written);
- b.close();
-
- assertEquals(expect.length, b.length());
- {
- final byte[] r = b.toByteArray();
- assertNotNull(r);
- assertEquals(expect.length, r.length);
- assertArrayEquals(expect, r);
- }
- {
- final ByteArrayOutputStream o = new ByteArrayOutputStream();
- b.writeTo(o, null);
- o.close();
- final byte[] r = o.toByteArray();
- assertEquals(expect.length, r.length);
- assertArrayEquals(expect, r);
- }
- } finally {
- b.destroy();
- }
- }
-
- @Test
- public void testHeap() throws IOException {
- @SuppressWarnings("resource" /* java 7 */)
- final TemporaryBuffer b = new TemporaryBuffer.Heap(2 * 8 * 1024);
- final byte[] r = new byte[8 * 1024];
- b.write(r);
- b.write(r);
- try {
- b.write(1);
- fail("accepted too many bytes of data");
- } catch (IOException e) {
- assertEquals("In-memory buffer limit exceeded", e.getMessage());
- }
- }
- }
|