Java
153 строки · 5.6 Кб
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;
31import java.util.ConcurrentModificationException;
32import java.util.Iterator;
33import org.junit.Ignore;
34
35/**
36* A generic JUnit test which tests {@code remove} operations on a collection. Can't be invoked
37* directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
38*
39* @author George van den Driessche
40*/
41@GwtCompatible
42@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
43@SuppressWarnings("JUnit4ClassUsedInJUnit3")
44public class CollectionRemoveTester<E> extends AbstractCollectionTester<E> {
45@CollectionFeature.Require(SUPPORTS_REMOVE)
46@CollectionSize.Require(absent = ZERO)
47public void testRemove_present() {
48int initialSize = collection.size();
49assertTrue("remove(present) should return true", collection.remove(e0()));
50assertEquals(
51"remove(present) should decrease a collection's size by one.",
52initialSize - 1,
53collection.size());
54expectMissing(e0());
55}
56
57@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
58@CollectionSize.Require(SEVERAL)
59public void testRemovePresentConcurrentWithIteration() {
60try {
61Iterator<E> iterator = collection.iterator();
62assertTrue(collection.remove(e0()));
63iterator.next();
64fail("Expected ConcurrentModificationException");
65} catch (ConcurrentModificationException expected) {
66// success
67}
68}
69
70@CollectionFeature.Require(SUPPORTS_REMOVE)
71public void testRemove_notPresent() {
72assertFalse("remove(notPresent) should return false", collection.remove(e3()));
73expectUnchanged();
74}
75
76@CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
77@CollectionSize.Require(absent = ZERO)
78public void testRemove_nullPresent() {
79collection = getSubjectGenerator().create(createArrayWithNullElement());
80
81int initialSize = collection.size();
82assertTrue("remove(null) should return true", collection.remove(null));
83assertEquals(
84"remove(present) should decrease a collection's size by one.",
85initialSize - 1,
86collection.size());
87expectMissing((E) null);
88}
89
90@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
91@CollectionSize.Require(absent = ZERO)
92public void testRemove_unsupported() {
93try {
94collection.remove(e0());
95fail("remove(present) should throw UnsupportedOperationException");
96} catch (UnsupportedOperationException expected) {
97}
98expectUnchanged();
99assertTrue("remove(present) should not remove the element", collection.contains(e0()));
100}
101
102@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
103public void testRemove_unsupportedNotPresent() {
104try {
105assertFalse(
106"remove(notPresent) should return false or throw UnsupportedOperationException",
107collection.remove(e3()));
108} catch (UnsupportedOperationException tolerated) {
109}
110expectUnchanged();
111expectMissing(e3());
112}
113
114@CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
115public void testRemove_nullNotSupported() {
116try {
117assertFalse(
118"remove(null) should return false or throw NullPointerException",
119collection.remove(null));
120} catch (NullPointerException tolerated) {
121}
122expectUnchanged();
123}
124
125@CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
126public void testRemove_nullAllowed() {
127assertFalse("remove(null) should return false", collection.remove(null));
128expectUnchanged();
129}
130
131@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
132@CollectionSize.Require(absent = ZERO)
133public void testIteratorRemove_unsupported() {
134Iterator<E> iterator = collection.iterator();
135iterator.next();
136try {
137iterator.remove();
138fail("iterator.remove() should throw UnsupportedOperationException");
139} catch (UnsupportedOperationException expected) {
140}
141expectUnchanged();
142assertTrue(collection.contains(e0()));
143}
144
145@CollectionFeature.Require(SUPPORTS_REMOVE)
146public void testRemove_wrongType() {
147try {
148assertFalse(collection.remove(WrongType.VALUE));
149} catch (ClassCastException tolerated) {
150}
151expectUnchanged();
152}
153}
154