CollectionRemoveTester.java revision 3c77433663281544363151bf284b0240dfd22a42
1/*
2 * Copyright (C) 2008 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
20import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
21import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
22import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
23import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
24import static com.google.common.collect.testing.features.CollectionSize.ZERO;
25
26import com.google.common.annotations.GwtCompatible;
27import com.google.common.collect.testing.AbstractCollectionTester;
28import com.google.common.collect.testing.WrongType;
29import com.google.common.collect.testing.features.CollectionFeature;
30import com.google.common.collect.testing.features.CollectionSize;
31
32import java.util.ConcurrentModificationException;
33import java.util.Iterator;
34
35/**
36 * A generic JUnit test which tests {@code remove} operations on a collection.
37 * Can't be invoked directly; please see
38 * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
39 *
40 * <p>This class is GWT compatible.
41 *
42 * @author George van den Driessche
43 */
44@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
45@GwtCompatible
46public class CollectionRemoveTester<E> extends AbstractCollectionTester<E> {
47  @CollectionFeature.Require(SUPPORTS_REMOVE)
48  @CollectionSize.Require(absent = ZERO)
49  public void testRemove_present() {
50    int initialSize = collection.size();
51    assertTrue("remove(present) should return true",
52        collection.remove(samples.e0));
53    assertEquals("remove(present) should decrease a collection's size by one.",
54        initialSize - 1, collection.size());
55    expectMissing(samples.e0);
56  }
57
58  @CollectionFeature.Require({SUPPORTS_REMOVE,
59      FAILS_FAST_ON_CONCURRENT_MODIFICATION})
60  @CollectionSize.Require(SEVERAL)
61  public void testRemovePresentConcurrentWithIteration() {
62    try {
63      Iterator<E> iterator = collection.iterator();
64      assertTrue(collection.remove(samples.e0));
65      iterator.next();
66      fail("Expected ConcurrentModificationException");
67    } catch (ConcurrentModificationException expected) {
68      // success
69    }
70  }
71
72  @CollectionFeature.Require(SUPPORTS_REMOVE)
73  public void testRemove_notPresent() {
74    assertFalse("remove(notPresent) should return false",
75        collection.remove(samples.e3));
76    expectUnchanged();
77  }
78
79  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
80  @CollectionSize.Require(absent = ZERO)
81  public void testRemove_nullPresent() {
82    collection = getSubjectGenerator().create(createArrayWithNullElement());
83
84    int initialSize = collection.size();
85    assertTrue("remove(null) should return true", collection.remove(null));
86    assertEquals("remove(present) should decrease a collection's size by one.",
87        initialSize - 1, collection.size());
88    expectMissing((E) null);
89  }
90
91  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
92  @CollectionSize.Require(absent = ZERO)
93  public void testRemove_unsupported() {
94    try {
95      collection.remove(samples.e0);
96      fail("remove(present) should throw UnsupportedOperationException");
97    } catch (UnsupportedOperationException expected) {
98    }
99    expectUnchanged();
100    assertTrue("remove(present) should not remove the element",
101        collection.contains(samples.e0));
102  }
103
104  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
105  public void testRemove_unsupportedNotPresent() {
106    try {
107      assertFalse("remove(notPresent) should return false or throw "
108          + "UnsupportedOperationException",
109          collection.remove(samples.e3));
110    } catch (UnsupportedOperationException tolerated) {
111    }
112    expectUnchanged();
113    expectMissing(samples.e3);
114  }
115
116  @CollectionFeature.Require(
117      value = SUPPORTS_REMOVE,
118      absent = ALLOWS_NULL_QUERIES)
119  public void testRemove_nullNotSupported() {
120    try {
121      assertFalse("remove(null) should return false or throw "
122          + "NullPointerException",
123          collection.remove(null));
124    } catch (NullPointerException tolerated) {
125    }
126    expectUnchanged();
127  }
128
129  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
130  public void testRemove_nullAllowed() {
131    assertFalse("remove(null) should return false", collection.remove(null));
132    expectUnchanged();
133  }
134
135  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
136  @CollectionSize.Require(absent = ZERO)
137  public void testIteratorRemove_unsupported() {
138    Iterator<E> iterator = collection.iterator();
139    iterator.next();
140    try {
141      iterator.remove();
142      fail("iterator.remove() should throw UnsupportedOperationException");
143    } catch (UnsupportedOperationException expected) {
144    }
145    expectUnchanged();
146    assertTrue(collection.contains(samples.e0));
147  }
148
149  @CollectionFeature.Require(SUPPORTS_REMOVE)
150  public void testRemove_wrongType() {
151    try {
152      assertFalse(collection.remove(WrongType.VALUE));
153    } catch (ClassCastException tolerated) {
154    }
155    expectUnchanged();
156  }
157}
158