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.junit.Ignore;
043
044/**
045 * A generic JUnit test which tests {@code iterator} operations on a collection. Can't be invoked
046 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
047 *
048 * @author Chris Povirk
049 */
050@GwtCompatible(emulated = true)
051@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
052public class CollectionIteratorTester<E> extends AbstractCollectionTester<E> {
053  public void testIterator() {
054    List<E> iteratorElements = new ArrayList<E>();
055    for (E element : collection) { // uses iterator()
056      iteratorElements.add(element);
057    }
058    Helpers.assertEqualIgnoringOrder(Arrays.asList(createSamplesArray()), iteratorElements);
059  }
060
061  @CollectionFeature.Require(KNOWN_ORDER)
062  public void testIterationOrdering() {
063    List<E> iteratorElements = new ArrayList<E>();
064    for (E element : collection) { // uses iterator()
065      iteratorElements.add(element);
066    }
067    List<E> expected = Helpers.copyToList(getOrderedElements());
068    assertEquals("Different ordered iteration", expected, iteratorElements);
069  }
070
071  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
072  @CollectionSize.Require(absent = ZERO)
073  public void testIterator_nullElement() {
074    initCollectionWithNullElement();
075    List<E> iteratorElements = new ArrayList<E>();
076    for (E element : collection) { // uses iterator()
077      iteratorElements.add(element);
078    }
079    Helpers.assertEqualIgnoringOrder(asList(createArrayWithNullElement()), iteratorElements);
080  }
081
082  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
083  @CollectionSize.Require(absent = ZERO)
084  public void testIterator_removeAffectsBackingCollection() {
085    int originalSize = collection.size();
086    Iterator<E> iterator = collection.iterator();
087    Object element = iterator.next();
088    // If it's an Entry, it may become invalid once it's removed from the Map. Copy it.
089    if (element instanceof Entry) {
090      Entry<?, ?> entry = (Entry<?, ?>) element;
091      element = mapEntry(entry.getKey(), entry.getValue());
092    }
093    assertTrue(collection.contains(element)); // sanity check
094    iterator.remove();
095    assertFalse(collection.contains(element));
096    assertEquals(originalSize - 1, collection.size());
097  }
098
099  @CollectionFeature.Require({KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
100  public void testIterator_knownOrderRemoveSupported() {
101    runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
102  }
103
104  @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
105  public void testIterator_knownOrderRemoveUnsupported() {
106    runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.KNOWN_ORDER, getOrderedElements());
107  }
108
109  @CollectionFeature.Require(absent = KNOWN_ORDER, value = SUPPORTS_ITERATOR_REMOVE)
110  public void testIterator_unknownOrderRemoveSupported() {
111    runIteratorTest(MODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
112  }
113
114  @CollectionFeature.Require(absent = {KNOWN_ORDER, SUPPORTS_ITERATOR_REMOVE})
115  public void testIterator_unknownOrderRemoveUnsupported() {
116    runIteratorTest(UNMODIFIABLE, IteratorTester.KnownOrder.UNKNOWN_ORDER, getSampleElements());
117  }
118
119  private void runIteratorTest(
120      Set<IteratorFeature> features, IteratorTester.KnownOrder knownOrder, Iterable<E> elements) {
121    new IteratorTester<E>(
122        Platform.collectionIteratorTesterNumIterations(), features, elements, knownOrder) {
123      @Override
124      protected Iterator<E> newTargetIterator() {
125        resetCollection();
126        return collection.iterator();
127      }
128
129      @Override
130      protected void verify(List<E> elements) {
131        expectContents(elements);
132      }
133    }.test();
134  }
135
136  public void testIteratorNoSuchElementException() {
137    Iterator<E> iterator = collection.iterator();
138    while (iterator.hasNext()) {
139      iterator.next();
140    }
141
142    try {
143      iterator.next();
144      fail("iterator.next() should throw NoSuchElementException");
145    } catch (NoSuchElementException expected) {
146    }
147  }
148}