guava
131 строка · 4.8 Кб
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.41public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> {42public void testEquals_otherMapWithSameEntries() {43assertTrue(44"A Map should equal any other Map containing the same entries.",45getMap().equals(newHashMap(getSampleEntries())));46}47
48@CollectionSize.Require(absent = CollectionSize.ZERO)49public void testEquals_otherMapWithDifferentEntries() {50Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1));51other.put(k3(), v3());52assertFalse(53"A Map should not equal another Map containing different entries.", getMap().equals(other));54}55
56@CollectionSize.Require(absent = CollectionSize.ZERO)57@MapFeature.Require(ALLOWS_NULL_KEYS)58public void testEquals_containingNullKey() {59Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);60entries.add(entry(null, v3()));61
62resetContainer(getSubjectGenerator().create(entries.toArray()));63assertTrue(64"A Map should equal any other Map containing the same entries,"65+ " even if some keys are null.",66getMap().equals(newHashMap(entries)));67}68
69@CollectionSize.Require(absent = CollectionSize.ZERO)70public void testEquals_otherContainsNullKey() {71Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);72entries.add(entry(null, v3()));73Map<K, V> other = newHashMap(entries);74
75assertFalse(76"Two Maps should not be equal if exactly one of them contains a null key.",77getMap().equals(other));78}79
80@CollectionSize.Require(absent = CollectionSize.ZERO)81@MapFeature.Require(ALLOWS_NULL_VALUES)82public void testEquals_containingNullValue() {83Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);84entries.add(entry(k3(), null));85
86resetContainer(getSubjectGenerator().create(entries.toArray()));87assertTrue(88"A Map should equal any other Map containing the same entries,"89+ " even if some values are null.",90getMap().equals(newHashMap(entries)));91}92
93@CollectionSize.Require(absent = CollectionSize.ZERO)94public void testEquals_otherContainsNullValue() {95Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);96entries.add(entry(k3(), null));97Map<K, V> other = newHashMap(entries);98
99assertFalse(100"Two Maps should not be equal if exactly one of them contains a null value.",101getMap().equals(other));102}103
104@CollectionSize.Require(absent = CollectionSize.ZERO)105public void testEquals_smallerMap() {106Collection<Entry<K, V>> fewerEntries = getSampleEntries(getNumEntries() - 1);107assertFalse(108"Maps of different sizes should not be equal.", getMap().equals(newHashMap(fewerEntries)));109}110
111public void testEquals_largerMap() {112Collection<Entry<K, V>> moreEntries = getSampleEntries(getNumEntries() + 1);113assertFalse(114"Maps of different sizes should not be equal.", getMap().equals(newHashMap(moreEntries)));115}116
117public void testEquals_list() {118assertFalse(119"A List should never equal a Map.",120getMap().equals(Helpers.copyToList(getMap().entrySet())));121}122
123private static <K, V> HashMap<K, V> newHashMap(124Collection<? extends Entry<? extends K, ? extends V>> entries) {125HashMap<K, V> map = new HashMap<>();126for (Entry<? extends K, ? extends V> entry : entries) {127map.put(entry.getKey(), entry.getValue());128}129return map;130}131}
132