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; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.collect.testing.AbstractCollectionTester; 027import com.google.common.collect.testing.features.CollectionFeature; 028import com.google.common.collect.testing.features.CollectionSize; 029import java.util.Collection; 030import java.util.ConcurrentModificationException; 031import java.util.Iterator; 032import java.util.function.Predicate; 033import org.junit.Ignore; 034 035/** 036 * A generic JUnit test which tests {@link Collection#removeIf}. Can't be invoked directly; please 037 * see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 038 * 039 * @author Louis Wasserman 040 */ 041@GwtCompatible 042@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 043public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> { 044 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 045 public void testRemoveIf_alwaysFalse() { 046 assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false)); 047 expectUnchanged(); 048 } 049 050 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 051 @CollectionSize.Require(absent = ZERO) 052 public void testRemoveIf_sometimesTrue() { 053 assertTrue( 054 "removeIf(isEqual(present)) should return true", 055 collection.removeIf(Predicate.isEqual(samples.e0()))); 056 expectMissing(samples.e0()); 057 } 058 059 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 060 @CollectionSize.Require(absent = ZERO) 061 public void testRemoveIf_allPresent() { 062 assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true)); 063 expectContents(); 064 } 065 066 @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 067 @CollectionSize.Require(SEVERAL) 068 public void testRemoveIfSomeMatchesConcurrentWithIteration() { 069 try { 070 Iterator<E> iterator = collection.iterator(); 071 assertTrue(collection.removeIf(Predicate.isEqual(samples.e0()))); 072 iterator.next(); 073 fail("Expected ConcurrentModificationException"); 074 } catch (ConcurrentModificationException expected) { 075 // success 076 } 077 } 078 079 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 080 @CollectionSize.Require(ZERO) 081 public void testRemoveIf_unsupportedEmptyCollection() { 082 try { 083 assertFalse( 084 "removeIf(Predicate) should return false or throw " + "UnsupportedOperationException", 085 collection.removeIf( 086 x -> { 087 throw new AssertionError("predicate should never be called"); 088 })); 089 } catch (UnsupportedOperationException tolerated) { 090 } 091 expectUnchanged(); 092 } 093 094 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 095 @CollectionSize.Require(absent = ZERO) 096 public void testRemoveIf_alwaysTrueUnsupported() { 097 try { 098 collection.removeIf(x -> true); 099 fail("removeIf(x -> true) should throw " + "UnsupportedOperationException"); 100 } catch (UnsupportedOperationException expected) { 101 } 102 expectUnchanged(); 103 assertTrue(collection.contains(samples.e0())); 104 } 105}