• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
18 import static com.google.common.collect.testing.features.CollectionSize.ONE;
19 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
24 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import com.google.common.annotations.GwtCompatible;
28 import com.google.common.collect.Multimap;
29 import com.google.common.collect.testing.Helpers;
30 import com.google.common.collect.testing.features.CollectionFeature;
31 import com.google.common.collect.testing.features.CollectionSize;
32 import com.google.common.collect.testing.features.MapFeature;
33 
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>> {
testEntries()45   public void testEntries() {
46     assertThat(multimap().entries()).has().exactlyAs(getSampleElements());
47   }
48 
49   @CollectionSize.Require(absent = ZERO)
50   @MapFeature.Require(ALLOWS_NULL_KEYS)
testContainsEntryWithNullKeyPresent()51   public void testContainsEntryWithNullKeyPresent() {
52     initMultimapWithNullKey();
53     assertThat(multimap().entries()).has().allOf(
54         Helpers.mapEntry((K) null, getValueForNullKey()));
55   }
56 
57   @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
testContainsEntryWithNullKeyAbsent()58   public void testContainsEntryWithNullKeyAbsent() {
59     assertFalse(multimap().entries().contains(Helpers.mapEntry(null, sampleValues().e0)));
60   }
61 
62   @CollectionSize.Require(absent = ZERO)
63   @MapFeature.Require(ALLOWS_NULL_VALUES)
testContainsEntryWithNullValuePresent()64   public void testContainsEntryWithNullValuePresent() {
65     initMultimapWithNullValue();
66     assertThat(multimap().entries()).has().allOf(
67         Helpers.mapEntry(getKeyForNullValue(), (V) null));
68   }
69 
70   @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
testContainsEntryWithNullValueAbsent()71   public void testContainsEntryWithNullValueAbsent() {
72     assertFalse(multimap().entries().contains(
73         Helpers.mapEntry(sampleKeys().e0, null)));
74   }
75 
76   @CollectionSize.Require(absent = ZERO)
77   @MapFeature.Require(SUPPORTS_REMOVE)
testRemovePropagatesToMultimap()78   public void testRemovePropagatesToMultimap() {
79     assertTrue(multimap().entries().remove(
80         Helpers.mapEntry(sampleKeys().e0, sampleValues().e0)));
81     expectMissing(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0));
82     assertEquals(getNumElements() - 1, multimap().size());
83     assertFalse(multimap().containsEntry(sampleKeys().e0, sampleValues().e0));
84   }
85 
86   @CollectionSize.Require(absent = ZERO)
87   @MapFeature.Require(SUPPORTS_REMOVE)
testRemoveAllPropagatesToMultimap()88   public void testRemoveAllPropagatesToMultimap() {
89     assertTrue(multimap().entries().removeAll(
90         Collections.singleton(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0))));
91     expectMissing(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0));
92     assertEquals(getNumElements() - 1, multimap().size());
93     assertFalse(multimap().containsEntry(sampleKeys().e0, sampleValues().e0));
94   }
95 
96   @CollectionSize.Require(absent = ZERO)
97   @MapFeature.Require(SUPPORTS_REMOVE)
testRetainAllPropagatesToMultimap()98   public void testRetainAllPropagatesToMultimap() {
99     multimap().entries().retainAll(
100         Collections.singleton(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0)));
101     assertEquals(
102         getSubjectGenerator().create(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0)),
103         multimap());
104     assertEquals(1, multimap().size());
105     assertTrue(multimap().containsEntry(sampleKeys().e0, sampleValues().e0));
106   }
107 
108   @CollectionSize.Require(ONE)
109   @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
testIteratorRemovePropagatesToMultimap()110   public void testIteratorRemovePropagatesToMultimap() {
111     Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
112     assertEquals(
113         Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
114         iterator.next());
115     iterator.remove();
116     assertTrue(multimap().isEmpty());
117   }
118 
119   @CollectionSize.Require(absent = ZERO)
120   @MapFeature.Require(SUPPORTS_REMOVE)
testEntriesRemainValidAfterRemove()121   public void testEntriesRemainValidAfterRemove() {
122     Iterator<Entry<K, V>> iterator = multimap().entries().iterator();
123     Entry<K, V> entry = iterator.next();
124     K key = entry.getKey();
125     V value = entry.getValue();
126     multimap().removeAll(key);
127     assertEquals(key, entry.getKey());
128     assertEquals(value, entry.getValue());
129   }
130 }
131 
132