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