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