1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }