guava
152 строки · 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.43public class CollectionRemoveTester<E> extends AbstractCollectionTester<E> {44@CollectionFeature.Require(SUPPORTS_REMOVE)45@CollectionSize.Require(absent = ZERO)46public void testRemove_present() {47int initialSize = collection.size();48assertTrue("remove(present) should return true", collection.remove(e0()));49assertEquals(50"remove(present) should decrease a collection's size by one.",51initialSize - 1,52collection.size());53expectMissing(e0());54}55
56@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})57@CollectionSize.Require(SEVERAL)58public void testRemovePresentConcurrentWithIteration() {59try {60Iterator<E> iterator = collection.iterator();61assertTrue(collection.remove(e0()));62iterator.next();63fail("Expected ConcurrentModificationException");64} catch (ConcurrentModificationException expected) {65// success66}67}68
69@CollectionFeature.Require(SUPPORTS_REMOVE)70public void testRemove_notPresent() {71assertFalse("remove(notPresent) should return false", collection.remove(e3()));72expectUnchanged();73}74
75@CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})76@CollectionSize.Require(absent = ZERO)77public void testRemove_nullPresent() {78collection = getSubjectGenerator().create(createArrayWithNullElement());79
80int initialSize = collection.size();81assertTrue("remove(null) should return true", collection.remove(null));82assertEquals(83"remove(present) should decrease a collection's size by one.",84initialSize - 1,85collection.size());86expectMissing((E) null);87}88
89@CollectionFeature.Require(absent = SUPPORTS_REMOVE)90@CollectionSize.Require(absent = ZERO)91public void testRemove_unsupported() {92try {93collection.remove(e0());94fail("remove(present) should throw UnsupportedOperationException");95} catch (UnsupportedOperationException expected) {96}97expectUnchanged();98assertTrue("remove(present) should not remove the element", collection.contains(e0()));99}100
101@CollectionFeature.Require(absent = SUPPORTS_REMOVE)102public void testRemove_unsupportedNotPresent() {103try {104assertFalse(105"remove(notPresent) should return false or throw UnsupportedOperationException",106collection.remove(e3()));107} catch (UnsupportedOperationException tolerated) {108}109expectUnchanged();110expectMissing(e3());111}112
113@CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)114public void testRemove_nullNotSupported() {115try {116assertFalse(117"remove(null) should return false or throw NullPointerException",118collection.remove(null));119} catch (NullPointerException tolerated) {120}121expectUnchanged();122}123
124@CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})125public void testRemove_nullAllowed() {126assertFalse("remove(null) should return false", collection.remove(null));127expectUnchanged();128}129
130@CollectionFeature.Require(absent = SUPPORTS_REMOVE)131@CollectionSize.Require(absent = ZERO)132public void testIteratorRemove_unsupported() {133Iterator<E> iterator = collection.iterator();134iterator.next();135try {136iterator.remove();137fail("iterator.remove() should throw UnsupportedOperationException");138} catch (UnsupportedOperationException expected) {139}140expectUnchanged();141assertTrue(collection.contains(e0()));142}143
144@CollectionFeature.Require(SUPPORTS_REMOVE)145public void testRemove_wrongType() {146try {147assertFalse(collection.remove(WrongType.VALUE));148} catch (ClassCastException tolerated) {149}150expectUnchanged();151}152}
153