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  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 }