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  public class ArrayIteratorTest<E> extends AbstractIteratorTest<E> {
34  
35      protected String[] testArray = { "One", "Two", "Three" };
36  
37      public ArrayIteratorTest() {
38          super(ArrayIteratorTest.class.getSimpleName());
39      }
40  
41      @Override
42      public ArrayIterator<E> makeEmptyIterator() {
43          return new ArrayIterator<>(new Object[0]);
44      }
45  
46      @Override
47      public ArrayIterator<E> makeObject() {
48          return new ArrayIterator<>(testArray);
49      }
50  
51      @Override
52      public boolean supportsRemove() {
53          return false;
54      }
55  
56      @Test
57      public void testIterator() {
58          final Iterator<E> iter = makeObject();
59          for (final String testValue : testArray) {
60              final E iterValue = iter.next();
61  
62              assertEquals(testValue, iterValue, "Iteration value is correct");
63          }
64  
65          assertFalse(iter.hasNext(), "Iterator should now be empty");
66  
67          assertThrows(NoSuchElementException.class, iter::next, "NoSuchElementException must be thrown");
68      }
69  
70      @Test
71      public void testNullArray() {
72          assertThrows(NullPointerException.class, () -> new ArrayIterator<>(null));
73      }
74  
75      @Test
76      public void testReset() {
77          final ArrayIterator<E> it = makeObject();
78          it.next();
79          it.reset();
80          assertEquals("One", it.next());
81      }
82  
83  }