View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.Helpers.assertContains;
18  import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
19  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
20  import static com.google.common.collect.testing.features.CollectionSize.ONE;
21  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
23  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
24  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
25  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
26  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
27  
28  import com.google.common.annotations.GwtCompatible;
29  import com.google.common.collect.Multimap;
30  import com.google.common.collect.testing.Helpers;
31  import com.google.common.collect.testing.features.CollectionFeature;
32  import com.google.common.collect.testing.features.CollectionSize;
33  import com.google.common.collect.testing.features.MapFeature;
34  import java.util.Collections;
35  import java.util.Iterator;
36  import java.util.Map.Entry;
37  
38  /**
39   * Tester for {@code Multimap.entries}.
40   *
41   * @author Louis Wasserman
42   */
43  @GwtCompatible
44  public class MultimapEntriesTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
45    public void testEntries() {
46      assertEqualIgnoringOrder(getSampleElements(), multimap().entries());
47    }
48  
49    @CollectionSize.Require(absent = ZERO)
50    @MapFeature.Require(ALLOWS_NULL_KEYS)
51    public void testContainsEntryWithNullKeyPresent() {
52      initMultimapWithNullKey();
53      assertContains(multimap().entries(), Helpers.mapEntry((K) null, getValueForNullKey()));
54    }
55  
56    @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
57    public void testContainsEntryWithNullKeyAbsent() {
58      assertFalse(multimap().entries().contains(Helpers.mapEntry(null, v0())));
59    }
60  
61    @CollectionSize.Require(absent = ZERO)
62    @MapFeature.Require(ALLOWS_NULL_VALUES)
63    public void testContainsEntryWithNullValuePresent() {
64      initMultimapWithNullValue();
65      assertContains(multimap().entries(), Helpers.mapEntry(getKeyForNullValue(), (V) null));
66    }
67  
68    @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
69    public void testContainsEntryWithNullValueAbsent() {
70      assertFalse(multimap().entries().contains(Helpers.mapEntry(k0(), null)));
71    }
72  
73    @CollectionSize.Require(absent = ZERO)
74    @MapFeature.Require(SUPPORTS_REMOVE)
75    public void testRemovePropagatesToMultimap() {
76      assertTrue(multimap().entries().remove(Helpers.mapEntry(k0(), v0())));
77      expectMissing(Helpers.mapEntry(k0(), v0()));
78      assertEquals(getNumElements() - 1, multimap().size());
79      assertFalse(multimap().containsEntry(k0(), v0()));
80    }
81  
82    @CollectionSize.Require(absent = ZERO)
83    @MapFeature.Require(SUPPORTS_REMOVE)
84    public void testRemoveAllPropagatesToMultimap() {
85      assertTrue(multimap().entries().removeAll(Collections.singleton(Helpers.mapEntry(k0(), v0()))));
86      expectMissing(Helpers.mapEntry(k0(), v0()));
87      assertEquals(getNumElements() - 1, multimap().size());
88      assertFalse(multimap().containsEntry(k0(), v0()));
89    }
90  
91    @CollectionSize.Require(absent = ZERO)
92    @MapFeature.Require(SUPPORTS_REMOVE)
93    public void testRetainAllPropagatesToMultimap() {
94      multimap().entries().retainAll(Collections.singleton(Helpers.mapEntry(k0(), v0())));
95      assertEquals(getSubjectGenerator().create(Helpers.mapEntry(k0(), v0())), multimap());
96      assertEquals(1, multimap().size());
97      assertTrue(multimap().containsEntry(k0(), v0()));
98    }
99  
100   @CollectionSize.Require(ONE)
101   @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
102   public void testIteratorRemovePropagatesToMultimap() {
103     Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
104     assertEquals(Helpers.mapEntry(k0(), v0()), iterator.next());
105     iterator.remove();
106     assertTrue(multimap().isEmpty());
107   }
108 
109   @CollectionSize.Require(absent = ZERO)
110   @MapFeature.Require(SUPPORTS_REMOVE)
111   public void testEntriesRemainValidAfterRemove() {
112     Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
113     Entry<K, V> entry = iterator.next();
114     K key = entry.getKey();
115     V value = entry.getValue();
116     multimap().removeAll(key);
117     assertEquals(key, entry.getKey());
118     assertEquals(value, entry.getValue());
119   }
120 }