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