View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io.function;
19  
20  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.io.Closeable;
25  import java.io.IOException;
26  import java.io.StringReader;
27  import java.io.UncheckedIOException;
28  import java.nio.file.Files;
29  import java.util.Arrays;
30  import java.util.Optional;
31  import java.util.concurrent.atomic.AtomicReference;
32  import java.util.stream.Stream;
33  
34  import org.apache.commons.io.IOExceptionList;
35  import org.apache.commons.io.IOUtils;
36  import org.apache.commons.io.file.PathUtils;
37  import org.apache.commons.io.test.ThrowOnCloseReader;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Tests {@link IOConsumer}.
42   */
43  public class IOConsumerTest {
44  
45      @Test
46      void testAccept() throws IOException {
47          IOConsumer.noop().accept(null);
48          IOConsumer.noop().accept(".");
49          Uncheck.accept(Files::size, PathUtils.current());
50          //
51          final AtomicReference<String> ref = new AtomicReference<>();
52          final IOConsumer<String> consumer = s -> ref.set(s + "1");
53          consumer.accept("A");
54          assertEquals("A1", ref.get());
55      }
56  
57      @Test
58      void testAndThen() throws IOException {
59          final AtomicReference<String> ref = new AtomicReference<>();
60          final IOConsumer<String> consumer1 = s -> ref.set(s + "1");
61          final IOConsumer<String> consumer2 = s -> ref.set(ref.get() + "2" + s);
62          consumer1.andThen(consumer2).accept("B");
63          assertEquals("B12B", ref.get());
64      }
65  
66      @Test
67      public void testAsConsumer() {
68          assertThrows(UncheckedIOException.class, () -> Optional.of("a").ifPresent(TestUtils.throwingIOConsumer().asConsumer()));
69          final AtomicReference<String> ref = new AtomicReference<>();
70          final IOConsumer<String> consumer1 = s -> ref.set(s + "1");
71          Optional.of("a").ifPresent(consumer1.asConsumer());
72          assertEquals("a1", ref.get());
73      }
74  
75      @Test
76      public void testForAllArrayOf1() throws IOException {
77          IOConsumer.forAll(TestUtils.throwingIOConsumer(), (String[]) null);
78          IOConsumer.forAll(null, (String[]) null);
79          assertThrows(IOExceptionList.class, () -> IOConsumer.forAll(TestUtils.throwingIOConsumer(), "1"));
80          //
81          final AtomicReference<String> ref = new AtomicReference<>("0");
82          final IOConsumer<String> consumer1 = s -> ref.set(ref.get() + s);
83          IOConsumer.forAll(consumer1, "1");
84          assertEquals("01", ref.get());
85      }
86  
87      @Test
88      public void testForAllArrayOf2() throws IOException {
89          IOConsumer.forAll(TestUtils.throwingIOConsumer(), (String[]) null);
90          IOConsumer.forAll(null, (String[]) null);
91          assertThrows(IOExceptionList.class, () -> IOConsumer.forAll(TestUtils.throwingIOConsumer(), "1", "2"));
92          //
93          final AtomicReference<String> ref = new AtomicReference<>("0");
94          final IOConsumer<String> consumer1 = s -> ref.set(ref.get() + s);
95          IOConsumer.forAll(consumer1, "1", "2");
96          assertEquals("012", ref.get());
97      }
98  
99      @Test
100     public void testForAllIterableOf1() throws IOException {
101         IOConsumer.forAll(TestUtils.throwingIOConsumer(), (Iterable<Object>) null);
102         IOConsumer.forAll(null, (Iterable<Object>) null);
103         assertThrows(IOExceptionList.class, () -> IOConsumer.forAll(TestUtils.throwingIOConsumer(), Arrays.asList("1")));
104 
105         final AtomicReference<String> ref = new AtomicReference<>("0");
106         final IOConsumer<String> consumer1 = s -> ref.set(ref.get() + s);
107         IOConsumer.forAll(consumer1, Arrays.asList("1"));
108         assertEquals("01", ref.get());
109     }
110 
111     @Test
112     public void testForAllIterableOf2() throws IOException {
113         IOConsumer.forAll(TestUtils.throwingIOConsumer(), (Iterable<Object>) null);
114         IOConsumer.forAll(null, (Iterable<Object>) null);
115         assertThrows(IOExceptionList.class, () -> IOConsumer.forAll(TestUtils.throwingIOConsumer(), Arrays.asList("1", "2")));
116 
117         final AtomicReference<String> ref = new AtomicReference<>("0");
118         final IOConsumer<String> consumer1 = s -> ref.set(ref.get() + s);
119         IOConsumer.forAll(consumer1, Arrays.asList("1", "2"));
120         assertEquals("012", ref.get());
121     }
122 
123     @Test
124     public void testForAllStreamOf1() throws IOException {
125         IOConsumer.forAll(TestUtils.throwingIOConsumer(), (Stream<Object>) null);
126         IOConsumer.forAll(null, (Stream<Object>) null);
127         assertThrows(IOExceptionList.class, () -> IOConsumer.forAll(TestUtils.throwingIOConsumer(), Arrays.asList("1").stream()));
128 
129         final AtomicReference<String> ref = new AtomicReference<>("0");
130         final IOConsumer<String> consumer1 = s -> ref.set(ref.get() + s);
131         IOConsumer.forAll(consumer1, Arrays.asList("1").stream());
132         assertEquals("01", ref.get());
133     }
134 
135     @Test
136     public void testForAllStreamOf2() throws IOException {
137         IOConsumer.forAll(TestUtils.throwingIOConsumer(), (Stream<Object>) null);
138         IOConsumer.forAll(null, (Stream<Object>) null);
139         assertThrows(IOExceptionList.class, () -> IOConsumer.forAll(TestUtils.throwingIOConsumer(), Arrays.asList("1", "2").stream()));
140 
141         final AtomicReference<String> ref = new AtomicReference<>("0");
142         final IOConsumer<String> consumer1 = s -> ref.set(ref.get() + s);
143         IOConsumer.forAll(consumer1, Arrays.asList("1", "2").stream());
144         assertEquals("012", ref.get());
145     }
146 
147     @Test
148     public void testNoop() {
149         final Closeable nullCloseable = null;
150         final IOConsumer<IOException> noopConsumer = IOConsumer.noop(); // noop consumer doesn't throw
151         assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noopConsumer));
152         assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noopConsumer));
153         assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noopConsumer));
154     }
155 
156 }