|
18 | 18 | package org.apache.commons.io.function; |
19 | 19 |
|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
21 | | - |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | 22 | import java.io.Closeable; |
23 | 23 | import java.io.IOException; |
24 | 24 | import java.io.StringReader; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.util.concurrent.atomic.AtomicReference; |
25 | 27 |
|
26 | 28 | import org.apache.commons.io.IOUtils; |
| 29 | +import org.apache.commons.io.Uncheck; |
| 30 | +import org.apache.commons.io.file.PathUtils; |
27 | 31 | import org.apache.commons.io.test.ThrowOnCloseReader; |
28 | 32 | import org.junit.jupiter.api.Test; |
29 | 33 |
|
|
33 | 37 | public class IOConsumerTest { |
34 | 38 |
|
35 | 39 | @Test |
36 | | - public void testNoopIOConsumer() { |
| 40 | + void testAccept() throws IOException { |
| 41 | + IOConsumer.noop().accept(null); |
| 42 | + IOConsumer.noop().accept("."); |
| 43 | + Uncheck.accept(Files::size, PathUtils.current()); |
| 44 | + // |
| 45 | + final AtomicReference<String> ref = new AtomicReference<>(); |
| 46 | + final IOConsumer<String> consumer = s -> ref.set(s + "1"); |
| 47 | + consumer.accept("A"); |
| 48 | + assertEquals("A1", ref.get()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testAndThen() throws IOException { |
| 53 | + final AtomicReference<String> ref = new AtomicReference<>(); |
| 54 | + final IOConsumer<String> consumer1 = s -> ref.set(s + "1"); |
| 55 | + final IOConsumer<String> consumer2 = s -> ref.set(ref.get() + "2" + s); |
| 56 | + consumer1.andThen(consumer2).accept("B"); |
| 57 | + assertEquals("B12B", ref.get()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testNoop() { |
37 | 62 | final Closeable nullCloseable = null; |
38 | 63 | final IOConsumer<IOException> noopConsumer = IOConsumer.noop(); // noop consumer doesn't throw |
39 | 64 | assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noopConsumer)); |
40 | 65 | assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noopConsumer)); |
41 | 66 | assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noopConsumer)); |
42 | 67 | } |
43 | 68 |
|
| 69 | + |
44 | 70 | } |
0 commit comments