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 2024/07/08 22:20:26 UTC

(commons-io) branch master updated (99673666d -> 78b2c5b8a)

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

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


    from 99673666d MemoryMappedFileInputStream.available() should return 0 after the stream is closed
     new f448f97af Add ObservableInputStream tests
     new 1378842f9 ProxyInputStream.read() should return -1 after the stream is closed
     new e2fb56b7c Add MessageDigestCalculatingInputStream tests
     new a51bc332b Add ObservableInputStream tests
     new 77ac672b4 Add MessageDigestInputStream tests
     new 78b2c5b8a Add QueueInputStream tests

The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  1 +
 .../apache/commons/io/input/ProxyInputStream.java  |  5 +-
 .../MessageDigestCalculatingInputStreamTest.java   | 42 +++++++++++++++++
 .../io/input/MessageDigestInputStreamTest.java     | 47 +++++++++++++++++++
 .../io/input/ObservableInputStreamTest.java        | 54 +++++++++++++++++++---
 .../commons/io/input/ProxyInputStreamTest.java     | 16 ++++++-
 .../commons/io/input/QueueInputStreamTest.java     | 36 +++++++++++++++
 7 files changed, 191 insertions(+), 10 deletions(-)


(commons-io) 01/06: Add ObservableInputStream tests

Posted by gg...@apache.org.
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-io.git

commit f448f97af45a5e4dcbb8937499076c1538f7c81d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 8 15:54:27 2024 -0400

    Add ObservableInputStream tests
---
 .../io/input/ObservableInputStreamTest.java        | 42 ++++++++++++++++++----
 1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
index 9d12f9913..9d0e94392 100644
--- a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
@@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.input.ObservableInputStream.Observer;
@@ -127,23 +128,52 @@ public class ObservableInputStreamTest {
 
     }
 
+    private ObservableInputStream brokenObservableInputStream() {
+        return new ObservableInputStream(BrokenInputStream.INSTANCE);
+    }
+
+    private InputStream createInputStream() {
+        final byte[] buffer = MessageDigestInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
+        return new ObservableInputStream(new ByteArrayInputStream(buffer));
+    }
+
+    @SuppressWarnings("resource")
+    @Test
+    public void testAvailableAfterClose() throws Exception {
+        final InputStream shadow;
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            shadow = in;
+        }
+        assertEquals(0, shadow.available());
+    }
+
+    @Test
+    public void testAvailableAfterOpen() throws Exception {
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            assertNotEquals(IOUtils.EOF, in.read());
+            assertTrue(in.available() > 0);
+        }
+    }
+
     @Test
     public void testBrokenInputStreamRead() throws IOException {
-        try (ObservableInputStream ois = new ObservableInputStream(BrokenInputStream.INSTANCE)) {
+        try (ObservableInputStream ois = brokenObservableInputStream()) {
             assertThrows(IOException.class, ois::read);
         }
     }
 
     @Test
     public void testBrokenInputStreamReadBuffer() throws IOException {
-        try (ObservableInputStream ois = new ObservableInputStream(BrokenInputStream.INSTANCE)) {
+        try (ObservableInputStream ois = brokenObservableInputStream()) {
             assertThrows(IOException.class, () -> ois.read(new byte[1]));
         }
     }
 
     @Test
     public void testBrokenInputStreamReadSubBuffer() throws IOException {
-        try (ObservableInputStream ois = new ObservableInputStream(BrokenInputStream.INSTANCE)) {
+        try (ObservableInputStream ois = brokenObservableInputStream()) {
             assertThrows(IOException.class, () -> ois.read(new byte[2], 0, 1));
         }
     }
@@ -277,10 +307,8 @@ public class ObservableInputStreamTest {
         final byte[] buffer = IOUtils.byteArray();
         final LengthObserver lengthObserver = new LengthObserver();
         final MethodCountObserver methodCountObserver = new MethodCountObserver();
-        try (ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer),
-            lengthObserver, methodCountObserver)) {
-            assertEquals(IOUtils.DEFAULT_BUFFER_SIZE,
-                IOUtils.copy(ois, NullOutputStream.INSTANCE, bufferSize));
+        try (ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer), lengthObserver, methodCountObserver)) {
+            assertEquals(IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.copy(ois, NullOutputStream.INSTANCE, bufferSize));
         }
         assertEquals(IOUtils.DEFAULT_BUFFER_SIZE, lengthObserver.getTotal());
         assertEquals(1, methodCountObserver.getClosedCount());


(commons-io) 02/06: ProxyInputStream.read() should return -1 after the stream is closed

Posted by gg...@apache.org.
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-io.git

commit 1378842f9c5bb3657449679716f9125dcbda6336
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 8 16:37:58 2024 -0400

    ProxyInputStream.read() should return -1 after the stream is closed
---
 src/changes/changes.xml                                  |  1 +
 .../org/apache/commons/io/input/ProxyInputStream.java    |  5 ++++-
 .../apache/commons/io/input/ProxyInputStreamTest.java    | 16 ++++++++++++++--
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cfc22cf10..5971bcce2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -77,6 +77,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix"                due-to="Gary Gregory">NullInputStream.read() should return -1 after the stream is closed.</action>
       <action dev="ggregory" type="fix"                due-to="Gary Gregory">MemoryMappedFileInputStream.available() should return 0 after the stream is closed.</action>
       <action dev="ggregory" type="fix"                due-to="Gary Gregory">MemoryMappedFileInputStream.read() should return -1 after the stream is closed.</action>
+      <action dev="ggregory" type="fix"                due-to="Gary Gregory">ProxyInputStream.read() should return -1 after the stream is closed.</action>
       <!-- UPDATE -->
       <action dev="ggregory" type="update"             due-to="Dependabot">Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.17 #615, #621, #631, #635.</action>
       <action dev="ggregory" type="update"             due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0.</action>
diff --git a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
index 1d51c6ac3..1a974ce6c 100644
--- a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
@@ -180,13 +180,16 @@ public abstract class ProxyInputStream extends FilterInputStream {
     }
 
     /**
-     * Invokes the delegate's {@code read()} method.
+     * Invokes the delegate's {@code read()} method unless the stream is closed.
      *
      * @return the byte read or -1 if the end of stream
      * @throws IOException if an I/O error occurs.
      */
     @Override
     public int read() throws IOException {
+        if (isClosed()) {
+            return EOF;
+        }
         try {
             beforeRead(1);
             final int b = in.read();
diff --git a/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java
index 159d2a6c6..7ec2b0a52 100644
--- a/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java
@@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertSame;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 import org.apache.commons.io.IOUtils;
@@ -66,6 +67,13 @@ public class ProxyInputStreamTest<T extends ProxyInputStream> {
         assertEquals(0, shadow.available());
     }
 
+    @Test
+    public void testAvailableAfterOpen() throws IOException {
+        try (T inputStream = createFixture()) {
+            assertEquals(3, inputStream.available());
+        }
+    }
+
     @Test
     public void testAvailableAll() throws IOException {
         try (T inputStream = createFixture()) {
@@ -110,8 +118,12 @@ public class ProxyInputStreamTest<T extends ProxyInputStream> {
     @SuppressWarnings("resource")
     @Test
     public void testReadAfterClose() throws IOException {
-        final T shadow;
-        try (T inputStream = createFixture()) {
+        InputStream shadow;
+        try (InputStream inputStream = createFixture()) {
+            shadow = inputStream;
+        }
+        assertEquals(IOUtils.EOF, shadow.read());
+        try (InputStream inputStream = new ProxyInputStreamFixture(new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)))) {
             shadow = inputStream;
         }
         assertEquals(IOUtils.EOF, shadow.read());


(commons-io) 05/06: Add MessageDigestInputStream tests

Posted by gg...@apache.org.
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-io.git

commit 77ac672b4fcf430a428a9172b0a08cee47c993dc
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 8 16:43:49 2024 -0400

    Add MessageDigestInputStream tests
---
 .../io/input/MessageDigestInputStreamTest.java     | 47 ++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/src/test/java/org/apache/commons/io/input/MessageDigestInputStreamTest.java b/src/test/java/org/apache/commons/io/input/MessageDigestInputStreamTest.java
index f516a9b39..3f6f263ff 100644
--- a/src/test/java/org/apache/commons/io/input/MessageDigestInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/MessageDigestInputStreamTest.java
@@ -17,13 +17,21 @@
 package org.apache.commons.io.input;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Random;
 
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.codec.digest.MessageDigestAlgorithms;
+import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -38,6 +46,34 @@ public class MessageDigestInputStreamTest {
         return buffer;
     }
 
+    private InputStream createInputStream() throws IOException, NoSuchAlgorithmException {
+        // @formatter:off
+        return MessageDigestInputStream.builder()
+                .setMessageDigest(MessageDigest.getInstance(MessageDigestAlgorithms.SHA_512))
+                .setInputStream(new ByteArrayInputStream(MessageDigestInputStreamTest.generateRandomByteStream(256))).get();
+        // @formatter:on
+    }
+
+    @SuppressWarnings("resource")
+    @Test
+    public void testAvailableAfterClose() throws Exception {
+        final InputStream shadow;
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            shadow = in;
+        }
+        assertEquals(0, shadow.available());
+    }
+
+    @Test
+    public void testAvailableAfterOpen() throws Exception {
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            assertNotEquals(IOUtils.EOF, in.read());
+            assertTrue(in.available() > 0);
+        }
+    }
+
     @Test
     public void testNoDefault() throws Exception {
         assertThrows(IllegalStateException.class, () -> MessageDigestInputStream.builder().get());
@@ -62,4 +98,15 @@ public class MessageDigestInputStreamTest {
         }
     }
 
+    @SuppressWarnings("resource")
+    @Test
+    public void testReadAfterClose() throws Exception {
+        final InputStream shadow;
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            shadow = in;
+        }
+        assertEquals(IOUtils.EOF, shadow.read());
+    }
+
 }


(commons-io) 04/06: Add ObservableInputStream tests

Posted by gg...@apache.org.
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-io.git

commit a51bc332b02c19eedd0c2c02a0dd411f4d0dc564
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 8 16:38:18 2024 -0400

    Add ObservableInputStream tests
---
 .../apache/commons/io/input/ObservableInputStreamTest.java   | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
index 9d0e94392..685cfd56d 100644
--- a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
@@ -332,4 +332,16 @@ public class ObservableInputStreamTest {
     public void testNotificationCallbacksBufferSizeDefault() throws Exception {
         testNotificationCallbacks(IOUtils.DEFAULT_BUFFER_SIZE);
     }
+
+    @SuppressWarnings("resource")
+    @Test
+    public void testReadAfterClose() throws Exception {
+        final InputStream shadow;
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            shadow = in;
+        }
+        assertEquals(IOUtils.EOF, shadow.read());
+    }
+
 }


(commons-io) 03/06: Add MessageDigestCalculatingInputStream tests

Posted by gg...@apache.org.
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-io.git

commit e2fb56b7c731261ad0c819e376ed8aa63a524244
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 8 16:38:12 2024 -0400

    Add MessageDigestCalculatingInputStream tests
---
 .../MessageDigestCalculatingInputStreamTest.java   | 42 ++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java b/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
index 9a98186ca..e778dda44 100644
--- a/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
@@ -17,12 +17,18 @@
 package org.apache.commons.io.input;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.security.MessageDigest;
 
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.codec.digest.MessageDigestAlgorithms;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.input.MessageDigestCalculatingInputStream.Builder;
 import org.junit.jupiter.api.Test;
 
@@ -32,6 +38,31 @@ import org.junit.jupiter.api.Test;
 @SuppressWarnings("deprecation")
 public class MessageDigestCalculatingInputStreamTest {
 
+    private InputStream createInputStream() throws IOException {
+        return MessageDigestCalculatingInputStream.builder()
+                .setInputStream(new ByteArrayInputStream(MessageDigestInputStreamTest.generateRandomByteStream(256))).get();
+    }
+
+    @SuppressWarnings("resource")
+    @Test
+    public void testAvailableAfterClose() throws Exception {
+        final InputStream shadow;
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            shadow = in;
+        }
+        assertEquals(0, shadow.available());
+    }
+
+    @Test
+    public void testAvailableAfterOpen() throws Exception {
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            assertNotEquals(IOUtils.EOF, in.read());
+            assertTrue(in.available() > 0);
+        }
+    }
+
     @Test
     public void testNormalUse() throws Exception {
         for (int i = 256; i < 8192; i *= 2) {
@@ -75,4 +106,15 @@ public class MessageDigestCalculatingInputStreamTest {
         }
     }
 
+    @SuppressWarnings("resource")
+    @Test
+    public void testReadAfterClose() throws Exception {
+        final InputStream shadow;
+        try (InputStream in = createInputStream()) {
+            assertTrue(in.available() > 0);
+            shadow = in;
+        }
+        assertEquals(IOUtils.EOF, shadow.read());
+    }
+
 }


(commons-io) 06/06: Add QueueInputStream tests

Posted by gg...@apache.org.
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-io.git

commit 78b2c5b8a2abdc32e9e993fe80da07a6f35560fb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 8 16:50:28 2024 -0400

    Add QueueInputStream tests
---
 .../commons/io/input/QueueInputStreamTest.java     | 36 ++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java b/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java
index dcb90cbc7..10fa4960c 100644
--- a/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java
@@ -95,6 +95,30 @@ public class QueueInputStreamTest {
         return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name());
     }
 
+    @SuppressWarnings("resource")
+    @ParameterizedTest(name = "inputData={0}")
+    @MethodSource("inputData")
+    public void testAvailableAfterClose(final String inputData) throws IOException {
+        final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
+        final InputStream shadow;
+        try (InputStream inputStream = new QueueInputStream(queue)) {
+            shadow = inputStream;
+        }
+        assertEquals(0, shadow.available());
+    }
+
+    @ParameterizedTest(name = "inputData={0}")
+    @MethodSource("inputData")
+    public void testAvailableAfterOpen(final String inputData) throws IOException {
+        final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
+        try (InputStream inputStream = new QueueInputStream(queue)) {
+            // Always 0 because read() blocks.
+            assertEquals(0, inputStream.available());
+            IOUtils.toString(inputStream, StandardCharsets.UTF_8);
+            assertEquals(0, inputStream.available());
+        }
+    }
+
     @ParameterizedTest(name = "inputData={0}")
     @MethodSource("inputData")
     public void testBufferedReads(final String inputData) throws IOException {
@@ -139,6 +163,18 @@ public class QueueInputStreamTest {
         assertThrows(IllegalArgumentException.class, () -> QueueInputStream.builder().setTimeout(Duration.ofMillis(-1)).get(), "waitTime must not be negative");
     }
 
+    @SuppressWarnings("resource")
+    @ParameterizedTest(name = "inputData={0}")
+    @MethodSource("inputData")
+    public void testReadAfterClose(final String inputData) throws IOException {
+        final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
+        final InputStream shadow;
+        try (InputStream inputStream = new QueueInputStream(queue)) {
+            shadow = inputStream;
+        }
+        assertEquals(IOUtils.EOF, shadow.read());
+    }
+
     @Test
     public void testResetArguments() throws IOException {
         try (QueueInputStream queueInputStream = QueueInputStream.builder().setTimeout(null).get()) {