001/*
002 * Copyright (C) 2013 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.Helpers.getMethod;
020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
021import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
023import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
024import static com.google.common.collect.testing.features.CollectionSize.ZERO;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.annotations.GwtIncompatible;
028import com.google.common.annotations.J2ktIncompatible;
029import com.google.common.collect.testing.AbstractCollectionTester;
030import com.google.common.collect.testing.SpliteratorTester;
031import com.google.common.collect.testing.features.CollectionFeature;
032import com.google.common.collect.testing.features.CollectionSize;
033import java.lang.reflect.Method;
034import java.util.Spliterator;
035import org.junit.Ignore;
036
037/**
038 * A generic JUnit test which tests {@code spliterator} operations on a collection. Can't be invoked
039 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
040 *
041 * @author Louis Wasserman
042 */
043@GwtCompatible(emulated = true)
044@Ignore("test runners must not instantiate and run this directly, only via suites we build")
045// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
046@SuppressWarnings("JUnit4ClassUsedInJUnit3")
047public class CollectionSpliteratorTester<E> extends AbstractCollectionTester<E> {
048
049  @CollectionFeature.Require(absent = KNOWN_ORDER)
050  public void testSpliteratorUnknownOrder() {
051    SpliteratorTester.of(collection::spliterator).expect(getSampleElements());
052  }
053
054  @CollectionFeature.Require(KNOWN_ORDER)
055  public void testSpliteratorKnownOrder() {
056    SpliteratorTester.of(collection::spliterator).expect(getOrderedElements()).inOrder();
057  }
058
059  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
060  @CollectionSize.Require(absent = ZERO)
061  public void testSpliteratorNullable() {
062    initCollectionWithNullElement();
063    assertFalse(collection.spliterator().hasCharacteristics(Spliterator.NONNULL));
064  }
065
066  @CollectionFeature.Require(SUPPORTS_ADD)
067  public void testSpliteratorNotImmutable_collectionAllowsAdd() {
068    // If add is supported, verify that IMMUTABLE is not reported.
069    assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
070  }
071
072  @CollectionFeature.Require(SUPPORTS_REMOVE)
073  public void testSpliteratorNotImmutable_collectionAllowsRemove() {
074    // If remove is supported, verify that IMMUTABLE is not reported.
075    assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
076  }
077
078  @J2ktIncompatible
079  @GwtIncompatible // reflection
080  public static Method getSpliteratorNotImmutableCollectionAllowsAddMethod() {
081    return getMethod(
082        CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_collectionAllowsAdd");
083  }
084
085  @J2ktIncompatible
086  @GwtIncompatible // reflection
087  public static Method getSpliteratorNotImmutableCollectionAllowsRemoveMethod() {
088    return getMethod(
089        CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_collectionAllowsRemove");
090  }
091}