001/* 002 * Copyright (C) 2012 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.google; 018 019import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 020import static com.google.common.collect.testing.Helpers.copyToList; 021import static com.google.common.collect.testing.Helpers.mapEntry; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 026import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 027import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 028import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows; 029 030import com.google.common.annotations.GwtCompatible; 031import com.google.common.collect.ImmutableList; 032import com.google.common.collect.Multimap; 033import com.google.common.collect.testing.features.CollectionSize; 034import com.google.common.collect.testing.features.MapFeature; 035import java.util.Collection; 036import java.util.Iterator; 037import java.util.List; 038import java.util.Map.Entry; 039import org.junit.Ignore; 040 041/** 042 * Tests for {@link Multimap#remove(Object, Object)}. 043 * 044 * @author Louis Wasserman 045 */ 046@GwtCompatible 047@Ignore("test runners must not instantiate and run this directly, only via suites we build") 048// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 049@SuppressWarnings("JUnit4ClassUsedInJUnit3") 050public class MultimapRemoveEntryTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 051 @MapFeature.Require(SUPPORTS_REMOVE) 052 public void testRemoveAbsent() { 053 assertFalse(multimap().remove(k0(), v1())); 054 expectUnchanged(); 055 } 056 057 @CollectionSize.Require(absent = ZERO) 058 @MapFeature.Require(SUPPORTS_REMOVE) 059 public void testRemovePresent() { 060 assertTrue(multimap().remove(k0(), v0())); 061 062 assertFalse(multimap().containsEntry(k0(), v0())); 063 expectMissing(e0()); 064 assertEquals(getNumElements() - 1, multimap().size()); 065 assertGet(k0(), ImmutableList.<V>of()); 066 } 067 068 @CollectionSize.Require(absent = ZERO) 069 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS}) 070 public void testRemoveNullKeyPresent() { 071 initMultimapWithNullKey(); 072 073 assertTrue(multimap().remove(null, getValueForNullKey())); 074 075 expectMissing(mapEntry((K) null, getValueForNullKey())); 076 assertGet(getKeyForNullValue(), ImmutableList.<V>of()); 077 } 078 079 @CollectionSize.Require(absent = ZERO) 080 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) 081 public void testRemoveNullValuePresent() { 082 initMultimapWithNullValue(); 083 084 assertTrue(multimap().remove(getKeyForNullValue(), null)); 085 086 expectMissing(mapEntry(getKeyForNullValue(), (V) null)); 087 assertGet(getKeyForNullValue(), ImmutableList.<V>of()); 088 } 089 090 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES}) 091 public void testRemoveNullKeyAbsent() { 092 assertFalse(multimap().remove(null, v0())); 093 expectUnchanged(); 094 } 095 096 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES}) 097 public void testRemoveNullValueAbsent() { 098 assertFalse(multimap().remove(k0(), null)); 099 expectUnchanged(); 100 } 101 102 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES) 103 public void testRemoveNullValueForbidden() { 104 assertThrows(NullPointerException.class, () -> multimap().remove(k0(), null)); 105 expectUnchanged(); 106 } 107 108 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES) 109 public void testRemoveNullKeyForbidden() { 110 assertThrows(NullPointerException.class, () -> multimap().remove(null, v0())); 111 expectUnchanged(); 112 } 113 114 @MapFeature.Require(SUPPORTS_REMOVE) 115 @CollectionSize.Require(absent = ZERO) 116 public void testRemovePropagatesToGet() { 117 List<Entry<K, V>> entries = copyToList(multimap().entries()); 118 for (Entry<K, V> entry : entries) { 119 resetContainer(); 120 121 K key = entry.getKey(); 122 V value = entry.getValue(); 123 Collection<V> collection = multimap().get(key); 124 assertNotNull(collection); 125 Collection<V> expectedCollection = copyToList(collection); 126 127 multimap().remove(key, value); 128 expectedCollection.remove(value); 129 130 assertEqualIgnoringOrder(expectedCollection, collection); 131 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 132 } 133 } 134 135 @MapFeature.Require(SUPPORTS_REMOVE) 136 @CollectionSize.Require(absent = ZERO) 137 public void testRemovePropagatesToAsMap() { 138 List<Entry<K, V>> entries = copyToList(multimap().entries()); 139 for (Entry<K, V> entry : entries) { 140 resetContainer(); 141 142 K key = entry.getKey(); 143 V value = entry.getValue(); 144 Collection<V> collection = multimap().asMap().get(key); 145 assertNotNull(collection); 146 Collection<V> expectedCollection = copyToList(collection); 147 148 multimap().remove(key, value); 149 expectedCollection.remove(value); 150 151 assertEqualIgnoringOrder(expectedCollection, collection); 152 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 153 } 154 } 155 156 @MapFeature.Require(SUPPORTS_REMOVE) 157 @CollectionSize.Require(absent = ZERO) 158 public void testRemovePropagatesToAsMapEntrySet() { 159 List<Entry<K, V>> entries = copyToList(multimap().entries()); 160 for (Entry<K, V> entry : entries) { 161 resetContainer(); 162 163 K key = entry.getKey(); 164 V value = entry.getValue(); 165 166 Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator(); 167 Collection<V> collection = null; 168 while (asMapItr.hasNext()) { 169 Entry<K, Collection<V>> asMapEntry = asMapItr.next(); 170 if (key.equals(asMapEntry.getKey())) { 171 collection = asMapEntry.getValue(); 172 break; 173 } 174 } 175 assertNotNull(collection); 176 Collection<V> expectedCollection = copyToList(collection); 177 178 multimap().remove(key, value); 179 expectedCollection.remove(value); 180 181 assertEqualIgnoringOrder(expectedCollection, collection); 182 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 183 } 184 } 185}