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 ArrayIterator to ensure that the next() method will actually
30   * perform the iteration rather than the hasNext() method.
31   * The code of this test was supplied by Mauricio S. Moura.
32   *
33   * @param <E> the type of elements tested by this iterator.
34   */
35  public class ArrayIteratorTest<E> extends AbstractIteratorTest<E> {
36  
37      protected String[] testArray = { "One", "Two", "Three" };
38  
39      @Override
40      public ArrayIterator<E> makeEmptyIterator() {
41          return new ArrayIterator<>(new Object[0]);
42      }
43  
44      @Override
45      public ArrayIterator<E> makeObject() {
46          return new ArrayIterator<>(testArray);
47      }
48  
49      @Override
50      public boolean supportsRemove() {
51          return false;
52      }
53  
54      @Test
55      public void testIterator() {
56          final Iterator<E> iter = makeObject();
57          for (final String testValue : testArray) {
58              final E iterValue = iter.next();
59  
60              assertEquals(testValue, iterValue, "Iteration value is correct");
61          }
62  
63          assertFalse(iter.hasNext(), "Iterator should now be empty");
64  
65          assertThrows(NoSuchElementException.class, iter::next, "NoSuchElementException must be thrown");
66      }
67  
68      @Test
69      public void testNullArray() {
70          assertThrows(NullPointerException.class, () -> new ArrayIterator<>(null));
71      }
72  
73      @Test
74      public void testReset() {
75          final ArrayIterator<E> it = makeObject();
76          it.next();
77          it.reset();
78          assertEquals("One", it.next());
79      }
80  
81  }