MultimapRemoveEntryTester.java revision 7dd252788645e940eada959bdde927426e2531c9
1/* 2 * Copyright (C) 2012 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 17package com.google.common.collect.testing.google; 18 19import static com.google.common.collect.testing.features.CollectionSize.ZERO; 20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_QUERIES; 22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 24import static org.truth0.Truth.ASSERT; 25 26import com.google.common.annotations.GwtCompatible; 27import com.google.common.collect.ImmutableList; 28import com.google.common.collect.Multimap; 29import com.google.common.collect.testing.Helpers; 30import com.google.common.collect.testing.features.CollectionSize; 31import com.google.common.collect.testing.features.MapFeature; 32 33import java.util.Collection; 34import java.util.Iterator; 35import java.util.List; 36import java.util.Map.Entry; 37 38/** 39 * Tests for {@link Multimap#remove(Object, Object)}. 40 * 41 * @author Louis Wasserman 42 */ 43@GwtCompatible 44public class MultimapRemoveEntryTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 45 @MapFeature.Require(SUPPORTS_REMOVE) 46 public void testRemoveAbsent() { 47 assertFalse(multimap().remove(sampleKeys().e0, sampleValues().e1)); 48 expectUnchanged(); 49 } 50 51 @CollectionSize.Require(absent = ZERO) 52 @MapFeature.Require(SUPPORTS_REMOVE) 53 public void testRemovePresent() { 54 assertTrue(multimap().remove(sampleKeys().e0, sampleValues().e0)); 55 56 expectMissing(samples.e0); 57 assertGet(sampleKeys().e0, ImmutableList.<V>of()); 58 } 59 60 @CollectionSize.Require(absent = ZERO) 61 @MapFeature.Require({ SUPPORTS_REMOVE, ALLOWS_NULL_KEYS }) 62 public void testRemoveNullKeyPresent() { 63 initMultimapWithNullKey(); 64 65 assertTrue(multimap().remove(null, getValueForNullKey())); 66 67 expectMissing(Helpers.mapEntry((K) null, getValueForNullKey())); 68 assertGet(getKeyForNullValue(), ImmutableList.<V>of()); 69 } 70 71 @CollectionSize.Require(absent = ZERO) 72 @MapFeature.Require({ SUPPORTS_REMOVE, ALLOWS_NULL_VALUES }) 73 public void testRemoveNullValuePresent() { 74 initMultimapWithNullValue(); 75 76 assertTrue(multimap().remove(getKeyForNullValue(), null)); 77 78 expectMissing(Helpers.mapEntry(getKeyForNullValue(), (V) null)); 79 assertGet(getKeyForNullValue(), ImmutableList.<V>of()); 80 } 81 82 @MapFeature.Require({ SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES}) 83 public void testRemoveNullKeyAbsent() { 84 assertFalse(multimap().remove(null, sampleValues().e0)); 85 expectUnchanged(); 86 } 87 88 @MapFeature.Require({ SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES}) 89 public void testRemoveNullValueAbsent() { 90 assertFalse(multimap().remove(sampleKeys().e0, null)); 91 expectUnchanged(); 92 } 93 94 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES) 95 public void testRemoveNullValueForbidden() { 96 try { 97 multimap().remove(sampleKeys().e0, null); 98 fail("Expected NullPointerException"); 99 } catch (NullPointerException expected) { 100 // success 101 } 102 expectUnchanged(); 103 } 104 105 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES) 106 public void testRemoveNullKeyForbidden() { 107 try { 108 multimap().remove(null, sampleValues().e0); 109 fail("Expected NullPointerException"); 110 } catch (NullPointerException expected) { 111 // success 112 } 113 expectUnchanged(); 114 } 115 116 @MapFeature.Require(SUPPORTS_REMOVE) 117 @CollectionSize.Require(absent = ZERO) 118 public void testRemovePropagatesToGet() { 119 List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries()); 120 for (Entry<K, V> entry : entries) { 121 resetContainer(); 122 123 K key = entry.getKey(); 124 V value = entry.getValue(); 125 Collection<V> collection = multimap().get(key); 126 assertNotNull(collection); 127 Collection<V> expectedCollection = Helpers.copyToList(collection); 128 129 multimap().remove(key, value); 130 expectedCollection.remove(value); 131 132 ASSERT.<V, Collection<V>>that(collection).has().allFrom(expectedCollection); 133 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 134 } 135 } 136 137 @MapFeature.Require(SUPPORTS_REMOVE) 138 @CollectionSize.Require(absent = ZERO) 139 public void testRemovePropagatesToAsMap() { 140 List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries()); 141 for (Entry<K, V> entry : entries) { 142 resetContainer(); 143 144 K key = entry.getKey(); 145 V value = entry.getValue(); 146 Collection<V> collection = multimap().asMap().get(key); 147 assertNotNull(collection); 148 Collection<V> expectedCollection = Helpers.copyToList(collection); 149 150 multimap().remove(key, value); 151 expectedCollection.remove(value); 152 153 ASSERT.<V, Collection<V>>that(collection).has().allFrom(expectedCollection); 154 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 155 } 156 } 157 158 @MapFeature.Require(SUPPORTS_REMOVE) 159 @CollectionSize.Require(absent = ZERO) 160 public void testRemovePropagatesToAsMapEntrySet() { 161 List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries()); 162 for (Entry<K, V> entry : entries) { 163 resetContainer(); 164 165 K key = entry.getKey(); 166 V value = entry.getValue(); 167 168 Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator(); 169 Collection<V> collection = null; 170 while (asMapItr.hasNext()) { 171 Entry<K, Collection<V>> asMapEntry = asMapItr.next(); 172 if (key.equals(asMapEntry.getKey())) { 173 collection = asMapEntry.getValue(); 174 break; 175 } 176 } 177 assertNotNull(collection); 178 Collection<V> expectedCollection = Helpers.copyToList(collection); 179 180 multimap().remove(key, value); 181 expectedCollection.remove(value); 182 183 ASSERT.<V, Collection<V>>that(collection).has().allFrom(expectedCollection); 184 assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); 185 } 186 } 187} 188