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