View Javadoc
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  
31  /**
32   * Tests {@link java.util.Map#equals}.
33   *
34   * @author George van den Driessche
35   * @author Chris Povirk
36   */
37  @GwtCompatible
38  public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> {
39    public void testEquals_otherMapWithSameEntries() {
40      assertTrue(
41          "A Map should equal any other Map containing the same entries.",
42          getMap().equals(newHashMap(getSampleEntries())));
43    }
44  
45    @CollectionSize.Require(absent = CollectionSize.ZERO)
46    public void testEquals_otherMapWithDifferentEntries() {
47      Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1));
48      other.put(k3(), v3());
49      assertFalse(
50          "A Map should not equal another Map containing different entries.", getMap().equals(other));
51    }
52  
53    @CollectionSize.Require(absent = CollectionSize.ZERO)
54    @MapFeature.Require(ALLOWS_NULL_KEYS)
55    public void testEquals_containingNullKey() {
56      Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
57      entries.add(entry(null, v3()));
58  
59      resetContainer(getSubjectGenerator().create(entries.toArray()));
60      assertTrue(
61          "A Map should equal any other Map containing the same entries,"
62              + " even if some keys are null.",
63          getMap().equals(newHashMap(entries)));
64    }
65  
66    @CollectionSize.Require(absent = CollectionSize.ZERO)
67    public void testEquals_otherContainsNullKey() {
68      Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
69      entries.add(entry(null, v3()));
70      Map<K, V> other = newHashMap(entries);
71  
72      assertFalse(
73          "Two Maps should not be equal if exactly one of them contains a null key.",
74          getMap().equals(other));
75    }
76  
77    @CollectionSize.Require(absent = CollectionSize.ZERO)
78    @MapFeature.Require(ALLOWS_NULL_VALUES)
79    public void testEquals_containingNullValue() {
80      Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
81      entries.add(entry(k3(), null));
82  
83      resetContainer(getSubjectGenerator().create(entries.toArray()));
84      assertTrue(
85          "A Map should equal any other Map containing the same entries,"
86              + " even if some values are null.",
87          getMap().equals(newHashMap(entries)));
88    }
89  
90    @CollectionSize.Require(absent = CollectionSize.ZERO)
91    public void testEquals_otherContainsNullValue() {
92      Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
93      entries.add(entry(k3(), null));
94      Map<K, V> other = newHashMap(entries);
95  
96      assertFalse(
97          "Two Maps should not be equal if exactly one of them contains a null value.",
98          getMap().equals(other));
99    }
100 
101   @CollectionSize.Require(absent = CollectionSize.ZERO)
102   public void testEquals_smallerMap() {
103     Collection<Map.Entry<K, V>> fewerEntries = getSampleEntries(getNumEntries() - 1);
104     assertFalse(
105         "Maps of different sizes should not be equal.", getMap().equals(newHashMap(fewerEntries)));
106   }
107 
108   public void testEquals_largerMap() {
109     Collection<Map.Entry<K, V>> moreEntries = getSampleEntries(getNumEntries() + 1);
110     assertFalse(
111         "Maps of different sizes should not be equal.", getMap().equals(newHashMap(moreEntries)));
112   }
113 
114   public void testEquals_list() {
115     assertFalse(
116         "A List should never equal a Map.",
117         getMap().equals(Helpers.copyToList(getMap().entrySet())));
118   }
119 
120   private static <K, V> HashMap<K, V> newHashMap(
121       Collection<? extends Map.Entry<? extends K, ? extends V>> entries) {
122     HashMap<K, V> map = new HashMap<>();
123     for (Map.Entry<? extends K, ? extends V> entry : entries) {
124       map.put(entry.getKey(), entry.getValue());
125     }
126     return map;
127   }
128 }