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