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 16:28:46 UTC
[commons-io] branch master updated: Add Uncheck.run(IORunnable, Supplier)
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
The following commit(s) were added to refs/heads/master by this push:
new 5f6432f9 Add Uncheck.run(IORunnable, Supplier<String>)
5f6432f9 is described below
commit 5f6432f9a532752a6aa16b4c4cb62604720814b9
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 12:28:41 2023 -0400
Add Uncheck.run(IORunnable, Supplier<String>)
---
src/changes/changes.xml | 3 ++
.../org/apache/commons/io/function/Uncheck.java | 33 +++++++++++++++++++---
.../java/org/apache/commons/io/UncheckIOTest.java | 31 +++++++++++++++++++-
3 files changed, 62 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 447d6f12..badc1289 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -73,6 +73,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add Uncheck.getAsLong(IOLongSupplier).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add Uncheck.run(IORunnable, Supplier<String>)
+ </action>
<!-- FIX -->
<action dev="ggregory" type="fix" issue="IO-799" due-to="Jeroen van der Vegt, Gary Gregory">
ReaderInputStream.read() throws an exception instead of returning -1 when called again after returning -1.
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 3c272da6..e9da1633 100644
--- a/src/main/java/org/apache/commons/io/function/Uncheck.java
+++ b/src/main/java/org/apache/commons/io/function/Uncheck.java
@@ -19,6 +19,7 @@ package org.apache.commons.io.function;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.util.function.Supplier;
/**
* Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}.
@@ -245,6 +246,22 @@ public final class Uncheck {
}
}
+ /**
+ * Runs an IO runnable.
+ *
+ * @param runnable The runnable to run.
+ * @param message The UncheckedIOException message if an I/O error occurs.
+ * @throws UncheckedIOException if an I/O error occurs.
+ * @since 2.14.0
+ */
+ public static void run(final IORunnable runnable, final Supplier<String> message) {
+ try {
+ runnable.run();
+ } catch (final IOException e) {
+ throw wrap(e, message);
+ }
+ }
+
/**
* Tests an IO predicate.
*
@@ -262,10 +279,7 @@ public final class Uncheck {
}
/**
- * Creates a new UncheckedIOException for the given detail message.
- * <p>
- * This method exists because there is no String constructor in {@link UncheckedIOException}.
- * </p>
+ * Creates a new UncheckedIOException for the given exception.
*
* @param e The exception to wrap.
* @return a new {@link UncheckedIOException}.
@@ -274,6 +288,17 @@ public final class Uncheck {
return new UncheckedIOException(e);
}
+ /**
+ * Creates a new UncheckedIOException for the given exception and detail message.
+ *
+ * @param e The exception to wrap.
+ * @param message The UncheckedIOException message if an I/O error occurs.
+ * @return a new {@link UncheckedIOException}.
+ */
+ private static UncheckedIOException wrap(final IOException e, final Supplier<String> message) {
+ return new UncheckedIOException(message.get(), e);
+ }
+
/**
* No instances needed.
*/
diff --git a/src/test/java/org/apache/commons/io/UncheckIOTest.java b/src/test/java/org/apache/commons/io/UncheckIOTest.java
index 845f2604..8ea4d796 100644
--- a/src/test/java/org/apache/commons/io/UncheckIOTest.java
+++ b/src/test/java/org/apache/commons/io/UncheckIOTest.java
@@ -18,8 +18,11 @@
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;
@@ -28,6 +31,7 @@ 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;
/**
@@ -35,7 +39,9 @@ import org.junit.jupiter.api.Test;
*/
public class UncheckIOTest {
- private static final byte[] BYTES = {'a', 'b'};
+ private static final String CAUSE_MESSAGE = "CauseMessage";
+ private static final String CUSTOM_MESSAGE = "Custom message";
+ private static final byte[] BYTES = { 'a', 'b' };
private ByteArrayInputStream newInputStream() {
return new ByteArrayInputStream(BYTES);
@@ -100,4 +106,27 @@ public class UncheckIOTest {
Uncheck.run(() -> stream.skip(1));
assertEquals('b', Uncheck.get(stream::read).intValue());
}
+
+ /**
+ * Tests {@link Uncheck#run(IORunnable)}.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testRunMessage() throws IOException {
+ 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);
+ //
+ try {
+ Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+ fail();
+ } catch (final UncheckedIOException e) {
+ assertEquals(CUSTOM_MESSAGE, e.getMessage());
+ final IOException cause = e.getCause();
+ assertEquals(expected.getClass(), cause.getClass());
+ assertEquals(CAUSE_MESSAGE, cause.getMessage());
+ }
+ }
}