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.assertContentsAnyOrder;
18  import static com.google.common.collect.testing.Helpers.assertContentsInOrder;
19  import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
20  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
21  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
24  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
25  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
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.Iterables;
30  import com.google.common.collect.Multimap;
31  import com.google.common.collect.testing.Helpers;
32  import com.google.common.collect.testing.features.CollectionFeature;
33  import com.google.common.collect.testing.features.CollectionSize;
34  import com.google.common.collect.testing.features.MapFeature;
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Map.Entry;
40  import java.util.Set;
41  
42  /**
43   * Tests for {@link Multimap#asMap}.
44   *
45   * @author Louis Wasserman
46   */
47  @GwtCompatible
48  public class MultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
49    public void testAsMapGet() {
50      for (K key : sampleKeys()) {
51        List<V> expectedValues = new ArrayList<>();
52        for (Entry<K, V> entry : getSampleElements()) {
53          if (entry.getKey().equals(key)) {
54            expectedValues.add(entry.getValue());
55          }
56        }
57  
58        Collection<V> collection = multimap().asMap().get(key);
59        if (expectedValues.isEmpty()) {
60          assertNull(collection);
61        } else {
62          assertEqualIgnoringOrder(expectedValues, collection);
63        }
64      }
65    }
66  
67    @CollectionSize.Require(absent = ZERO)
68    @MapFeature.Require(ALLOWS_NULL_KEYS)
69    public void testAsMapGetNullKeyPresent() {
70      initMultimapWithNullKey();
71      assertContentsAnyOrder(multimap().asMap().get(null), getValueForNullKey());
72    }
73  
74    @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
75    public void testAsMapGetNullKeyAbsent() {
76      assertNull(multimap().asMap().get(null));
77    }
78  
79    @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
80    public void testAsMapGetNullKeyUnsupported() {
81      try {
82        multimap().asMap().get(null);
83        fail("Expected NullPointerException");
84      } catch (NullPointerException expected) {
85      }
86    }
87  
88    @CollectionSize.Require(absent = ZERO)
89    @MapFeature.Require(SUPPORTS_REMOVE)
90    public void testAsMapRemove() {
91      assertContentsInOrder(multimap().asMap().remove(k0()), v0());
92      assertGet(k0());
93      assertEquals(getNumElements() - 1, multimap().size());
94    }
95  
96    @CollectionSize.Require(SEVERAL)
97    @MapFeature.Require(SUPPORTS_PUT)
98    public void testAsMapEntrySetReflectsPutSameKey() {
99      resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
100 
101     Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
102     Collection<V> valueCollection = Iterables.getOnlyElement(asMapEntrySet).getValue();
103     assertContentsAnyOrder(valueCollection, v0(), v3());
104     assertTrue(multimap().put(k0(), v4()));
105     assertContentsAnyOrder(valueCollection, v0(), v3(), v4());
106   }
107 
108   @CollectionSize.Require(SEVERAL)
109   @MapFeature.Require(SUPPORTS_PUT)
110   public void testAsMapEntrySetReflectsPutDifferentKey() {
111     resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
112 
113     Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
114     assertTrue(multimap().put(k1(), v4()));
115     assertEquals(2, asMapEntrySet.size());
116   }
117 
118   @CollectionSize.Require(SEVERAL)
119   @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
120   public void testAsMapEntrySetRemovePropagatesToMultimap() {
121     resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
122     Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
123     Entry<K, Collection<V>> asMapEntry0 = Iterables.getOnlyElement(asMapEntrySet);
124     assertTrue(multimap().put(k1(), v4()));
125     assertTrue(asMapEntrySet.remove(asMapEntry0));
126     assertEquals(1, multimap().size());
127     assertContentsInOrder(multimap().keySet(), k1());
128   }
129 
130   @CollectionSize.Require(SEVERAL)
131   @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
132   public void testAsMapEntrySetIteratorRemovePropagatesToMultimap() {
133     resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()));
134     Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet();
135     Iterator<Entry<K, Collection<V>>> asMapEntryItr = asMapEntrySet.iterator();
136     asMapEntryItr.next();
137     asMapEntryItr.remove();
138     assertTrue(multimap().isEmpty());
139   }
140 }