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.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.nio.file.Path;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import org.apache.commons.lang3.JavaVersion;
33  import org.apache.commons.lang3.SystemUtils;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Tests {@link IOIterator}.
39   */
40  class IOIteratorTest {
41  
42      private IOIterator<Path> iterator;
43  
44      @BeforeEach
45      public void beforeEach() {
46          iterator = IOIterator.adapt(newPathList().iterator());
47      }
48  
49      private List<Path> newPathList() {
50          return Arrays.asList(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B);
51      }
52  
53      @Test
54      void testAdaptIterable() throws IOException {
55          assertEquals(TestConstants.ABS_PATH_A, IOIterator.adapt(newPathList()).next());
56      }
57  
58      @Test
59      void testAdaptIterator() throws IOException {
60          assertEquals(TestConstants.ABS_PATH_A, iterator.next());
61      }
62  
63      @Test
64      void testAsIterator() {
65          final Iterator<Path> asIterator = iterator.asIterator();
66          assertTrue(asIterator.hasNext());
67          assertEquals(TestConstants.ABS_PATH_A, asIterator.next());
68          assertThrows(UnsupportedOperationException.class, asIterator::remove);
69      }
70  
71      @Test
72      void testForEachRemaining() throws IOException {
73          final List<Path> list = new ArrayList<>();
74          iterator.forEachRemaining(p -> list.add(p.toRealPath()));
75          assertFalse(iterator.hasNext());
76          assertEquals(newPathList(), list);
77      }
78  
79      @Test
80      void testHasNext() throws IOException {
81          assertTrue(iterator.hasNext());
82          iterator.forEachRemaining(Path::toRealPath);
83          assertFalse(iterator.hasNext());
84      }
85  
86      @Test
87      void testNext() throws IOException {
88          assertEquals(TestConstants.ABS_PATH_A, iterator.next());
89      }
90  
91      @Test
92      void testRemove() throws IOException {
93          final Class<? extends Exception> exClass = SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8) ? IllegalStateException.class
94              : UnsupportedOperationException.class;
95          assertThrows(exClass, iterator::remove);
96          assertThrows(exClass, iterator::remove);
97          iterator.next();
98          assertThrows(UnsupportedOperationException.class, iterator::remove);
99          assertThrows(UnsupportedOperationException.class, iterator::remove);
100     }
101 
102 }