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