guava
265 строк · 9.4 Кб
1/*
2* Copyright (C) 2007 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.CollectionSize.ZERO;
20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
24
25import com.google.common.annotations.GwtCompatible;
26import com.google.common.annotations.GwtIncompatible;
27import com.google.common.annotations.J2ktIncompatible;
28import com.google.common.collect.testing.AbstractMapTester;
29import com.google.common.collect.testing.Helpers;
30import com.google.common.collect.testing.features.CollectionSize;
31import com.google.common.collect.testing.features.MapFeature;
32import com.google.errorprone.annotations.CanIgnoreReturnValue;
33import java.lang.reflect.Method;
34import java.util.ConcurrentModificationException;
35import java.util.Iterator;
36import java.util.Map.Entry;
37import org.junit.Ignore;
38
39/**
40* A generic JUnit test which tests {@code put} operations on a map. Can't be invoked directly;
41* please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
42*
43* @author Chris Povirk
44* @author Kevin Bourrillion
45*/
46@GwtCompatible(emulated = true)
47@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
48public class MapPutTester<K, V> extends AbstractMapTester<K, V> {
49private Entry<K, V> nullKeyEntry;
50private Entry<K, V> nullValueEntry;
51private Entry<K, V> nullKeyValueEntry;
52private Entry<K, V> presentKeyNullValueEntry;
53
54@Override
55public void setUp() throws Exception {
56super.setUp();
57nullKeyEntry = entry(null, v3());
58nullValueEntry = entry(k3(), null);
59nullKeyValueEntry = entry(null, null);
60presentKeyNullValueEntry = entry(k0(), null);
61}
62
63@MapFeature.Require(SUPPORTS_PUT)
64@CollectionSize.Require(absent = ZERO)
65public void testPut_supportedPresent() {
66assertEquals("put(present, value) should return the old value", v0(), getMap().put(k0(), v3()));
67expectReplacement(entry(k0(), v3()));
68}
69
70@MapFeature.Require(SUPPORTS_PUT)
71public void testPut_supportedNotPresent() {
72assertNull("put(notPresent, value) should return null", put(e3()));
73expectAdded(e3());
74}
75
76@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
77@CollectionSize.Require(absent = ZERO)
78public void testPutAbsentConcurrentWithEntrySetIteration() {
79try {
80Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
81put(e3());
82iterator.next();
83fail("Expected ConcurrentModificationException");
84} catch (ConcurrentModificationException expected) {
85// success
86}
87}
88
89@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
90@CollectionSize.Require(absent = ZERO)
91public void testPutAbsentConcurrentWithKeySetIteration() {
92try {
93Iterator<K> iterator = getMap().keySet().iterator();
94put(e3());
95iterator.next();
96fail("Expected ConcurrentModificationException");
97} catch (ConcurrentModificationException expected) {
98// success
99}
100}
101
102@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
103@CollectionSize.Require(absent = ZERO)
104public void testPutAbsentConcurrentWithValueIteration() {
105try {
106Iterator<V> iterator = getMap().values().iterator();
107put(e3());
108iterator.next();
109fail("Expected ConcurrentModificationException");
110} catch (ConcurrentModificationException expected) {
111// success
112}
113}
114
115@MapFeature.Require(absent = SUPPORTS_PUT)
116public void testPut_unsupportedNotPresent() {
117try {
118put(e3());
119fail("put(notPresent, value) should throw");
120} catch (UnsupportedOperationException expected) {
121}
122expectUnchanged();
123expectMissing(e3());
124}
125
126@MapFeature.Require(absent = SUPPORTS_PUT)
127@CollectionSize.Require(absent = ZERO)
128public void testPut_unsupportedPresentExistingValue() {
129try {
130assertEquals("put(present, existingValue) should return present or throw", v0(), put(e0()));
131} catch (UnsupportedOperationException tolerated) {
132}
133expectUnchanged();
134}
135
136@MapFeature.Require(absent = SUPPORTS_PUT)
137@CollectionSize.Require(absent = ZERO)
138public void testPut_unsupportedPresentDifferentValue() {
139try {
140getMap().put(k0(), v3());
141fail("put(present, differentValue) should throw");
142} catch (UnsupportedOperationException expected) {
143}
144expectUnchanged();
145}
146
147@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
148public void testPut_nullKeySupportedNotPresent() {
149assertNull("put(null, value) should return null", put(nullKeyEntry));
150expectAdded(nullKeyEntry);
151}
152
153@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
154@CollectionSize.Require(absent = ZERO)
155public void testPut_nullKeySupportedPresent() {
156Entry<K, V> newEntry = entry(null, v3());
157initMapWithNullKey();
158assertEquals(
159"put(present, value) should return the associated value",
160getValueForNullKey(),
161put(newEntry));
162
163Entry<K, V>[] expected = createArrayWithNullKey();
164expected[getNullLocation()] = newEntry;
165expectContents(expected);
166}
167
168@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
169public void testPut_nullKeyUnsupported() {
170try {
171put(nullKeyEntry);
172fail("put(null, value) should throw");
173} catch (NullPointerException expected) {
174}
175expectUnchanged();
176expectNullKeyMissingWhenNullKeysUnsupported(
177"Should not contain null key after unsupported put(null, value)");
178}
179
180@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
181public void testPut_nullValueSupported() {
182assertNull("put(key, null) should return null", put(nullValueEntry));
183expectAdded(nullValueEntry);
184}
185
186@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
187public void testPut_nullValueUnsupported() {
188try {
189put(nullValueEntry);
190fail("put(key, null) should throw");
191} catch (NullPointerException expected) {
192}
193expectUnchanged();
194expectNullValueMissingWhenNullValuesUnsupported(
195"Should not contain null value after unsupported put(key, null)");
196}
197
198@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
199@CollectionSize.Require(absent = ZERO)
200public void testPut_replaceWithNullValueSupported() {
201assertEquals(
202"put(present, null) should return the associated value",
203v0(),
204put(presentKeyNullValueEntry));
205expectReplacement(presentKeyNullValueEntry);
206}
207
208@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
209@CollectionSize.Require(absent = ZERO)
210public void testPut_replaceWithNullValueUnsupported() {
211try {
212put(presentKeyNullValueEntry);
213fail("put(present, null) should throw");
214} catch (NullPointerException expected) {
215}
216expectUnchanged();
217expectNullValueMissingWhenNullValuesUnsupported(
218"Should not contain null after unsupported put(present, null)");
219}
220
221@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
222@CollectionSize.Require(absent = ZERO)
223public void testPut_replaceNullValueWithNullSupported() {
224initMapWithNullValue();
225assertNull(
226"put(present, null) should return the associated value (null)",
227getMap().put(getKeyForNullValue(), null));
228expectContents(createArrayWithNullValue());
229}
230
231@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
232@CollectionSize.Require(absent = ZERO)
233public void testPut_replaceNullValueWithNonNullSupported() {
234Entry<K, V> newEntry = entry(getKeyForNullValue(), v3());
235initMapWithNullValue();
236assertNull("put(present, value) should return the associated value (null)", put(newEntry));
237
238Entry<K, V>[] expected = createArrayWithNullValue();
239expected[getNullLocation()] = newEntry;
240expectContents(expected);
241}
242
243@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
244public void testPut_nullKeyAndValueSupported() {
245assertNull("put(null, null) should return null", put(nullKeyValueEntry));
246expectAdded(nullKeyValueEntry);
247}
248
249@CanIgnoreReturnValue
250private V put(Entry<K, V> entry) {
251return getMap().put(entry.getKey(), entry.getValue());
252}
253
254/**
255* Returns the {@link Method} instance for {@link #testPut_nullKeyUnsupported()} so that tests of
256* {@link java.util.TreeMap} can suppress it with {@code
257* FeatureSpecificTestSuiteBuilder.suppressing()} until <a
258* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
259*/
260@J2ktIncompatible
261@GwtIncompatible // reflection
262public static Method getPutNullKeyUnsupportedMethod() {
263return Helpers.getMethod(MapPutTester.class, "testPut_nullKeyUnsupported");
264}
265}
266