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