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