Java
152 строки · 5.3 Кб
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_VALUES;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
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 org.junit.Ignore;
30
31/**
32* A generic JUnit test which tests {@link Map#replace(Object, Object, Object)}. Can't be invoked
33* directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
34*
35* @author Louis Wasserman
36*/
37@GwtCompatible
38@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
39@SuppressWarnings("JUnit4ClassUsedInJUnit3")
40public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
41
42@MapFeature.Require(SUPPORTS_PUT)
43@CollectionSize.Require(absent = ZERO)
44public void testReplaceEntry_supportedPresent() {
45try {
46assertTrue(getMap().replace(k0(), v0(), v3()));
47expectReplacement(entry(k0(), v3()));
48} catch (ClassCastException tolerated) { // for ClassToInstanceMap
49expectUnchanged();
50}
51}
52
53@MapFeature.Require(SUPPORTS_PUT)
54@CollectionSize.Require(absent = ZERO)
55public void testReplaceEntry_supportedPresentUnchanged() {
56assertTrue(getMap().replace(k0(), v0(), v0()));
57expectUnchanged();
58}
59
60@MapFeature.Require(SUPPORTS_PUT)
61@CollectionSize.Require(absent = ZERO)
62public void testReplaceEntry_supportedWrongValue() {
63assertFalse(getMap().replace(k0(), v3(), v4()));
64expectUnchanged();
65}
66
67@MapFeature.Require(SUPPORTS_PUT)
68public void testReplaceEntry_supportedAbsentKey() {
69assertFalse(getMap().replace(k3(), v3(), v4()));
70expectUnchanged();
71}
72
73@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
74@CollectionSize.Require(absent = ZERO)
75public void testReplaceEntry_presentNullValueUnsupported() {
76try {
77getMap().replace(k0(), v0(), null);
78fail("Expected NullPointerException");
79} catch (NullPointerException expected) {
80}
81expectUnchanged();
82}
83
84@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
85@CollectionSize.Require(absent = ZERO)
86public void testReplaceEntry_wrongValueNullValueUnsupported() {
87try {
88assertFalse(getMap().replace(k0(), v3(), null));
89} catch (NullPointerException tolerated) {
90// the operation would be a no-op, so exceptions are allowed but not required
91}
92expectUnchanged();
93}
94
95@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
96public void testReplaceEntry_absentKeyNullValueUnsupported() {
97try {
98assertFalse(getMap().replace(k3(), v3(), null));
99} catch (NullPointerException tolerated) {
100// the operation would be a no-op, so exceptions are allowed but not required
101}
102expectUnchanged();
103}
104
105@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES})
106public void testReplaceEntry_nullDifferentFromAbsent() {
107assertFalse(getMap().replace(k3(), null, v3()));
108expectUnchanged();
109}
110
111@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
112public void testReplaceEntry_expectNullUnsupported() {
113try {
114assertFalse(getMap().replace(k3(), null, v3()));
115} catch (NullPointerException tolerated) {
116// the operation would be a no-op, so exceptions are allowed but not required
117}
118expectUnchanged();
119}
120
121@MapFeature.Require(absent = SUPPORTS_PUT)
122@CollectionSize.Require(absent = ZERO)
123public void testReplaceEntry_unsupportedPresent() {
124try {
125getMap().replace(k0(), v0(), v3());
126fail("Expected UnsupportedOperationException");
127} catch (UnsupportedOperationException expected) {
128}
129expectUnchanged();
130}
131
132@MapFeature.Require(absent = SUPPORTS_PUT)
133@CollectionSize.Require(absent = ZERO)
134public void testReplaceEntry_unsupportedWrongValue() {
135try {
136getMap().replace(k0(), v3(), v4());
137} catch (UnsupportedOperationException tolerated) {
138// the operation would be a no-op, so exceptions are allowed but not required
139}
140expectUnchanged();
141}
142
143@MapFeature.Require(absent = SUPPORTS_PUT)
144public void testReplaceEntry_unsupportedAbsentKey() {
145try {
146getMap().replace(k3(), v3(), v4());
147} catch (UnsupportedOperationException tolerated) {
148// the operation would be a no-op, so exceptions are allowed but not required
149}
150expectUnchanged();
151}
152}
153