| 1 |
/* |
| 2 |
* Copyright (C) 2013 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.features.CollectionFeature.ALLOWS_NULL_VALUES; |
| 20 |
import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER; |
| 21 |
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; |
| 22 |
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; |
| 23 |
import static com.google.common.collect.testing.features.CollectionSize.ZERO; |
| 24 |
|
| 25 |
import com.google.common.annotations.GwtCompatible; |
| 26 |
import com.google.common.annotations.GwtIncompatible; |
| 27 |
import com.google.common.collect.testing.AbstractCollectionTester; |
| 28 |
import com.google.common.collect.testing.Helpers; |
| 29 |
import com.google.common.collect.testing.SpliteratorTester; |
| 30 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 31 |
import com.google.common.collect.testing.features.CollectionSize; |
| 32 |
import java.lang.reflect.Method; |
| 33 |
import java.util.Spliterator; |
| 34 |
import org.junit.Ignore; |
| 35 |
|
| 36 |
/** |
| 37 |
* A generic JUnit test which tests {@code spliterator} operations on a collection. Can't be invoked |
| 38 |
* directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. |
| 39 |
* |
| 40 |
* @author Louis Wasserman |
| 41 |
*/ |
| 42 |
@GwtCompatible(emulated = true) |
| 43 |
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. |
| 44 |
public class CollectionSpliteratorTester<E> extends AbstractCollectionTester<E> { |
| 45 |
|
| 46 |
@CollectionFeature.Require(absent = KNOWN_ORDER) |
| 47 |
public void testSpliteratorUnknownOrder() { |
| 48 |
synchronized (collection) { |
| 49 |
SpliteratorTester.of(collection::spliterator).expect(getSampleElements()); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
@CollectionFeature.Require(KNOWN_ORDER) |
| 54 |
public void testSpliteratorKnownOrder() { |
| 55 |
synchronized (collection) { |
| 56 |
SpliteratorTester.of(collection::spliterator).expect(getOrderedElements()).inOrder(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@CollectionFeature.Require(ALLOWS_NULL_VALUES) |
| 61 |
@CollectionSize.Require(absent = ZERO) |
| 62 |
public void testSpliteratorNullable() { |
| 63 |
initCollectionWithNullElement(); |
| 64 |
synchronized (collection) { // for Collections.synchronized |
| 65 |
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.NONNULL)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
@CollectionFeature.Require(SUPPORTS_ADD) |
| 70 |
public void testSpliteratorNotImmutable_CollectionAllowsAdd() { |
| 71 |
// If add is supported, verify that IMMUTABLE is not reported. |
| 72 |
synchronized (collection) { // for Collections.synchronized |
| 73 |
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE)); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 78 |
public void testSpliteratorNotImmutable_CollectionAllowsRemove() { |
| 79 |
// If remove is supported, verify that IMMUTABLE is not reported. |
| 80 |
synchronized (collection) { // for Collections.synchronized |
| 81 |
assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE)); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@GwtIncompatible // reflection |
| 86 |
public static Method getSpliteratorNotImmutableCollectionAllowsAddMethod() { |
| 87 |
return Helpers.getMethod( |
| 88 |
CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_CollectionAllowsAdd"); |
| 89 |
} |
| 90 |
|
| 91 |
@GwtIncompatible // reflection |
| 92 |
public static Method getSpliteratorNotImmutableCollectionAllowsRemoveMethod() { |
| 93 |
return Helpers.getMethod( |
| 94 |
CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_CollectionAllowsRemove"); |
| 95 |
} |
| 96 |
} |