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.CollectionSize.SEVERAL; 20 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 22 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 23 import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 24 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 25 26 import com.google.common.annotations.GwtCompatible; 27 import com.google.common.collect.testing.AbstractMapTester; 28 import com.google.common.collect.testing.WrongType; 29 import com.google.common.collect.testing.features.CollectionSize; 30 import com.google.common.collect.testing.features.MapFeature; 31 import java.util.ConcurrentModificationException; 32 import java.util.Iterator; 33 import java.util.Map.Entry; 34 import org.junit.Ignore; 35 36 /** 37 * A generic JUnit test which tests {@code remove} operations on a map. Can't be invoked directly; 38 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 39 * 40 * @author George van den Driessche 41 * @author Chris Povirk 42 */ 43 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 44 @GwtCompatible 45 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 46 public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> { 47 @MapFeature.Require(SUPPORTS_REMOVE) 48 @CollectionSize.Require(absent = ZERO) testRemove_present()49 public void testRemove_present() { 50 int initialSize = getMap().size(); 51 assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0())); 52 assertEquals( 53 "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size()); 54 expectMissing(e0()); 55 } 56 57 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 58 @CollectionSize.Require(SEVERAL) testRemovePresentConcurrentWithEntrySetIteration()59 public void testRemovePresentConcurrentWithEntrySetIteration() { 60 try { 61 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); 62 getMap().remove(k0()); 63 iterator.next(); 64 fail("Expected ConcurrentModificationException"); 65 } catch (ConcurrentModificationException expected) { 66 // success 67 } 68 } 69 70 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 71 @CollectionSize.Require(SEVERAL) testRemovePresentConcurrentWithKeySetIteration()72 public void testRemovePresentConcurrentWithKeySetIteration() { 73 try { 74 Iterator<K> iterator = getMap().keySet().iterator(); 75 getMap().remove(k0()); 76 iterator.next(); 77 fail("Expected ConcurrentModificationException"); 78 } catch (ConcurrentModificationException expected) { 79 // success 80 } 81 } 82 83 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 84 @CollectionSize.Require(SEVERAL) testRemovePresentConcurrentWithValuesIteration()85 public void testRemovePresentConcurrentWithValuesIteration() { 86 try { 87 Iterator<V> iterator = getMap().values().iterator(); 88 getMap().remove(k0()); 89 iterator.next(); 90 fail("Expected ConcurrentModificationException"); 91 } catch (ConcurrentModificationException expected) { 92 // success 93 } 94 } 95 96 @MapFeature.Require(SUPPORTS_REMOVE) testRemove_notPresent()97 public void testRemove_notPresent() { 98 assertNull("remove(notPresent) should return null", getMap().remove(k3())); 99 expectUnchanged(); 100 } 101 102 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS}) 103 @CollectionSize.Require(absent = ZERO) testRemove_nullPresent()104 public void testRemove_nullPresent() { 105 initMapWithNullKey(); 106 107 int initialSize = getMap().size(); 108 assertEquals( 109 "remove(null) should return the associated value", 110 getValueForNullKey(), 111 getMap().remove(null)); 112 assertEquals( 113 "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size()); 114 expectMissing(entry(null, getValueForNullKey())); 115 } 116 117 @MapFeature.Require(absent = SUPPORTS_REMOVE) 118 @CollectionSize.Require(absent = ZERO) testRemove_unsupported()119 public void testRemove_unsupported() { 120 try { 121 getMap().remove(k0()); 122 fail("remove(present) should throw UnsupportedOperationException"); 123 } catch (UnsupportedOperationException expected) { 124 } 125 expectUnchanged(); 126 assertEquals("remove(present) should not remove the element", v0(), get(k0())); 127 } 128 129 @MapFeature.Require(absent = SUPPORTS_REMOVE) testRemove_unsupportedNotPresent()130 public void testRemove_unsupportedNotPresent() { 131 try { 132 assertNull( 133 "remove(notPresent) should return null or throw UnsupportedOperationException", 134 getMap().remove(k3())); 135 } catch (UnsupportedOperationException tolerated) { 136 } 137 expectUnchanged(); 138 expectMissing(e3()); 139 } 140 141 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES) testRemove_nullQueriesNotSupported()142 public void testRemove_nullQueriesNotSupported() { 143 try { 144 assertNull( 145 "remove(null) should return null or throw NullPointerException", getMap().remove(null)); 146 } catch (NullPointerException tolerated) { 147 } 148 expectUnchanged(); 149 } 150 151 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES}) testRemove_nullSupportedMissing()152 public void testRemove_nullSupportedMissing() { 153 assertNull("remove(null) should return null", getMap().remove(null)); 154 expectUnchanged(); 155 } 156 157 @MapFeature.Require(SUPPORTS_REMOVE) testRemove_wrongType()158 public void testRemove_wrongType() { 159 try { 160 assertNull(getMap().remove(WrongType.VALUE)); 161 } catch (ClassCastException tolerated) { 162 } 163 expectUnchanged(); 164 } 165 } 166