guava
164 строки · 5.9 Кб
1/*
2* Copyright (C) 2008 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.SEVERAL;
20import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
23import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
24import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
25
26import com.google.common.annotations.GwtCompatible;
27import com.google.common.collect.testing.AbstractMapTester;
28import com.google.common.collect.testing.WrongType;
29import com.google.common.collect.testing.features.CollectionSize;
30import com.google.common.collect.testing.features.MapFeature;
31import java.util.ConcurrentModificationException;
32import java.util.Iterator;
33import java.util.Map.Entry;
34import org.junit.Ignore;
35
36/**
37* A generic JUnit test which tests {@code remove} operations on a map. Can't be invoked directly;
38* please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
39*
40* @author George van den Driessche
41* @author Chris Povirk
42*/
43@GwtCompatible
44@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
45public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> {
46@MapFeature.Require(SUPPORTS_REMOVE)
47@CollectionSize.Require(absent = ZERO)
48public void testRemove_present() {
49int initialSize = getMap().size();
50assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0()));
51assertEquals(
52"remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
53expectMissing(e0());
54}
55
56@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
57@CollectionSize.Require(SEVERAL)
58public void testRemovePresentConcurrentWithEntrySetIteration() {
59try {
60Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
61getMap().remove(k0());
62iterator.next();
63fail("Expected ConcurrentModificationException");
64} catch (ConcurrentModificationException expected) {
65// success
66}
67}
68
69@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
70@CollectionSize.Require(SEVERAL)
71public void testRemovePresentConcurrentWithKeySetIteration() {
72try {
73Iterator<K> iterator = getMap().keySet().iterator();
74getMap().remove(k0());
75iterator.next();
76fail("Expected ConcurrentModificationException");
77} catch (ConcurrentModificationException expected) {
78// success
79}
80}
81
82@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
83@CollectionSize.Require(SEVERAL)
84public void testRemovePresentConcurrentWithValuesIteration() {
85try {
86Iterator<V> iterator = getMap().values().iterator();
87getMap().remove(k0());
88iterator.next();
89fail("Expected ConcurrentModificationException");
90} catch (ConcurrentModificationException expected) {
91// success
92}
93}
94
95@MapFeature.Require(SUPPORTS_REMOVE)
96public void testRemove_notPresent() {
97assertNull("remove(notPresent) should return null", getMap().remove(k3()));
98expectUnchanged();
99}
100
101@MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
102@CollectionSize.Require(absent = ZERO)
103public void testRemove_nullPresent() {
104initMapWithNullKey();
105
106int initialSize = getMap().size();
107assertEquals(
108"remove(null) should return the associated value",
109getValueForNullKey(),
110getMap().remove(null));
111assertEquals(
112"remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
113expectMissing(entry(null, getValueForNullKey()));
114}
115
116@MapFeature.Require(absent = SUPPORTS_REMOVE)
117@CollectionSize.Require(absent = ZERO)
118public void testRemove_unsupported() {
119try {
120getMap().remove(k0());
121fail("remove(present) should throw UnsupportedOperationException");
122} catch (UnsupportedOperationException expected) {
123}
124expectUnchanged();
125assertEquals("remove(present) should not remove the element", v0(), get(k0()));
126}
127
128@MapFeature.Require(absent = SUPPORTS_REMOVE)
129public void testRemove_unsupportedNotPresent() {
130try {
131assertNull(
132"remove(notPresent) should return null or throw UnsupportedOperationException",
133getMap().remove(k3()));
134} catch (UnsupportedOperationException tolerated) {
135}
136expectUnchanged();
137expectMissing(e3());
138}
139
140@MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
141public void testRemove_nullQueriesNotSupported() {
142try {
143assertNull(
144"remove(null) should return null or throw NullPointerException", getMap().remove(null));
145} catch (NullPointerException tolerated) {
146}
147expectUnchanged();
148}
149
150@MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES})
151public void testRemove_nullSupportedMissing() {
152assertNull("remove(null) should return null", getMap().remove(null));
153expectUnchanged();
154}
155
156@MapFeature.Require(SUPPORTS_REMOVE)
157public void testRemove_wrongType() {
158try {
159assertNull(getMap().remove(WrongType.VALUE));
160} catch (ClassCastException tolerated) {
161}
162expectUnchanged();
163}
164}
165