Java
106 строк · 3.7 Кб
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_VALUE_QUERIES;
22import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
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* Tester for {@link Map#remove(Object, Object)}. Can't be invoked directly; please see {@link
33* 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 MapRemoveEntryTester<K, V> extends AbstractMapTester<K, V> {
41@MapFeature.Require(SUPPORTS_REMOVE)
42@CollectionSize.Require(absent = ZERO)
43public void testRemove_supportedPresent() {
44assertTrue(getMap().remove(k0(), v0()));
45expectMissing(e0());
46}
47
48@MapFeature.Require(SUPPORTS_REMOVE)
49public void testRemove_supportedPresentKeyWrongValue() {
50assertFalse(getMap().remove(k0(), v3()));
51expectUnchanged();
52}
53
54@MapFeature.Require(SUPPORTS_REMOVE)
55public void testRemove_supportedWrongKeyPresentValue() {
56assertFalse(getMap().remove(k3(), v0()));
57expectUnchanged();
58}
59
60@MapFeature.Require(SUPPORTS_REMOVE)
61public void testRemove_supportedAbsentKeyAbsentValue() {
62assertFalse(getMap().remove(k3(), v3()));
63expectUnchanged();
64}
65
66@MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
67public void testRemove_nullKeyQueriesUnsupported() {
68try {
69assertFalse(getMap().remove(null, v3()));
70} catch (NullPointerException tolerated) {
71// since the operation would be a no-op, the exception is not required
72}
73expectUnchanged();
74}
75
76@MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES)
77public void testRemove_nullValueQueriesUnsupported() {
78try {
79assertFalse(getMap().remove(k3(), null));
80} catch (NullPointerException tolerated) {
81// since the operation would be a no-op, the exception is not required
82}
83expectUnchanged();
84}
85
86@MapFeature.Require(absent = SUPPORTS_REMOVE)
87@CollectionSize.Require(absent = ZERO)
88public void testRemove_unsupportedPresent() {
89try {
90getMap().remove(k0(), v0());
91fail("Expected UnsupportedOperationException");
92} catch (UnsupportedOperationException expected) {
93}
94expectUnchanged();
95}
96
97@MapFeature.Require(absent = SUPPORTS_REMOVE)
98public void testRemove_unsupportedAbsent() {
99try {
100assertFalse(getMap().remove(k0(), v3()));
101} catch (UnsupportedOperationException tolerated) {
102// since the operation would be a no-op, the exception is not required
103}
104expectUnchanged();
105}
106}
107