aboutsummaryrefslogtreecommitdiffstats
path: root/guava-testlib/src/com/google/common/collect/testing/testers/MapEqualsTester.java
blob: 66b71e7c2e64b0aa8e8b6db92b89bed1ce09bbe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2008 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect.testing.testers;

import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;

import com.google.common.collect.testing.AbstractMapTester;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Tests {@link java.util.Map#equals}.
 *
 * <p>This class is GWT compatible.
 *
 * @author George van den Driessche
 * @author Chris Povirk
 */
public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> {
  public void testEquals_otherMapWithSameEntries() {
    assertTrue(
        "A Map should equal any other Map containing the same entries.",
        getMap().equals(newHashMap(getSampleEntries())));
  }

  @CollectionSize.Require(absent = CollectionSize.ZERO)
  public void testEquals_otherMapWithDifferentEntries() {
    Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1));
    Entry<K, V> e3 = getSubjectGenerator().samples().e3;
    other.put(e3.getKey(), e3.getValue());
    assertFalse(
        "A Map should not equal another Map containing different entries.",
        getMap().equals(other)
    );
  }

  @CollectionSize.Require(absent = CollectionSize.ZERO)
  @MapFeature.Require(ALLOWS_NULL_KEYS)
  public void testEquals_containingNullKey() {
    Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
    entries.add(entry(null, samples.e3.getValue()));

    resetContainer(getSubjectGenerator().create(entries.toArray()));
    assertTrue("A Map should equal any other Map containing the same entries,"
        + " even if some keys are null.",
        getMap().equals(newHashMap(entries)));
  }

  @CollectionSize.Require(absent = CollectionSize.ZERO)
  public void testEquals_otherContainsNullKey() {
    Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
    entries.add(entry(null, samples.e3.getValue()));
    Map<K, V> other = newHashMap(entries);

    assertFalse(
        "Two Maps should not be equal if exactly one of them contains a null "
        + "key.",
        getMap().equals(other));
  }

  @CollectionSize.Require(absent = CollectionSize.ZERO)
  @MapFeature.Require(ALLOWS_NULL_VALUES)
  public void testEquals_containingNullValue() {
    Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
    entries.add(entry(samples.e3.getKey(), null));

    resetContainer(getSubjectGenerator().create(entries.toArray()));
    assertTrue("A Map should equal any other Map containing the same entries,"
        + " even if some values are null.",
        getMap().equals(newHashMap(entries)));
  }

  @CollectionSize.Require(absent = CollectionSize.ZERO)
  public void testEquals_otherContainsNullValue() {
    Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
    entries.add(entry(samples.e3.getKey(), null));
    Map<K, V> other = newHashMap(entries);

    assertFalse(
        "Two Maps should not be equal if exactly one of them contains a null "
        + "value.",
        getMap().equals(other));
  }

  @CollectionSize.Require(absent = CollectionSize.ZERO)
  public void testEquals_smallerMap() {
    Collection<Map.Entry<K, V>> fewerEntries
        = getSampleEntries(getNumEntries() - 1);
    assertFalse("Maps of different sizes should not be equal.",
        getMap().equals(newHashMap(fewerEntries)));
  }

  public void testEquals_largerMap() {
    Collection<Map.Entry<K, V>> moreEntries
        = getSampleEntries(getNumEntries() + 1);
    assertFalse("Maps of different sizes should not be equal.",
        getMap().equals(newHashMap(moreEntries)));
  }

  public void testEquals_list() {
    assertFalse("A List should never equal a Map.",
        getMap().equals(Helpers.copyToList(getMap().entrySet())));
  }

  private static <K, V> HashMap<K, V> newHashMap(
      Collection<? extends Map.Entry<? extends K, ? extends V>> entries) {
    HashMap<K, V> map = new HashMap<K, V>();
    for (Map.Entry<? extends K, ? extends V> entry : entries) {
      map.put(entry.getKey(), entry.getValue());
    }
    return map;
  }
}