Java
206 строк · 6.5 Кб
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 junit.framework.AssertionFailedError;
30import org.junit.Ignore;
31
32/**
33* A generic JUnit test which tests {@link Map#computeIfAbsent}. Can't be invoked directly; please
34* see {@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 MapComputeIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
42
43@MapFeature.Require(SUPPORTS_PUT)
44public void testComputeIfAbsent_supportedAbsent() {
45assertEquals(
46"computeIfAbsent(notPresent, function) should return new value",
47v3(),
48getMap()
49.computeIfAbsent(
50k3(),
51k -> {
52assertEquals(k3(), k);
53return v3();
54}));
55expectAdded(e3());
56}
57
58@MapFeature.Require(SUPPORTS_PUT)
59@CollectionSize.Require(absent = ZERO)
60public void testComputeIfAbsent_supportedPresent() {
61assertEquals(
62"computeIfAbsent(present, function) should return existing value",
63v0(),
64getMap()
65.computeIfAbsent(
66k0(),
67k -> {
68throw new AssertionFailedError();
69}));
70expectUnchanged();
71}
72
73@MapFeature.Require(SUPPORTS_PUT)
74public void testComputeIfAbsent_functionReturnsNullNotInserted() {
75assertNull(
76"computeIfAbsent(absent, returnsNull) should return null",
77getMap()
78.computeIfAbsent(
79k3(),
80k -> {
81assertEquals(k3(), k);
82return null;
83}));
84expectUnchanged();
85}
86
87@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
88@CollectionSize.Require(absent = ZERO)
89public void testComputeIfAbsent_nullTreatedAsAbsent() {
90initMapWithNullValue();
91assertEquals(
92"computeIfAbsent(presentAssignedToNull, function) should return newValue",
93getValueForNullKey(),
94getMap()
95.computeIfAbsent(
96getKeyForNullValue(),
97k -> {
98assertEquals(getKeyForNullValue(), k);
99return getValueForNullKey();
100}));
101expectReplacement(entry(getKeyForNullValue(), getValueForNullKey()));
102}
103
104@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
105public void testComputeIfAbsent_nullKeySupported() {
106getMap()
107.computeIfAbsent(
108null,
109k -> {
110assertNull(k);
111return v3();
112});
113expectAdded(entry(null, v3()));
114}
115
116static class ExpectedException extends RuntimeException {}
117
118@MapFeature.Require(SUPPORTS_PUT)
119public void testComputeIfAbsent_functionThrows() {
120try {
121getMap()
122.computeIfAbsent(
123k3(),
124k -> {
125assertEquals(k3(), k);
126throw new ExpectedException();
127});
128fail("Expected ExpectedException");
129} catch (ExpectedException expected) {
130}
131expectUnchanged();
132}
133
134@MapFeature.Require(absent = SUPPORTS_PUT)
135public void testComputeIfAbsent_unsupportedAbsent() {
136try {
137getMap()
138.computeIfAbsent(
139k3(),
140k -> {
141// allowed to be called
142assertEquals(k3(), k);
143return v3();
144});
145fail("computeIfAbsent(notPresent, function) should throw");
146} catch (UnsupportedOperationException expected) {
147}
148expectUnchanged();
149}
150
151@MapFeature.Require(absent = SUPPORTS_PUT)
152@CollectionSize.Require(absent = ZERO)
153public void testComputeIfAbsent_unsupportedPresentExistingValue() {
154try {
155assertEquals(
156"computeIfAbsent(present, returnsCurrentValue) should return present or throw",
157v0(),
158getMap()
159.computeIfAbsent(
160k0(),
161k -> {
162assertEquals(k0(), k);
163return v0();
164}));
165} catch (UnsupportedOperationException tolerated) {
166}
167expectUnchanged();
168}
169
170@MapFeature.Require(absent = SUPPORTS_PUT)
171@CollectionSize.Require(absent = ZERO)
172public void testComputeIfAbsent_unsupportedPresentDifferentValue() {
173try {
174assertEquals(
175"computeIfAbsent(present, returnsDifferentValue) should return present or throw",
176v0(),
177getMap()
178.computeIfAbsent(
179k0(),
180k -> {
181assertEquals(k0(), k);
182return v3();
183}));
184} catch (UnsupportedOperationException tolerated) {
185}
186expectUnchanged();
187}
188
189@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
190public void testComputeIfAbsent_nullKeyUnsupported() {
191try {
192getMap()
193.computeIfAbsent(
194null,
195k -> {
196assertNull(k);
197return v3();
198});
199fail("computeIfAbsent(null, function) should throw");
200} catch (NullPointerException expected) {
201}
202expectUnchanged();
203expectNullKeyMissingWhenNullKeysUnsupported(
204"Should not contain null key after unsupported computeIfAbsent(null, function)");
205}
206}
207