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 org.junit.Assert.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; 039 040/** 041 * Tests for {@link Multimap#remove(Object, Object)}. 042 * 043 * @author Louis Wasserman 044 */ 045@GwtCompatible 046public class MultimapRemoveEntryTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 047 @MapFeature.Require(SUPPORTS_REMOVE) 048 public void testRemoveAbsent() { 049 assertFalse(multimap().remove(k0(), v1())); 050 expectUnchanged(); 051 } 052 053 @CollectionSize.Require(absent = ZERO) 054 @MapFeature.Require(SUPPORTS_REMOVE) 055 public void testRemovePresent() { 056 assertTrue(multimap().remove(k0(), v0())); 057 058 assertFalse(multimap().containsEntry(k0(), v0())); 059 expectMissing(e0()); 060 assertEquals(getNumElements() - 1, multimap().size()); 061 assertGet(k0(), ImmutableList.of()); 062 } 063 064 @CollectionSize.Require(absent = ZERO) 065 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS}) 066 public void testRemoveNullKeyPresent() { 067 initMultimapWithNullKey(); 068 069 assertTrue(multimap().remove(null, getValueForNullKey())); 070 071 expectMissing(mapEntry(null, getValueForNullKey())); 072 assertGet(getKeyForNullValue(), ImmutableList.of()); 073 } 074 075 @CollectionSize.Require(absent = ZERO) 076 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) 077 public void testRemoveNullValuePresent() { 078 initMultimapWithNullValue(); 079 080 assertTrue(multimap().remove(getKeyForNullValue(), null)); 081 082 expectMissing(mapEntry(getKeyForNullValue(), null)); 083 assertGet(getKeyForNullValue(), ImmutableList.of()); 084 } 085 086 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES}) 087 public void testRemoveNullKeyAbsent() { 088 assertFalse(multimap().remove(null, v0())); 089 expectUnchanged(); 090 } 091 092 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES}) 093 public void testRemoveNullValueAbsent() { 094 assertFalse(multimap().remove(k0(), null)); 095 expectUnchanged(); 096 } 097 098 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES) 099 public void testRemoveNullValueForbidden() { 100 assertThrows(NullPointerException.class, () -> multimap().remove(k0(), null)); 101 expectUnchanged(); 102 } 103 104 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES) 105 public void testRemoveNullKeyForbidden() { 106 assertThrows(NullPointerException.class, () -> multimap().remove(null, v0())); 107 expectUnchanged(); 108 } 109 110 @MapFeature.Require(SUPPORTS_REMOVE) 111 @CollectionSize.Require(absent = ZERO) 112 public void testRemovePropagatesToGet() { 113 List<Entry<K, V>> entries = copyToList(multimap().entries()); 114 for (Entry<K, V> entry : entries) { 115 resetContainer(); 116 117 K key = entry.getKey(); 118 V value = entry.getValue(); 119 Collection<V> collection = multimap().get(key); 120 assertNotNull(collection); 121 Collection<V> expectedCollection = copyToList(collection); 122 123 multimap().remove(key, value); 124 expectedCollection.remove(value); 125 126 assertEqualIgnoringOrder(expectedCollection, collection); 127 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 128 } 129 } 130 131 @MapFeature.Require(SUPPORTS_REMOVE) 132 @CollectionSize.Require(absent = ZERO) 133 public void testRemovePropagatesToAsMap() { 134 List<Entry<K, V>> entries = copyToList(multimap().entries()); 135 for (Entry<K, V> entry : entries) { 136 resetContainer(); 137 138 K key = entry.getKey(); 139 V value = entry.getValue(); 140 Collection<V> collection = multimap().asMap().get(key); 141 assertNotNull(collection); 142 Collection<V> expectedCollection = copyToList(collection); 143 144 multimap().remove(key, value); 145 expectedCollection.remove(value); 146 147 assertEqualIgnoringOrder(expectedCollection, collection); 148 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 149 } 150 } 151 152 @MapFeature.Require(SUPPORTS_REMOVE) 153 @CollectionSize.Require(absent = ZERO) 154 public void testRemovePropagatesToAsMapEntrySet() { 155 List<Entry<K, V>> entries = copyToList(multimap().entries()); 156 for (Entry<K, V> entry : entries) { 157 resetContainer(); 158 159 K key = entry.getKey(); 160 V value = entry.getValue(); 161 162 Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator(); 163 Collection<V> collection = null; 164 while (asMapItr.hasNext()) { 165 Entry<K, Collection<V>> asMapEntry = asMapItr.next(); 166 if (key.equals(asMapEntry.getKey())) { 167 collection = asMapEntry.getValue(); 168 break; 169 } 170 } 171 assertNotNull(collection); 172 Collection<V> expectedCollection = copyToList(collection); 173 174 multimap().remove(key, value); 175 expectedCollection.remove(value); 176 177 assertEqualIgnoringOrder(expectedCollection, collection); 178 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 179 } 180 } 181}