Java
208 строк · 7.8 Кб
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.CollectionFeature.ALLOWS_NULL_QUERIES;
20import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
21import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
22import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
23import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
24import static com.google.common.collect.testing.features.CollectionSize.ZERO;
25
26import com.google.common.annotations.GwtCompatible;
27import com.google.common.collect.testing.AbstractCollectionTester;
28import com.google.common.collect.testing.MinimalCollection;
29import com.google.common.collect.testing.WrongType;
30import com.google.common.collect.testing.features.CollectionFeature;
31import com.google.common.collect.testing.features.CollectionSize;
32import java.util.AbstractSet;
33import java.util.Collections;
34import java.util.ConcurrentModificationException;
35import java.util.Iterator;
36import org.junit.Ignore;
37
38/**
39* A generic JUnit test which tests {@code removeAll} operations on a collection. Can't be invoked
40* directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
41*
42* @author George van den Driessche
43* @author Chris Povirk
44*/
45@GwtCompatible
46@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
47@SuppressWarnings("JUnit4ClassUsedInJUnit3")
48public class CollectionRemoveAllTester<E> extends AbstractCollectionTester<E> {
49@CollectionFeature.Require(SUPPORTS_REMOVE)
50public void testRemoveAll_emptyCollection() {
51assertFalse(
52"removeAll(emptyCollection) should return false",
53collection.removeAll(MinimalCollection.of()));
54expectUnchanged();
55}
56
57@CollectionFeature.Require(SUPPORTS_REMOVE)
58public void testRemoveAll_nonePresent() {
59assertFalse(
60"removeAll(disjointCollection) should return false",
61collection.removeAll(MinimalCollection.of(e3())));
62expectUnchanged();
63}
64
65@CollectionFeature.Require(SUPPORTS_REMOVE)
66@CollectionSize.Require(absent = ZERO)
67public void testRemoveAll_allPresent() {
68assertTrue(
69"removeAll(intersectingCollection) should return true",
70collection.removeAll(MinimalCollection.of(e0())));
71expectMissing(e0());
72}
73
74@CollectionFeature.Require(SUPPORTS_REMOVE)
75@CollectionSize.Require(absent = ZERO)
76public void testRemoveAll_somePresent() {
77assertTrue(
78"removeAll(intersectingCollection) should return true",
79collection.removeAll(MinimalCollection.of(e0(), e3())));
80expectMissing(e0());
81}
82
83@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
84@CollectionSize.Require(SEVERAL)
85public void testRemoveAllSomePresentConcurrentWithIteration() {
86try {
87Iterator<E> iterator = collection.iterator();
88assertTrue(collection.removeAll(MinimalCollection.of(e0(), e3())));
89iterator.next();
90fail("Expected ConcurrentModificationException");
91} catch (ConcurrentModificationException expected) {
92// success
93}
94}
95
96/** Trigger the {@code other.size() >= this.size()} case in {@link AbstractSet#removeAll}. */
97@CollectionFeature.Require(SUPPORTS_REMOVE)
98@CollectionSize.Require(absent = ZERO)
99public void testRemoveAll_somePresentLargeCollectionToRemove() {
100assertTrue(
101"removeAll(largeIntersectingCollection) should return true",
102collection.removeAll(MinimalCollection.of(e0(), e0(), e0(), e3(), e3(), e3())));
103expectMissing(e0());
104}
105
106@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
107public void testRemoveAll_unsupportedEmptyCollection() {
108try {
109assertFalse(
110"removeAll(emptyCollection) should return false or throw "
111+ "UnsupportedOperationException",
112collection.removeAll(MinimalCollection.of()));
113} catch (UnsupportedOperationException tolerated) {
114}
115expectUnchanged();
116}
117
118@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
119public void testRemoveAll_unsupportedNonePresent() {
120try {
121assertFalse(
122"removeAll(disjointCollection) should return false or throw "
123+ "UnsupportedOperationException",
124collection.removeAll(MinimalCollection.of(e3())));
125} catch (UnsupportedOperationException tolerated) {
126}
127expectUnchanged();
128}
129
130@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
131@CollectionSize.Require(absent = ZERO)
132public void testRemoveAll_unsupportedPresent() {
133try {
134collection.removeAll(MinimalCollection.of(e0()));
135fail("removeAll(intersectingCollection) should throw UnsupportedOperationException");
136} catch (UnsupportedOperationException expected) {
137}
138expectUnchanged();
139assertTrue(collection.contains(e0()));
140}
141
142/*
143* AbstractCollection fails the removeAll(null) test when the subject
144* collection is empty, but we'd still like to test removeAll(null) when we
145* can. We split the test into empty and non-empty cases. This allows us to
146* suppress only the former.
147*/
148
149@CollectionFeature.Require(SUPPORTS_REMOVE)
150@CollectionSize.Require(ZERO)
151public void testRemoveAll_nullCollectionReferenceEmptySubject() {
152try {
153collection.removeAll(null);
154// Returning successfully is not ideal, but tolerated.
155} catch (NullPointerException tolerated) {
156}
157}
158
159@CollectionFeature.Require(SUPPORTS_REMOVE)
160@CollectionSize.Require(absent = ZERO)
161public void testRemoveAll_nullCollectionReferenceNonEmptySubject() {
162try {
163collection.removeAll(null);
164fail("removeAll(null) should throw NullPointerException");
165} catch (NullPointerException expected) {
166}
167}
168
169@CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
170public void testRemoveAll_containsNullNo() {
171MinimalCollection<?> containsNull = MinimalCollection.of((Object) null);
172try {
173assertFalse(
174"removeAll(containsNull) should return false or throw",
175collection.removeAll(containsNull));
176} catch (NullPointerException tolerated) {
177}
178expectUnchanged();
179}
180
181@CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
182public void testRemoveAll_containsNullNoButAllowed() {
183MinimalCollection<?> containsNull = MinimalCollection.of((Object) null);
184assertFalse("removeAll(containsNull) should return false", collection.removeAll(containsNull));
185expectUnchanged();
186}
187
188@CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
189@CollectionSize.Require(absent = ZERO)
190public void testRemoveAll_containsNullYes() {
191initCollectionWithNullElement();
192assertTrue(
193"removeAll(containsNull) should return true",
194collection.removeAll(Collections.singleton(null)));
195// TODO: make this work with MinimalCollection
196}
197
198@CollectionFeature.Require(SUPPORTS_REMOVE)
199public void testRemoveAll_containsWrongType() {
200try {
201assertFalse(
202"removeAll(containsWrongType) should return false or throw",
203collection.removeAll(MinimalCollection.of(WrongType.VALUE)));
204} catch (ClassCastException tolerated) {
205}
206expectUnchanged();
207}
208}
209