001/*
002 * Copyright (C) 2008 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.ALLOWS_NULL_QUERIES;
020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
021import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
023import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
024import static com.google.common.collect.testing.features.CollectionSize.ZERO;
025import static org.junit.Assert.assertThrows;
026
027import com.google.common.annotations.GwtCompatible;
028import com.google.common.collect.testing.AbstractCollectionTester;
029import com.google.common.collect.testing.WrongType;
030import com.google.common.collect.testing.features.CollectionFeature;
031import com.google.common.collect.testing.features.CollectionSize;
032import java.util.ConcurrentModificationException;
033import java.util.Iterator;
034
035/**
036 * A generic JUnit test which tests {@code remove} operations on a collection. Can't be invoked
037 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
038 *
039 * @author George van den Driessche
040 */
041@GwtCompatible
042public class CollectionRemoveTester<E> extends AbstractCollectionTester<E> {
043  @CollectionFeature.Require(SUPPORTS_REMOVE)
044  @CollectionSize.Require(absent = ZERO)
045  public void testRemove_present() {
046    int initialSize = collection.size();
047    assertTrue("remove(present) should return true", collection.remove(e0()));
048    assertEquals(
049        "remove(present) should decrease a collection's size by one.",
050        initialSize - 1,
051        collection.size());
052    expectMissing(e0());
053  }
054
055  @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
056  @CollectionSize.Require(SEVERAL)
057  public void testRemovePresentConcurrentWithIteration() {
058    Iterator<E> iterator = collection.iterator();
059    assertTrue(collection.remove(e0()));
060    assertThrows(ConcurrentModificationException.class, iterator::next);
061  }
062
063  @CollectionFeature.Require(SUPPORTS_REMOVE)
064  public void testRemove_notPresent() {
065    assertFalse("remove(notPresent) should return false", collection.remove(e3()));
066    expectUnchanged();
067  }
068
069  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
070  @CollectionSize.Require(absent = ZERO)
071  public void testRemove_nullPresent() {
072    collection = getSubjectGenerator().create(createArrayWithNullElement());
073
074    int initialSize = collection.size();
075    assertTrue("remove(null) should return true", collection.remove(null));
076    assertEquals(
077        "remove(present) should decrease a collection's size by one.",
078        initialSize - 1,
079        collection.size());
080    expectMissing((E) null);
081  }
082
083  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
084  @CollectionSize.Require(absent = ZERO)
085  public void testRemove_unsupported() {
086    assertThrows(UnsupportedOperationException.class, () -> collection.remove(e0()));
087    expectUnchanged();
088    assertTrue("remove(present) should not remove the element", collection.contains(e0()));
089  }
090
091  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
092  public void testRemove_unsupportedNotPresent() {
093    try {
094      assertFalse(
095          "remove(notPresent) should return false or throw UnsupportedOperationException",
096          collection.remove(e3()));
097    } catch (UnsupportedOperationException tolerated) {
098    }
099    expectUnchanged();
100    expectMissing(e3());
101  }
102
103  @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
104  public void testRemove_nullNotSupported() {
105    try {
106      assertFalse(
107          "remove(null) should return false or throw NullPointerException",
108          collection.remove(null));
109    } catch (NullPointerException tolerated) {
110    }
111    expectUnchanged();
112  }
113
114  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
115  public void testRemove_nullAllowed() {
116    assertFalse("remove(null) should return false", collection.remove(null));
117    expectUnchanged();
118  }
119
120  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
121  @CollectionSize.Require(absent = ZERO)
122  public void testIteratorRemove_unsupported() {
123    Iterator<E> iterator = collection.iterator();
124    iterator.next();
125    assertThrows(UnsupportedOperationException.class, iterator::remove);
126    expectUnchanged();
127    assertTrue(collection.contains(e0()));
128  }
129
130  @CollectionFeature.Require(SUPPORTS_REMOVE)
131  public void testRemove_wrongType() {
132    try {
133      assertFalse(collection.remove(WrongType.VALUE));
134    } catch (ClassCastException tolerated) {
135    }
136    expectUnchanged();
137  }
138}