You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/02/05 15:31:45 UTC

[commons-compress] 02/02: Refactor test

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit ddc083b442fc66bcb085cd0e6747e7c056fa0c72
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 5 10:31:39 2023 -0500

    Refactor test
---
 .../apache/commons/compress/AbstractTestCase.java   |  4 ++++
 .../brotli/BrotliCompressorInputStreamTest.java     | 21 +++++++--------------
 2 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/src/test/java/org/apache/commons/compress/AbstractTestCase.java b/src/test/java/org/apache/commons/compress/AbstractTestCase.java
index 19bd49d0..cc60895e 100644
--- a/src/test/java/org/apache/commons/compress/AbstractTestCase.java
+++ b/src/test/java/org/apache/commons/compress/AbstractTestCase.java
@@ -75,6 +75,10 @@ public abstract class AbstractTestCase {
         return Files.createTempDirectory(name).toFile();
     }
 
+    public static InputStream newInputStream(final String path) throws IOException {
+        return Files.newInputStream(getPath(path));
+    }
+
     public static void rmdir(final File f) {
         final String[] s = f.list();
         if (s != null) {
diff --git a/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java
index 72a0b254..6fc183be 100644
--- a/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java
@@ -37,8 +37,7 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
 
     @Test
     public void availableShouldReturnZero() throws IOException {
-        final File input = getFile("brotli.testdata.compressed");
-        try (InputStream is = Files.newInputStream(input.toPath());
+        try (InputStream is = newInputStream("brotli.testdata.compressed");
                 final BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
             assertEquals(0, in.available());
         }
@@ -46,9 +45,8 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
 
     @Test
     public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
-        final File input = getFile("brotli.testdata.compressed");
         final byte[] buf = new byte[2];
-        try (InputStream is = Files.newInputStream(input.toPath());
+        try (InputStream is = newInputStream("brotli.testdata.compressed");
                 final BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
             IOUtils.toByteArray(in);
             assertEquals(-1, in.read(buf));
@@ -58,8 +56,7 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
 
     @Test
     public void shouldBeAbleToSkipAByte() throws IOException {
-        final File input = getFile("brotli.testdata.compressed");
-        try (InputStream is = Files.newInputStream(input.toPath());
+        try (InputStream is = newInputStream("brotli.testdata.compressed");
                 final BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
             assertEquals(1, in.skip(1));
         }
@@ -67,8 +64,7 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
 
     @Test
     public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
-        final File input = getFile("brotli.testdata.compressed");
-        try (InputStream is = Files.newInputStream(input.toPath());
+        try (InputStream is = newInputStream("brotli.testdata.compressed");
                 final BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
             IOUtils.toByteArray(in);
             assertEquals(-1, in.read());
@@ -78,8 +74,7 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
 
     @Test
     public void singleByteReadWorksAsExpected() throws IOException {
-        final File input = getFile("brotli.testdata.compressed");
-        try (InputStream is = Files.newInputStream(input.toPath());
+        try (InputStream is = newInputStream("brotli.testdata.compressed");
                 final BrotliCompressorInputStream in = new BrotliCompressorInputStream(is)) {
             // starts with filename "XXX"
             assertEquals('X', in.read());
@@ -93,9 +88,8 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
      */
     @Test
     public void testBrotliDecode() throws IOException {
-        final File input = getFile("brotli.testdata.compressed");
         final File expected = getFile("brotli.testdata.uncompressed");
-        try (InputStream inputStream = Files.newInputStream(input.toPath());
+        try (InputStream inputStream = newInputStream("brotli.testdata.compressed");
                 BrotliCompressorInputStream brotliInputStream = new BrotliCompressorInputStream(inputStream)) {
             final byte[] b = new byte[20];
             IOUtils.read(expected, b);
@@ -110,9 +104,8 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
 
     @Test
     public void testBrotliUnarchive() throws Exception {
-        final File input = getFile("bla.tar.br");
         final File output = new File(dir, "bla.tar");
-        try (InputStream is = Files.newInputStream(input.toPath())) {
+        try (InputStream is = newInputStream("bla.tar.br")) {
             try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("br", is)) {
                 Files.copy(in, output.toPath());
             }