Java
205 строк · 6.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;
23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
24
25import com.google.common.annotations.GwtCompatible;
26import com.google.common.collect.testing.AbstractMapTester;
27import com.google.common.collect.testing.features.CollectionSize;
28import com.google.common.collect.testing.features.MapFeature;
29import java.util.Map;
30import org.junit.Ignore;
31
32/**
33* A generic JUnit test which tests {@link Map#compute}. 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 MapComputeTester<K, V> extends AbstractMapTester<K, V> {
42@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
43public void testCompute_absentToPresent() {
44assertEquals(
45"Map.compute(absent, functionReturningValue) should return value",
46v3(),
47getMap()
48.compute(
49k3(),
50(k, v) -> {
51assertEquals(k3(), k);
52assertNull(v);
53return v3();
54}));
55expectAdded(e3());
56assertEquals(getNumElements() + 1, getMap().size());
57}
58
59@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
60public void testCompute_absentToAbsent() {
61assertNull(
62"Map.compute(absent, functionReturningNull) should return null",
63getMap()
64.compute(
65k3(),
66(k, v) -> {
67assertEquals(k3(), k);
68assertNull(v);
69return null;
70}));
71expectUnchanged();
72assertEquals(getNumElements(), getMap().size());
73}
74
75@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
76@CollectionSize.Require(absent = ZERO)
77public void testCompute_presentToPresent() {
78assertEquals(
79"Map.compute(present, functionReturningValue) should return new value",
80v3(),
81getMap()
82.compute(
83k0(),
84(k, v) -> {
85assertEquals(k0(), k);
86assertEquals(v0(), v);
87return v3();
88}));
89expectReplacement(entry(k0(), v3()));
90assertEquals(getNumElements(), getMap().size());
91}
92
93@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
94@CollectionSize.Require(absent = ZERO)
95public void testCompute_presentToAbsent() {
96assertNull(
97"Map.compute(present, functionReturningNull) should return null",
98getMap()
99.compute(
100k0(),
101(k, v) -> {
102assertEquals(k0(), k);
103assertEquals(v0(), v);
104return null;
105}));
106expectMissing(e0());
107expectMissingKeys(k0());
108assertEquals(getNumElements() - 1, getMap().size());
109}
110
111@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
112@CollectionSize.Require(absent = ZERO)
113public void testCompute_presentNullToPresentNonnull() {
114initMapWithNullValue();
115V value = getValueForNullKey();
116assertEquals(
117"Map.compute(presentMappedToNull, functionReturningValue) should return new value",
118value,
119getMap()
120.compute(
121getKeyForNullValue(),
122(k, v) -> {
123assertEquals(getKeyForNullValue(), k);
124assertNull(v);
125return value;
126}));
127expectReplacement(entry(getKeyForNullValue(), value));
128assertEquals(getNumElements(), getMap().size());
129}
130
131@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
132@CollectionSize.Require(absent = ZERO)
133public void testCompute_presentNullToNull() {
134// The spec is somewhat ambiguous about this case, but the actual default implementation
135// in Map will remove a present null.
136initMapWithNullValue();
137assertNull(
138"Map.compute(presentMappedToNull, functionReturningNull) should return null",
139getMap()
140.compute(
141getKeyForNullValue(),
142(k, v) -> {
143assertEquals(getKeyForNullValue(), k);
144assertNull(v);
145return null;
146}));
147expectMissingKeys(getKeyForNullValue());
148assertEquals(getNumElements() - 1, getMap().size());
149}
150
151@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
152@CollectionSize.Require(absent = ZERO)
153public void testCompute_nullKeyPresentToPresent() {
154initMapWithNullKey();
155assertEquals(
156"Map.compute(present, functionReturningValue) should return new value",
157v3(),
158getMap()
159.compute(
160null,
161(k, v) -> {
162assertNull(k);
163assertEquals(getValueForNullKey(), v);
164return v3();
165}));
166assertEquals(getNumElements(), getMap().size());
167}
168
169static class ExpectedException extends RuntimeException {}
170
171@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
172@CollectionSize.Require(absent = ZERO)
173public void testCompute_presentFunctionThrows() {
174try {
175getMap()
176.compute(
177k0(),
178(k, v) -> {
179assertEquals(k0(), k);
180assertEquals(v0(), v);
181throw new ExpectedException();
182});
183fail("Expected ExpectedException");
184} catch (ExpectedException expected) {
185}
186expectUnchanged();
187}
188
189@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
190public void testCompute_absentFunctionThrows() {
191try {
192getMap()
193.compute(
194k3(),
195(k, v) -> {
196assertEquals(k3(), k);
197assertNull(v);
198throw new ExpectedException();
199});
200fail("Expected ExpectedException");
201} catch (ExpectedException expected) {
202}
203expectUnchanged();
204}
205}
206