001/*
002 * Copyright (C) 2008 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_VALUES;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.testing.Helpers;
023import com.google.common.collect.testing.MinimalSet;
024import com.google.common.collect.testing.features.CollectionFeature;
025import com.google.common.collect.testing.features.CollectionSize;
026import java.util.Collection;
027import java.util.Set;
028import org.junit.Ignore;
029
030/**
031 * Tests {@link java.util.Set#equals}.
032 *
033 * @author George van den Driessche
034 */
035@GwtCompatible
036@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
037@SuppressWarnings("JUnit4ClassUsedInJUnit3")
038public class SetEqualsTester<E> extends AbstractSetTester<E> {
039  public void testEquals_otherSetWithSameElements() {
040    assertTrue(
041        "A Set should equal any other Set containing the same elements.",
042        getSet().equals(MinimalSet.from(getSampleElements())));
043  }
044
045  @CollectionSize.Require(absent = CollectionSize.ZERO)
046  public void testEquals_otherSetWithDifferentElements() {
047    Collection<E> elements = getSampleElements(getNumElements() - 1);
048    elements.add(getSubjectGenerator().samples().e3());
049
050    assertFalse(
051        "A Set should not equal another Set containing different elements.",
052        getSet().equals(MinimalSet.from(elements)));
053  }
054
055  @CollectionSize.Require(absent = CollectionSize.ZERO)
056  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
057  public void testEquals_containingNull() {
058    Collection<E> elements = getSampleElements(getNumElements() - 1);
059    elements.add(null);
060
061    collection = getSubjectGenerator().create(elements.toArray());
062    assertTrue(
063        "A Set should equal any other Set containing the same elements,"
064            + " even if some elements are null.",
065        getSet().equals(MinimalSet.from(elements)));
066  }
067
068  @CollectionSize.Require(absent = CollectionSize.ZERO)
069  public void testEquals_otherContainsNull() {
070    Collection<E> elements = getSampleElements(getNumElements() - 1);
071    elements.add(null);
072    Set<E> other = MinimalSet.from(elements);
073
074    assertFalse(
075        "Two Sets should not be equal if exactly one of them contains null.",
076        getSet().equals(other));
077  }
078
079  @CollectionSize.Require(absent = CollectionSize.ZERO)
080  public void testEquals_smallerSet() {
081    Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
082    assertFalse(
083        "Sets of different sizes should not be equal.",
084        getSet().equals(MinimalSet.from(fewerElements)));
085  }
086
087  public void testEquals_largerSet() {
088    Collection<E> moreElements = getSampleElements(getNumElements() + 1);
089    assertFalse(
090        "Sets of different sizes should not be equal.",
091        getSet().equals(MinimalSet.from(moreElements)));
092  }
093
094  public void testEquals_list() {
095    assertFalse("A List should never equal a Set.", getSet().equals(Helpers.copyToList(getSet())));
096  }
097}