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.assertArrayEquals;
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  
24  import java.util.ListIterator;
25  import java.util.NoSuchElementException;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test the ArrayListIterator class.
31   *
32   * @param <E> the type of elements tested by this iterator.
33   */
34  public class ArrayListIteratorTest<E> extends ArrayIteratorTest<E> {
35  
36      public ArrayListIterator<E> makeArrayListIterator(final Object array) {
37          return new ArrayListIterator<>(array);
38      }
39  
40      @Override
41      public ArrayListIterator<E> makeEmptyIterator() {
42          return new ArrayListIterator<>(new Object[0]);
43      }
44  
45      @Override
46      public ArrayListIterator<E> makeObject() {
47          return new ArrayListIterator<>(testArray);
48      }
49  
50      @Override
51      public boolean supportsRemove() {
52          return false;
53      }
54  
55      /**
56       * Test the basic ListIterator functionality - going backwards using
57       * {@code previous()}.
58       */
59      @Test
60      public void testListIterator() {
61          final ListIterator<E> iter = makeObject();
62          // TestArrayIterator#testIterator() has already tested the iterator forward,
63          //  now we need to test it in reverse
64          // fast-forward the iterator to the end...
65          while (iter.hasNext()) {
66              iter.next();
67          }
68          for (int x = testArray.length - 1; x >= 0; x--) {
69              final Object testValue = testArray[x];
70              final Object iterValue = iter.previous();
71              assertEquals(testValue, iterValue, "Iteration value is correct");
72          }
73          assertFalse(iter.hasPrevious(), "Iterator should now be empty");
74          assertThrows(NoSuchElementException.class, iter::previous);
75      }
76  
77      /**
78       * Tests the {@link java.util.ListIterator#set} operation.
79       */
80      @Test
81      @SuppressWarnings("unchecked")
82      public void testListIteratorSet() {
83          final String[] testData = { "a", "b", "c" };
84  
85          final String[] result = { "0", "1", "2" };
86  
87          ListIterator<E> iter = makeArrayListIterator(testData);
88          int x = 0;
89  
90          while (iter.hasNext()) {
91              iter.next();
92              iter.set((E) Integer.toString(x));
93              x++;
94          }
95  
96          assertArrayEquals(testData, result, "The two arrays should have the same value, i.e. {0,1,2}");
97  
98          // a call to set() before a call to next() or previous() should throw an IllegalStateException
99          iter = makeArrayListIterator(testArray);
100 
101         final ListIterator<E> finalIter = iter;
102         assertThrows(IllegalStateException.class, () -> finalIter.set((E) "should fail"), "ListIterator#set should fail if next() or previous() have not yet been called.");
103     }
104 
105 }