Java
132 строки · 4.9 Кб
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;27import java.util.Collection;28import java.util.HashMap;29import java.util.Map;30import java.util.Map.Entry;31import org.junit.Ignore;32
33/**
34* Tests {@link java.util.Map#equals}.
35*
36* @author George van den Driessche
37* @author Chris Povirk
38*/
39@GwtCompatible
40@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.41@SuppressWarnings("JUnit4ClassUsedInJUnit3")42public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> {43public void testEquals_otherMapWithSameEntries() {44assertTrue(45"A Map should equal any other Map containing the same entries.",46getMap().equals(newHashMap(getSampleEntries())));47}48
49@CollectionSize.Require(absent = CollectionSize.ZERO)50public void testEquals_otherMapWithDifferentEntries() {51Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1));52other.put(k3(), v3());53assertFalse(54"A Map should not equal another Map containing different entries.", getMap().equals(other));55}56
57@CollectionSize.Require(absent = CollectionSize.ZERO)58@MapFeature.Require(ALLOWS_NULL_KEYS)59public void testEquals_containingNullKey() {60Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);61entries.add(entry(null, v3()));62
63resetContainer(getSubjectGenerator().create(entries.toArray()));64assertTrue(65"A Map should equal any other Map containing the same entries,"66+ " even if some keys are null.",67getMap().equals(newHashMap(entries)));68}69
70@CollectionSize.Require(absent = CollectionSize.ZERO)71public void testEquals_otherContainsNullKey() {72Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);73entries.add(entry(null, v3()));74Map<K, V> other = newHashMap(entries);75
76assertFalse(77"Two Maps should not be equal if exactly one of them contains a null key.",78getMap().equals(other));79}80
81@CollectionSize.Require(absent = CollectionSize.ZERO)82@MapFeature.Require(ALLOWS_NULL_VALUES)83public void testEquals_containingNullValue() {84Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);85entries.add(entry(k3(), null));86
87resetContainer(getSubjectGenerator().create(entries.toArray()));88assertTrue(89"A Map should equal any other Map containing the same entries,"90+ " even if some values are null.",91getMap().equals(newHashMap(entries)));92}93
94@CollectionSize.Require(absent = CollectionSize.ZERO)95public void testEquals_otherContainsNullValue() {96Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);97entries.add(entry(k3(), null));98Map<K, V> other = newHashMap(entries);99
100assertFalse(101"Two Maps should not be equal if exactly one of them contains a null value.",102getMap().equals(other));103}104
105@CollectionSize.Require(absent = CollectionSize.ZERO)106public void testEquals_smallerMap() {107Collection<Entry<K, V>> fewerEntries = getSampleEntries(getNumEntries() - 1);108assertFalse(109"Maps of different sizes should not be equal.", getMap().equals(newHashMap(fewerEntries)));110}111
112public void testEquals_largerMap() {113Collection<Entry<K, V>> moreEntries = getSampleEntries(getNumEntries() + 1);114assertFalse(115"Maps of different sizes should not be equal.", getMap().equals(newHashMap(moreEntries)));116}117
118public void testEquals_list() {119assertFalse(120"A List should never equal a Map.",121getMap().equals(Helpers.copyToList(getMap().entrySet())));122}123
124private static <K, V> Map<K, V> newHashMap(125Collection<? extends Entry<? extends K, ? extends V>> entries) {126HashMap<K, V> map = new HashMap<>();127for (Entry<? extends K, ? extends V> entry : entries) {128map.put(entry.getKey(), entry.getValue());129}130return map;131}132}
133