1 /* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect.testing.testers; 18 19 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 21 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.collect.testing.AbstractMapTester; 24 import com.google.common.collect.testing.Helpers; 25 import com.google.common.collect.testing.features.CollectionSize; 26 import com.google.common.collect.testing.features.MapFeature; 27 import java.util.Collection; 28 import java.util.HashMap; 29 import java.util.Map; 30 import java.util.Map.Entry; 31 import org.junit.Ignore; 32 33 /** 34 * Tests {@link java.util.Map#equals}. 35 * 36 * @author George van den Driessche 37 * @author Chris Povirk 38 */ 39 @GwtCompatible 40 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 41 public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> { testEquals_otherMapWithSameEntries()42 public void testEquals_otherMapWithSameEntries() { 43 assertTrue( 44 "A Map should equal any other Map containing the same entries.", 45 getMap().equals(newHashMap(getSampleEntries()))); 46 } 47 48 @CollectionSize.Require(absent = CollectionSize.ZERO) testEquals_otherMapWithDifferentEntries()49 public void testEquals_otherMapWithDifferentEntries() { 50 Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1)); 51 other.put(k3(), v3()); 52 assertFalse( 53 "A Map should not equal another Map containing different entries.", getMap().equals(other)); 54 } 55 56 @CollectionSize.Require(absent = CollectionSize.ZERO) 57 @MapFeature.Require(ALLOWS_NULL_KEYS) testEquals_containingNullKey()58 public void testEquals_containingNullKey() { 59 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 60 entries.add(entry(null, v3())); 61 62 resetContainer(getSubjectGenerator().create(entries.toArray())); 63 assertTrue( 64 "A Map should equal any other Map containing the same entries," 65 + " even if some keys are null.", 66 getMap().equals(newHashMap(entries))); 67 } 68 69 @CollectionSize.Require(absent = CollectionSize.ZERO) testEquals_otherContainsNullKey()70 public void testEquals_otherContainsNullKey() { 71 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 72 entries.add(entry(null, v3())); 73 Map<K, V> other = newHashMap(entries); 74 75 assertFalse( 76 "Two Maps should not be equal if exactly one of them contains a null key.", 77 getMap().equals(other)); 78 } 79 80 @CollectionSize.Require(absent = CollectionSize.ZERO) 81 @MapFeature.Require(ALLOWS_NULL_VALUES) testEquals_containingNullValue()82 public void testEquals_containingNullValue() { 83 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 84 entries.add(entry(k3(), null)); 85 86 resetContainer(getSubjectGenerator().create(entries.toArray())); 87 assertTrue( 88 "A Map should equal any other Map containing the same entries," 89 + " even if some values are null.", 90 getMap().equals(newHashMap(entries))); 91 } 92 93 @CollectionSize.Require(absent = CollectionSize.ZERO) testEquals_otherContainsNullValue()94 public void testEquals_otherContainsNullValue() { 95 Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); 96 entries.add(entry(k3(), null)); 97 Map<K, V> other = newHashMap(entries); 98 99 assertFalse( 100 "Two Maps should not be equal if exactly one of them contains a null value.", 101 getMap().equals(other)); 102 } 103 104 @CollectionSize.Require(absent = CollectionSize.ZERO) testEquals_smallerMap()105 public void testEquals_smallerMap() { 106 Collection<Entry<K, V>> fewerEntries = getSampleEntries(getNumEntries() - 1); 107 assertFalse( 108 "Maps of different sizes should not be equal.", getMap().equals(newHashMap(fewerEntries))); 109 } 110 testEquals_largerMap()111 public void testEquals_largerMap() { 112 Collection<Entry<K, V>> moreEntries = getSampleEntries(getNumEntries() + 1); 113 assertFalse( 114 "Maps of different sizes should not be equal.", getMap().equals(newHashMap(moreEntries))); 115 } 116 testEquals_list()117 public void testEquals_list() { 118 assertFalse( 119 "A List should never equal a Map.", 120 getMap().equals(Helpers.copyToList(getMap().entrySet()))); 121 } 122 newHashMap( Collection<? extends Entry<? extends K, ? extends V>> entries)123 private static <K, V> HashMap<K, V> newHashMap( 124 Collection<? extends Entry<? extends K, ? extends V>> entries) { 125 HashMap<K, V> map = new HashMap<>(); 126 for (Entry<? extends K, ? extends V> entry : entries) { 127 map.put(entry.getKey(), entry.getValue()); 128 } 129 return map; 130 } 131 } 132