Java
175 строк · 6.3 Кб
1/*
2* Copyright (C) 2013 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.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
20import static com.google.common.collect.testing.features.CollectionSize.ONE;
21import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
23import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
24import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
25import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
26import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
27
28import com.google.common.annotations.GwtCompatible;
29import com.google.common.annotations.GwtIncompatible;
30import com.google.common.annotations.J2ktIncompatible;
31import com.google.common.collect.testing.AbstractMapTester;
32import com.google.common.collect.testing.Helpers;
33import com.google.common.collect.testing.features.CollectionFeature;
34import com.google.common.collect.testing.features.CollectionSize;
35import com.google.common.collect.testing.features.MapFeature;
36import java.lang.reflect.Method;
37import java.util.Iterator;
38import java.util.Map.Entry;
39import java.util.Set;
40import org.junit.Ignore;
41
42/**
43* Tests {@link java.util.Map#entrySet}.
44*
45* @author Louis Wasserman
46* @param <K> The key type of the map implementation under test.
47* @param <V> The value type of the map implementation under test.
48*/
49@GwtCompatible
50@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
51@SuppressWarnings("JUnit4ClassUsedInJUnit3")
52public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
53private enum IncomparableType {
54INSTANCE;
55}
56
57@CollectionSize.Require(ONE)
58@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
59public void testEntrySetIteratorRemove() {
60Set<Entry<K, V>> entrySet = getMap().entrySet();
61Iterator<Entry<K, V>> entryItr = entrySet.iterator();
62assertEquals(e0(), entryItr.next());
63entryItr.remove();
64assertTrue(getMap().isEmpty());
65assertFalse(entrySet.contains(e0()));
66}
67
68public void testContainsEntryWithIncomparableKey() {
69try {
70assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0())));
71} catch (ClassCastException acceptable) {
72// allowed by the spec
73}
74}
75
76public void testContainsEntryWithIncomparableValue() {
77try {
78assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE)));
79} catch (ClassCastException acceptable) {
80// allowed by the spec
81}
82}
83
84@MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
85public void testContainsEntryWithNullKeyAbsent() {
86assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0())));
87}
88
89@CollectionSize.Require(absent = ZERO)
90@MapFeature.Require(ALLOWS_NULL_KEYS)
91public void testContainsEntryWithNullKeyPresent() {
92initMapWithNullKey();
93assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
94}
95
96@MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
97public void testContainsEntryWithNullValueAbsent() {
98assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null)));
99}
100
101@CollectionSize.Require(absent = ZERO)
102@MapFeature.Require(ALLOWS_NULL_VALUES)
103public void testContainsEntryWithNullValuePresent() {
104initMapWithNullValue();
105assertTrue(getMap().entrySet().contains(Helpers.mapEntry(getKeyForNullValue(), null)));
106}
107
108@MapFeature.Require(SUPPORTS_PUT)
109@CollectionSize.Require(absent = ZERO)
110public void testSetValue() {
111for (Entry<K, V> entry : getMap().entrySet()) {
112if (entry.getKey().equals(k0())) {
113assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
114break;
115}
116}
117expectReplacement(entry(k0(), v3()));
118}
119
120@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
121@CollectionSize.Require(absent = ZERO)
122public void testSetValueWithNullValuesPresent() {
123for (Entry<K, V> entry : getMap().entrySet()) {
124if (entry.getKey().equals(k0())) {
125assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(null));
126break;
127}
128}
129expectReplacement(entry(k0(), (V) null));
130}
131
132@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
133@CollectionSize.Require(absent = ZERO)
134public void testSetValueWithNullValuesAbsent() {
135for (Entry<K, V> entry : getMap().entrySet()) {
136try {
137entry.setValue(null);
138fail("Expected NullPointerException");
139} catch (NullPointerException exception) {
140break;
141}
142}
143expectUnchanged();
144}
145
146@J2ktIncompatible
147@GwtIncompatible // reflection
148public static Method getContainsEntryWithIncomparableKeyMethod() {
149return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
150}
151
152@J2ktIncompatible
153@GwtIncompatible // reflection
154public static Method getContainsEntryWithIncomparableValueMethod() {
155return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
156}
157
158@J2ktIncompatible
159@GwtIncompatible // reflection
160public static Method getSetValueMethod() {
161return Helpers.getMethod(MapEntrySetTester.class, "testSetValue");
162}
163
164@J2ktIncompatible
165@GwtIncompatible // reflection
166public static Method getSetValueWithNullValuesPresentMethod() {
167return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent");
168}
169
170@J2ktIncompatible
171@GwtIncompatible // reflection
172public static Method getSetValueWithNullValuesAbsentMethod() {
173return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent");
174}
175}
176