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 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 }