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.
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.<V>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(Helpers.mapEntry((K) null, getValueForNullKey()));
072    assertGet(getKeyForNullValue(), ImmutableList.<V>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(Helpers.mapEntry(getKeyForNullValue(), (V) null));
083    assertGet(getKeyForNullValue(), ImmutableList.<V>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    try {
101      multimap().remove(k0(), null);
102      fail("Expected NullPointerException");
103    } catch (NullPointerException expected) {
104      // success
105    }
106    expectUnchanged();
107  }
108
109  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
110  public void testRemoveNullKeyForbidden() {
111    try {
112      multimap().remove(null, v0());
113      fail("Expected NullPointerException");
114    } catch (NullPointerException expected) {
115      // success
116    }
117    expectUnchanged();
118  }
119
120  @MapFeature.Require(SUPPORTS_REMOVE)
121  @CollectionSize.Require(absent = ZERO)
122  public void testRemovePropagatesToGet() {
123    List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries());
124    for (Entry<K, V> entry : entries) {
125      resetContainer();
126
127      K key = entry.getKey();
128      V value = entry.getValue();
129      Collection<V> collection = multimap().get(key);
130      assertNotNull(collection);
131      Collection<V> expectedCollection = Helpers.copyToList(collection);
132
133      multimap().remove(key, value);
134      expectedCollection.remove(value);
135
136      assertEqualIgnoringOrder(expectedCollection, collection);
137      assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key));
138    }
139  }
140
141  @MapFeature.Require(SUPPORTS_REMOVE)
142  @CollectionSize.Require(absent = ZERO)
143  public void testRemovePropagatesToAsMap() {
144    List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries());
145    for (Entry<K, V> entry : entries) {
146      resetContainer();
147
148      K key = entry.getKey();
149      V value = entry.getValue();
150      Collection<V> collection = multimap().asMap().get(key);
151      assertNotNull(collection);
152      Collection<V> expectedCollection = Helpers.copyToList(collection);
153
154      multimap().remove(key, value);
155      expectedCollection.remove(value);
156
157      assertEqualIgnoringOrder(expectedCollection, collection);
158      assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key));
159    }
160  }
161
162  @MapFeature.Require(SUPPORTS_REMOVE)
163  @CollectionSize.Require(absent = ZERO)
164  public void testRemovePropagatesToAsMapEntrySet() {
165    List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries());
166    for (Entry<K, V> entry : entries) {
167      resetContainer();
168
169      K key = entry.getKey();
170      V value = entry.getValue();
171
172      Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator();
173      Collection<V> collection = null;
174      while (asMapItr.hasNext()) {
175        Entry<K, Collection<V>> asMapEntry = asMapItr.next();
176        if (key.equals(asMapEntry.getKey())) {
177          collection = asMapEntry.getValue();
178          break;
179        }
180      }
181      assertNotNull(collection);
182      Collection<V> expectedCollection = Helpers.copyToList(collection);
183
184      multimap().remove(key, value);
185      expectedCollection.remove(value);
186
187      assertEqualIgnoringOrder(expectedCollection, collection);
188      assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key));
189    }
190  }
191}