| 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 |
|
| 17 |
package com.google.common.collect.testing.testers; |
| 18 |
|
| 19 |
import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE; |
| 20 |
import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE; |
| 21 |
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; |
| 22 |
import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX; |
| 23 |
import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET; |
| 24 |
import static com.google.common.collect.testing.testers.Platform.listListIteratorTesterNumIterations; |
| 25 |
import static java.util.Collections.singleton; |
| 26 |
|
| 27 |
import com.google.common.annotations.GwtCompatible; |
| 28 |
import com.google.common.annotations.GwtIncompatible; |
| 29 |
import com.google.common.collect.testing.Helpers; |
| 30 |
import com.google.common.collect.testing.IteratorFeature; |
| 31 |
import com.google.common.collect.testing.ListIteratorTester; |
| 32 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 33 |
import com.google.common.collect.testing.features.ListFeature; |
| 34 |
import java.lang.reflect.Method; |
| 35 |
import java.util.List; |
| 36 |
import java.util.ListIterator; |
| 37 |
import java.util.Set; |
| 38 |
import java.util.concurrent.CopyOnWriteArraySet; |
| 39 |
import org.junit.Ignore; |
| 40 |
|
| 41 |
/** |
| 42 |
* A generic JUnit test which tests {@code listIterator} operations on a list. Can't be invoked |
| 43 |
* directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. |
| 44 |
* |
| 45 |
* @author Chris Povirk |
| 46 |
* @author Kevin Bourrillion |
| 47 |
*/ |
| 48 |
@GwtCompatible(emulated = true) |
| 49 |
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. |
| 50 |
public class ListListIteratorTester<E> extends AbstractListTester<E> { |
| 51 |
@CollectionFeature.Require(absent = SUPPORTS_REMOVE) |
| 52 |
@ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX}) |
| 53 |
public void testListIterator_unmodifiable() { |
| 54 |
runListIteratorTest(UNMODIFIABLE); |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* For now, we don't cope with testing this when the list supports only some |
| 59 |
* modification operations. |
| 60 |
*/ |
| 61 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 62 |
@ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX}) |
| 63 |
public void testListIterator_fullyModifiable() { |
| 64 |
runListIteratorTest(MODIFIABLE); |
| 65 |
} |
| 66 |
|
| 67 |
private void runListIteratorTest(Set<IteratorFeature> features) { |
| 68 |
new ListIteratorTester<E>( |
| 69 |
listListIteratorTesterNumIterations(), |
| 70 |
singleton(e4()), |
| 71 |
features, |
| 72 |
Helpers.copyToList(getOrderedElements()), |
| 73 |
0) { |
| 74 |
@Override |
| 75 |
protected ListIterator<E> newTargetIterator() { |
| 76 |
resetCollection(); |
| 77 |
return getList().listIterator(); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
protected void verify(List<E> elements) { |
| 82 |
expectContents(elements); |
| 83 |
} |
| 84 |
}.test(); |
| 85 |
} |
| 86 |
|
| 87 |
public void testListIterator_tooLow() { |
| 88 |
try { |
| 89 |
getList().listIterator(-1); |
| 90 |
fail(); |
| 91 |
} catch (IndexOutOfBoundsException expected) { |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public void testListIterator_tooHigh() { |
| 96 |
try { |
| 97 |
getList().listIterator(getNumElements() + 1); |
| 98 |
fail(); |
| 99 |
} 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 |
} |