Java
138 строк · 4.9 Кб
1/*
2* Copyright (C) 2015 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.SUPPORTS_PUT;23
24import com.google.common.annotations.GwtCompatible;25import com.google.common.collect.testing.AbstractMapTester;26import com.google.common.collect.testing.features.CollectionSize;27import com.google.common.collect.testing.features.MapFeature;28import java.util.Map;29import java.util.Map.Entry;30import org.junit.Ignore;31
32/**
33* A generic JUnit test which tests {@link Map#putIfAbsent}. Can't be invoked directly; please see
34* {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
35*
36* @author Louis Wasserman
37*/
38@GwtCompatible
39@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.40@SuppressWarnings("JUnit4ClassUsedInJUnit3")41public class MapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {42
43@MapFeature.Require(SUPPORTS_PUT)44public void testPutIfAbsent_supportedAbsent() {45assertNull(46"putIfAbsent(notPresent, value) should return null", getMap().putIfAbsent(k3(), v3()));47expectAdded(e3());48}49
50@MapFeature.Require(SUPPORTS_PUT)51@CollectionSize.Require(absent = ZERO)52public void testPutIfAbsent_supportedPresent() {53assertEquals(54"putIfAbsent(present, value) should return existing value",55v0(),56getMap().putIfAbsent(k0(), v3()));57expectUnchanged();58}59
60@MapFeature.Require(absent = SUPPORTS_PUT)61public void testPutIfAbsent_unsupportedAbsent() {62try {63getMap().putIfAbsent(k3(), v3());64fail("putIfAbsent(notPresent, value) should throw");65} catch (UnsupportedOperationException expected) {66}67expectUnchanged();68expectMissing(e3());69}70
71@MapFeature.Require(absent = SUPPORTS_PUT)72@CollectionSize.Require(absent = ZERO)73public void testPutIfAbsent_unsupportedPresentExistingValue() {74try {75assertEquals(76"putIfAbsent(present, existingValue) should return present or throw",77v0(),78getMap().putIfAbsent(k0(), v0()));79} catch (UnsupportedOperationException tolerated) {80}81expectUnchanged();82}83
84@MapFeature.Require(absent = SUPPORTS_PUT)85@CollectionSize.Require(absent = ZERO)86public void testPutIfAbsent_unsupportedPresentDifferentValue() {87try {88getMap().putIfAbsent(k0(), v3());89} catch (UnsupportedOperationException tolerated) {90}91expectUnchanged();92}93
94@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)95public void testPutIfAbsent_nullKeyUnsupported() {96try {97getMap().putIfAbsent(null, v3());98fail("putIfAbsent(null, value) should throw");99} catch (NullPointerException expected) {100}101expectUnchanged();102expectNullKeyMissingWhenNullKeysUnsupported(103"Should not contain null key after unsupported putIfAbsent(null, value)");104}105
106@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)107public void testPutIfAbsent_nullValueUnsupportedAndKeyAbsent() {108try {109getMap().putIfAbsent(k3(), null);110fail("putIfAbsent(key, null) should throw");111} catch (NullPointerException expected) {112}113expectUnchanged();114expectNullValueMissingWhenNullValuesUnsupported(115"Should not contain null value after unsupported putIfAbsent(key, null)");116}117
118@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)119@CollectionSize.Require(absent = ZERO)120public void testPutIfAbsent_nullValueUnsupportedAndKeyPresent() {121try {122getMap().putIfAbsent(k0(), null);123} catch (NullPointerException tolerated) {124}125expectUnchanged();126expectNullValueMissingWhenNullValuesUnsupported(127"Should not contain null after unsupported putIfAbsent(present, null)");128}129
130@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})131public void testPut_nullValueSupported() {132Entry<K, V> nullValueEntry = entry(k3(), null);133assertNull(134"putIfAbsent(key, null) should return null",135getMap().putIfAbsent(nullValueEntry.getKey(), nullValueEntry.getValue()));136expectAdded(nullValueEntry);137}138}
139