View Javadoc
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  
39  /**
40   * Tester for {@link Multimap#put}.
41   *
42   * @author Louis Wasserman
43   */
44  @GwtCompatible
45  public class MultimapPutTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
46    @MapFeature.Require(absent = SUPPORTS_PUT)
47    public void testPutUnsupported() {
48      try {
49        multimap().put(k3(), v3());
50        fail("Expected UnsupportedOperationException");
51      } catch (UnsupportedOperationException expected) {
52      }
53    }
54  
55    @MapFeature.Require(SUPPORTS_PUT)
56    public void testPutEmpty() {
57      int size = getNumElements();
58  
59      assertGet(k3(), ImmutableList.<V>of());
60  
61      assertTrue(multimap().put(k3(), v3()));
62  
63      assertGet(k3(), v3());
64      assertEquals(size + 1, multimap().size());
65    }
66  
67    @MapFeature.Require(SUPPORTS_PUT)
68    @CollectionSize.Require(absent = ZERO)
69    public void testPutPresent() {
70      int size = getNumElements();
71  
72      assertGet(k0(), v0());
73  
74      assertTrue(multimap().put(k0(), v3()));
75  
76      assertGet(k0(), v0(), v3());
77      assertEquals(size + 1, multimap().size());
78    }
79  
80    @MapFeature.Require(SUPPORTS_PUT)
81    public void testPutTwoElements() {
82      int size = getNumElements();
83  
84      List<V> values = Helpers.copyToList(multimap().get(k0()));
85  
86      assertTrue(multimap().put(k0(), v1()));
87      assertTrue(multimap().put(k0(), v2()));
88  
89      values.add(v1());
90      values.add(v2());
91  
92      assertGet(k0(), values);
93      assertEquals(size + 2, multimap().size());
94    }
95  
96    @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
97    public void testPutNullValue_supported() {
98      int size = getNumElements();
99  
100     multimap().put(k3(), null);
101 
102     assertGet(k3(), Lists.newArrayList((V) null)); // ImmutableList.of can't take null.
103     assertEquals(size + 1, multimap().size());
104   }
105 
106   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
107   public void testPutNullValue_unsupported() {
108     try {
109       multimap().put(k1(), null);
110       fail();
111     } catch (NullPointerException expected) {
112     }
113 
114     expectUnchanged();
115   }
116 
117   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
118   public void testPutNullKey() {
119     int size = getNumElements();
120 
121     multimap().put(null, v3());
122 
123     assertGet(null, v3());
124     assertEquals(size + 1, multimap().size());
125   }
126 
127   @MapFeature.Require(SUPPORTS_PUT)
128   public void testPutNotPresentKeyPropagatesToGet() {
129     int size = getNumElements();
130     Collection<V> collection = multimap().get(k3());
131     assertEmpty(collection);
132     multimap().put(k3(), v3());
133     assertContains(collection, v3());
134     assertEquals(size + 1, multimap().size());
135   }
136 
137   @MapFeature.Require(SUPPORTS_PUT)
138   public void testPutNotPresentKeyPropagatesToEntries() {
139     Collection<Entry<K, V>> entries = multimap().entries();
140     assertFalse(entries.contains(Helpers.mapEntry(k3(), v3())));
141     multimap().put(k3(), v3());
142     assertContains(entries, Helpers.mapEntry(k3(), v3()));
143   }
144 
145   @CollectionSize.Require(absent = ZERO)
146   @MapFeature.Require(SUPPORTS_PUT)
147   public void testPutPresentKeyPropagatesToEntries() {
148     Collection<Entry<K, V>> entries = multimap().entries();
149     assertFalse(entries.contains(Helpers.mapEntry(k0(), v3())));
150     multimap().put(k0(), v3());
151     assertContains(entries, Helpers.mapEntry(k0(), v3()));
152   }
153 
154   @MapFeature.Require(SUPPORTS_PUT)
155   @CollectionSize.Require(absent = ZERO)
156   public void testPutPresentKeyPropagatesToGet() {
157     List<K> keys = Helpers.copyToList(multimap().keySet());
158     for (K key : keys) {
159       resetContainer();
160 
161       int size = getNumElements();
162 
163       Collection<V> collection = multimap().get(key);
164       Collection<V> expectedCollection = Helpers.copyToList(collection);
165 
166       multimap().put(key, v3());
167       expectedCollection.add(v3());
168       assertEqualIgnoringOrder(expectedCollection, collection);
169       assertEquals(size + 1, multimap().size());
170     }
171   }
172 
173   @MapFeature.Require(SUPPORTS_PUT)
174   @CollectionSize.Require(absent = ZERO)
175   public void testPutPresentKeyPropagatesToAsMapGet() {
176     List<K> keys = Helpers.copyToList(multimap().keySet());
177     for (K key : keys) {
178       resetContainer();
179 
180       int size = getNumElements();
181 
182       Collection<V> collection = multimap().asMap().get(key);
183       assertNotNull(collection);
184       Collection<V> expectedCollection = Helpers.copyToList(collection);
185 
186       multimap().put(key, v3());
187       expectedCollection.add(v3());
188       assertEqualIgnoringOrder(expectedCollection, collection);
189       assertEquals(size + 1, multimap().size());
190     }
191   }
192 
193   @MapFeature.Require(SUPPORTS_PUT)
194   @CollectionSize.Require(absent = ZERO)
195   public void testPutPresentKeyPropagatesToAsMapEntrySet() {
196     List<K> keys = Helpers.copyToList(multimap().keySet());
197     for (K key : keys) {
198       resetContainer();
199 
200       int size = getNumElements();
201 
202       Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator();
203       Collection<V> collection = null;
204       while (asMapItr.hasNext()) {
205         Entry<K, Collection<V>> asMapEntry = asMapItr.next();
206         if (key.equals(asMapEntry.getKey())) {
207           collection = asMapEntry.getValue();
208           break;
209         }
210       }
211       assertNotNull(collection);
212       Collection<V> expectedCollection = Helpers.copyToList(collection);
213 
214       multimap().put(key, v3());
215       expectedCollection.add(v3());
216       assertEqualIgnoringOrder(expectedCollection, collection);
217       assertEquals(size + 1, multimap().size());
218     }
219   }
220 }