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@SuppressWarnings("JUnit4ClassUsedInJUnit3")
053@ElementTypesAreNonnullByDefault
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        Helpers.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    try {
093      getList().listIterator(-1);
094      fail();
095    } catch (IndexOutOfBoundsException expected) {
096    }
097  }
098
099  public void testListIterator_tooHigh() {
100    try {
101      getList().listIterator(getNumElements() + 1);
102      fail();
103    } catch (IndexOutOfBoundsException expected) {
104    }
105  }
106
107  public void testListIterator_atSize() {
108    getList().listIterator(getNumElements());
109    // TODO: run the iterator through ListIteratorTester
110  }
111
112  /**
113   * Returns the {@link Method} instance for {@link #testListIterator_fullyModifiable()} so that
114   * tests of {@link CopyOnWriteArraySet} can suppress it with {@code
115   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
116   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570575">Sun bug 6570575</a> is fixed.
117   */
118  @J2ktIncompatible
119  @GwtIncompatible // reflection
120  public static Method getListIteratorFullyModifiableMethod() {
121    return Helpers.getMethod(ListListIteratorTester.class, "testListIterator_fullyModifiable");
122  }
123
124  /**
125   * Returns the {@link Method} instance for {@link #testListIterator_unmodifiable()} so that it can
126   * be suppressed in GWT tests.
127   */
128  @J2ktIncompatible
129  @GwtIncompatible // reflection
130  public static Method getListIteratorUnmodifiableMethod() {
131    return Helpers.getMethod(ListListIteratorTester.class, "testListIterator_unmodifiable");
132  }
133}