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