Java
111 строк · 3.8 Кб
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_KEY_QUERIES;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
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#replace(Object, Object)}. Can't be invoked directly;
34* please see {@link com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
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 MapReplaceTester<K, V> extends AbstractMapTester<K, V> {
42
43@MapFeature.Require(SUPPORTS_PUT)
44@CollectionSize.Require(absent = ZERO)
45public void testReplace_supportedPresent() {
46try {
47assertEquals(v0(), getMap().replace(k0(), v3()));
48expectReplacement(entry(k0(), v3()));
49} catch (ClassCastException tolerated) { // for ClassToInstanceMap
50expectUnchanged();
51}
52}
53
54@MapFeature.Require(SUPPORTS_PUT)
55@CollectionSize.Require(absent = ZERO)
56public void testReplace_supportedPresentNoChange() {
57assertEquals(v0(), getMap().replace(k0(), v0()));
58expectUnchanged();
59}
60
61@MapFeature.Require(SUPPORTS_PUT)
62public void testReplace_supportedAbsent() {
63assertNull(getMap().replace(k3(), v3()));
64expectUnchanged();
65}
66
67@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
68@CollectionSize.Require(absent = ZERO)
69public void testReplace_presentNullValueUnsupported() {
70try {
71getMap().replace(k0(), null);
72fail("Expected NullPointerException");
73} catch (NullPointerException expected) {
74}
75expectUnchanged();
76}
77
78@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
79public void testReplace_absentNullValueUnsupported() {
80try {
81getMap().replace(k3(), null);
82} catch (NullPointerException tolerated) {
83// permitted not to throw because it would be a no-op
84}
85expectUnchanged();
86}
87
88@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
89public void testReplace_absentNullKeyUnsupported() {
90try {
91getMap().replace(null, v3());
92} catch (NullPointerException tolerated) {
93// permitted not to throw because it would be a no-op
94}
95expectUnchanged();
96}
97
98@MapFeature.Require(absent = SUPPORTS_PUT)
99@CollectionSize.Require(absent = ZERO)
100public void testReplace_unsupportedPresent() {
101try {
102getMap().replace(k0(), v3());
103fail("Expected UnsupportedOperationException");
104} catch (UnsupportedOperationException expected) {
105} catch (ClassCastException tolerated) {
106// for ClassToInstanceMap
107}
108
109expectUnchanged();
110}
111}
112