001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES; 020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 021import static com.google.common.collect.testing.features.CollectionSize.ZERO; 022 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.collect.testing.AbstractCollectionTester; 025import com.google.common.collect.testing.MinimalCollection; 026import com.google.common.collect.testing.WrongType; 027import com.google.common.collect.testing.features.CollectionFeature; 028import com.google.common.collect.testing.features.CollectionSize; 029import java.util.Collection; 030import org.junit.Ignore; 031 032/** 033 * A generic JUnit test which tests {@code containsAll()} operations on a collection. Can't be 034 * invoked directly; please see {@link 035 * com.google.common.collect.testing.CollectionTestSuiteBuilder}. 036 * 037 * @author Kevin Bourrillion 038 * @author Chris Povirk 039 */ 040@GwtCompatible 041@Ignore("test runners must not instantiate and run this directly, only via suites we build") 042// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 043@SuppressWarnings("JUnit4ClassUsedInJUnit3") 044public class CollectionContainsAllTester<E> extends AbstractCollectionTester<E> { 045 public void testContainsAll_empty() { 046 assertTrue( 047 "containsAll(empty) should return true", collection.containsAll(MinimalCollection.of())); 048 } 049 050 @CollectionSize.Require(absent = ZERO) 051 public void testContainsAll_subset() { 052 assertTrue( 053 "containsAll(subset) should return true", 054 collection.containsAll(MinimalCollection.of(e0()))); 055 } 056 057 public void testContainsAll_sameElements() { 058 assertTrue( 059 "containsAll(sameElements) should return true", 060 collection.containsAll(MinimalCollection.of(createSamplesArray()))); 061 } 062 063 @SuppressWarnings("ModifyingCollectionWithItself") 064 public void testContainsAll_self() { 065 assertTrue("containsAll(this) should return true", collection.containsAll(collection)); 066 } 067 068 public void testContainsAll_partialOverlap() { 069 assertFalse( 070 "containsAll(partialOverlap) should return false", 071 collection.containsAll(MinimalCollection.of(e0(), e3()))); 072 } 073 074 public void testContainsAll_disjoint() { 075 assertFalse( 076 "containsAll(disjoint) should return false", 077 collection.containsAll(MinimalCollection.of(e3()))); 078 } 079 080 @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES) 081 public void testContainsAll_nullNotAllowed() { 082 try { 083 assertFalse(collection.containsAll(MinimalCollection.of((E) null))); 084 } catch (NullPointerException tolerated) { 085 } 086 } 087 088 @CollectionFeature.Require(ALLOWS_NULL_QUERIES) 089 public void testContainsAll_nullAllowed() { 090 assertFalse(collection.containsAll(MinimalCollection.of((E) null))); 091 } 092 093 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 094 @CollectionSize.Require(absent = ZERO) 095 public void testContainsAll_nullPresent() { 096 initCollectionWithNullElement(); 097 assertTrue(collection.containsAll(MinimalCollection.of((E) null))); 098 } 099 100 public void testContainsAll_wrongType() { 101 Collection<WrongType> wrong = MinimalCollection.of(WrongType.VALUE); 102 try { 103 assertFalse( 104 "containsAll(wrongType) should return false or throw", collection.containsAll(wrong)); 105 } catch (ClassCastException tolerated) { 106 } 107 } 108}