View Javadoc
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.features.CollectionFeature.ALLOWS_NULL_QUERIES;
20  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
21  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.testing.AbstractCollectionTester;
25  import com.google.common.collect.testing.MinimalCollection;
26  import com.google.common.collect.testing.WrongType;
27  import com.google.common.collect.testing.features.CollectionFeature;
28  import com.google.common.collect.testing.features.CollectionSize;
29  import java.util.Collection;
30  
31  /**
32   * A generic JUnit test which tests {@code containsAll()} operations on a
33   * collection. Can't be invoked directly; please see
34   * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
35   *
36   * @author Kevin Bourrillion
37   * @author Chris Povirk
38   */
39  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
40  @GwtCompatible
41  public class CollectionContainsAllTester<E> extends AbstractCollectionTester<E> {
42    public void testContainsAll_empty() {
43      assertTrue(
44          "containsAll(empty) should return true", collection.containsAll(MinimalCollection.of()));
45    }
46  
47    @CollectionSize.Require(absent = ZERO)
48    public void testContainsAll_subset() {
49      assertTrue(
50          "containsAll(subset) should return true",
51          collection.containsAll(MinimalCollection.of(e0())));
52    }
53  
54    public void testContainsAll_sameElements() {
55      assertTrue(
56          "containsAll(sameElements) should return true",
57          collection.containsAll(MinimalCollection.of(createSamplesArray())));
58    }
59  
60    @SuppressWarnings("ModifyingCollectionWithItself")
61    public void testContainsAll_self() {
62      assertTrue("containsAll(this) should return true", collection.containsAll(collection));
63    }
64  
65    public void testContainsAll_partialOverlap() {
66      assertFalse(
67          "containsAll(partialOverlap) should return false",
68          collection.containsAll(MinimalCollection.of(e0(), e3())));
69    }
70  
71    public void testContainsAll_disjoint() {
72      assertFalse(
73          "containsAll(disjoint) should return false",
74          collection.containsAll(MinimalCollection.of(e3())));
75    }
76  
77    @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
78    public void testContainsAll_nullNotAllowed() {
79      try {
80        assertFalse(collection.containsAll(MinimalCollection.of((E) null)));
81      } catch (NullPointerException tolerated) {
82      }
83    }
84  
85    @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
86    public void testContainsAll_nullAllowed() {
87      assertFalse(collection.containsAll(MinimalCollection.of((E) null)));
88    }
89  
90    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
91    @CollectionSize.Require(absent = ZERO)
92    public void testContainsAll_nullPresent() {
93      initCollectionWithNullElement();
94      assertTrue(collection.containsAll(MinimalCollection.of((E) null)));
95    }
96  
97    public void testContainsAll_wrongType() {
98      Collection<WrongType> wrong = MinimalCollection.of(WrongType.VALUE);
99      try {
100       assertFalse(
101           "containsAll(wrongType) should return false or throw", collection.containsAll(wrong));
102     } catch (ClassCastException tolerated) {
103     }
104   }
105 }