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.
037public class SetEqualsTester<E> extends AbstractSetTester<E> {
038  public void testEquals_otherSetWithSameElements() {
039    assertTrue(
040        "A Set should equal any other Set containing the same elements.",
041        getSet().equals(MinimalSet.from(getSampleElements())));
042  }
043
044  @CollectionSize.Require(absent = CollectionSize.ZERO)
045  public void testEquals_otherSetWithDifferentElements() {
046    Collection<E> elements = getSampleElements(getNumElements() - 1);
047    elements.add(getSubjectGenerator().samples().e3());
048
049    assertFalse(
050        "A Set should not equal another Set containing different elements.",
051        getSet().equals(MinimalSet.from(elements)));
052  }
053
054  @CollectionSize.Require(absent = CollectionSize.ZERO)
055  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
056  public void testEquals_containingNull() {
057    Collection<E> elements = getSampleElements(getNumElements() - 1);
058    elements.add(null);
059
060    collection = getSubjectGenerator().create(elements.toArray());
061    assertTrue(
062        "A Set should equal any other Set containing the same elements,"
063            + " even if some elements are null.",
064        getSet().equals(MinimalSet.from(elements)));
065  }
066
067  @CollectionSize.Require(absent = CollectionSize.ZERO)
068  public void testEquals_otherContainsNull() {
069    Collection<E> elements = getSampleElements(getNumElements() - 1);
070    elements.add(null);
071    Set<E> other = MinimalSet.from(elements);
072
073    assertFalse(
074        "Two Sets should not be equal if exactly one of them contains null.",
075        getSet().equals(other));
076  }
077
078  @CollectionSize.Require(absent = CollectionSize.ZERO)
079  public void testEquals_smallerSet() {
080    Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
081    assertFalse(
082        "Sets of different sizes should not be equal.",
083        getSet().equals(MinimalSet.from(fewerElements)));
084  }
085
086  public void testEquals_largerSet() {
087    Collection<E> moreElements = getSampleElements(getNumElements() + 1);
088    assertFalse(
089        "Sets of different sizes should not be equal.",
090        getSet().equals(MinimalSet.from(moreElements)));
091  }
092
093  public void testEquals_list() {
094    assertFalse("A List should never equal a Set.", getSet().equals(Helpers.copyToList(getSet())));
095  }
096}