View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.iterators;
18  
19  import java.util.Iterator;
20  import java.util.NoSuchElementException;
21  
22  /**
23   * Tests the ObjectArrayIterator.
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              // expected
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 }