guava
174 строки · 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.
51public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
52private enum IncomparableType {
53INSTANCE;
54}
55
56@CollectionSize.Require(ONE)
57@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
58public void testEntrySetIteratorRemove() {
59Set<Entry<K, V>> entrySet = getMap().entrySet();
60Iterator<Entry<K, V>> entryItr = entrySet.iterator();
61assertEquals(e0(), entryItr.next());
62entryItr.remove();
63assertTrue(getMap().isEmpty());
64assertFalse(entrySet.contains(e0()));
65}
66
67public void testContainsEntryWithIncomparableKey() {
68try {
69assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0())));
70} catch (ClassCastException acceptable) {
71// allowed by the spec
72}
73}
74
75public void testContainsEntryWithIncomparableValue() {
76try {
77assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE)));
78} catch (ClassCastException acceptable) {
79// allowed by the spec
80}
81}
82
83@MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
84public void testContainsEntryWithNullKeyAbsent() {
85assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0())));
86}
87
88@CollectionSize.Require(absent = ZERO)
89@MapFeature.Require(ALLOWS_NULL_KEYS)
90public void testContainsEntryWithNullKeyPresent() {
91initMapWithNullKey();
92assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
93}
94
95@MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
96public void testContainsEntryWithNullValueAbsent() {
97assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null)));
98}
99
100@CollectionSize.Require(absent = ZERO)
101@MapFeature.Require(ALLOWS_NULL_VALUES)
102public void testContainsEntryWithNullValuePresent() {
103initMapWithNullValue();
104assertTrue(getMap().entrySet().contains(Helpers.mapEntry(getKeyForNullValue(), null)));
105}
106
107@MapFeature.Require(SUPPORTS_PUT)
108@CollectionSize.Require(absent = ZERO)
109public void testSetValue() {
110for (Entry<K, V> entry : getMap().entrySet()) {
111if (entry.getKey().equals(k0())) {
112assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
113break;
114}
115}
116expectReplacement(entry(k0(), v3()));
117}
118
119@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
120@CollectionSize.Require(absent = ZERO)
121public void testSetValueWithNullValuesPresent() {
122for (Entry<K, V> entry : getMap().entrySet()) {
123if (entry.getKey().equals(k0())) {
124assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(null));
125break;
126}
127}
128expectReplacement(entry(k0(), (V) null));
129}
130
131@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
132@CollectionSize.Require(absent = ZERO)
133public void testSetValueWithNullValuesAbsent() {
134for (Entry<K, V> entry : getMap().entrySet()) {
135try {
136entry.setValue(null);
137fail("Expected NullPointerException");
138} catch (NullPointerException exception) {
139break;
140}
141}
142expectUnchanged();
143}
144
145@J2ktIncompatible
146@GwtIncompatible // reflection
147public static Method getContainsEntryWithIncomparableKeyMethod() {
148return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
149}
150
151@J2ktIncompatible
152@GwtIncompatible // reflection
153public static Method getContainsEntryWithIncomparableValueMethod() {
154return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
155}
156
157@J2ktIncompatible
158@GwtIncompatible // reflection
159public static Method getSetValueMethod() {
160return Helpers.getMethod(MapEntrySetTester.class, "testSetValue");
161}
162
163@J2ktIncompatible
164@GwtIncompatible // reflection
165public static Method getSetValueWithNullValuesPresentMethod() {
166return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent");
167}
168
169@J2ktIncompatible
170@GwtIncompatible // reflection
171public static Method getSetValueWithNullValuesAbsentMethod() {
172return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent");
173}
174}
175