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
23 import java.util.Iterator;
24 import java.util.NoSuchElementException;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 public class ObjectArrayIteratorTest<E> extends AbstractIteratorTest<E> {
32
33 protected String[] testArray = { "One", "Two", "Three" };
34
35 public ObjectArrayIteratorTest() {
36 super(ObjectArrayIteratorTest.class.getSimpleName());
37 }
38
39 @SuppressWarnings("unchecked")
40 public ObjectArrayIterator<E> makeArrayIterator() {
41 return new ObjectArrayIterator<>();
42 }
43
44 public ObjectArrayIterator<E> makeArrayIterator(final E[] array) {
45 return new ObjectArrayIterator<>(array);
46 }
47
48 public ObjectArrayIterator<E> makeArrayIterator(final E[] array, final int index) {
49 return new ObjectArrayIterator<>(array, index);
50 }
51
52 public ObjectArrayIterator<E> makeArrayIterator(final E[] array, final int start, final int end) {
53 return new ObjectArrayIterator<>(array, start, end);
54 }
55
56 @Override
57 @SuppressWarnings("unchecked")
58 public ObjectArrayIterator<E> makeEmptyIterator() {
59 return new ObjectArrayIterator<>((E[]) new Object[0]);
60 }
61
62 @Override
63 @SuppressWarnings("unchecked")
64 public ObjectArrayIterator<E> makeObject() {
65 return new ObjectArrayIterator<>((E[]) testArray);
66 }
67
68 @Override
69 public boolean supportsRemove() {
70 return false;
71 }
72
73 @Test
74 public void testIterator() {
75 final Iterator<E> iter = makeObject();
76 for (final String testValue : testArray) {
77 final E iterValue = iter.next();
78
79 assertEquals(testValue, iterValue, "Iteration value is correct");
80 }
81
82 assertFalse(iter.hasNext(), "Iterator should now be empty");
83
84 try {
85 iter.next();
86 } catch (final Exception e) {
87 assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
88 }
89 }
90
91 @Test
92 public void testNullArray() {
93 assertThrows(NullPointerException.class, () -> makeArrayIterator(null));
94 }
95
96 @Test
97 @SuppressWarnings("unchecked")
98 public void testReset() {
99 final ObjectArrayIterator<E> it = makeArrayIterator((E[]) testArray);
100 it.next();
101 it.reset();
102 assertEquals("One", it.next());
103 }
104
105 }