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 30 import 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 * @author Kevin Bourrillion 38 * @author Chris Povirk 39 */ 40 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 41 @GwtCompatible 42 public class CollectionContainsAllTester<E> 43 extends AbstractCollectionTester<E> { 44 public void testContainsAll_empty() { 45 assertTrue("containsAll(empty) should return true", 46 collection.containsAll(MinimalCollection.of())); 47 } 48 49 @CollectionSize.Require(absent = ZERO) 50 public void testContainsAll_subset() { 51 assertTrue("containsAll(subset) should return true", 52 collection.containsAll(MinimalCollection.of(samples.e0))); 53 } 54 55 public void testContainsAll_sameElements() { 56 assertTrue("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", 63 collection.containsAll(collection)); 64 } 65 66 public void testContainsAll_partialOverlap() { 67 assertFalse("containsAll(partialOverlap) should return false", 68 collection.containsAll(MinimalCollection.of(samples.e0, samples.e3))); 69 } 70 71 public void testContainsAll_disjoint() { 72 assertFalse("containsAll(disjoint) should return false", 73 collection.containsAll(MinimalCollection.of(samples.e3))); 74 } 75 76 @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES) 77 public void testContainsAll_nullNotAllowed() { 78 try { 79 assertFalse(collection.containsAll(MinimalCollection.of((E) null))); 80 } catch (NullPointerException tolerated) {} 81 } 82 83 @CollectionFeature.Require(ALLOWS_NULL_QUERIES) 84 public void testContainsAll_nullAllowed() { 85 assertFalse(collection.containsAll(MinimalCollection.of((E) null))); 86 } 87 88 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 89 @CollectionSize.Require(absent = ZERO) 90 public void testContainsAll_nullPresent() { 91 initCollectionWithNullElement(); 92 assertTrue(collection.containsAll(MinimalCollection.of((E) null))); 93 } 94 95 public void testContainsAll_wrongType() { 96 Collection<WrongType> wrong = MinimalCollection.of(WrongType.VALUE); 97 try { 98 assertFalse("containsAll(wrongType) should return false or throw", 99 collection.containsAll(wrong)); 100 } catch (ClassCastException tolerated) { 101 } 102 } 103 } 104