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.IteratorFeature.MODIFIABLE;
20  import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
21  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
22  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.AbstractCollectionTester;
26  import com.google.common.collect.testing.Helpers;
27  import com.google.common.collect.testing.IteratorFeature;
28  import com.google.common.collect.testing.IteratorTester;
29  import com.google.common.collect.testing.features.CollectionFeature;
30  
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.NoSuchElementException;
36  import java.util.Set;
37  
38  /**
39   * A generic JUnit test which tests {@code iterator} operations on a collection.
40   * Can't be invoked directly; please see
41   * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
42   *
43   * @author Chris Povirk
44   */
45  @GwtCompatible(emulated = true)
46  public class CollectionIteratorTester<E> extends AbstractCollectionTester<E> {
47    public void testIterator() {
48      List<E> iteratorElements = new ArrayList<E>();
49      for (E element : collection) { // uses iterator()
50        iteratorElements.add(element);
51      }
52      Helpers.assertEqualIgnoringOrder(
53          Arrays.asList(createSamplesArray()), iteratorElements);
54    }
55  
56    @CollectionFeature.Require(KNOWN_ORDER)
57    public void testIterationOrdering() {
58      List<E> iteratorElements = new ArrayList<E>();
59      for (E element : collection) { // uses iterator()
60        iteratorElements.add(element);
61      }
62      List<E> expected = Helpers.copyToList(getOrderedElements());
63      assertEquals("Different ordered iteration", expected, iteratorElements);
64    }
65  
66    // TODO: switch to DerivedIteratorTestSuiteBuilder
67  
68    @CollectionFeature.Require({KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
69    public void testIterator_knownOrderRemoveSupported() {
70      runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER,
71          getOrderedElements());
72    }
73  
74    @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
75    public void testIterator_knownOrderRemoveUnsupported() {
76      runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER,
77          getOrderedElements());
78    }
79  
80    @CollectionFeature.Require(absent = KNOWN_ORDER, value = SUPPORTS_ITERATOR_REMOVE)
81    public void testIterator_unknownOrderRemoveSupported() {
82      runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER,
83          getSampleElements());
84    }
85  
86    @CollectionFeature.Require(absent = {KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
87    public void testIterator_unknownOrderRemoveUnsupported() {
88      runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER,
89          getSampleElements());
90    }
91  
92    private void runIteratorTest(Set<IteratorFeature> features,
93        IteratorTester.KnownOrder knownOrder, Iterable<E> elements) {
94      new IteratorTester<E>(Platform.collectionIteratorTesterNumIterations(), features, elements,
95          knownOrder) {
96        @Override protected Iterator<E> newTargetIterator() {
97          resetCollection();
98          return collection.iterator();
99        }
100 
101       @Override protected void verify(List<E> elements) {
102         expectContents(elements);
103       }
104     }.test();
105   }
106 
107   public void testIteratorNoSuchElementException() {
108     Iterator<E> iterator = collection.iterator();
109     while (iterator.hasNext()) {
110       iterator.next();
111     }
112 
113     try {
114       iterator.next();
115       fail("iterator.next() should throw NoSuchElementException");
116     } catch (NoSuchElementException expected) {}
117   }
118 }
119