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.collect.testing.Helpers;
030import com.google.common.collect.testing.IteratorFeature;
031import com.google.common.collect.testing.ListIteratorTester;
032import com.google.common.collect.testing.features.CollectionFeature;
033import com.google.common.collect.testing.features.ListFeature;
034import java.lang.reflect.Method;
035import java.util.List;
036import java.util.ListIterator;
037import java.util.Set;
038import java.util.concurrent.CopyOnWriteArraySet;
039import org.junit.Ignore;
040
041/**
042 * A generic JUnit test which tests {@code listIterator} operations on a list. Can't be invoked
043 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
044 *
045 * @author Chris Povirk
046 * @author Kevin Bourrillion
047 */
048@GwtCompatible(emulated = true)
049@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
050public class ListListIteratorTester<E> extends AbstractListTester<E> {
051  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
052  @ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
053  public void testListIterator_unmodifiable() {
054    runListIteratorTest(UNMODIFIABLE);
055  }
056
057  /*
058   * For now, we don't cope with testing this when the list supports only some
059   * modification operations.
060   */
061  @CollectionFeature.Require(SUPPORTS_REMOVE)
062  @ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
063  public void testListIterator_fullyModifiable() {
064    runListIteratorTest(MODIFIABLE);
065  }
066
067  private void runListIteratorTest(Set<IteratorFeature> features) {
068    new ListIteratorTester<E>(
069        listListIteratorTesterNumIterations(),
070        singleton(e4()),
071        features,
072        Helpers.copyToList(getOrderedElements()),
073        0) {
074      @Override
075      protected ListIterator<E> newTargetIterator() {
076        resetCollection();
077        return getList().listIterator();
078      }
079
080      @Override
081      protected void verify(List<E> elements) {
082        expectContents(elements);
083      }
084    }.test();
085  }
086
087  public void testListIterator_tooLow() {
088    try {
089      getList().listIterator(-1);
090      fail();
091    } catch (IndexOutOfBoundsException expected) {
092    }
093  }
094
095  public void testListIterator_tooHigh() {
096    try {
097      getList().listIterator(getNumElements() + 1);
098      fail();
099    } catch (IndexOutOfBoundsException expected) {
100    }
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  @GwtIncompatible // reflection
115  public static Method getListIteratorFullyModifiableMethod() {
116    return Helpers.getMethod(ListListIteratorTester.class, "testListIterator_fullyModifiable");
117  }
118
119  /**
120   * Returns the {@link Method} instance for {@link #testListIterator_unmodifiable()} so that it can
121   * be suppressed in GWT tests.
122   */
123  @GwtIncompatible // reflection
124  public static Method getListIteratorUnmodifiableMethod() {
125    return Helpers.getMethod(ListListIteratorTester.class, "testListIterator_unmodifiable");
126  }
127}