Java
266 строк · 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.48@SuppressWarnings("JUnit4ClassUsedInJUnit3")49public class MapPutTester<K, V> extends AbstractMapTester<K, V> {50private Entry<K, V> nullKeyEntry;51private Entry<K, V> nullValueEntry;52private Entry<K, V> nullKeyValueEntry;53private Entry<K, V> presentKeyNullValueEntry;54
55@Override56public void setUp() throws Exception {57super.setUp();58nullKeyEntry = entry(null, v3());59nullValueEntry = entry(k3(), null);60nullKeyValueEntry = entry(null, null);61presentKeyNullValueEntry = entry(k0(), null);62}63
64@MapFeature.Require(SUPPORTS_PUT)65@CollectionSize.Require(absent = ZERO)66public void testPut_supportedPresent() {67assertEquals("put(present, value) should return the old value", v0(), getMap().put(k0(), v3()));68expectReplacement(entry(k0(), v3()));69}70
71@MapFeature.Require(SUPPORTS_PUT)72public void testPut_supportedNotPresent() {73assertNull("put(notPresent, value) should return null", put(e3()));74expectAdded(e3());75}76
77@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})78@CollectionSize.Require(absent = ZERO)79public void testPutAbsentConcurrentWithEntrySetIteration() {80try {81Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();82put(e3());83iterator.next();84fail("Expected ConcurrentModificationException");85} catch (ConcurrentModificationException expected) {86// success87}88}89
90@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})91@CollectionSize.Require(absent = ZERO)92public void testPutAbsentConcurrentWithKeySetIteration() {93try {94Iterator<K> iterator = getMap().keySet().iterator();95put(e3());96iterator.next();97fail("Expected ConcurrentModificationException");98} catch (ConcurrentModificationException expected) {99// success100}101}102
103@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})104@CollectionSize.Require(absent = ZERO)105public void testPutAbsentConcurrentWithValueIteration() {106try {107Iterator<V> iterator = getMap().values().iterator();108put(e3());109iterator.next();110fail("Expected ConcurrentModificationException");111} catch (ConcurrentModificationException expected) {112// success113}114}115
116@MapFeature.Require(absent = SUPPORTS_PUT)117public void testPut_unsupportedNotPresent() {118try {119put(e3());120fail("put(notPresent, value) should throw");121} catch (UnsupportedOperationException expected) {122}123expectUnchanged();124expectMissing(e3());125}126
127@MapFeature.Require(absent = SUPPORTS_PUT)128@CollectionSize.Require(absent = ZERO)129public void testPut_unsupportedPresentExistingValue() {130try {131assertEquals("put(present, existingValue) should return present or throw", v0(), put(e0()));132} catch (UnsupportedOperationException tolerated) {133}134expectUnchanged();135}136
137@MapFeature.Require(absent = SUPPORTS_PUT)138@CollectionSize.Require(absent = ZERO)139public void testPut_unsupportedPresentDifferentValue() {140try {141getMap().put(k0(), v3());142fail("put(present, differentValue) should throw");143} catch (UnsupportedOperationException expected) {144}145expectUnchanged();146}147
148@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})149public void testPut_nullKeySupportedNotPresent() {150assertNull("put(null, value) should return null", put(nullKeyEntry));151expectAdded(nullKeyEntry);152}153
154@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})155@CollectionSize.Require(absent = ZERO)156public void testPut_nullKeySupportedPresent() {157Entry<K, V> newEntry = entry(null, v3());158initMapWithNullKey();159assertEquals(160"put(present, value) should return the associated value",161getValueForNullKey(),162put(newEntry));163
164Entry<K, V>[] expected = createArrayWithNullKey();165expected[getNullLocation()] = newEntry;166expectContents(expected);167}168
169@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)170public void testPut_nullKeyUnsupported() {171try {172put(nullKeyEntry);173fail("put(null, value) should throw");174} catch (NullPointerException expected) {175}176expectUnchanged();177expectNullKeyMissingWhenNullKeysUnsupported(178"Should not contain null key after unsupported put(null, value)");179}180
181@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})182public void testPut_nullValueSupported() {183assertNull("put(key, null) should return null", put(nullValueEntry));184expectAdded(nullValueEntry);185}186
187@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)188public void testPut_nullValueUnsupported() {189try {190put(nullValueEntry);191fail("put(key, null) should throw");192} catch (NullPointerException expected) {193}194expectUnchanged();195expectNullValueMissingWhenNullValuesUnsupported(196"Should not contain null value after unsupported put(key, null)");197}198
199@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})200@CollectionSize.Require(absent = ZERO)201public void testPut_replaceWithNullValueSupported() {202assertEquals(203"put(present, null) should return the associated value",204v0(),205put(presentKeyNullValueEntry));206expectReplacement(presentKeyNullValueEntry);207}208
209@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)210@CollectionSize.Require(absent = ZERO)211public void testPut_replaceWithNullValueUnsupported() {212try {213put(presentKeyNullValueEntry);214fail("put(present, null) should throw");215} catch (NullPointerException expected) {216}217expectUnchanged();218expectNullValueMissingWhenNullValuesUnsupported(219"Should not contain null after unsupported put(present, null)");220}221
222@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})223@CollectionSize.Require(absent = ZERO)224public void testPut_replaceNullValueWithNullSupported() {225initMapWithNullValue();226assertNull(227"put(present, null) should return the associated value (null)",228getMap().put(getKeyForNullValue(), null));229expectContents(createArrayWithNullValue());230}231
232@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})233@CollectionSize.Require(absent = ZERO)234public void testPut_replaceNullValueWithNonNullSupported() {235Entry<K, V> newEntry = entry(getKeyForNullValue(), v3());236initMapWithNullValue();237assertNull("put(present, value) should return the associated value (null)", put(newEntry));238
239Entry<K, V>[] expected = createArrayWithNullValue();240expected[getNullLocation()] = newEntry;241expectContents(expected);242}243
244@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})245public void testPut_nullKeyAndValueSupported() {246assertNull("put(null, null) should return null", put(nullKeyValueEntry));247expectAdded(nullKeyValueEntry);248}249
250@CanIgnoreReturnValue251private V put(Entry<K, V> entry) {252return getMap().put(entry.getKey(), entry.getValue());253}254
255/**256* Returns the {@link Method} instance for {@link #testPut_nullKeyUnsupported()} so that tests of
257* {@link java.util.TreeMap} can suppress it with {@code
258* FeatureSpecificTestSuiteBuilder.suppressing()} until <a
259* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
260*/
261@J2ktIncompatible262@GwtIncompatible // reflection263public static Method getPutNullKeyUnsupportedMethod() {264return Helpers.getMethod(MapPutTester.class, "testPut_nullKeyUnsupported");265}266}
267