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;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.testing.AbstractCollectionTester;
028import com.google.common.collect.testing.WrongType;
029import com.google.common.collect.testing.features.CollectionFeature;
030import com.google.common.collect.testing.features.CollectionSize;
031import java.util.ConcurrentModificationException;
032import java.util.Iterator;
033import org.junit.Ignore;
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@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
042@GwtCompatible
043@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
044public class CollectionRemoveTester<E> extends AbstractCollectionTester<E> {
045  @CollectionFeature.Require(SUPPORTS_REMOVE)
046  @CollectionSize.Require(absent = ZERO)
047  public void testRemove_present() {
048    int initialSize = collection.size();
049    assertTrue("remove(present) should return true", collection.remove(e0()));
050    assertEquals(
051        "remove(present) should decrease a collection's size by one.",
052        initialSize - 1,
053        collection.size());
054    expectMissing(e0());
055  }
056
057  @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
058  @CollectionSize.Require(SEVERAL)
059  public void testRemovePresentConcurrentWithIteration() {
060    try {
061      Iterator<E> iterator = collection.iterator();
062      assertTrue(collection.remove(e0()));
063      iterator.next();
064      fail("Expected ConcurrentModificationException");
065    } catch (ConcurrentModificationException expected) {
066      // success
067    }
068  }
069
070  @CollectionFeature.Require(SUPPORTS_REMOVE)
071  public void testRemove_notPresent() {
072    assertFalse("remove(notPresent) should return false", collection.remove(e3()));
073    expectUnchanged();
074  }
075
076  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
077  @CollectionSize.Require(absent = ZERO)
078  public void testRemove_nullPresent() {
079    collection = getSubjectGenerator().create(createArrayWithNullElement());
080
081    int initialSize = collection.size();
082    assertTrue("remove(null) should return true", collection.remove(null));
083    assertEquals(
084        "remove(present) should decrease a collection's size by one.",
085        initialSize - 1,
086        collection.size());
087    expectMissing((E) null);
088  }
089
090  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
091  @CollectionSize.Require(absent = ZERO)
092  public void testRemove_unsupported() {
093    try {
094      collection.remove(e0());
095      fail("remove(present) should throw UnsupportedOperationException");
096    } catch (UnsupportedOperationException expected) {
097    }
098    expectUnchanged();
099    assertTrue("remove(present) should not remove the element", collection.contains(e0()));
100  }
101
102  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
103  public void testRemove_unsupportedNotPresent() {
104    try {
105      assertFalse(
106          "remove(notPresent) should return false or throw UnsupportedOperationException",
107          collection.remove(e3()));
108    } catch (UnsupportedOperationException tolerated) {
109    }
110    expectUnchanged();
111    expectMissing(e3());
112  }
113
114  @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
115  public void testRemove_nullNotSupported() {
116    try {
117      assertFalse(
118          "remove(null) should return false or throw NullPointerException",
119          collection.remove(null));
120    } catch (NullPointerException tolerated) {
121    }
122    expectUnchanged();
123  }
124
125  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
126  public void testRemove_nullAllowed() {
127    assertFalse("remove(null) should return false", collection.remove(null));
128    expectUnchanged();
129  }
130
131  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
132  @CollectionSize.Require(absent = ZERO)
133  public void testIteratorRemove_unsupported() {
134    Iterator<E> iterator = collection.iterator();
135    iterator.next();
136    try {
137      iterator.remove();
138      fail("iterator.remove() should throw UnsupportedOperationException");
139    } catch (UnsupportedOperationException expected) {
140    }
141    expectUnchanged();
142    assertTrue(collection.contains(e0()));
143  }
144
145  @CollectionFeature.Require(SUPPORTS_REMOVE)
146  public void testRemove_wrongType() {
147    try {
148      assertFalse(collection.remove(WrongType.VALUE));
149    } catch (ClassCastException tolerated) {
150    }
151    expectUnchanged();
152  }
153}