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.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  public 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      public void testAdapt() throws IOException {
55          assertEquals(TestConstants.ABS_PATH_A, iterator.next());
56      }
57  
58      @Test
59      public void testAsIterator() {
60          final Iterator<Path> asIterator = iterator.asIterator();
61          assertTrue(asIterator.hasNext());
62          assertEquals(TestConstants.ABS_PATH_A, asIterator.next());
63          assertThrows(UnsupportedOperationException.class, asIterator::remove);
64      }
65  
66      @Test
67      public void testForEachRemaining() throws IOException {
68          final List<Path> list = new ArrayList<>();
69          iterator.forEachRemaining(p -> list.add(p.toRealPath()));
70          assertFalse(iterator.hasNext());
71          assertEquals(newPathList(), list);
72      }
73  
74      @Test
75      public void testHasNext() throws IOException {
76          assertTrue(iterator.hasNext());
77          iterator.forEachRemaining(Path::toRealPath);
78          assertFalse(iterator.hasNext());
79      }
80  
81      @Test
82      public void testNext() throws IOException {
83          assertEquals(TestConstants.ABS_PATH_A, iterator.next());
84      }
85  
86      @Test
87      public void testRemove() throws IOException {
88          final Class<? extends Exception> exClass = SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8) ? IllegalStateException.class
89              : UnsupportedOperationException.class;
90          assertThrows(exClass, iterator::remove);
91          assertThrows(exClass, iterator::remove);
92          iterator.next();
93          assertThrows(UnsupportedOperationException.class, iterator::remove);
94          assertThrows(UnsupportedOperationException.class, iterator::remove);
95      }
96  
97  }