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.MapFeature.ALLOWS_NULL_KEYS; 020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 021 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.collect.testing.AbstractMapTester; 024import com.google.common.collect.testing.Helpers; 025import com.google.common.collect.testing.features.CollectionSize; 026import com.google.common.collect.testing.features.MapFeature; 027import java.util.Collection; 028import java.util.HashMap; 029import java.util.Map; 030import java.util.Map.Entry; 031import org.junit.Ignore; 032 033/** 034 * Tests {@link java.util.Map#equals}. 035 * 036 * @author George van den Driessche 037 * @author Chris Povirk 038 */ 039@GwtCompatible 040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 041@SuppressWarnings("JUnit4ClassUsedInJUnit3") 042public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> { 043 public void testEquals_otherMapWithSameEntries() { 044 assertTrue( 045 "A Map should equal any other Map containing the same entries.", 046 getMap().equals(newHashMap(getSampleEntries()))); 047 } 048 049 @CollectionSize.Require(absent = CollectionSize.ZERO) 050 public void testEquals_otherMapWithDifferentEntries() { 051 Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1)); 052 other.put(k3(), v3()); 053 assertFalse( 054 "A Map should not equal another Map containing different entries.", getMap().equals(other)); 055 } 056 057 @CollectionSize.Require(absent = CollectionSize.ZERO) 058 @MapFeature.Require(ALLOWS_NULL_KEYS) 059 public void testEquals_containingNullKey() { 060 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 061 entries.add(entry(null, v3())); 062 063 resetContainer(getSubjectGenerator().create(entries.toArray())); 064 assertTrue( 065 "A Map should equal any other Map containing the same entries," 066 + " even if some keys are null.", 067 getMap().equals(newHashMap(entries))); 068 } 069 070 @CollectionSize.Require(absent = CollectionSize.ZERO) 071 public void testEquals_otherContainsNullKey() { 072 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 073 entries.add(entry(null, v3())); 074 Map<K, V> other = newHashMap(entries); 075 076 assertFalse( 077 "Two Maps should not be equal if exactly one of them contains a null key.", 078 getMap().equals(other)); 079 } 080 081 @CollectionSize.Require(absent = CollectionSize.ZERO) 082 @MapFeature.Require(ALLOWS_NULL_VALUES) 083 public void testEquals_containingNullValue() { 084 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 085 entries.add(entry(k3(), null)); 086 087 resetContainer(getSubjectGenerator().create(entries.toArray())); 088 assertTrue( 089 "A Map should equal any other Map containing the same entries," 090 + " even if some values are null.", 091 getMap().equals(newHashMap(entries))); 092 } 093 094 @CollectionSize.Require(absent = CollectionSize.ZERO) 095 public void testEquals_otherContainsNullValue() { 096 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 097 entries.add(entry(k3(), null)); 098 Map<K, V> other = newHashMap(entries); 099 100 assertFalse( 101 "Two Maps should not be equal if exactly one of them contains a null value.", 102 getMap().equals(other)); 103 } 104 105 @CollectionSize.Require(absent = CollectionSize.ZERO) 106 public void testEquals_smallerMap() { 107 Collection<Entry<K, V>> fewerEntries = getSampleEntries(getNumEntries() - 1); 108 assertFalse( 109 "Maps of different sizes should not be equal.", getMap().equals(newHashMap(fewerEntries))); 110 } 111 112 public void testEquals_largerMap() { 113 Collection<Entry<K, V>> moreEntries = getSampleEntries(getNumEntries() + 1); 114 assertFalse( 115 "Maps of different sizes should not be equal.", getMap().equals(newHashMap(moreEntries))); 116 } 117 118 public void testEquals_list() { 119 assertFalse( 120 "A List should never equal a Map.", 121 getMap().equals(Helpers.copyToList(getMap().entrySet()))); 122 } 123 124 private static <K, V> Map<K, V> newHashMap( 125 Collection<? extends Entry<? extends K, ? extends V>> entries) { 126 HashMap<K, V> map = new HashMap<>(); 127 for (Entry<? extends K, ? extends V> entry : entries) { 128 map.put(entry.getKey(), entry.getValue()); 129 } 130 return map; 131 } 132}