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