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