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