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@SuppressWarnings("unchecked") // too many "unchecked generic array creations" 043@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 044public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> { 045 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 046 public void testRemoveIf_alwaysFalse() { 047 assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false)); 048 expectUnchanged(); 049 } 050 051 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 052 @CollectionSize.Require(absent = ZERO) 053 public void testRemoveIf_sometimesTrue() { 054 assertTrue( 055 "removeIf(isEqual(present)) should return true", 056 collection.removeIf(Predicate.isEqual(samples.e0()))); 057 expectMissing(samples.e0()); 058 } 059 060 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 061 @CollectionSize.Require(absent = ZERO) 062 public void testRemoveIf_allPresent() { 063 assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true)); 064 expectContents(); 065 } 066 067 @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 068 @CollectionSize.Require(SEVERAL) 069 public void testRemoveIfSomeMatchesConcurrentWithIteration() { 070 try { 071 Iterator<E> iterator = collection.iterator(); 072 assertTrue(collection.removeIf(Predicate.isEqual(samples.e0()))); 073 iterator.next(); 074 fail("Expected ConcurrentModificationException"); 075 } catch (ConcurrentModificationException expected) { 076 // success 077 } 078 } 079 080 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 081 @CollectionSize.Require(ZERO) 082 public void testRemoveIf_unsupportedEmptyCollection() { 083 try { 084 assertFalse( 085 "removeIf(Predicate) should return false or throw " + "UnsupportedOperationException", 086 collection.removeIf( 087 x -> { 088 throw new AssertionError("predicate should never be called"); 089 })); 090 } catch (UnsupportedOperationException tolerated) { 091 } 092 expectUnchanged(); 093 } 094 095 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 096 @CollectionSize.Require(absent = ZERO) 097 public void testRemoveIf_alwaysTrueUnsupported() { 098 try { 099 collection.removeIf(x -> true); 100 fail("removeIf(x -> true) should throw " + "UnsupportedOperationException"); 101 } catch (UnsupportedOperationException expected) { 102 } 103 expectUnchanged(); 104 assertTrue(collection.contains(samples.e0())); 105 } 106}