Java
200 строк · 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.
46@SuppressWarnings("JUnit4ClassUsedInJUnit3")
47public class MapMergeTester<K, V> extends AbstractMapTester<K, V> {
48@MapFeature.Require(SUPPORTS_PUT)
49public void testAbsent() {
50assertEquals(
51"Map.merge(absent, value, function) should return value",
52v3(),
53getMap()
54.merge(
55k3(),
56v3(),
57(oldV, newV) -> {
58throw new AssertionFailedError(
59"Should not call merge function if key was absent");
60}));
61expectAdded(e3());
62}
63
64@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
65@CollectionSize.Require(absent = ZERO)
66public void testMappedToNull() {
67initMapWithNullValue();
68assertEquals(
69"Map.merge(keyMappedToNull, value, function) should return value",
70v3(),
71getMap()
72.merge(
73getKeyForNullValue(),
74v3(),
75(oldV, newV) -> {
76throw new AssertionFailedError(
77"Should not call merge function if key was mapped to null");
78}));
79expectReplacement(entry(getKeyForNullValue(), v3()));
80}
81
82@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
83public void testMergeAbsentNullKey() {
84assertEquals(
85"Map.merge(null, value, function) should return value",
86v3(),
87getMap()
88.merge(
89null,
90v3(),
91(oldV, newV) -> {
92throw new AssertionFailedError(
93"Should not call merge function if key was absent");
94}));
95expectAdded(entry(null, v3()));
96}
97
98@MapFeature.Require(SUPPORTS_PUT)
99@CollectionSize.Require(absent = ZERO)
100public void testMergePresent() {
101assertEquals(
102"Map.merge(present, value, function) should return function result",
103v4(),
104getMap()
105.merge(
106k0(),
107v3(),
108(oldV, newV) -> {
109assertEquals(v0(), oldV);
110assertEquals(v3(), newV);
111return v4();
112}));
113expectReplacement(entry(k0(), v4()));
114}
115
116private static class ExpectedException extends RuntimeException {}
117
118@MapFeature.Require(SUPPORTS_PUT)
119@CollectionSize.Require(absent = ZERO)
120public void testMergeFunctionThrows() {
121try {
122getMap()
123.merge(
124k0(),
125v3(),
126(oldV, newV) -> {
127assertEquals(v0(), oldV);
128assertEquals(v3(), newV);
129throw new ExpectedException();
130});
131fail("Expected ExpectedException");
132} catch (ExpectedException expected) {
133}
134expectUnchanged();
135}
136
137@MapFeature.Require(SUPPORTS_REMOVE)
138@CollectionSize.Require(absent = ZERO)
139public void testMergePresentToNull() {
140assertNull(
141"Map.merge(present, value, functionReturningNull) should return null",
142getMap()
143.merge(
144k0(),
145v3(),
146(oldV, newV) -> {
147assertEquals(v0(), oldV);
148assertEquals(v3(), newV);
149return null;
150}));
151expectMissing(e0());
152}
153
154public void testMergeNullValue() {
155try {
156getMap()
157.merge(
158k0(),
159null,
160(oldV, newV) -> {
161throw new AssertionFailedError("Should not call merge function if value was null");
162});
163fail("Expected NullPointerException or UnsupportedOperationException");
164} catch (NullPointerException | UnsupportedOperationException expected) {
165}
166}
167
168public void testMergeNullFunction() {
169try {
170getMap().merge(k0(), v3(), null);
171fail("Expected NullPointerException or UnsupportedOperationException");
172} catch (NullPointerException | UnsupportedOperationException expected) {
173}
174}
175
176@MapFeature.Require(absent = SUPPORTS_PUT)
177public void testMergeUnsupported() {
178try {
179getMap()
180.merge(
181k3(),
182v3(),
183(oldV, newV) -> {
184throw new AssertionFailedError();
185});
186fail("Expected UnsupportedOperationException");
187} catch (UnsupportedOperationException expected) {
188}
189}
190
191/**
192* Returns the {@link Method} instance for {@link #testMergeNullValue()} so that tests of {@link
193* Hashtable} can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}.
194*/
195@J2ktIncompatible
196@GwtIncompatible // reflection
197public static Method getMergeNullValueMethod() {
198return Helpers.getMethod(MapMergeTester.class, "testMergeNullValue");
199}
200}
201