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:21:41 UTC

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

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 b4caca0c Add Uncheck.get(IOSupplier, Supplier<String>)
     new d6e836d4 Merge 2 similar test classes
     new c6d891ec Add Uncheck.getAsInt(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                            |   4 +-
 .../org/apache/commons/io/function/Uncheck.java    |  17 +++
 .../java/org/apache/commons/io/UncheckIOTest.java  | 154 ---------------------
 .../apache/commons/io/function/UncheckTest.java    | 131 +++++++++++++++++-
 4 files changed, 149 insertions(+), 157 deletions(-)
 delete mode 100644 src/test/java/org/apache/commons/io/UncheckIOTest.java


[commons-io] 01/02: Merge 2 similar test classes

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

    Merge 2 similar test classes
---
 .../java/org/apache/commons/io/UncheckIOTest.java  | 154 ---------------------
 .../apache/commons/io/function/UncheckTest.java    | 112 ++++++++++++++-
 2 files changed, 111 insertions(+), 155 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/UncheckIOTest.java b/src/test/java/org/apache/commons/io/UncheckIOTest.java
deleted file mode 100644
index 3611cc3c..00000000
--- a/src/test/java/org/apache/commons/io/UncheckIOTest.java
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.commons.io;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.UncheckedIOException;
-
-import org.apache.commons.io.function.IOBiFunction;
-import org.apache.commons.io.function.IOConsumer;
-import org.apache.commons.io.function.IOFunction;
-import org.apache.commons.io.function.IORunnable;
-import org.apache.commons.io.function.IOSupplier;
-import org.apache.commons.io.function.IOTriFunction;
-import org.apache.commons.io.function.Uncheck;
-import org.apache.commons.io.input.BrokenInputStream;
-import org.junit.jupiter.api.Test;
-
-/**
- * Tests {@link Uncheck}.
- */
-public class UncheckIOTest {
-
-    private static final String CAUSE_MESSAGE = "CauseMessage";
-    private static final String CUSTOM_MESSAGE = "Custom message";
-    private static final byte[] BYTES = { 'a', 'b' };
-
-    private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) {
-        assertEquals(CUSTOM_MESSAGE, e.getMessage());
-        final IOException cause = e.getCause();
-        assertEquals(expected.getClass(), cause.getClass());
-        assertEquals(CAUSE_MESSAGE, cause.getMessage());
-    }
-
-    private ByteArrayInputStream newInputStream() {
-        return new ByteArrayInputStream(BYTES);
-    }
-
-    /**
-     * Tests {@link Uncheck#accept(IOConsumer, Object)}.
-     */
-    @Test
-    public void testAccept() {
-        final ByteArrayInputStream stream = newInputStream();
-        Uncheck.accept(n -> stream.skip(n), 1);
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#apply(IOFunction, Object)}.
-     */
-    @Test
-    public void testApply1() {
-        final ByteArrayInputStream stream = newInputStream();
-        assertEquals(1, Uncheck.apply(n -> stream.skip(n), 1).intValue());
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#apply(IOBiFunction, Object, Object)}.
-     */
-    @Test
-    public void testApply2() {
-        final ByteArrayInputStream stream = newInputStream();
-        final byte[] buf = new byte[BYTES.length];
-        assertEquals(1, Uncheck.apply((o, l) -> stream.read(buf, o, l), 0, 1).intValue());
-        assertEquals('a', buf[0]);
-    }
-
-    /**
-     * Tests {@link Uncheck#apply(IOTriFunction, Object, Object, Object)}.
-     */
-    @Test
-    public void testApply3() {
-        final ByteArrayInputStream stream = newInputStream();
-        final byte[] buf = new byte[BYTES.length];
-        assertEquals(1, Uncheck.apply((b, o, l) -> stream.read(b, o, l), buf, 0, 1).intValue());
-        assertEquals('a', buf[0]);
-    }
-
-    /**
-     * Tests {@link Uncheck#get(IOSupplier)}.
-     */
-    @Test
-    public void testGet() {
-        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#get(IOSupplier, Supplier)}.
-     */
-    @Test
-    public void testGetMessage() {
-        // No exception
-        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE);
-        // Exception
-        final IOException expected = new IOException(CAUSE_MESSAGE);
-        try {
-            Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
-            fail();
-        } catch (final UncheckedIOException e) {
-            assertUncheckedIOException(expected, e);
-        }
-    }
-
-    /**
-     * Tests {@link Uncheck#run(IORunnable)}.
-     */
-    @Test
-    public void testRun() {
-        final ByteArrayInputStream stream = newInputStream();
-        Uncheck.run(() -> stream.skip(1));
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-    }
-
-    /**
-     * Tests {@link Uncheck#run(IORunnable, Supplier))}.
-     *
-     * @throws IOException
-     */
-    @Test
-    public void testRunMessage() throws IOException {
-        // No exception
-        final ByteArrayInputStream stream = newInputStream();
-        Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE);
-        assertEquals('b', Uncheck.get(stream::read).intValue());
-        final IOException expected = new IOException(CAUSE_MESSAGE);
-        // Exception
-        try {
-            Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
-            fail();
-        } catch (final UncheckedIOException e) {
-            assertUncheckedIOException(expected, e);
-        }
-    }
-}
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 fedd08a2..67186897 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -20,12 +20,16 @@ package org.apache.commons.io.function;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
 
+import org.apache.commons.io.input.BrokenInputStream;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -34,11 +38,22 @@ import org.junit.jupiter.api.Test;
  */
 public class UncheckTest {
 
+    private static final byte[] BYTES = { 'a', 'b' };
+    private static final String CAUSE_MESSAGE = "CauseMessage";
+    private static final String CUSTOM_MESSAGE = "Custom message";
+
+    private AtomicInteger atomicInt;
     private AtomicReference<String> ref1;
     private AtomicReference<String> ref2;
     private AtomicReference<String> ref3;
     private AtomicReference<String> ref4;
-    private AtomicInteger atomicInt;
+
+    private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) {
+        assertEquals(CUSTOM_MESSAGE, e.getMessage());
+        final IOException cause = e.getCause();
+        assertEquals(expected.getClass(), cause.getClass());
+        assertEquals(CAUSE_MESSAGE, cause.getMessage());
+    }
 
     @BeforeEach
     public void initEach() {
@@ -49,6 +64,20 @@ public class UncheckTest {
         atomicInt = new AtomicInteger();
     }
 
+    private ByteArrayInputStream newInputStream() {
+        return new ByteArrayInputStream(BYTES);
+    }
+
+    /**
+     * Tests {@link Uncheck#accept(IOConsumer, Object)}.
+     */
+    @Test
+    public void testAccept() {
+        final ByteArrayInputStream stream = newInputStream();
+        Uncheck.accept(n -> stream.skip(n), 1);
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+    }
+
     @Test
     public void testAcceptIOBiConsumerOfTUTU() {
         assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u) -> {
@@ -89,6 +118,38 @@ public class UncheckTest {
         assertEquals("new3", ref3.get());
     }
 
+    /**
+     * Tests {@link Uncheck#apply(IOFunction, Object)}.
+     */
+    @Test
+    public void testApply1() {
+        final ByteArrayInputStream stream = newInputStream();
+        assertEquals(1, Uncheck.apply(n -> stream.skip(n), 1).intValue());
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+    }
+
+    /**
+     * Tests {@link Uncheck#apply(IOBiFunction, Object, Object)}.
+     */
+    @Test
+    public void testApply2() {
+        final ByteArrayInputStream stream = newInputStream();
+        final byte[] buf = new byte[BYTES.length];
+        assertEquals(1, Uncheck.apply((o, l) -> stream.read(buf, o, l), 0, 1).intValue());
+        assertEquals('a', buf[0]);
+    }
+
+    /**
+     * Tests {@link Uncheck#apply(IOTriFunction, Object, Object, Object)}.
+     */
+    @Test
+    public void testApply3() {
+        final ByteArrayInputStream stream = newInputStream();
+        final byte[] buf = new byte[BYTES.length];
+        assertEquals(1, Uncheck.apply((b, o, l) -> stream.read(b, o, l), buf, 0, 1).intValue());
+        assertEquals('a', buf[0]);
+    }
+
     @Test
     public void testApplyIOBiFunctionOfTURTU() {
         assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u) -> {
@@ -150,8 +211,12 @@ public class UncheckTest {
         assertEquals("new3", ref3.get());
     }
 
+    /**
+     * Tests {@link Uncheck#get(IOSupplier)}.
+     */
     @Test
     public void testGet() {
+        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue());
         assertThrows(UncheckedIOException.class, () -> Uncheck.get(() -> {
             throw new IOException();
         }));
@@ -170,8 +235,32 @@ public class UncheckTest {
         assertEquals(1, atomicInt.get());
     }
 
+    /**
+     * Tests {@link Uncheck#get(IOSupplier, Supplier)}.
+     */
+    @Test
+    public void testGetMessage() {
+        // No exception
+        assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE);
+        // Exception
+        final IOException expected = new IOException(CAUSE_MESSAGE);
+        try {
+            Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+            fail();
+        } catch (final UncheckedIOException e) {
+            assertUncheckedIOException(expected, e);
+        }
+    }
+
+    /**
+     * Tests {@link Uncheck#run(IORunnable)}.
+     */
     @Test
     public void testRun() {
+        final ByteArrayInputStream stream = newInputStream();
+        Uncheck.run(() -> stream.skip(1));
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+        //
         assertThrows(UncheckedIOException.class, () -> Uncheck.run(() -> {
             throw new IOException();
         }));
@@ -180,6 +269,27 @@ public class UncheckTest {
         assertEquals("new1", ref1.get());
     }
 
+    /**
+     * Tests {@link Uncheck#run(IORunnable, Supplier))}.
+     *
+     * @throws IOException
+     */
+    @Test
+    public void testRunMessage() throws IOException {
+        // No exception
+        final ByteArrayInputStream stream = newInputStream();
+        Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE);
+        assertEquals('b', Uncheck.get(stream::read).intValue());
+        final IOException expected = new IOException(CAUSE_MESSAGE);
+        // Exception
+        try {
+            Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+            fail();
+        } catch (final UncheckedIOException e) {
+            assertUncheckedIOException(expected, e);
+        }
+    }
+
     @Test
     public void testTest() {
         assertThrows(UncheckedIOException.class, () -> Uncheck.test(t -> {


[commons-io] 02/02: Add Uncheck.getAsInt(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 c6d891ec9368bda27544d1fda8d534a99cf068e6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 13:21:37 2023 -0400

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

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index dc2024d3..13256551 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,10 +65,10 @@ The <action> type attribute can be add,update,fix,remove.
         Add IOIntSupplier.
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
-        Add Uncheck.getAsInt(IOIntSupplier).
+        Add IOLongSupplier.
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
-        Add IOLongSupplier.
+        Add Uncheck.getAsInt(IOIntSupplier [, Supplier&lt;String&gt;]).
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add Uncheck.getAsLong(IOLongSupplier).
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 6419db31..13085cf9 100644
--- a/src/main/java/org/apache/commons/io/function/Uncheck.java
+++ b/src/main/java/org/apache/commons/io/function/Uncheck.java
@@ -233,6 +233,23 @@ public final class Uncheck {
         }
     }
 
+    /**
+     * Gets the result from an IO int 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 int getAsInt(final IOIntSupplier supplier, final Supplier<String> message) {
+        try {
+            return supplier.getAsInt();
+        } catch (final IOException e) {
+            throw wrap(e, message);
+        }
+    }
+
     /**
      * Gets the result from an IO long supplier.
      *
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 67186897..6597166d 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -235,6 +235,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);
+        }
+    }
+
     /**
      * Tests {@link Uncheck#get(IOSupplier, Supplier)}.
      */