guava
182 строки · 5.8 Кб
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.
41public class MapComputeIfPresentTester<K, V> extends AbstractMapTester<K, V> {
42
43@MapFeature.Require(SUPPORTS_PUT)
44public void testComputeIfPresent_supportedAbsent() {
45assertNull(
46"computeIfPresent(notPresent, function) should return null",
47getMap()
48.computeIfPresent(
49k3(),
50(k, v) -> {
51throw new AssertionFailedError();
52}));
53expectUnchanged();
54}
55
56@MapFeature.Require(SUPPORTS_PUT)
57@CollectionSize.Require(absent = ZERO)
58public void testComputeIfPresent_supportedPresent() {
59assertEquals(
60"computeIfPresent(present, function) should return new value",
61v3(),
62getMap()
63.computeIfPresent(
64k0(),
65(k, v) -> {
66assertEquals(k0(), k);
67assertEquals(v0(), v);
68return v3();
69}));
70expectReplacement(entry(k0(), v3()));
71}
72
73@MapFeature.Require(SUPPORTS_PUT)
74@CollectionSize.Require(absent = ZERO)
75public void testComputeIfPresent_functionReturnsNull() {
76assertNull(
77"computeIfPresent(present, returnsNull) should return null",
78getMap()
79.computeIfPresent(
80k0(),
81(k, v) -> {
82assertEquals(k0(), k);
83assertEquals(v0(), v);
84return null;
85}));
86expectMissing(e0());
87}
88
89@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
90@CollectionSize.Require(absent = ZERO)
91public void testComputeIfPresent_nullTreatedAsAbsent() {
92initMapWithNullValue();
93assertNull(
94"computeIfPresent(presentAssignedToNull, function) should return null",
95getMap()
96.computeIfPresent(
97getKeyForNullValue(),
98(k, v) -> {
99throw new AssertionFailedError();
100}));
101expectReplacement(entry(getKeyForNullValue(), null));
102}
103
104static class ExpectedException extends RuntimeException {}
105
106@MapFeature.Require(SUPPORTS_PUT)
107@CollectionSize.Require(absent = ZERO)
108public void testComputeIfPresent_functionThrows() {
109try {
110getMap()
111.computeIfPresent(
112k0(),
113(k, v) -> {
114assertEquals(k0(), k);
115assertEquals(v0(), v);
116throw new ExpectedException();
117});
118fail("Expected ExpectedException");
119} catch (ExpectedException expected) {
120}
121expectUnchanged();
122}
123
124@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
125@CollectionSize.Require(absent = ZERO)
126public void testComputeIfPresent_nullKeySupportedPresent() {
127initMapWithNullKey();
128assertEquals(
129"computeIfPresent(null, function) should return new value",
130v3(),
131getMap()
132.computeIfPresent(
133null,
134(k, v) -> {
135assertNull(k);
136assertEquals(getValueForNullKey(), v);
137return v3();
138}));
139
140Entry<K, V>[] expected = createArrayWithNullKey();
141expected[getNullLocation()] = entry(null, v3());
142expectContents(expected);
143}
144
145@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
146public void testComputeIfPresent_nullKeySupportedAbsent() {
147assertNull(
148"computeIfPresent(null, function) should return null",
149getMap()
150.computeIfPresent(
151null,
152(k, v) -> {
153throw new AssertionFailedError();
154}));
155expectUnchanged();
156}
157
158@MapFeature.Require(absent = SUPPORTS_PUT)
159public void testComputeIfPresent_unsupportedAbsent() {
160try {
161getMap()
162.computeIfPresent(
163k3(),
164(k, v) -> {
165throw new AssertionFailedError();
166});
167} catch (UnsupportedOperationException tolerated) {
168}
169expectUnchanged();
170}
171
172@MapFeature.Require(absent = SUPPORTS_PUT)
173@CollectionSize.Require(absent = ZERO)
174public void testComputeIfPresent_unsupportedPresent() {
175try {
176getMap().computeIfPresent(k0(), (k, v) -> v3());
177fail("Expected UnsupportedOperationException");
178} catch (UnsupportedOperationException expected) {
179}
180expectUnchanged();
181}
182}
183