View Javadoc
1   /*
2    * Copyright (C) 2015 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.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
20  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
21  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
22  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
23  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.testing.AbstractCollectionTester;
27  import com.google.common.collect.testing.features.CollectionFeature;
28  import com.google.common.collect.testing.features.CollectionSize;
29  import java.util.Collection;
30  import java.util.ConcurrentModificationException;
31  import java.util.Iterator;
32  import java.util.function.Predicate;
33  
34  /**
35   * A generic JUnit test which tests {@link Collection#removeIf}. Can't be invoked directly;
36   * please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
37   *
38   * @author Louis Wasserman
39   */
40  @GwtCompatible
41  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
42  public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> {
43    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
44    public void testRemoveIf_alwaysFalse() {
45      assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false));
46      expectUnchanged();
47    }
48  
49    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
50    @CollectionSize.Require(absent = ZERO)
51    public void testRemoveIf_sometimesTrue() {
52      assertTrue("removeIf(isEqual(present)) should return true",
53          collection.removeIf(Predicate.isEqual(samples.e0())));
54      expectMissing(samples.e0());
55    }
56  
57    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
58    @CollectionSize.Require(absent = ZERO)
59    public void testRemoveIf_allPresent() {
60      assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true));
61      expectContents();
62    }
63  
64    @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
65    @CollectionSize.Require(SEVERAL)
66    public void testRemoveIfSomeMatchesConcurrentWithIteration() {
67      try {
68        Iterator<E> iterator = collection.iterator();
69        assertTrue(collection.removeIf(Predicate.isEqual(samples.e0())));
70        iterator.next();
71        fail("Expected ConcurrentModificationException");
72      } catch (ConcurrentModificationException expected) {
73        // success
74      }
75    }
76  
77    @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
78    @CollectionSize.Require(ZERO)
79    public void testRemoveIf_unsupportedEmptyCollection() {
80      try {
81        assertFalse("removeIf(Predicate) should return false or throw "
82            + "UnsupportedOperationException",
83          collection.removeIf(x -> {
84            throw new AssertionError("predicate should never be called");
85          }));
86      } catch (UnsupportedOperationException tolerated) {
87      }
88      expectUnchanged();
89    }
90  
91    @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
92    @CollectionSize.Require(absent = ZERO)
93    public void testRemoveIf_alwaysTrueUnsupported() {
94      try {
95        collection.removeIf(x -> true);
96        fail("removeIf(x -> true) should throw "
97            + "UnsupportedOperationException");
98      } catch (UnsupportedOperationException expected) {
99      }
100     expectUnchanged();
101     assertTrue(collection.contains(samples.e0()));
102   }
103 }