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