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 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   * Tests the ObjectArrayIterator.
30   *
31   * @param <E> the type of elements tested by this iterator.
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  }