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 b52680ce466bc42af19a0eee1587b4e02d437f73 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jul 14 10:31:31 2024 -0400 Add CountingInputStreamTest.testCloseHandleIOException() --- .../java/org/apache/commons/io/input/BrokenInputStream.java | 11 ++++++++++- .../org/apache/commons/io/input/CountingInputStreamTest.java | 6 ++++++ .../org/apache/commons/io/input/ProxyInputStreamTest.java | 10 +++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/io/input/BrokenInputStream.java b/src/main/java/org/apache/commons/io/input/BrokenInputStream.java index 4130d3fac..4a375f260 100644 --- a/src/main/java/org/apache/commons/io/input/BrokenInputStream.java +++ b/src/main/java/org/apache/commons/io/input/BrokenInputStream.java @@ -103,6 +103,15 @@ public class BrokenInputStream extends InputStream { throw rethrow(); } + /** + * Gets the Throwable to throw. Package-private for testing. + * + * @return the Throwable to throw. + */ + Throwable getThrowable() { + return exceptionSupplier.get(); + } + /** * Throws the configured exception. * @@ -130,7 +139,7 @@ public class BrokenInputStream extends InputStream { * @return Throws the configured exception from its supplier. */ private RuntimeException rethrow() { - return Erase.rethrow(exceptionSupplier.get()); + return Erase.rethrow(getThrowable()); } /** diff --git a/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java index a08eb7448..7d19fe450 100644 --- a/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java @@ -55,6 +55,12 @@ public class CountingInputStreamTest { } } + @Test + public void testCloseHandleIOException() throws IOException { + final IOException exception = new IOException(); + ProxyInputStreamTest.testCloseHandleIOException(new CountingInputStream(new BrokenInputStream(exception))); + } + @Test public void testCounting() throws Exception { final String text = "A piece of text"; diff --git a/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java index fea05aee4..8bc181410 100644 --- a/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java @@ -56,12 +56,16 @@ public class ProxyInputStreamTest<T extends ProxyInputStream> { @SuppressWarnings("resource") static <T, B extends AbstractStreamBuilder<T, B>> void testCloseHandleIOException(final AbstractStreamBuilder<T, B> builder) throws IOException { final IOException exception = new IOException(); - @SuppressWarnings({ "deprecation" }) - final ProxyInputStream inputStream = (ProxyInputStream) builder.setInputStream(new BrokenInputStream(exception)).get(); + testCloseHandleIOException((ProxyInputStream) builder.setInputStream(new BrokenInputStream(() -> exception)).get()); + } + + @SuppressWarnings("resource") + static void testCloseHandleIOException(final ProxyInputStream inputStream) throws IOException { assertFalse(inputStream.isClosed(), "closed"); final ProxyInputStream spy = spy(inputStream); assertThrows(IOException.class, spy::close); - verify(spy).handleIOException(exception); + final BrokenInputStream unwrap = (BrokenInputStream) inputStream.unwrap(); + verify(spy).handleIOException((IOException) unwrap.getThrowable()); assertFalse(spy.isClosed(), "closed"); }