001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.Helpers.mapEntry;
020import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
021import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
022import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
023import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
024import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
025import static com.google.common.collect.testing.features.CollectionSize.ZERO;
026import static java.util.Arrays.asList;
027
028import com.google.common.annotations.GwtCompatible;
029import com.google.common.collect.testing.AbstractCollectionTester;
030import com.google.common.collect.testing.Helpers;
031import com.google.common.collect.testing.IteratorFeature;
032import com.google.common.collect.testing.IteratorTester;
033import com.google.common.collect.testing.features.CollectionFeature;
034import com.google.common.collect.testing.features.CollectionSize;
035import java.util.ArrayList;
036import java.util.Arrays;
037import java.util.Iterator;
038import java.util.List;
039import java.util.Map.Entry;
040import java.util.NoSuchElementException;
041import java.util.Set;
042import org.checkerframework.checker.nullness.qual.Nullable;
043import org.junit.Ignore;
044
045/**
046 * A generic JUnit test which tests {@code iterator} operations on a collection. Can't be invoked
047 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
048 *
049 * @author Chris Povirk
050 */
051@GwtCompatible(emulated = true)
052@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
053@SuppressWarnings("JUnit4ClassUsedInJUnit3")
054@ElementTypesAreNonnullByDefault
055public class CollectionIteratorTester<E extends @Nullable Object>
056    extends AbstractCollectionTester<E> {
057  public void testIterator() {
058    List<E> iteratorElements = new ArrayList<>();
059    for (E element : collection) { // uses iterator()
060      iteratorElements.add(element);
061    }
062    Helpers.assertEqualIgnoringOrder(Arrays.asList(createSamplesArray()), iteratorElements);
063  }
064
065  @CollectionFeature.Require(KNOWN_ORDER)
066  public void testIterationOrdering() {
067    List<E> iteratorElements = new ArrayList<>();
068    for (E element : collection) { // uses iterator()
069      iteratorElements.add(element);
070    }
071    List<E> expected = Helpers.copyToList(getOrderedElements());
072    assertEquals("Different ordered iteration", expected, iteratorElements);
073  }
074
075  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
076  @CollectionSize.Require(absent = ZERO)
077  public void testIterator_nullElement() {
078    initCollectionWithNullElement();
079    List<E> iteratorElements = new ArrayList<>();
080    for (E element : collection) { // uses iterator()
081      iteratorElements.add(element);
082    }
083    Helpers.assertEqualIgnoringOrder(asList(createArrayWithNullElement()), iteratorElements);
084  }
085
086  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
087  @CollectionSize.Require(absent = ZERO)
088  public void testIterator_removeAffectsBackingCollection() {
089    int originalSize = collection.size();
090    Iterator<E> iterator = collection.iterator();
091    Object element = iterator.next();
092    // If it's an Entry, it may become invalid once it's removed from the Map. Copy it.
093    if (element instanceof Entry) {
094      Entry<?, ?> entry = (Entry<?, ?>) element;
095      element = mapEntry(entry.getKey(), entry.getValue());
096    }
097    assertTrue(collection.contains(element)); // sanity check
098    iterator.remove();
099    assertFalse(collection.contains(element));
100    assertEquals(originalSize - 1, collection.size());
101  }
102
103  @CollectionFeature.Require({KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
104  public void testIterator_knownOrderRemoveSupported() {
105    runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
106  }
107
108  @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
109  public void testIterator_knownOrderRemoveUnsupported() {
110    runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
111  }
112
113  @CollectionFeature.Require(absent = KNOWN_ORDER, value = SUPPORTS_ITERATOR_REMOVE)
114  public void testIterator_unknownOrderRemoveSupported() {
115    runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
116  }
117
118  @CollectionFeature.Require(absent = {KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
119  public void testIterator_unknownOrderRemoveUnsupported() {
120    runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
121  }
122
123  private void runIteratorTest(
124      Set<IteratorFeature> features, IteratorTester.KnownOrder knownOrder, Iterable<E> elements) {
125    new IteratorTester<E>(
126        Platform.collectionIteratorTesterNumIterations(), features, elements, knownOrder) {
127      @Override
128      protected Iterator<E> newTargetIterator() {
129        resetCollection();
130        return collection.iterator();
131      }
132
133      @Override
134      protected void verify(List<E> elements) {
135        expectContents(elements);
136      }
137    }.test();
138  }
139
140  public void testIteratorNoSuchElementException() {
141    Iterator<E> iterator = collection.iterator();
142    while (iterator.hasNext()) {
143      iterator.next();
144    }
145
146    try {
147      iterator.next();
148      fail("iterator.next() should throw NoSuchElementException");
149    } catch (NoSuchElementException expected) {
150    }
151  }
152}