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.Helpers.copyToList; 020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 021 022import com.google.common.annotations.GwtCompatible; 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("test runners must not instantiate and run this directly, only via suites we build") 037// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 038@SuppressWarnings("JUnit4ClassUsedInJUnit3") 039public class SetEqualsTester<E> extends AbstractSetTester<E> { 040 public void testEquals_otherSetWithSameElements() { 041 assertTrue( 042 "A Set should equal any other Set containing the same elements.", 043 getSet().equals(MinimalSet.from(getSampleElements()))); 044 } 045 046 @CollectionSize.Require(absent = CollectionSize.ZERO) 047 public void testEquals_otherSetWithDifferentElements() { 048 Collection<E> elements = getSampleElements(getNumElements() - 1); 049 elements.add(getSubjectGenerator().samples().e3()); 050 051 assertFalse( 052 "A Set should not equal another Set containing different elements.", 053 getSet().equals(MinimalSet.from(elements))); 054 } 055 056 @CollectionSize.Require(absent = CollectionSize.ZERO) 057 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 058 public void testEquals_containingNull() { 059 Collection<E> elements = getSampleElements(getNumElements() - 1); 060 elements.add(null); 061 062 collection = getSubjectGenerator().create(elements.toArray()); 063 assertTrue( 064 "A Set should equal any other Set containing the same elements," 065 + " even if some elements are null.", 066 getSet().equals(MinimalSet.from(elements))); 067 } 068 069 @CollectionSize.Require(absent = CollectionSize.ZERO) 070 public void testEquals_otherContainsNull() { 071 Collection<E> elements = getSampleElements(getNumElements() - 1); 072 elements.add(null); 073 Set<E> other = MinimalSet.from(elements); 074 075 assertFalse( 076 "Two Sets should not be equal if exactly one of them contains null.", 077 getSet().equals(other)); 078 } 079 080 @CollectionSize.Require(absent = CollectionSize.ZERO) 081 public void testEquals_smallerSet() { 082 Collection<E> fewerElements = getSampleElements(getNumElements() - 1); 083 assertFalse( 084 "Sets of different sizes should not be equal.", 085 getSet().equals(MinimalSet.from(fewerElements))); 086 } 087 088 public void testEquals_largerSet() { 089 Collection<E> moreElements = getSampleElements(getNumElements() + 1); 090 assertFalse( 091 "Sets of different sizes should not be equal.", 092 getSet().equals(MinimalSet.from(moreElements))); 093 } 094 095 public void testEquals_list() { 096 assertFalse("A List should never equal a Set.", getSet().equals(copyToList(getSet()))); 097 } 098}