View Javadoc
1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.Helpers.mapEntry;
20  import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
21  import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
22  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
23  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
24  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.collect.testing.AbstractCollectionTester;
28  import com.google.common.collect.testing.Helpers;
29  import com.google.common.collect.testing.IteratorFeature;
30  import com.google.common.collect.testing.IteratorTester;
31  import com.google.common.collect.testing.features.CollectionFeature;
32  import com.google.common.collect.testing.features.CollectionSize;
33  import java.util.ArrayList;
34  import java.util.Arrays;
35  import java.util.Iterator;
36  import java.util.List;
37  import java.util.Map.Entry;
38  import java.util.NoSuchElementException;
39  import java.util.Set;
40  
41  /**
42   * A generic JUnit test which tests {@code iterator} operations on a collection.
43   * Can't be invoked directly; please see
44   * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
45   *
46   * @author Chris Povirk
47   */
48  @GwtCompatible(emulated = true)
49  public class CollectionIteratorTester<E> extends AbstractCollectionTester<E> {
50    public void testIterator() {
51      List<E> iteratorElements = new ArrayList<E>();
52      for (E element : collection) { // uses iterator()
53        iteratorElements.add(element);
54      }
55      Helpers.assertEqualIgnoringOrder(Arrays.asList(createSamplesArray()), iteratorElements);
56    }
57  
58    @CollectionFeature.Require(KNOWN_ORDER)
59    public void testIterationOrdering() {
60      List<E> iteratorElements = new ArrayList<E>();
61      for (E element : collection) { // uses iterator()
62        iteratorElements.add(element);
63      }
64      List<E> expected = Helpers.copyToList(getOrderedElements());
65      assertEquals("Different ordered iteration", expected, iteratorElements);
66    }
67  
68    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
69    @CollectionSize.Require(absent = ZERO)
70    public void testIterator_removeAffectsBackingCollection() {
71      int originalSize = collection.size();
72      Iterator<E> iterator = collection.iterator();
73      Object element = iterator.next();
74      // If it's an Entry, it may become invalid once it's removed from the Map. Copy it.
75      if (element instanceof Entry) {
76        Entry<?, ?> entry = (Entry<?, ?>) element;
77        element = mapEntry(entry.getKey(), entry.getValue());
78      }
79      assertTrue(collection.contains(element)); // sanity check
80      iterator.remove();
81      assertFalse(collection.contains(element));
82      assertEquals(originalSize - 1, collection.size());
83    }
84  
85    @CollectionFeature.Require({KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
86    public void testIterator_knownOrderRemoveSupported() {
87      runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
88    }
89  
90    @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
91    public void testIterator_knownOrderRemoveUnsupported() {
92      runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
93    }
94  
95    @CollectionFeature.Require(absent = KNOWN_ORDER, value = SUPPORTS_ITERATOR_REMOVE)
96    public void testIterator_unknownOrderRemoveSupported() {
97      runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
98    }
99  
100   @CollectionFeature.Require(absent = {KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
101   public void testIterator_unknownOrderRemoveUnsupported() {
102     runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
103   }
104 
105   private void runIteratorTest(
106       Set<IteratorFeature> features, IteratorTester.KnownOrder knownOrder, Iterable<E> elements) {
107     new IteratorTester<E>(
108         Platform.collectionIteratorTesterNumIterations(), features, elements, knownOrder) {
109       @Override
110       protected Iterator<E> newTargetIterator() {
111         resetCollection();
112         return collection.iterator();
113       }
114 
115       @Override
116       protected void verify(List<E> elements) {
117         expectContents(elements);
118       }
119     }.test();
120   }
121 
122   public void testIteratorNoSuchElementException() {
123     Iterator<E> iterator = collection.iterator();
124     while (iterator.hasNext()) {
125       iterator.next();
126     }
127 
128     try {
129       iterator.next();
130       fail("iterator.next() should throw NoSuchElementException");
131     } catch (NoSuchElementException expected) {
132     }
133   }
134 }