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 // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
042public class CollectionContainsAllTester<E> extends AbstractCollectionTester<E> {
043  public void testContainsAll_empty() {
044    assertTrue(
045        "containsAll(empty) should return true", collection.containsAll(MinimalCollection.of()));
046  }
047
048  @CollectionSize.Require(absent = ZERO)
049  public void testContainsAll_subset() {
050    assertTrue(
051        "containsAll(subset) should return true",
052        collection.containsAll(MinimalCollection.of(e0())));
053  }
054
055  public void testContainsAll_sameElements() {
056    assertTrue(
057        "containsAll(sameElements) should return true",
058        collection.containsAll(MinimalCollection.of(createSamplesArray())));
059  }
060
061  @SuppressWarnings("ModifyingCollectionWithItself")
062  public void testContainsAll_self() {
063    assertTrue("containsAll(this) should return true", collection.containsAll(collection));
064  }
065
066  public void testContainsAll_partialOverlap() {
067    assertFalse(
068        "containsAll(partialOverlap) should return false",
069        collection.containsAll(MinimalCollection.of(e0(), e3())));
070  }
071
072  public void testContainsAll_disjoint() {
073    assertFalse(
074        "containsAll(disjoint) should return false",
075        collection.containsAll(MinimalCollection.of(e3())));
076  }
077
078  @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
079  public void testContainsAll_nullNotAllowed() {
080    try {
081      assertFalse(collection.containsAll(MinimalCollection.of((E) null)));
082    } catch (NullPointerException tolerated) {
083    }
084  }
085
086  @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
087  public void testContainsAll_nullAllowed() {
088    assertFalse(collection.containsAll(MinimalCollection.of((E) null)));
089  }
090
091  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
092  @CollectionSize.Require(absent = ZERO)
093  public void testContainsAll_nullPresent() {
094    initCollectionWithNullElement();
095    assertTrue(collection.containsAll(MinimalCollection.of((E) null)));
096  }
097
098  public void testContainsAll_wrongType() {
099    Collection<WrongType> wrong = MinimalCollection.of(WrongType.VALUE);
100    try {
101      assertFalse(
102          "containsAll(wrongType) should return false or throw", collection.containsAll(wrong));
103    } catch (ClassCastException tolerated) {
104    }
105  }
106}