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