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