Java
165 строк · 6.0 Кб
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.
45@SuppressWarnings("JUnit4ClassUsedInJUnit3")
46public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> {
47@MapFeature.Require(SUPPORTS_REMOVE)
48@CollectionSize.Require(absent = ZERO)
49public void testRemove_present() {
50int initialSize = getMap().size();
51assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0()));
52assertEquals(
53"remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
54expectMissing(e0());
55}
56
57@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
58@CollectionSize.Require(SEVERAL)
59public void testRemovePresentConcurrentWithEntrySetIteration() {
60try {
61Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
62getMap().remove(k0());
63iterator.next();
64fail("Expected ConcurrentModificationException");
65} catch (ConcurrentModificationException expected) {
66// success
67}
68}
69
70@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
71@CollectionSize.Require(SEVERAL)
72public void testRemovePresentConcurrentWithKeySetIteration() {
73try {
74Iterator<K> iterator = getMap().keySet().iterator();
75getMap().remove(k0());
76iterator.next();
77fail("Expected ConcurrentModificationException");
78} catch (ConcurrentModificationException expected) {
79// success
80}
81}
82
83@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
84@CollectionSize.Require(SEVERAL)
85public void testRemovePresentConcurrentWithValuesIteration() {
86try {
87Iterator<V> iterator = getMap().values().iterator();
88getMap().remove(k0());
89iterator.next();
90fail("Expected ConcurrentModificationException");
91} catch (ConcurrentModificationException expected) {
92// success
93}
94}
95
96@MapFeature.Require(SUPPORTS_REMOVE)
97public void testRemove_notPresent() {
98assertNull("remove(notPresent) should return null", getMap().remove(k3()));
99expectUnchanged();
100}
101
102@MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
103@CollectionSize.Require(absent = ZERO)
104public void testRemove_nullPresent() {
105initMapWithNullKey();
106
107int initialSize = getMap().size();
108assertEquals(
109"remove(null) should return the associated value",
110getValueForNullKey(),
111getMap().remove(null));
112assertEquals(
113"remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
114expectMissing(entry(null, getValueForNullKey()));
115}
116
117@MapFeature.Require(absent = SUPPORTS_REMOVE)
118@CollectionSize.Require(absent = ZERO)
119public void testRemove_unsupported() {
120try {
121getMap().remove(k0());
122fail("remove(present) should throw UnsupportedOperationException");
123} catch (UnsupportedOperationException expected) {
124}
125expectUnchanged();
126assertEquals("remove(present) should not remove the element", v0(), get(k0()));
127}
128
129@MapFeature.Require(absent = SUPPORTS_REMOVE)
130public void testRemove_unsupportedNotPresent() {
131try {
132assertNull(
133"remove(notPresent) should return null or throw UnsupportedOperationException",
134getMap().remove(k3()));
135} catch (UnsupportedOperationException tolerated) {
136}
137expectUnchanged();
138expectMissing(e3());
139}
140
141@MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
142public void testRemove_nullQueriesNotSupported() {
143try {
144assertNull(
145"remove(null) should return null or throw NullPointerException", getMap().remove(null));
146} catch (NullPointerException tolerated) {
147}
148expectUnchanged();
149}
150
151@MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES})
152public void testRemove_nullSupportedMissing() {
153assertNull("remove(null) should return null", getMap().remove(null));
154expectUnchanged();
155}
156
157@MapFeature.Require(SUPPORTS_REMOVE)
158public void testRemove_wrongType() {
159try {
160assertNull(getMap().remove(WrongType.VALUE));
161} catch (ClassCastException tolerated) {
162}
163expectUnchanged();
164}
165}
166