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;
030
031/**
032 * A generic JUnit test which tests {@code containsAll()} operations on a collection. Can't be
033 * invoked directly; please see {@link
034 * com.google.common.collect.testing.CollectionTestSuiteBuilder}.
035 *
036 * @author Kevin Bourrillion
037 * @author Chris Povirk
038 */
039@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
040@GwtCompatible
041public class CollectionContainsAllTester<E> extends AbstractCollectionTester<E> {
042  public void testContainsAll_empty() {
043    assertTrue(
044        "containsAll(empty) should return true", collection.containsAll(MinimalCollection.of()));
045  }
046
047  @CollectionSize.Require(absent = ZERO)
048  public void testContainsAll_subset() {
049    assertTrue(
050        "containsAll(subset) should return true",
051        collection.containsAll(MinimalCollection.of(e0())));
052  }
053
054  public void testContainsAll_sameElements() {
055    assertTrue(
056        "containsAll(sameElements) should return true",
057        collection.containsAll(MinimalCollection.of(createSamplesArray())));
058  }
059
060  @SuppressWarnings("ModifyingCollectionWithItself")
061  public void testContainsAll_self() {
062    assertTrue("containsAll(this) should return true", collection.containsAll(collection));
063  }
064
065  public void testContainsAll_partialOverlap() {
066    assertFalse(
067        "containsAll(partialOverlap) should return false",
068        collection.containsAll(MinimalCollection.of(e0(), e3())));
069  }
070
071  public void testContainsAll_disjoint() {
072    assertFalse(
073        "containsAll(disjoint) should return false",
074        collection.containsAll(MinimalCollection.of(e3())));
075  }
076
077  @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
078  public void testContainsAll_nullNotAllowed() {
079    try {
080      assertFalse(collection.containsAll(MinimalCollection.of((E) null)));
081    } catch (NullPointerException tolerated) {
082    }
083  }
084
085  @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
086  public void testContainsAll_nullAllowed() {
087    assertFalse(collection.containsAll(MinimalCollection.of((E) null)));
088  }
089
090  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
091  @CollectionSize.Require(absent = ZERO)
092  public void testContainsAll_nullPresent() {
093    initCollectionWithNullElement();
094    assertTrue(collection.containsAll(MinimalCollection.of((E) null)));
095  }
096
097  public void testContainsAll_wrongType() {
098    Collection<WrongType> wrong = MinimalCollection.of(WrongType.VALUE);
099    try {
100      assertFalse(
101          "containsAll(wrongType) should return false or throw", collection.containsAll(wrong));
102    } catch (ClassCastException tolerated) {
103    }
104  }
105}