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