ListListIteratorTester.java revision 7dd252788645e940eada959bdde927426e2531c9
1/*
2 * Copyright (C) 2007 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
20import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
21import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
22import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
23import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
24import static com.google.common.collect.testing.testers.Platform.listListIteratorTesterNumIterations;
25import static java.util.Collections.singleton;
26
27import com.google.common.annotations.GwtCompatible;
28import com.google.common.annotations.GwtIncompatible;
29import com.google.common.collect.testing.Helpers;
30import com.google.common.collect.testing.IteratorFeature;
31import com.google.common.collect.testing.ListIteratorTester;
32import com.google.common.collect.testing.features.CollectionFeature;
33import com.google.common.collect.testing.features.ListFeature;
34
35import java.lang.reflect.Method;
36import java.util.List;
37import java.util.ListIterator;
38import java.util.Set;
39import java.util.concurrent.CopyOnWriteArraySet;
40
41/**
42 * A generic JUnit test which tests {@code listIterator} operations on a list.
43 * Can't be invoked directly; please see
44 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
45 *
46 * <p>This class is GWT compatible.
47 *
48 * @author Chris Povirk
49 * @author Kevin Bourrillion
50 */
51@GwtCompatible(emulated = true)
52public class ListListIteratorTester<E> extends AbstractListTester<E> {
53  // TODO: switch to DerivedIteratorTestSuiteBuilder
54
55  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
56  @ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
57  public void testListIterator_unmodifiable() {
58    runListIteratorTest(UNMODIFIABLE);
59  }
60
61  /*
62   * For now, we don't cope with testing this when the list supports only some
63   * modification operations.
64   */
65  @CollectionFeature.Require(SUPPORTS_REMOVE)
66  @ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
67  public void testListIterator_fullyModifiable() {
68    runListIteratorTest(MODIFIABLE);
69  }
70
71  private void runListIteratorTest(Set<IteratorFeature> features) {
72    new ListIteratorTester<E>(
73        listListIteratorTesterNumIterations(), singleton(samples.e4), features,
74        Helpers.copyToList(getOrderedElements()), 0) {
75      {
76        // TODO: don't set this universally
77        stopTestingWhenAddThrowsException();
78      }
79
80      @Override protected ListIterator<E> newTargetIterator() {
81        resetCollection();
82        return getList().listIterator();
83      }
84
85      @Override protected void verify(List<E> elements) {
86        expectContents(elements);
87      }
88    }.test();
89  }
90
91  public void testListIterator_tooLow() {
92    try {
93      getList().listIterator(-1);
94      fail();
95    } catch (IndexOutOfBoundsException expected) {
96    }
97  }
98
99  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
114   * {@link #testListIterator_fullyModifiable()} so that tests of
115   * {@link CopyOnWriteArraySet} can suppress it with
116   * {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
117   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570575">Sun bug
118   * 6570575</a> is fixed.
119   */
120  @GwtIncompatible("reflection")
121  public static Method getListIteratorFullyModifiableMethod() {
122    return Helpers.getMethod(
123        ListListIteratorTester.class, "testListIterator_fullyModifiable");
124  }
125
126  /**
127   * Returns the {@link Method} instance for
128   * {@link #testListIterator_unmodifiable()} so that it can be suppressed in
129   * GWT tests.
130   */
131  @GwtIncompatible("reflection")
132  public static Method getListIteratorUnmodifiableMethod() {
133    return Helpers.getMethod(
134        ListListIteratorTester.class, "testListIterator_unmodifiable");
135  }
136}
137