• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.google.common.collect.testing.testers;
18 
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
20 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
21 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
22 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
23 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
24 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
25 
26 import com.google.common.annotations.GwtCompatible;
27 import com.google.common.collect.testing.AbstractCollectionTester;
28 import com.google.common.collect.testing.MinimalCollection;
29 import com.google.common.collect.testing.WrongType;
30 import com.google.common.collect.testing.features.CollectionFeature;
31 import com.google.common.collect.testing.features.CollectionSize;
32 import java.util.AbstractSet;
33 import java.util.Collections;
34 import java.util.ConcurrentModificationException;
35 import java.util.Iterator;
36 import 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 @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
46 @GwtCompatible
47 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
48 public class CollectionRemoveAllTester<E> extends AbstractCollectionTester<E> {
49   @CollectionFeature.Require(SUPPORTS_REMOVE)
testRemoveAll_emptyCollection()50   public void testRemoveAll_emptyCollection() {
51     assertFalse(
52         "removeAll(emptyCollection) should return false",
53         collection.removeAll(MinimalCollection.of()));
54     expectUnchanged();
55   }
56 
57   @CollectionFeature.Require(SUPPORTS_REMOVE)
testRemoveAll_nonePresent()58   public void testRemoveAll_nonePresent() {
59     assertFalse(
60         "removeAll(disjointCollection) should return false",
61         collection.removeAll(MinimalCollection.of(e3())));
62     expectUnchanged();
63   }
64 
65   @CollectionFeature.Require(SUPPORTS_REMOVE)
66   @CollectionSize.Require(absent = ZERO)
testRemoveAll_allPresent()67   public void testRemoveAll_allPresent() {
68     assertTrue(
69         "removeAll(intersectingCollection) should return true",
70         collection.removeAll(MinimalCollection.of(e0())));
71     expectMissing(e0());
72   }
73 
74   @CollectionFeature.Require(SUPPORTS_REMOVE)
75   @CollectionSize.Require(absent = ZERO)
testRemoveAll_somePresent()76   public void testRemoveAll_somePresent() {
77     assertTrue(
78         "removeAll(intersectingCollection) should return true",
79         collection.removeAll(MinimalCollection.of(e0(), e3())));
80     expectMissing(e0());
81   }
82 
83   @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
84   @CollectionSize.Require(SEVERAL)
testRemoveAllSomePresentConcurrentWithIteration()85   public void testRemoveAllSomePresentConcurrentWithIteration() {
86     try {
87       Iterator<E> iterator = collection.iterator();
88       assertTrue(collection.removeAll(MinimalCollection.of(e0(), e3())));
89       iterator.next();
90       fail("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)
testRemoveAll_somePresentLargeCollectionToRemove()99   public void testRemoveAll_somePresentLargeCollectionToRemove() {
100     assertTrue(
101         "removeAll(largeIntersectingCollection) should return true",
102         collection.removeAll(MinimalCollection.of(e0(), e0(), e0(), e3(), e3(), e3())));
103     expectMissing(e0());
104   }
105 
106   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
testRemoveAll_unsupportedEmptyCollection()107   public void testRemoveAll_unsupportedEmptyCollection() {
108     try {
109       assertFalse(
110           "removeAll(emptyCollection) should return false or throw "
111               + "UnsupportedOperationException",
112           collection.removeAll(MinimalCollection.of()));
113     } catch (UnsupportedOperationException tolerated) {
114     }
115     expectUnchanged();
116   }
117 
118   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
testRemoveAll_unsupportedNonePresent()119   public void testRemoveAll_unsupportedNonePresent() {
120     try {
121       assertFalse(
122           "removeAll(disjointCollection) should return false or throw "
123               + "UnsupportedOperationException",
124           collection.removeAll(MinimalCollection.of(e3())));
125     } catch (UnsupportedOperationException tolerated) {
126     }
127     expectUnchanged();
128   }
129 
130   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
131   @CollectionSize.Require(absent = ZERO)
testRemoveAll_unsupportedPresent()132   public void testRemoveAll_unsupportedPresent() {
133     try {
134       collection.removeAll(MinimalCollection.of(e0()));
135       fail("removeAll(intersectingCollection) should throw UnsupportedOperationException");
136     } catch (UnsupportedOperationException expected) {
137     }
138     expectUnchanged();
139     assertTrue(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)
testRemoveAll_nullCollectionReferenceEmptySubject()151   public void testRemoveAll_nullCollectionReferenceEmptySubject() {
152     try {
153       collection.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)
testRemoveAll_nullCollectionReferenceNonEmptySubject()161   public void testRemoveAll_nullCollectionReferenceNonEmptySubject() {
162     try {
163       collection.removeAll(null);
164       fail("removeAll(null) should throw NullPointerException");
165     } catch (NullPointerException expected) {
166     }
167   }
168 
169   @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
testRemoveAll_containsNullNo()170   public void testRemoveAll_containsNullNo() {
171     MinimalCollection<?> containsNull = MinimalCollection.of((Object) null);
172     try {
173       assertFalse(
174           "removeAll(containsNull) should return false or throw",
175           collection.removeAll(containsNull));
176     } catch (NullPointerException tolerated) {
177     }
178     expectUnchanged();
179   }
180 
181   @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
testRemoveAll_containsNullNoButAllowed()182   public void testRemoveAll_containsNullNoButAllowed() {
183     MinimalCollection<?> containsNull = MinimalCollection.of((Object) null);
184     assertFalse("removeAll(containsNull) should return false", collection.removeAll(containsNull));
185     expectUnchanged();
186   }
187 
188   @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
189   @CollectionSize.Require(absent = ZERO)
testRemoveAll_containsNullYes()190   public void testRemoveAll_containsNullYes() {
191     initCollectionWithNullElement();
192     assertTrue(
193         "removeAll(containsNull) should return true",
194         collection.removeAll(Collections.singleton(null)));
195     // TODO: make this work with MinimalCollection
196   }
197 
198   @CollectionFeature.Require(SUPPORTS_REMOVE)
testRemoveAll_containsWrongType()199   public void testRemoveAll_containsWrongType() {
200     try {
201       assertFalse(
202           "removeAll(containsWrongType) should return false or throw",
203           collection.removeAll(MinimalCollection.of(WrongType.VALUE)));
204     } catch (ClassCastException tolerated) {
205     }
206     expectUnchanged();
207   }
208 }
209