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.features.CollectionFeature.ALLOWS_NULL_VALUES;
020import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
021import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
023import static com.google.common.collect.testing.features.CollectionSize.ZERO;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.annotations.GwtIncompatible;
027import com.google.common.annotations.J2ktIncompatible;
028import com.google.common.collect.testing.AbstractCollectionTester;
029import com.google.common.collect.testing.Helpers;
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 // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045public class CollectionSpliteratorTester<E> extends AbstractCollectionTester<E> {
046
047  @CollectionFeature.Require(absent = KNOWN_ORDER)
048  public void testSpliteratorUnknownOrder() {
049    synchronized (collection) {
050      SpliteratorTester.of(collection::spliterator).expect(getSampleElements());
051    }
052  }
053
054  @CollectionFeature.Require(KNOWN_ORDER)
055  public void testSpliteratorKnownOrder() {
056    synchronized (collection) {
057      SpliteratorTester.of(collection::spliterator).expect(getOrderedElements()).inOrder();
058    }
059  }
060
061  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
062  @CollectionSize.Require(absent = ZERO)
063  public void testSpliteratorNullable() {
064    initCollectionWithNullElement();
065    synchronized (collection) { // for Collections.synchronized
066      assertFalse(collection.spliterator().hasCharacteristics(Spliterator.NONNULL));
067    }
068  }
069
070  @CollectionFeature.Require(SUPPORTS_ADD)
071  public void testSpliteratorNotImmutable_CollectionAllowsAdd() {
072    // If add is supported, verify that IMMUTABLE is not reported.
073    synchronized (collection) { // for Collections.synchronized
074      assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
075    }
076  }
077
078  @CollectionFeature.Require(SUPPORTS_REMOVE)
079  public void testSpliteratorNotImmutable_CollectionAllowsRemove() {
080    // If remove is supported, verify that IMMUTABLE is not reported.
081    synchronized (collection) { // for Collections.synchronized
082      assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE));
083    }
084  }
085
086  @J2ktIncompatible
087  @GwtIncompatible // reflection
088  public static Method getSpliteratorNotImmutableCollectionAllowsAddMethod() {
089    return Helpers.getMethod(
090        CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_CollectionAllowsAdd");
091  }
092
093  @J2ktIncompatible
094  @GwtIncompatible // reflection
095  public static Method getSpliteratorNotImmutableCollectionAllowsRemoveMethod() {
096    return Helpers.getMethod(
097        CollectionSpliteratorTester.class, "testSpliteratorNotImmutable_CollectionAllowsRemove");
098  }
099}