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