guava
199 строк · 6.5 Кб
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;
23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
24
25import com.google.common.annotations.GwtCompatible;
26import com.google.common.annotations.GwtIncompatible;
27import com.google.common.annotations.J2ktIncompatible;
28import com.google.common.collect.testing.AbstractMapTester;
29import com.google.common.collect.testing.Helpers;
30import com.google.common.collect.testing.features.CollectionSize;
31import com.google.common.collect.testing.features.MapFeature;
32import java.lang.reflect.Method;
33import java.util.Hashtable;
34import java.util.Map;
35import junit.framework.AssertionFailedError;
36import org.junit.Ignore;
37
38/**
39* A generic JUnit test which tests {@link Map#merge}. Can't be invoked directly; please see {@link
40* com.google.common.collect.testing.MapTestSuiteBuilder}.
41*
42* @author Louis Wasserman
43*/
44@GwtCompatible(emulated = true)
45@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
46public class MapMergeTester<K, V> extends AbstractMapTester<K, V> {
47@MapFeature.Require(SUPPORTS_PUT)
48public void testAbsent() {
49assertEquals(
50"Map.merge(absent, value, function) should return value",
51v3(),
52getMap()
53.merge(
54k3(),
55v3(),
56(oldV, newV) -> {
57throw new AssertionFailedError(
58"Should not call merge function if key was absent");
59}));
60expectAdded(e3());
61}
62
63@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
64@CollectionSize.Require(absent = ZERO)
65public void testMappedToNull() {
66initMapWithNullValue();
67assertEquals(
68"Map.merge(keyMappedToNull, value, function) should return value",
69v3(),
70getMap()
71.merge(
72getKeyForNullValue(),
73v3(),
74(oldV, newV) -> {
75throw new AssertionFailedError(
76"Should not call merge function if key was mapped to null");
77}));
78expectReplacement(entry(getKeyForNullValue(), v3()));
79}
80
81@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
82public void testMergeAbsentNullKey() {
83assertEquals(
84"Map.merge(null, value, function) should return value",
85v3(),
86getMap()
87.merge(
88null,
89v3(),
90(oldV, newV) -> {
91throw new AssertionFailedError(
92"Should not call merge function if key was absent");
93}));
94expectAdded(entry(null, v3()));
95}
96
97@MapFeature.Require(SUPPORTS_PUT)
98@CollectionSize.Require(absent = ZERO)
99public void testMergePresent() {
100assertEquals(
101"Map.merge(present, value, function) should return function result",
102v4(),
103getMap()
104.merge(
105k0(),
106v3(),
107(oldV, newV) -> {
108assertEquals(v0(), oldV);
109assertEquals(v3(), newV);
110return v4();
111}));
112expectReplacement(entry(k0(), v4()));
113}
114
115private static class ExpectedException extends RuntimeException {}
116
117@MapFeature.Require(SUPPORTS_PUT)
118@CollectionSize.Require(absent = ZERO)
119public void testMergeFunctionThrows() {
120try {
121getMap()
122.merge(
123k0(),
124v3(),
125(oldV, newV) -> {
126assertEquals(v0(), oldV);
127assertEquals(v3(), newV);
128throw new ExpectedException();
129});
130fail("Expected ExpectedException");
131} catch (ExpectedException expected) {
132}
133expectUnchanged();
134}
135
136@MapFeature.Require(SUPPORTS_REMOVE)
137@CollectionSize.Require(absent = ZERO)
138public void testMergePresentToNull() {
139assertNull(
140"Map.merge(present, value, functionReturningNull) should return null",
141getMap()
142.merge(
143k0(),
144v3(),
145(oldV, newV) -> {
146assertEquals(v0(), oldV);
147assertEquals(v3(), newV);
148return null;
149}));
150expectMissing(e0());
151}
152
153public void testMergeNullValue() {
154try {
155getMap()
156.merge(
157k0(),
158null,
159(oldV, newV) -> {
160throw new AssertionFailedError("Should not call merge function if value was null");
161});
162fail("Expected NullPointerException or UnsupportedOperationException");
163} catch (NullPointerException | UnsupportedOperationException expected) {
164}
165}
166
167public void testMergeNullFunction() {
168try {
169getMap().merge(k0(), v3(), null);
170fail("Expected NullPointerException or UnsupportedOperationException");
171} catch (NullPointerException | UnsupportedOperationException expected) {
172}
173}
174
175@MapFeature.Require(absent = SUPPORTS_PUT)
176public void testMergeUnsupported() {
177try {
178getMap()
179.merge(
180k3(),
181v3(),
182(oldV, newV) -> {
183throw new AssertionFailedError();
184});
185fail("Expected UnsupportedOperationException");
186} catch (UnsupportedOperationException expected) {
187}
188}
189
190/**
191* Returns the {@link Method} instance for {@link #testMergeNullValue()} so that tests of {@link
192* Hashtable} can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}.
193*/
194@J2ktIncompatible
195@GwtIncompatible // reflection
196public static Method getMergeNullValueMethod() {
197return Helpers.getMethod(MapMergeTester.class, "testMergeNullValue");
198}
199}
200