Java
183 строки · 5.9 Кб
1/*
2* Copyright (C) 2016 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 junit.framework.AssertionFailedError;
31import org.junit.Ignore;
32
33/**
34* A generic JUnit test which tests {@link Map#computeIfPresent}. Can't be invoked directly; please
35* see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
36*
37* @author Louis Wasserman
38*/
39@GwtCompatible
40@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
41@SuppressWarnings("JUnit4ClassUsedInJUnit3")
42public class MapComputeIfPresentTester<K, V> extends AbstractMapTester<K, V> {
43
44@MapFeature.Require(SUPPORTS_PUT)
45public void testComputeIfPresent_supportedAbsent() {
46assertNull(
47"computeIfPresent(notPresent, function) should return null",
48getMap()
49.computeIfPresent(
50k3(),
51(k, v) -> {
52throw new AssertionFailedError();
53}));
54expectUnchanged();
55}
56
57@MapFeature.Require(SUPPORTS_PUT)
58@CollectionSize.Require(absent = ZERO)
59public void testComputeIfPresent_supportedPresent() {
60assertEquals(
61"computeIfPresent(present, function) should return new value",
62v3(),
63getMap()
64.computeIfPresent(
65k0(),
66(k, v) -> {
67assertEquals(k0(), k);
68assertEquals(v0(), v);
69return v3();
70}));
71expectReplacement(entry(k0(), v3()));
72}
73
74@MapFeature.Require(SUPPORTS_PUT)
75@CollectionSize.Require(absent = ZERO)
76public void testComputeIfPresent_functionReturnsNull() {
77assertNull(
78"computeIfPresent(present, returnsNull) should return null",
79getMap()
80.computeIfPresent(
81k0(),
82(k, v) -> {
83assertEquals(k0(), k);
84assertEquals(v0(), v);
85return null;
86}));
87expectMissing(e0());
88}
89
90@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
91@CollectionSize.Require(absent = ZERO)
92public void testComputeIfPresent_nullTreatedAsAbsent() {
93initMapWithNullValue();
94assertNull(
95"computeIfPresent(presentAssignedToNull, function) should return null",
96getMap()
97.computeIfPresent(
98getKeyForNullValue(),
99(k, v) -> {
100throw new AssertionFailedError();
101}));
102expectReplacement(entry(getKeyForNullValue(), null));
103}
104
105static class ExpectedException extends RuntimeException {}
106
107@MapFeature.Require(SUPPORTS_PUT)
108@CollectionSize.Require(absent = ZERO)
109public void testComputeIfPresent_functionThrows() {
110try {
111getMap()
112.computeIfPresent(
113k0(),
114(k, v) -> {
115assertEquals(k0(), k);
116assertEquals(v0(), v);
117throw new ExpectedException();
118});
119fail("Expected ExpectedException");
120} catch (ExpectedException expected) {
121}
122expectUnchanged();
123}
124
125@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
126@CollectionSize.Require(absent = ZERO)
127public void testComputeIfPresent_nullKeySupportedPresent() {
128initMapWithNullKey();
129assertEquals(
130"computeIfPresent(null, function) should return new value",
131v3(),
132getMap()
133.computeIfPresent(
134null,
135(k, v) -> {
136assertNull(k);
137assertEquals(getValueForNullKey(), v);
138return v3();
139}));
140
141Entry<K, V>[] expected = createArrayWithNullKey();
142expected[getNullLocation()] = entry(null, v3());
143expectContents(expected);
144}
145
146@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
147public void testComputeIfPresent_nullKeySupportedAbsent() {
148assertNull(
149"computeIfPresent(null, function) should return null",
150getMap()
151.computeIfPresent(
152null,
153(k, v) -> {
154throw new AssertionFailedError();
155}));
156expectUnchanged();
157}
158
159@MapFeature.Require(absent = SUPPORTS_PUT)
160public void testComputeIfPresent_unsupportedAbsent() {
161try {
162getMap()
163.computeIfPresent(
164k3(),
165(k, v) -> {
166throw new AssertionFailedError();
167});
168} catch (UnsupportedOperationException tolerated) {
169}
170expectUnchanged();
171}
172
173@MapFeature.Require(absent = SUPPORTS_PUT)
174@CollectionSize.Require(absent = ZERO)
175public void testComputeIfPresent_unsupportedPresent() {
176try {
177getMap().computeIfPresent(k0(), (k, v) -> v3());
178fail("Expected UnsupportedOperationException");
179} catch (UnsupportedOperationException expected) {
180}
181expectUnchanged();
182}
183}
184