001/* 002 * Copyright (C) 2015 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 020import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 021import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 022import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 023import static com.google.common.collect.testing.features.CollectionSize.ZERO; 024import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 025 026import com.google.common.annotations.GwtCompatible; 027import com.google.common.collect.testing.AbstractCollectionTester; 028import com.google.common.collect.testing.features.CollectionFeature; 029import com.google.common.collect.testing.features.CollectionSize; 030import java.util.Collection; 031import java.util.ConcurrentModificationException; 032import java.util.Iterator; 033import java.util.function.Predicate; 034import org.junit.Ignore; 035 036/** 037 * A generic JUnit test which tests {@link Collection#removeIf}. Can't be invoked directly; please 038 * see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 039 * 040 * @author Louis Wasserman 041 */ 042@GwtCompatible 043@Ignore("test runners must not instantiate and run this directly, only via suites we build") 044// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 045@SuppressWarnings("JUnit4ClassUsedInJUnit3") 046@IgnoreJRERequirement // We opt into library desugaring for our tests. 047public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> { 048 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 049 public void testRemoveIf_alwaysFalse() { 050 assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false)); 051 expectUnchanged(); 052 } 053 054 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 055 @CollectionSize.Require(absent = ZERO) 056 public void testRemoveIf_sometimesTrue() { 057 assertTrue( 058 "removeIf(isEqual(present)) should return true", 059 collection.removeIf(Predicate.isEqual(samples.e0()))); 060 expectMissing(samples.e0()); 061 } 062 063 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 064 @CollectionSize.Require(absent = ZERO) 065 public void testRemoveIf_allPresent() { 066 assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true)); 067 expectContents(); 068 } 069 070 @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 071 @CollectionSize.Require(SEVERAL) 072 public void testRemoveIfSomeMatchesConcurrentWithIteration() { 073 assertThrows( 074 ConcurrentModificationException.class, 075 () -> { 076 Iterator<E> iterator = collection.iterator(); 077 assertTrue(collection.removeIf(Predicate.isEqual(samples.e0()))); 078 iterator.next(); 079 }); 080 } 081 082 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 083 @CollectionSize.Require(ZERO) 084 public void testRemoveIf_unsupportedEmptyCollection() { 085 try { 086 assertFalse( 087 "removeIf(Predicate) should return false or throw " + "UnsupportedOperationException", 088 collection.removeIf( 089 x -> { 090 throw new AssertionError("predicate should never be called"); 091 })); 092 } catch (UnsupportedOperationException tolerated) { 093 } 094 expectUnchanged(); 095 } 096 097 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 098 @CollectionSize.Require(absent = ZERO) 099 public void testRemoveIf_alwaysTrueUnsupported() { 100 assertThrows(UnsupportedOperationException.class, () -> collection.removeIf(x -> true)); 101 expectUnchanged(); 102 assertTrue(collection.contains(samples.e0())); 103 } 104}