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 2025/02/23 15:40:58 UTC

(commons-io) branch master updated (a93b30c91 -> 1d2019f2a)

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 a93b30c91 Javadoc
     new e24dcb1a4 Reduce source code noise
     new 3b77766e7 The Consumer to IOUtils.closeQuietly(Closeable, Consumer) now accepts Exception, not just IOException
     new 1d2019f2a The Consumer to IOUtils.close(Closeable, IOConsumer) now accepts wrapped Exception, not just IOException

The 3 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                            |  2 +
 src/main/java/org/apache/commons/io/IOUtils.java   | 10 ++-
 .../java/org/apache/commons/io/IOUtilsTest.java    | 89 ++++++++++++++++------
 3 files changed, 76 insertions(+), 25 deletions(-)


(commons-io) 01/03: Reduce source code noise

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 e24dcb1a4499236fb0f4d1d48a34e97608812d59
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 23 08:52:12 2025 -0500

    Reduce source code noise
---
 src/test/java/org/apache/commons/io/IOUtilsTest.java | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index c641a73c5..777889e9b 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -365,12 +365,8 @@ public void testCloseQuietly_AllCloseableIOException() {
 
     @Test
     public void testCloseQuietly_CloseableIOException() {
-        assertDoesNotThrow(() -> {
-            IOUtils.closeQuietly(BrokenInputStream.INSTANCE);
-        });
-        assertDoesNotThrow(() -> {
-            IOUtils.closeQuietly(BrokenOutputStream.INSTANCE);
-        });
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenInputStream.INSTANCE));
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenOutputStream.INSTANCE));
     }
 
     @SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"


(commons-io) 03/03: The Consumer to IOUtils.close(Closeable, IOConsumer) now accepts wrapped Exception, not just IOException

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 1d2019f2a32fc5388e955c5a3a7fa4f236538d4a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 23 10:40:53 2025 -0500

    The Consumer to IOUtils.close(Closeable, IOConsumer) now accepts wrapped
    Exception, not just IOException
---
 src/changes/changes.xml                            |  1 +
 src/main/java/org/apache/commons/io/IOUtils.java   |  4 ++
 .../java/org/apache/commons/io/IOUtilsTest.java    | 46 +++++++++++++++-------
 3 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 93db4cab8..04ba4687b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix" issue="IO-868" due-to="Julian Reschke, Gary Gregory">Improve Javadoc for a BoundedInputStream builder() throwing IOException.</action>
       <action dev="ggregory" type="fix" issue="IO-868" due-to="Julian Reschke, Gary Gregory">Improve Javadoc for all implementations of AbstractOriginSupplier#get().</action>
       <action dev="ggregory" type="fix"                due-to="Gary Gregory">The Consumer to IOUtils.closeQuietly(Closeable, Consumer) now accepts Exception, not just IOException.</action>
+      <action dev="ggregory" type="fix"                due-to="Gary Gregory">The Consumer to IOUtils.close(Closeable, IOConsumer) now accepts wrapped Exception, not just IOException.</action>
       <!-- ADD -->
       <action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker, Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long, ChronoUnit).</action>
       <action dev="ggregory" type="add"                due-to="Gary Gregory">Add IOIterable.</action>
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index 762eadbec..2717aa01e 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -450,6 +450,10 @@ public static void close(final Closeable closeable, final IOConsumer<IOException
                 if (consumer != null) {
                     consumer.accept(e);
                 }
+            } catch (final Exception e) {
+                if (consumer != null) {
+                    consumer.accept(new IOException(e));
+                }
             }
         }
     }
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index 74db66fe3..9be837d4f 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -81,6 +81,7 @@
 import org.apache.commons.io.test.TestUtils;
 import org.apache.commons.io.test.ThrowOnCloseReader;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.BeforeEach;
@@ -321,28 +322,43 @@ public void testClose() {
 
     @Test
     public void testCloseConsumer() {
+        // null consumer
         final Closeable nullCloseable = null;
-        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, null)); // null consumer
-        assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), null)); // null consumer
-        assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), null)); // null consumer
-
-        final IOConsumer<IOException> nullConsumer = null; // null consumer doesn't throw
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, null));
+        assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), null));
+        assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), null));
+        // null consumer doesn't throw
+        final IOConsumer<IOException> nullConsumer = null;
         assertDoesNotThrow(() -> IOUtils.close(nullCloseable, nullConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), nullConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), nullConsumer));
-
-        final IOConsumer<IOException> silentConsumer = IOConsumer.noop(); // noop consumer doesn't throw
+        // noop consumer doesn't throw
+        final IOConsumer<IOException> silentConsumer = IOConsumer.noop();
         assertDoesNotThrow(() -> IOUtils.close(nullCloseable, silentConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), silentConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), silentConsumer));
-
-        final IOConsumer<IOException> noisyConsumer = i -> {
-            throw i;
-        }; // consumer passes on the throw
-        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noisyConsumer)); // no throw
-        assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noisyConsumer)); // no throw
-        assertThrows(IOException.class,
-            () -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noisyConsumer)); // closeable throws
+        // consumer passes on the throw
+        final IOConsumer<IOException> noisyConsumer = ExceptionUtils::rethrow;
+        // no throw
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noisyConsumer));
+        // no throw
+        assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noisyConsumer));
+        // closeable throws
+        assertThrows(IOException.class, () -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noisyConsumer));
+        // consumes other than IOException
+        final AtomicBoolean b = new AtomicBoolean();
+        final IOConsumer<IOException> consumer = e -> b.set(true);
+        // IOException subclass
+        assertDoesNotThrow(() -> IOUtils.close(new BrokenOutputStream((Throwable) new EOFException()), consumer));
+        assertTrue(b.get());
+        b.set(false);
+        // RuntimeException
+        assertDoesNotThrow(() -> IOUtils.close(new BrokenOutputStream(new RuntimeException()), consumer));
+        assertTrue(b.get());
+        b.set(false);
+        // RuntimeException subclass
+        assertDoesNotThrow(() -> IOUtils.close(new BrokenOutputStream(new UnsupportedOperationException()), consumer));
+        assertTrue(b.get());
     }
 
     @Test


(commons-io) 02/03: The Consumer to IOUtils.closeQuietly(Closeable, Consumer) now accepts Exception, not just IOException

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 3b77766e7ffa255528c1b189bfa94ba4f1006945
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 23 09:11:33 2025 -0500

    The Consumer to IOUtils.closeQuietly(Closeable, Consumer) now accepts
    Exception, not just IOException
---
 src/changes/changes.xml                            |  1 +
 src/main/java/org/apache/commons/io/IOUtils.java   |  6 ++--
 .../java/org/apache/commons/io/IOUtilsTest.java    | 35 +++++++++++++++++++++-
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 261c543ad..93db4cab8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix"                due-to="Gary Gregory">Deprecate DeferredFileOutputStream.getStream() in favor of getOutputStream().</action>
       <action dev="ggregory" type="fix" issue="IO-868" due-to="Julian Reschke, Gary Gregory">Improve Javadoc for a BoundedInputStream builder() throwing IOException.</action>
       <action dev="ggregory" type="fix" issue="IO-868" due-to="Julian Reschke, Gary Gregory">Improve Javadoc for all implementations of AbstractOriginSupplier#get().</action>
+      <action dev="ggregory" type="fix"                due-to="Gary Gregory">The Consumer to IOUtils.closeQuietly(Closeable, Consumer) now accepts Exception, not just IOException.</action>
       <!-- ADD -->
       <action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker, Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long, ChronoUnit).</action>
       <action dev="ggregory" type="add"                due-to="Gary Gregory">Add IOIterable.</action>
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index bfda61c36..762eadbec 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -575,14 +575,14 @@ public static void closeQuietly(final Closeable... closeables) {
      * Closes the given {@link Closeable} as a null-safe operation while consuming IOException by the given {@code consumer}.
      *
      * @param closeable The resource to close, may be null.
-     * @param consumer Consumes the IOException thrown by {@link Closeable#close()}.
+     * @param consumer Consumes the Exception thrown by {@link Closeable#close()}.
      * @since 2.7
      */
-    public static void closeQuietly(final Closeable closeable, final Consumer<IOException> consumer) {
+    public static void closeQuietly(final Closeable closeable, final Consumer<Exception> consumer) {
         if (closeable != null) {
             try {
                 closeable.close();
-            } catch (final IOException e) {
+            } catch (final Exception e) {
                 if (consumer != null) {
                     consumer.accept(e);
                 }
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index 777889e9b..74db66fe3 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -61,6 +61,8 @@
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
 import java.util.function.Supplier;
 import java.util.stream.Stream;
 
@@ -364,9 +366,40 @@ public void testCloseQuietly_AllCloseableIOException() {
     }
 
     @Test
-    public void testCloseQuietly_CloseableIOException() {
+    public void testCloseQuietly_CloseableException() {
+        // IOException
         assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenInputStream.INSTANCE));
         assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenOutputStream.INSTANCE));
+        // IOException subclass
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream((Throwable) new EOFException())));
+        // RuntimeException
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new RuntimeException())));
+        // RuntimeException subclass
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new UnsupportedOperationException())));
+    }
+
+    @Test
+    public void testCloseQuietly_CloseableExceptionConsumer() {
+        final AtomicBoolean b = new AtomicBoolean();
+        final Consumer<Exception> consumer = e -> b.set(true);
+        // IOException
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenInputStream.INSTANCE, consumer));
+        assertTrue(b.get());
+        b.set(false);
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenOutputStream.INSTANCE, consumer));
+        assertTrue(b.get());
+        b.set(false);
+        // IOException subclass
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream((Throwable) new EOFException()), consumer));
+        assertTrue(b.get());
+        b.set(false);
+        // RuntimeException
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new RuntimeException()), consumer));
+        assertTrue(b.get());
+        b.set(false);
+        // RuntimeException subclass
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new UnsupportedOperationException()), consumer));
+        assertTrue(b.get());
     }
 
     @SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"