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