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 java.util.Collections.singleton;
028import static org.junit.Assert.assertThrows;
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;
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
053@NullMarked
054public class ListListIteratorTester<E extends @Nullable Object> extends AbstractListTester<E> {
055  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
056  @ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
057  public void testListIterator_unmodifiable() {
058    runListIteratorTest(UNMODIFIABLE);
059  }
060
061  /*
062   * For now, we don't cope with testing this when the list supports only some
063   * modification operations.
064   */
065  @CollectionFeature.Require(SUPPORTS_REMOVE)
066  @ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
067  public void testListIterator_fullyModifiable() {
068    runListIteratorTest(MODIFIABLE);
069  }
070
071  private void runListIteratorTest(Set<IteratorFeature> features) {
072    new ListIteratorTester<E>(
073        listListIteratorTesterNumIterations(),
074        singleton(e4()),
075        features,
076        copyToList(getOrderedElements()),
077        0) {
078      @Override
079      protected ListIterator<E> newTargetIterator() {
080        resetCollection();
081        return getList().listIterator();
082      }
083
084      @Override
085      protected void verify(List<E> elements) {
086        expectContents(elements);
087      }
088    }.test();
089  }
090
091  public void testListIterator_tooLow() {
092    assertThrows(IndexOutOfBoundsException.class, () -> getList().listIterator(-1));
093  }
094
095  public void testListIterator_tooHigh() {
096    assertThrows(
097        IndexOutOfBoundsException.class, () -> getList().listIterator(getNumElements() + 1));
098  }
099
100  public void testListIterator_atSize() {
101    assertNotNull(getList().listIterator(getNumElements()));
102    // TODO: run the iterator through ListIteratorTester
103  }
104
105  /**
106   * Returns the {@link Method} instance for {@link #testListIterator_fullyModifiable()} so that
107   * tests of {@link CopyOnWriteArraySet} can suppress it with {@code
108   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
109   * href="https://bugs.openjdk.org/browse/JDK-6570575">JDK-6570575</a> is fixed.
110   */
111  @J2ktIncompatible
112  @GwtIncompatible // reflection
113  public static Method getListIteratorFullyModifiableMethod() {
114    return getMethod(ListListIteratorTester.class, "testListIterator_fullyModifiable");
115  }
116
117  /**
118   * Returns the {@link Method} instance for {@link #testListIterator_unmodifiable()} so that it can
119   * be suppressed in GWT tests.
120   */
121  @J2ktIncompatible
122  @GwtIncompatible // reflection
123  public static Method getListIteratorUnmodifiableMethod() {
124    return getMethod(ListListIteratorTester.class, "testListIterator_unmodifiable");
125  }
126}