Skip to content

Commit

Permalink
Add Uncheck.get(IOSupplier, Supplier<String>)
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jun 24, 2023
1 parent 5f6432f commit b4caca0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add Uncheck.run(IORunnable, Supplier&lt;String&gt;)
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add Uncheck.get(IOSupplier, Supplier&lt;String&gt;)
</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.
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/apache/commons/io/function/Uncheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ public static <T> T get(final IOSupplier<T> supplier) {
}
}

/**
* Gets the result from an IO supplier.
*
* @param <T> the return type of the operations.
* @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.
*/
public static <T> T get(final IOSupplier<T> supplier, final Supplier<String> message) {
try {
return supplier.get();
} catch (final IOException e) {
throw wrap(e, message);
}
}

/**
* Gets the result from an IO int supplier.
*
Expand Down
34 changes: 28 additions & 6 deletions src/test/java/org/apache/commons/io/UncheckIOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public class UncheckIOTest {
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);
}
Expand Down Expand Up @@ -97,6 +104,23 @@ 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)}.
*/
Expand All @@ -108,25 +132,23 @@ public void testRun() {
}

/**
* Tests {@link Uncheck#run(IORunnable)}.
* 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) {
assertEquals(CUSTOM_MESSAGE, e.getMessage());
final IOException cause = e.getCause();
assertEquals(expected.getClass(), cause.getClass());
assertEquals(CAUSE_MESSAGE, cause.getMessage());
assertUncheckedIOException(expected, e);
}
}
}

0 comments on commit b4caca0

Please sign in to comment.