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    *      https://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.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.io.IOException;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.Arrays;
28  import java.util.List;
29  import java.util.concurrent.atomic.AtomicInteger;
30  
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link IOIterable}.
36   */
37  class IOIterableTest {
38  
39      private static class Fixture implements IOIterable<Path> {
40  
41          List<Path> list = Arrays.asList(Paths.get("a"), Paths.get("b"));
42  
43          @Override
44          public IOIterator<Path> iterator() {
45              return IOIterator.adapt(list);
46          }
47  
48          @Override
49          public Iterable<Path> unwrap() {
50              return list;
51          }
52  
53      }
54  
55      private IOIterable<Path> iterable;
56      private Fixture fixture;
57  
58      @BeforeEach
59      public void beforeEach() {
60          fixture = new Fixture();
61          iterable = fixture;
62      }
63  
64      @Test
65      void testForEach() throws IOException {
66          final AtomicInteger ref = new AtomicInteger();
67          assertThrows(NullPointerException.class, () -> iterable.forEach(null));
68          iterable.forEach(e -> ref.incrementAndGet());
69          assertEquals(2, ref.get());
70      }
71  
72      @Test
73      void testIterator() throws IOException {
74          final AtomicInteger ref = new AtomicInteger();
75          iterable.iterator().forEachRemaining(e -> ref.incrementAndGet());
76          assertEquals(2, ref.get());
77      }
78  
79      @Test
80      void testSpliterator() throws IOException {
81          final AtomicInteger ref = new AtomicInteger();
82          iterable.spliterator().forEachRemaining(e -> ref.incrementAndGet());
83          assertEquals(2, ref.get());
84      }
85  
86      @Test
87      void testUnrwap() throws IOException {
88          assertSame(fixture.list, iterable.unwrap());
89          assertSame(fixture.unwrap(), iterable.unwrap());
90      }
91  }