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 17 package com.google.common.collect.testing.testers; 18 19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES; 20 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 21 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 22 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 23 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 24 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 25 26 import com.google.common.annotations.GwtCompatible; 27 import com.google.common.collect.testing.AbstractCollectionTester; 28 import com.google.common.collect.testing.WrongType; 29 import com.google.common.collect.testing.features.CollectionFeature; 30 import com.google.common.collect.testing.features.CollectionSize; 31 import java.util.ConcurrentModificationException; 32 import java.util.Iterator; 33 import 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 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 42 @GwtCompatible 43 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 44 public class CollectionRemoveTester<E> extends AbstractCollectionTester<E> { 45 @CollectionFeature.Require(SUPPORTS_REMOVE) 46 @CollectionSize.Require(absent = ZERO) testRemove_present()47 public void testRemove_present() { 48 int initialSize = collection.size(); 49 assertTrue("remove(present) should return true", collection.remove(e0())); 50 assertEquals( 51 "remove(present) should decrease a collection's size by one.", 52 initialSize - 1, 53 collection.size()); 54 expectMissing(e0()); 55 } 56 57 @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 58 @CollectionSize.Require(SEVERAL) testRemovePresentConcurrentWithIteration()59 public void testRemovePresentConcurrentWithIteration() { 60 try { 61 Iterator<E> iterator = collection.iterator(); 62 assertTrue(collection.remove(e0())); 63 iterator.next(); 64 fail("Expected ConcurrentModificationException"); 65 } catch (ConcurrentModificationException expected) { 66 // success 67 } 68 } 69 70 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemove_notPresent()71 public void testRemove_notPresent() { 72 assertFalse("remove(notPresent) should return false", collection.remove(e3())); 73 expectUnchanged(); 74 } 75 76 @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) 77 @CollectionSize.Require(absent = ZERO) testRemove_nullPresent()78 public void testRemove_nullPresent() { 79 collection = getSubjectGenerator().create(createArrayWithNullElement()); 80 81 int initialSize = collection.size(); 82 assertTrue("remove(null) should return true", collection.remove(null)); 83 assertEquals( 84 "remove(present) should decrease a collection's size by one.", 85 initialSize - 1, 86 collection.size()); 87 expectMissing((E) null); 88 } 89 90 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 91 @CollectionSize.Require(absent = ZERO) testRemove_unsupported()92 public void testRemove_unsupported() { 93 try { 94 collection.remove(e0()); 95 fail("remove(present) should throw UnsupportedOperationException"); 96 } catch (UnsupportedOperationException expected) { 97 } 98 expectUnchanged(); 99 assertTrue("remove(present) should not remove the element", collection.contains(e0())); 100 } 101 102 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) testRemove_unsupportedNotPresent()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) testRemove_nullNotSupported()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}) testRemove_nullAllowed()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) testIteratorRemove_unsupported()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) testRemove_wrongType()146 public void testRemove_wrongType() { 147 try { 148 assertFalse(collection.remove(WrongType.VALUE)); 149 } catch (ClassCastException tolerated) { 150 } 151 expectUnchanged(); 152 } 153 } 154