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