guava
137 строк · 4.8 Кб
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.
40public class MapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
41
42@MapFeature.Require(SUPPORTS_PUT)
43public void testPutIfAbsent_supportedAbsent() {
44assertNull(
45"putIfAbsent(notPresent, value) should return null", getMap().putIfAbsent(k3(), v3()));
46expectAdded(e3());
47}
48
49@MapFeature.Require(SUPPORTS_PUT)
50@CollectionSize.Require(absent = ZERO)
51public void testPutIfAbsent_supportedPresent() {
52assertEquals(
53"putIfAbsent(present, value) should return existing value",
54v0(),
55getMap().putIfAbsent(k0(), v3()));
56expectUnchanged();
57}
58
59@MapFeature.Require(absent = SUPPORTS_PUT)
60public void testPutIfAbsent_unsupportedAbsent() {
61try {
62getMap().putIfAbsent(k3(), v3());
63fail("putIfAbsent(notPresent, value) should throw");
64} catch (UnsupportedOperationException expected) {
65}
66expectUnchanged();
67expectMissing(e3());
68}
69
70@MapFeature.Require(absent = SUPPORTS_PUT)
71@CollectionSize.Require(absent = ZERO)
72public void testPutIfAbsent_unsupportedPresentExistingValue() {
73try {
74assertEquals(
75"putIfAbsent(present, existingValue) should return present or throw",
76v0(),
77getMap().putIfAbsent(k0(), v0()));
78} catch (UnsupportedOperationException tolerated) {
79}
80expectUnchanged();
81}
82
83@MapFeature.Require(absent = SUPPORTS_PUT)
84@CollectionSize.Require(absent = ZERO)
85public void testPutIfAbsent_unsupportedPresentDifferentValue() {
86try {
87getMap().putIfAbsent(k0(), v3());
88} catch (UnsupportedOperationException tolerated) {
89}
90expectUnchanged();
91}
92
93@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
94public void testPutIfAbsent_nullKeyUnsupported() {
95try {
96getMap().putIfAbsent(null, v3());
97fail("putIfAbsent(null, value) should throw");
98} catch (NullPointerException expected) {
99}
100expectUnchanged();
101expectNullKeyMissingWhenNullKeysUnsupported(
102"Should not contain null key after unsupported putIfAbsent(null, value)");
103}
104
105@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
106public void testPutIfAbsent_nullValueUnsupportedAndKeyAbsent() {
107try {
108getMap().putIfAbsent(k3(), null);
109fail("putIfAbsent(key, null) should throw");
110} catch (NullPointerException expected) {
111}
112expectUnchanged();
113expectNullValueMissingWhenNullValuesUnsupported(
114"Should not contain null value after unsupported putIfAbsent(key, null)");
115}
116
117@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
118@CollectionSize.Require(absent = ZERO)
119public void testPutIfAbsent_nullValueUnsupportedAndKeyPresent() {
120try {
121getMap().putIfAbsent(k0(), null);
122} catch (NullPointerException tolerated) {
123}
124expectUnchanged();
125expectNullValueMissingWhenNullValuesUnsupported(
126"Should not contain null after unsupported putIfAbsent(present, null)");
127}
128
129@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
130public void testPut_nullValueSupported() {
131Entry<K, V> nullValueEntry = entry(k3(), null);
132assertNull(
133"putIfAbsent(key, null) should return null",
134getMap().putIfAbsent(nullValueEntry.getKey(), nullValueEntry.getValue()));
135expectAdded(nullValueEntry);
136}
137}
138