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