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