Java
106 строк · 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.
43@SuppressWarnings("JUnit4ClassUsedInJUnit3")
44public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> {
45@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
46public void testRemoveIf_alwaysFalse() {
47assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false));
48expectUnchanged();
49}
50
51@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
52@CollectionSize.Require(absent = ZERO)
53public void testRemoveIf_sometimesTrue() {
54assertTrue(
55"removeIf(isEqual(present)) should return true",
56collection.removeIf(Predicate.isEqual(samples.e0())));
57expectMissing(samples.e0());
58}
59
60@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
61@CollectionSize.Require(absent = ZERO)
62public void testRemoveIf_allPresent() {
63assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true));
64expectContents();
65}
66
67@CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
68@CollectionSize.Require(SEVERAL)
69public void testRemoveIfSomeMatchesConcurrentWithIteration() {
70try {
71Iterator<E> iterator = collection.iterator();
72assertTrue(collection.removeIf(Predicate.isEqual(samples.e0())));
73iterator.next();
74fail("Expected ConcurrentModificationException");
75} catch (ConcurrentModificationException expected) {
76// success
77}
78}
79
80@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
81@CollectionSize.Require(ZERO)
82public void testRemoveIf_unsupportedEmptyCollection() {
83try {
84assertFalse(
85"removeIf(Predicate) should return false or throw " + "UnsupportedOperationException",
86collection.removeIf(
87x -> {
88throw new AssertionError("predicate should never be called");
89}));
90} catch (UnsupportedOperationException tolerated) {
91}
92expectUnchanged();
93}
94
95@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
96@CollectionSize.Require(absent = ZERO)
97public void testRemoveIf_alwaysTrueUnsupported() {
98try {
99collection.removeIf(x -> true);
100fail("removeIf(x -> true) should throw " + "UnsupportedOperationException");
101} catch (UnsupportedOperationException expected) {
102}
103expectUnchanged();
104assertTrue(collection.contains(samples.e0()));
105}
106}
107