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/06/24 17:57:20 UTC

[commons-io] branch master updated (c6d891ec -> d3b51407)

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 c6d891ec Add Uncheck.getAsInt(IOIntSupplier, Supplier<String>)
     new 6fea8f40 Add missing test
     new d3b51407 Add Uncheck.getAsLong(IOIntSupplier, Supplier<String>)

The 2 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 +-
 .../org/apache/commons/io/function/Uncheck.java    | 17 ++++++++++++
 .../apache/commons/io/function/UncheckTest.java    | 32 ++++++++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletion(-)


[commons-io] 02/02: Add Uncheck.getAsLong(IOIntSupplier, Supplier)

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 d3b51407b3cbc3709d077ee43fe5c699b421edb1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 13:47:11 2023 -0400

    Add Uncheck.getAsLong(IOIntSupplier, Supplier<String>)
---
 src/changes/changes.xml                            |  2 +-
 .../org/apache/commons/io/function/Uncheck.java    | 17 ++++++++++++
 .../apache/commons/io/function/UncheckTest.java    | 31 +++++++++++++++++-----
 3 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 13256551..c285a8c7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -71,7 +71,7 @@ The <action> type attribute can be add,update,fix,remove.
         Add Uncheck.getAsInt(IOIntSupplier [, Supplier&lt;String&gt;]).
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
-        Add Uncheck.getAsLong(IOLongSupplier).
+        Add Uncheck.getAsLong(IOLongSupplier [, Supplier&lt;String&gt;]).
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add Uncheck.run(IORunnable, Supplier&lt;String&gt;)
diff --git a/src/main/java/org/apache/commons/io/function/Uncheck.java b/src/main/java/org/apache/commons/io/function/Uncheck.java
index 13085cf9..3b9c7d36 100644
--- a/src/main/java/org/apache/commons/io/function/Uncheck.java
+++ b/src/main/java/org/apache/commons/io/function/Uncheck.java
@@ -266,6 +266,23 @@ public final class Uncheck {
         }
     }
 
+    /**
+     * Gets the result from an IO long supplier.
+     *
+     * @param supplier Supplies the return value.
+     * @param message The UncheckedIOException message if an I/O error occurs.
+     * @return result from the supplier.
+     * @throws UncheckedIOException if an I/O error occurs.
+     * @since 2.14.0
+     */
+    public static long getAsLong(final IOLongSupplier supplier, final Supplier<String> message) {
+        try {
+            return supplier.getAsLong();
+        } catch (final IOException e) {
+            throw wrap(e, message);
+        }
+    }
+
     /**
      * Runs an IO runnable.
      *
diff --git a/src/test/java/org/apache/commons/io/function/UncheckTest.java b/src/test/java/org/apache/commons/io/function/UncheckTest.java
index d0785477..3eff77e8 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -238,6 +238,25 @@ public class UncheckTest {
         assertEquals(1, atomicInt.get());
     }
 
+    @Test
+    public void testGetAsIntMessage() {
+        // No exception
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
+            throw new IOException();
+        }, () -> CUSTOM_MESSAGE));
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER, () -> CUSTOM_MESSAGE));
+        assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1), () -> CUSTOM_MESSAGE));
+        assertEquals(1, atomicInt.get());
+        // exception
+        final IOException expected = new IOException(CAUSE_MESSAGE);
+        try {
+            Uncheck.getAsInt(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+            fail();
+        } catch (final UncheckedIOException e) {
+            assertUncheckedIOException(expected, e);
+        }
+    }
+
     @Test
     public void testGetAsLong() {
         assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
@@ -249,18 +268,18 @@ public class UncheckTest {
     }
 
     @Test
-    public void testGetAsIntMessage() {
+    public void testGetAsLongMessage() {
         // No exception
-        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
             throw new IOException();
         }, () -> CUSTOM_MESSAGE));
-        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER, () -> CUSTOM_MESSAGE));
-        assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1), () -> CUSTOM_MESSAGE));
-        assertEquals(1, atomicInt.get());
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(TestConstants.THROWING_IO_LONG_SUPPLIER, () -> CUSTOM_MESSAGE));
+        assertEquals(1L, Uncheck.getAsLong(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L), () -> CUSTOM_MESSAGE));
+        assertEquals(1L, atomicLong.get());
         // exception
         final IOException expected = new IOException(CAUSE_MESSAGE);
         try {
-            Uncheck.getAsInt(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+            Uncheck.getAsLong(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
             fail();
         } catch (final UncheckedIOException e) {
             assertUncheckedIOException(expected, e);


[commons-io] 01/02: Add missing test

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 6fea8f40e4af6358d11e4cdfaad57af23f1c7a0a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 13:25:13 2023 -0400

    Add missing test
---
 .../java/org/apache/commons/io/function/UncheckTest.java    | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/test/java/org/apache/commons/io/function/UncheckTest.java b/src/test/java/org/apache/commons/io/function/UncheckTest.java
index 6597166d..d0785477 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 
@@ -43,6 +44,7 @@ public class UncheckTest {
     private static final String CUSTOM_MESSAGE = "Custom message";
 
     private AtomicInteger atomicInt;
+    private AtomicLong atomicLong;
     private AtomicReference<String> ref1;
     private AtomicReference<String> ref2;
     private AtomicReference<String> ref3;
@@ -62,6 +64,7 @@ public class UncheckTest {
         ref3 = new AtomicReference<>();
         ref4 = new AtomicReference<>();
         atomicInt = new AtomicInteger();
+        atomicLong = new AtomicLong();
     }
 
     private ByteArrayInputStream newInputStream() {
@@ -235,6 +238,16 @@ public class UncheckTest {
         assertEquals(1, atomicInt.get());
     }
 
+    @Test
+    public void testGetAsLong() {
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
+            throw new IOException();
+        }));
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(TestConstants.THROWING_IO_LONG_SUPPLIER));
+        assertEquals(1L, Uncheck.getAsLong(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L)));
+        assertEquals(1L, atomicLong.get());
+    }
+
     @Test
     public void testGetAsIntMessage() {
         // No exception