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