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