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.CollectionSize.SEVERAL; 020import static com.google.common.collect.testing.features.CollectionSize.ZERO; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 023import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 024import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 025import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 026 027import com.google.common.annotations.GwtCompatible; 028import com.google.common.collect.testing.AbstractMapTester; 029import com.google.common.collect.testing.WrongType; 030import com.google.common.collect.testing.features.CollectionSize; 031import com.google.common.collect.testing.features.MapFeature; 032import java.util.ConcurrentModificationException; 033import java.util.Iterator; 034import java.util.Map.Entry; 035import org.junit.Ignore; 036 037/** 038 * A generic JUnit test which tests {@code remove} operations on a map. Can't be invoked directly; 039 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 040 * 041 * @author George van den Driessche 042 * @author Chris Povirk 043 */ 044@GwtCompatible 045@Ignore("test runners must not instantiate and run this directly, only via suites we build") 046// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 047@SuppressWarnings("JUnit4ClassUsedInJUnit3") 048public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> { 049 @MapFeature.Require(SUPPORTS_REMOVE) 050 @CollectionSize.Require(absent = ZERO) 051 public void testRemove_present() { 052 int initialSize = getMap().size(); 053 assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0())); 054 assertEquals( 055 "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size()); 056 expectMissing(e0()); 057 } 058 059 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 060 @CollectionSize.Require(SEVERAL) 061 public void testRemovePresentConcurrentWithEntrySetIteration() { 062 assertThrows( 063 ConcurrentModificationException.class, 064 () -> { 065 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); 066 getMap().remove(k0()); 067 iterator.next(); 068 }); 069 } 070 071 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 072 @CollectionSize.Require(SEVERAL) 073 public void testRemovePresentConcurrentWithKeySetIteration() { 074 assertThrows( 075 ConcurrentModificationException.class, 076 () -> { 077 Iterator<K> iterator = getMap().keySet().iterator(); 078 getMap().remove(k0()); 079 iterator.next(); 080 }); 081 } 082 083 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 084 @CollectionSize.Require(SEVERAL) 085 public void testRemovePresentConcurrentWithValuesIteration() { 086 assertThrows( 087 ConcurrentModificationException.class, 088 () -> { 089 Iterator<V> iterator = getMap().values().iterator(); 090 getMap().remove(k0()); 091 iterator.next(); 092 }); 093 } 094 095 @MapFeature.Require(SUPPORTS_REMOVE) 096 public void testRemove_notPresent() { 097 assertNull("remove(notPresent) should return null", getMap().remove(k3())); 098 expectUnchanged(); 099 } 100 101 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS}) 102 @CollectionSize.Require(absent = ZERO) 103 public void testRemove_nullPresent() { 104 initMapWithNullKey(); 105 106 int initialSize = getMap().size(); 107 assertEquals( 108 "remove(null) should return the associated value", 109 getValueForNullKey(), 110 getMap().remove(null)); 111 assertEquals( 112 "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size()); 113 expectMissing(entry(null, getValueForNullKey())); 114 } 115 116 @MapFeature.Require(absent = SUPPORTS_REMOVE) 117 @CollectionSize.Require(absent = ZERO) 118 public void testRemove_unsupported() { 119 assertThrows(UnsupportedOperationException.class, () -> getMap().remove(k0())); 120 expectUnchanged(); 121 assertEquals("remove(present) should not remove the element", v0(), get(k0())); 122 } 123 124 @MapFeature.Require(absent = SUPPORTS_REMOVE) 125 public void testRemove_unsupportedNotPresent() { 126 try { 127 assertNull( 128 "remove(notPresent) should return null or throw UnsupportedOperationException", 129 getMap().remove(k3())); 130 } catch (UnsupportedOperationException tolerated) { 131 } 132 expectUnchanged(); 133 expectMissing(e3()); 134 } 135 136 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES) 137 public void testRemove_nullQueriesNotSupported() { 138 try { 139 assertNull( 140 "remove(null) should return null or throw NullPointerException", getMap().remove(null)); 141 } catch (NullPointerException tolerated) { 142 } 143 expectUnchanged(); 144 } 145 146 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES}) 147 public void testRemove_nullSupportedMissing() { 148 assertNull("remove(null) should return null", getMap().remove(null)); 149 expectUnchanged(); 150 } 151 152 @MapFeature.Require(SUPPORTS_REMOVE) 153 public void testRemove_wrongType() { 154 try { 155 assertNull(getMap().remove(WrongType.VALUE)); 156 } catch (ClassCastException tolerated) { 157 } 158 expectUnchanged(); 159 } 160}