1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.iterators;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
26
27 import org.apache.commons.collections4.ResettableIterator;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33
34
35
36 public class SingletonIterator2Test<E> extends AbstractIteratorTest<E> {
37
38 private static final Object testValue = "foo";
39
40 @Override
41 @SuppressWarnings("unchecked")
42 public SingletonIterator<E> makeEmptyIterator() {
43 final SingletonIterator<E> iter = new SingletonIterator<>((E) testValue);
44 iter.next();
45 iter.remove();
46 iter.reset();
47 return iter;
48 }
49
50 @Override
51 @SuppressWarnings("unchecked")
52 public SingletonIterator<E> makeObject() {
53 return new SingletonIterator<>((E) testValue, false);
54 }
55
56 @Override
57 public boolean supportsEmptyIterator() {
58 return false;
59 }
60
61 @Override
62 public boolean supportsRemove() {
63 return false;
64 }
65
66 @Test
67 public void testIterator() {
68 final Iterator<E> iter = makeObject();
69 assertTrue(iter.hasNext(), "Iterator has a first item");
70 final E iterValue = iter.next();
71 assertEquals(testValue, iterValue, "Iteration value is correct");
72 assertFalse(iter.hasNext(), "Iterator should now be empty");
73 assertThrows(NoSuchElementException.class, iter::next);
74 }
75
76 @Test
77 public void testReset() {
78 final ResettableIterator<E> it = makeObject();
79
80 assertTrue(it.hasNext());
81 assertEquals(testValue, it.next());
82 assertFalse(it.hasNext());
83
84 it.reset();
85
86 assertTrue(it.hasNext());
87 assertEquals(testValue, it.next());
88 assertFalse(it.hasNext());
89
90 it.reset();
91 it.reset();
92
93 assertTrue(it.hasNext());
94 }
95
96 }