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