• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect.testing.google;
18 
19 import static com.google.common.collect.testing.Helpers.assertContains;
20 import static com.google.common.collect.testing.Helpers.assertEmpty;
21 import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
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_VALUES;
25 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
26 
27 import com.google.common.annotations.GwtCompatible;
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.Lists;
30 import com.google.common.collect.Multimap;
31 import com.google.common.collect.testing.Helpers;
32 import com.google.common.collect.testing.features.CollectionSize;
33 import com.google.common.collect.testing.features.MapFeature;
34 import java.util.Collection;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Map.Entry;
38 import org.checkerframework.checker.nullness.qual.Nullable;
39 import org.junit.Ignore;
40 
41 /**
42  * Tester for {@link Multimap#put}.
43  *
44  * @author Louis Wasserman
45  */
46 @GwtCompatible
47 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
48 @SuppressWarnings("JUnit4ClassUsedInJUnit3")
49 @ElementTypesAreNonnullByDefault
50 public class MultimapPutTester<K extends @Nullable Object, V extends @Nullable Object>
51     extends AbstractMultimapTester<K, V, Multimap<K, V>> {
52   @MapFeature.Require(absent = SUPPORTS_PUT)
testPutUnsupported()53   public void testPutUnsupported() {
54     try {
55       multimap().put(k3(), v3());
56       fail("Expected UnsupportedOperationException");
57     } catch (UnsupportedOperationException expected) {
58     }
59   }
60 
61   @MapFeature.Require(SUPPORTS_PUT)
testPutEmpty()62   public void testPutEmpty() {
63     int size = getNumElements();
64 
65     assertGet(k3(), ImmutableList.<V>of());
66 
67     assertTrue(multimap().put(k3(), v3()));
68 
69     assertGet(k3(), v3());
70     assertEquals(size + 1, multimap().size());
71   }
72 
73   @MapFeature.Require(SUPPORTS_PUT)
74   @CollectionSize.Require(absent = ZERO)
testPutPresent()75   public void testPutPresent() {
76     int size = getNumElements();
77 
78     assertGet(k0(), v0());
79 
80     assertTrue(multimap().put(k0(), v3()));
81 
82     assertGet(k0(), v0(), v3());
83     assertEquals(size + 1, multimap().size());
84   }
85 
86   @MapFeature.Require(SUPPORTS_PUT)
testPutTwoElements()87   public void testPutTwoElements() {
88     int size = getNumElements();
89 
90     List<V> values = Helpers.copyToList(multimap().get(k0()));
91 
92     assertTrue(multimap().put(k0(), v1()));
93     assertTrue(multimap().put(k0(), v2()));
94 
95     values.add(v1());
96     values.add(v2());
97 
98     assertGet(k0(), values);
99     assertEquals(size + 2, multimap().size());
100   }
101 
102   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
testPutNullValue_supported()103   public void testPutNullValue_supported() {
104     int size = getNumElements();
105 
106     multimap().put(k3(), null);
107 
108     assertGet(k3(), Lists.newArrayList((V) null)); // ImmutableList.of can't take null.
109     assertEquals(size + 1, multimap().size());
110   }
111 
112   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
testPutNullValue_unsupported()113   public void testPutNullValue_unsupported() {
114     try {
115       multimap().put(k1(), null);
116       fail();
117     } catch (NullPointerException expected) {
118     }
119 
120     expectUnchanged();
121   }
122 
123   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
testPutNullKey()124   public void testPutNullKey() {
125     int size = getNumElements();
126 
127     multimap().put(null, v3());
128 
129     assertGet(null, v3());
130     assertEquals(size + 1, multimap().size());
131   }
132 
133   @MapFeature.Require(SUPPORTS_PUT)
testPutNotPresentKeyPropagatesToGet()134   public void testPutNotPresentKeyPropagatesToGet() {
135     int size = getNumElements();
136     Collection<V> collection = multimap().get(k3());
137     assertEmpty(collection);
138     multimap().put(k3(), v3());
139     assertContains(collection, v3());
140     assertEquals(size + 1, multimap().size());
141   }
142 
143   @MapFeature.Require(SUPPORTS_PUT)
testPutNotPresentKeyPropagatesToEntries()144   public void testPutNotPresentKeyPropagatesToEntries() {
145     Collection<Entry<K, V>> entries = multimap().entries();
146     assertFalse(entries.contains(Helpers.mapEntry(k3(), v3())));
147     multimap().put(k3(), v3());
148     assertContains(entries, Helpers.mapEntry(k3(), v3()));
149   }
150 
151   @CollectionSize.Require(absent = ZERO)
152   @MapFeature.Require(SUPPORTS_PUT)
testPutPresentKeyPropagatesToEntries()153   public void testPutPresentKeyPropagatesToEntries() {
154     Collection<Entry<K, V>> entries = multimap().entries();
155     assertFalse(entries.contains(Helpers.mapEntry(k0(), v3())));
156     multimap().put(k0(), v3());
157     assertContains(entries, Helpers.mapEntry(k0(), v3()));
158   }
159 
160   @MapFeature.Require(SUPPORTS_PUT)
161   @CollectionSize.Require(absent = ZERO)
testPutPresentKeyPropagatesToGet()162   public void testPutPresentKeyPropagatesToGet() {
163     List<K> keys = Helpers.copyToList(multimap().keySet());
164     for (K key : keys) {
165       resetContainer();
166 
167       int size = getNumElements();
168 
169       Collection<V> collection = multimap().get(key);
170       Collection<V> expectedCollection = Helpers.copyToList(collection);
171 
172       multimap().put(key, v3());
173       expectedCollection.add(v3());
174       assertEqualIgnoringOrder(expectedCollection, collection);
175       assertEquals(size + 1, multimap().size());
176     }
177   }
178 
179   @MapFeature.Require(SUPPORTS_PUT)
180   @CollectionSize.Require(absent = ZERO)
testPutPresentKeyPropagatesToAsMapGet()181   public void testPutPresentKeyPropagatesToAsMapGet() {
182     List<K> keys = Helpers.copyToList(multimap().keySet());
183     for (K key : keys) {
184       resetContainer();
185 
186       int size = getNumElements();
187 
188       Collection<V> collection = multimap().asMap().get(key);
189       assertNotNull(collection);
190       Collection<V> expectedCollection = Helpers.copyToList(collection);
191 
192       multimap().put(key, v3());
193       expectedCollection.add(v3());
194       assertEqualIgnoringOrder(expectedCollection, collection);
195       assertEquals(size + 1, multimap().size());
196     }
197   }
198 
199   @MapFeature.Require(SUPPORTS_PUT)
200   @CollectionSize.Require(absent = ZERO)
testPutPresentKeyPropagatesToAsMapEntrySet()201   public void testPutPresentKeyPropagatesToAsMapEntrySet() {
202     List<K> keys = Helpers.copyToList(multimap().keySet());
203     for (K key : keys) {
204       resetContainer();
205 
206       int size = getNumElements();
207 
208       Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator();
209       Collection<V> collection = null;
210       while (asMapItr.hasNext()) {
211         Entry<K, Collection<V>> asMapEntry = asMapItr.next();
212         if (key.equals(asMapEntry.getKey())) {
213           collection = asMapEntry.getValue();
214           break;
215         }
216       }
217       assertNotNull(collection);
218       Collection<V> expectedCollection = Helpers.copyToList(collection);
219 
220       multimap().put(key, v3());
221       expectedCollection.add(v3());
222       assertEqualIgnoringOrder(expectedCollection, collection);
223       assertEquals(size + 1, multimap().size());
224     }
225   }
226 }
227