|
81 | 81 | import org.apache.commons.io.test.TestUtils; |
82 | 82 | import org.apache.commons.io.test.ThrowOnCloseReader; |
83 | 83 | import org.apache.commons.lang3.StringUtils; |
| 84 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
84 | 85 | import org.junit.jupiter.api.AfterAll; |
85 | 86 | import org.junit.jupiter.api.BeforeAll; |
86 | 87 | import org.junit.jupiter.api.BeforeEach; |
@@ -321,28 +322,43 @@ public void testClose() { |
321 | 322 |
|
322 | 323 | @Test |
323 | 324 | public void testCloseConsumer() { |
| 325 | + // null consumer |
324 | 326 | final Closeable nullCloseable = null; |
325 | | - assertDoesNotThrow(() -> IOUtils.close(nullCloseable, null)); // null consumer |
326 | | - assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), null)); // null consumer |
327 | | - assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), null)); // null consumer |
328 | | - |
329 | | - final IOConsumer<IOException> nullConsumer = null; // null consumer doesn't throw |
| 327 | + assertDoesNotThrow(() -> IOUtils.close(nullCloseable, null)); |
| 328 | + assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), null)); |
| 329 | + assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), null)); |
| 330 | + // null consumer doesn't throw |
| 331 | + final IOConsumer<IOException> nullConsumer = null; |
330 | 332 | assertDoesNotThrow(() -> IOUtils.close(nullCloseable, nullConsumer)); |
331 | 333 | assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), nullConsumer)); |
332 | 334 | assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), nullConsumer)); |
333 | | - |
334 | | - final IOConsumer<IOException> silentConsumer = IOConsumer.noop(); // noop consumer doesn't throw |
| 335 | + // noop consumer doesn't throw |
| 336 | + final IOConsumer<IOException> silentConsumer = IOConsumer.noop(); |
335 | 337 | assertDoesNotThrow(() -> IOUtils.close(nullCloseable, silentConsumer)); |
336 | 338 | assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), silentConsumer)); |
337 | 339 | assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), silentConsumer)); |
338 | | - |
339 | | - final IOConsumer<IOException> noisyConsumer = i -> { |
340 | | - throw i; |
341 | | - }; // consumer passes on the throw |
342 | | - assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noisyConsumer)); // no throw |
343 | | - assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noisyConsumer)); // no throw |
344 | | - assertThrows(IOException.class, |
345 | | - () -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noisyConsumer)); // closeable throws |
| 340 | + // consumer passes on the throw |
| 341 | + final IOConsumer<IOException> noisyConsumer = ExceptionUtils::rethrow; |
| 342 | + // no throw |
| 343 | + assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noisyConsumer)); |
| 344 | + // no throw |
| 345 | + assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noisyConsumer)); |
| 346 | + // closeable throws |
| 347 | + assertThrows(IOException.class, () -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noisyConsumer)); |
| 348 | + // consumes other than IOException |
| 349 | + final AtomicBoolean b = new AtomicBoolean(); |
| 350 | + final IOConsumer<IOException> consumer = e -> b.set(true); |
| 351 | + // IOException subclass |
| 352 | + assertDoesNotThrow(() -> IOUtils.close(new BrokenOutputStream((Throwable) new EOFException()), consumer)); |
| 353 | + assertTrue(b.get()); |
| 354 | + b.set(false); |
| 355 | + // RuntimeException |
| 356 | + assertDoesNotThrow(() -> IOUtils.close(new BrokenOutputStream(new RuntimeException()), consumer)); |
| 357 | + assertTrue(b.get()); |
| 358 | + b.set(false); |
| 359 | + // RuntimeException subclass |
| 360 | + assertDoesNotThrow(() -> IOUtils.close(new BrokenOutputStream(new UnsupportedOperationException()), consumer)); |
| 361 | + assertTrue(b.get()); |
346 | 362 | } |
347 | 363 |
|
348 | 364 | @Test |
|
0 commit comments