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 org.junit.Assert.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;
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
042public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> {
043  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
044  public void testRemoveIf_alwaysFalse() {
045    assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false));
046    expectUnchanged();
047  }
048
049  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
050  @CollectionSize.Require(absent = ZERO)
051  public void testRemoveIf_sometimesTrue() {
052    assertTrue(
053        "removeIf(isEqual(present)) should return true",
054        collection.removeIf(Predicate.isEqual(samples.e0())));
055    expectMissing(samples.e0());
056  }
057
058  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
059  @CollectionSize.Require(absent = ZERO)
060  public void testRemoveIf_allPresent() {
061    assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true));
062    expectContents();
063  }
064
065  @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
066  @CollectionSize.Require(SEVERAL)
067  public void testRemoveIfSomeMatchesConcurrentWithIteration() {
068    Iterator<E> iterator = collection.iterator();
069    assertTrue(collection.removeIf(Predicate.isEqual(samples.e0())));
070    assertThrows(ConcurrentModificationException.class, iterator::next);
071  }
072
073  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
074  @CollectionSize.Require(ZERO)
075  public void testRemoveIf_unsupportedEmptyCollection() {
076    try {
077      assertFalse(
078          "removeIf(Predicate) should return false or throw UnsupportedOperationException",
079          collection.removeIf(
080              x -> {
081                throw new AssertionError("predicate should never be called");
082              }));
083    } catch (UnsupportedOperationException tolerated) {
084    }
085    expectUnchanged();
086  }
087
088  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
089  @CollectionSize.Require(absent = ZERO)
090  public void testRemoveIf_alwaysTrueUnsupported() {
091    assertThrows(UnsupportedOperationException.class, () -> collection.removeIf(x -> true));
092    expectUnchanged();
093    assertTrue(collection.contains(samples.e0()));
094  }
095}