001/* 002 * Copyright (C) 2007 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.copyToList; 020import static com.google.common.collect.testing.Helpers.getMethod; 021import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE; 022import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE; 023import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 024import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX; 025import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET; 026import static com.google.common.collect.testing.testers.Platform.listListIteratorTesterNumIterations; 027import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 028import static java.util.Collections.singleton; 029 030import com.google.common.annotations.GwtCompatible; 031import com.google.common.annotations.GwtIncompatible; 032import com.google.common.annotations.J2ktIncompatible; 033import com.google.common.collect.testing.IteratorFeature; 034import com.google.common.collect.testing.ListIteratorTester; 035import com.google.common.collect.testing.features.CollectionFeature; 036import com.google.common.collect.testing.features.ListFeature; 037import java.lang.reflect.Method; 038import java.util.List; 039import java.util.ListIterator; 040import java.util.Set; 041import java.util.concurrent.CopyOnWriteArraySet; 042import org.checkerframework.checker.nullness.qual.Nullable; 043import org.junit.Ignore; 044 045/** 046 * A generic JUnit test which tests {@code listIterator} operations on a list. Can't be invoked 047 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 048 * 049 * @author Chris Povirk 050 * @author Kevin Bourrillion 051 */ 052@GwtCompatible(emulated = true) 053@Ignore("test runners must not instantiate and run this directly, only via suites we build") 054// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 055@SuppressWarnings("JUnit4ClassUsedInJUnit3") 056@ElementTypesAreNonnullByDefault 057public class ListListIteratorTester<E extends @Nullable Object> extends AbstractListTester<E> { 058 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 059 @ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX}) 060 public void testListIterator_unmodifiable() { 061 runListIteratorTest(UNMODIFIABLE); 062 } 063 064 /* 065 * For now, we don't cope with testing this when the list supports only some 066 * modification operations. 067 */ 068 @CollectionFeature.Require(SUPPORTS_REMOVE) 069 @ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX}) 070 public void testListIterator_fullyModifiable() { 071 runListIteratorTest(MODIFIABLE); 072 } 073 074 private void runListIteratorTest(Set<IteratorFeature> features) { 075 new ListIteratorTester<E>( 076 listListIteratorTesterNumIterations(), 077 singleton(e4()), 078 features, 079 copyToList(getOrderedElements()), 080 0) { 081 @Override 082 protected ListIterator<E> newTargetIterator() { 083 resetCollection(); 084 return getList().listIterator(); 085 } 086 087 @Override 088 protected void verify(List<E> elements) { 089 expectContents(elements); 090 } 091 }.test(); 092 } 093 094 public void testListIterator_tooLow() { 095 assertThrows(IndexOutOfBoundsException.class, () -> getList().listIterator(-1)); 096 } 097 098 public void testListIterator_tooHigh() { 099 assertThrows( 100 IndexOutOfBoundsException.class, () -> getList().listIterator(getNumElements() + 1)); 101 } 102 103 public void testListIterator_atSize() { 104 getList().listIterator(getNumElements()); 105 // TODO: run the iterator through ListIteratorTester 106 } 107 108 /** 109 * Returns the {@link Method} instance for {@link #testListIterator_fullyModifiable()} so that 110 * tests of {@link CopyOnWriteArraySet} can suppress it with {@code 111 * FeatureSpecificTestSuiteBuilder.suppressing()} until <a 112 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570575">Sun bug 6570575</a> is fixed. 113 */ 114 @J2ktIncompatible 115 @GwtIncompatible // reflection 116 public static Method getListIteratorFullyModifiableMethod() { 117 return getMethod(ListListIteratorTester.class, "testListIterator_fullyModifiable"); 118 } 119 120 /** 121 * Returns the {@link Method} instance for {@link #testListIterator_unmodifiable()} so that it can 122 * be suppressed in GWT tests. 123 */ 124 @J2ktIncompatible 125 @GwtIncompatible // reflection 126 public static Method getListIteratorUnmodifiableMethod() { 127 return getMethod(ListListIteratorTester.class, "testListIterator_unmodifiable"); 128 } 129}