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.assertAll;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.fail;
24  
25  import java.util.Iterator;
26  import java.util.NoSuchElementException;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests the ArrayIterator with primitive type arrays.
32   */
33  public class ArrayIterator2Test<E> extends AbstractIteratorTest<E> {
34  
35      protected int[] testArray = { 2, 4, 6, 8 };
36  
37      public ArrayIterator2Test() {
38          super(ArrayIterator2Test.class.getSimpleName());
39      }
40  
41      public ArrayIterator<E> makeArrayIterator(final Object array) {
42          return new ArrayIterator<>(array);
43      }
44  
45      public ArrayIterator<E> makeArrayIterator(final Object array, final int index) {
46          return new ArrayIterator<>(array, index);
47      }
48  
49      public ArrayIterator<E> makeArrayIterator(final Object array, final int start, final int end) {
50          return new ArrayIterator<>(array, start, end);
51      }
52  
53      @Override
54      public ArrayIterator<E> makeEmptyIterator() {
55          return new ArrayIterator<>(new int[0]);
56      }
57  
58      @Override
59      public ArrayIterator<E> makeObject() {
60          return new ArrayIterator<>(testArray);
61      }
62  
63      @Override
64      public boolean supportsRemove() {
65          return false;
66      }
67  
68      @Test
69      public void testIndexedArray() {
70          Iterator<E> iter = makeArrayIterator(testArray, 2);
71          int count = 0;
72          while (iter.hasNext()) {
73              ++count;
74              iter.next();
75          }
76  
77          assertEquals(count, testArray.length - 2, "the count should be right using ArrayIterator(Object,2) ");
78  
79          iter = makeArrayIterator(testArray, 1, testArray.length - 1);
80          count = 0;
81          while (iter.hasNext()) {
82              ++count;
83              iter.next();
84          }
85  
86          assertEquals(
87                  count,
88                  testArray.length - 2,
89                  "the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ");
90          assertAll(
91                  () -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, -1),
92                          "new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException"),
93                  () -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, testArray.length + 1),
94                          "new ArrayIterator(Object,length+1) should throw an ArrayIndexOutOfBoundsException"),
95                  () -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, 0, -1),
96                          "new ArrayIterator(Object,0,-1) should throw an ArrayIndexOutOfBoundsException"),
97                  () -> assertThrows(ArrayIndexOutOfBoundsException.class, () -> makeArrayIterator(testArray, 0, testArray.length + 1),
98                          "new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException"),
99                  () -> assertThrows(IllegalArgumentException.class, () -> makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2),
100                         "new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException")
101         );
102 
103         try {
104             iter = makeArrayIterator(testArray, 1, 1);
105             // expected not to fail
106         } catch (final IllegalArgumentException iae) {
107             // MODIFIED: an iterator over a zero-length section of array
108             //  should be perfectly legal behavior
109             fail("new ArrayIterator(Object,1,1) should NOT throw an IllegalArgumentException");
110         }
111     }
112 
113     @Test
114     public void testIterator() {
115         final Iterator<E> iter = makeObject();
116         for (final int element : testArray) {
117             final Integer testValue = Integer.valueOf(element);
118             final Number iterValue = (Number) iter.next();
119 
120             assertEquals(testValue, iterValue, "Iteration value is correct");
121         }
122 
123         assertFalse(iter.hasNext(), "Iterator should now be empty");
124 
125         try {
126             iter.next();
127         } catch (final Exception e) {
128             assertEquals(e.getClass(), new NoSuchElementException().getClass(), "NoSuchElementException must be thrown");
129         }
130     }
131 
132 }