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;
039
040/**
041 * A generic JUnit test which tests {@code listIterator} operations on a list. Can't be invoked
042 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
043 *
044 * @author Chris Povirk
045 * @author Kevin Bourrillion
046 */
047@GwtCompatible(emulated = true)
048public class ListListIteratorTester<E> extends AbstractListTester<E> {
049  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
050  @ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
051  public void testListIterator_unmodifiable() {
052    runListIteratorTest(UNMODIFIABLE);
053  }
054
055  /*
056   * For now, we don't cope with testing this when the list supports only some
057   * modification operations.
058   */
059  @CollectionFeature.Require(SUPPORTS_REMOVE)
060  @ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
061  public void testListIterator_fullyModifiable() {
062    runListIteratorTest(MODIFIABLE);
063  }
064
065  private void runListIteratorTest(Set<IteratorFeature> features) {
066    new ListIteratorTester<E>(
067        listListIteratorTesterNumIterations(),
068        singleton(e4()),
069        features,
070        Helpers.copyToList(getOrderedElements()),
071        0) {
072      @Override
073      protected ListIterator<E> newTargetIterator() {
074        resetCollection();
075        return getList().listIterator();
076      }
077
078      @Override
079      protected void verify(List<E> elements) {
080        expectContents(elements);
081      }
082    }.test();
083  }
084
085  public void testListIterator_tooLow() {
086    try {
087      getList().listIterator(-1);
088      fail();
089    } catch (IndexOutOfBoundsException expected) {
090    }
091  }
092
093  public void testListIterator_tooHigh() {
094    try {
095      getList().listIterator(getNumElements() + 1);
096      fail();
097    } catch (IndexOutOfBoundsException expected) {
098    }
099  }
100
101  public void testListIterator_atSize() {
102    getList().listIterator(getNumElements());
103    // TODO: run the iterator through ListIteratorTester
104  }
105
106  /**
107   * Returns the {@link Method} instance for {@link #testListIterator_fullyModifiable()} so that
108   * tests of {@link CopyOnWriteArraySet} can suppress it with {@code
109   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
110   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570575">Sun bug 6570575</a> is fixed.
111   */
112  @GwtIncompatible // reflection
113  public static Method getListIteratorFullyModifiableMethod() {
114    return Helpers.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  @GwtIncompatible // reflection
122  public static Method getListIteratorUnmodifiableMethod() {
123    return Helpers.getMethod(ListListIteratorTester.class, "testListIterator_unmodifiable");
124  }
125}