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.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.IOException;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicInteger;
30
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 class IOIterableTest {
38
39 private static class Fixture implements IOIterable<Path> {
40
41 List<Path> list = Arrays.asList(Paths.get("a"), Paths.get("b"));
42
43 @Override
44 public IOIterator<Path> iterator() {
45 return IOIterator.adapt(list);
46 }
47
48 @Override
49 public Iterable<Path> unwrap() {
50 return list;
51 }
52
53 }
54
55 private IOIterable<Path> iterable;
56 private Fixture fixture;
57
58 @BeforeEach
59 public void beforeEach() {
60 fixture = new Fixture();
61 iterable = fixture;
62 }
63
64 @Test
65 void testForEach() throws IOException {
66 final AtomicInteger ref = new AtomicInteger();
67 assertThrows(NullPointerException.class, () -> iterable.forEach(null));
68 iterable.forEach(e -> ref.incrementAndGet());
69 assertEquals(2, ref.get());
70 }
71
72 @Test
73 void testIterator() throws IOException {
74 final AtomicInteger ref = new AtomicInteger();
75 iterable.iterator().forEachRemaining(e -> ref.incrementAndGet());
76 assertEquals(2, ref.get());
77 }
78
79 @Test
80 void testSpliterator() throws IOException {
81 final AtomicInteger ref = new AtomicInteger();
82 iterable.spliterator().forEachRemaining(e -> ref.incrementAndGet());
83 assertEquals(2, ref.get());
84 }
85
86 @Test
87 void testUnrwap() throws IOException {
88 assertSame(fixture.list, iterable.unwrap());
89 assertSame(fixture.unwrap(), iterable.unwrap());
90 }
91 }