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