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