diff options
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java | 196 |
1 files changed, 109 insertions, 87 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java index d978804d57..01dcde29bd 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2008-2009, Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2008-2009, Google Inc. and others * - * 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 + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://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. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.util; @@ -61,6 +28,7 @@ import org.junit.Test; public class TemporaryBufferTest { @Test public void testEmpty() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); try { b.close(); @@ -75,6 +43,7 @@ public class TemporaryBufferTest { @Test public void testOneByte() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte test = (byte) new TestRng(getName()).nextInt(); try { @@ -87,10 +56,8 @@ public class TemporaryBufferTest { assertEquals(1, r.length); assertEquals(test, r[0]); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(1, r.length); assertEquals(test, r[0]); @@ -102,6 +69,7 @@ public class TemporaryBufferTest { @Test public void testOneBlock_BulkWrite() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.Block.SZ); @@ -118,10 +86,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -133,6 +99,7 @@ public class TemporaryBufferTest { @Test public void testOneBlockAndHalf_BulkWrite() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2); @@ -149,10 +116,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -164,6 +129,7 @@ public class TemporaryBufferTest { @Test public void testOneBlockAndHalf_SingleWrite() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2); @@ -178,10 +144,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -193,6 +157,7 @@ public class TemporaryBufferTest { @Test public void testOneBlockAndHalf_Copy() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2); @@ -208,10 +173,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -223,6 +186,7 @@ public class TemporaryBufferTest { @Test public void testLarge_SingleWrite() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3); @@ -236,10 +200,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -253,18 +215,18 @@ public class TemporaryBufferTest { 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); + try (TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4)) { + b.write(test); + 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 { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT + 1); @@ -279,10 +241,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -294,6 +254,7 @@ public class TemporaryBufferTest { @Test public void testInCoreLimit_SwitchBeforeAppendByte() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3); @@ -308,10 +269,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -323,6 +282,7 @@ public class TemporaryBufferTest { @Test public void testInCoreLimit_SwitchOnCopy() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final byte[] test = new TestRng(getName()) .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2); @@ -340,10 +300,8 @@ public class TemporaryBufferTest { assertEquals(test.length, r.length); assertArrayEquals(test, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(test.length, r.length); assertArrayEquals(test, r); @@ -355,7 +313,7 @@ public class TemporaryBufferTest { @Test public void testDestroyWhileOpen() throws IOException { - @SuppressWarnings("resource" /* java 7 */) + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); try { b.write(new TestRng(getName()) @@ -367,6 +325,7 @@ public class TemporaryBufferTest { @Test public void testRandomWrites() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block final TemporaryBuffer b = new TemporaryBuffer.LocalFile(null); final TestRng rng = new TestRng(getName()); final int max = TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2; @@ -399,10 +358,8 @@ public class TemporaryBufferTest { assertEquals(expect.length, r.length); assertArrayEquals(expect, r); } - { - final ByteArrayOutputStream o = new ByteArrayOutputStream(); + try (ByteArrayOutputStream o = new ByteArrayOutputStream()) { b.writeTo(o, null); - o.close(); final byte[] r = o.toByteArray(); assertEquals(expect.length, r.length); assertArrayEquals(expect, r); @@ -414,16 +371,16 @@ public class TemporaryBufferTest { @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()); + try (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()); + } } } @@ -449,4 +406,69 @@ public class TemporaryBufferTest { } } } + + @Test + public void testHeapToByteArrayWithLimit() throws IOException { + int sz = 2 * Block.SZ; + try (TemporaryBuffer b = new TemporaryBuffer.Heap(sz / 2, sz)) { + for (int i = 0; i < sz; i++) { + b.write('a' + i % 26); + } + byte[] prefix = b.toByteArray(5); + assertEquals(5, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(Block.SZ + 37); + assertEquals(Block.SZ + 37, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz + 37); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + } + } + + @Test + public void testFileToByteArrayWithLimit() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block + TemporaryBuffer b = new TemporaryBuffer.LocalFile(null, 2 * Block.SZ); + int sz = 3 * Block.SZ; + try { + for (int i = 0; i < sz; i++) { + b.write('a' + i % 26); + } + b.close(); + byte[] prefix = b.toByteArray(5); + assertEquals(5, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(Block.SZ + 37); + assertEquals(Block.SZ + 37, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz + 37); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + } finally { + b.destroy(); + } + } } |