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