001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.google;
018
019import static com.google.common.collect.testing.Helpers.assertContains;
020import static com.google.common.collect.testing.Helpers.assertEmpty;
021import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
022import static com.google.common.collect.testing.Helpers.copyToList;
023import static com.google.common.collect.testing.Helpers.mapEntry;
024import static com.google.common.collect.testing.features.CollectionSize.ZERO;
025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
026import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
027import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
028import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
029
030import com.google.common.annotations.GwtCompatible;
031import com.google.common.collect.ImmutableList;
032import com.google.common.collect.Lists;
033import com.google.common.collect.Multimap;
034import com.google.common.collect.testing.features.CollectionSize;
035import com.google.common.collect.testing.features.MapFeature;
036import java.util.Collection;
037import java.util.Iterator;
038import java.util.List;
039import java.util.Map.Entry;
040import org.checkerframework.checker.nullness.qual.Nullable;
041import org.junit.Ignore;
042
043/**
044 * Tester for {@link Multimap#put}.
045 *
046 * @author Louis Wasserman
047 */
048@GwtCompatible
049@Ignore("test runners must not instantiate and run this directly, only via suites we build")
050// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
051@SuppressWarnings("JUnit4ClassUsedInJUnit3")
052@ElementTypesAreNonnullByDefault
053public class MultimapPutTester<K extends @Nullable Object, V extends @Nullable Object>
054    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
055  @MapFeature.Require(absent = SUPPORTS_PUT)
056  public void testPutUnsupported() {
057    assertThrows(UnsupportedOperationException.class, () -> multimap().put(k3(), v3()));
058  }
059
060  @MapFeature.Require(SUPPORTS_PUT)
061  public void testPutEmpty() {
062    int size = getNumElements();
063
064    assertGet(k3(), ImmutableList.<V>of());
065
066    assertTrue(multimap().put(k3(), v3()));
067
068    assertGet(k3(), v3());
069    assertEquals(size + 1, multimap().size());
070  }
071
072  @MapFeature.Require(SUPPORTS_PUT)
073  @CollectionSize.Require(absent = ZERO)
074  public void testPutPresent() {
075    int size = getNumElements();
076
077    assertGet(k0(), v0());
078
079    assertTrue(multimap().put(k0(), v3()));
080
081    assertGet(k0(), v0(), v3());
082    assertEquals(size + 1, multimap().size());
083  }
084
085  @MapFeature.Require(SUPPORTS_PUT)
086  public void testPutTwoElements() {
087    int size = getNumElements();
088
089    List<V> values = copyToList(multimap().get(k0()));
090
091    assertTrue(multimap().put(k0(), v1()));
092    assertTrue(multimap().put(k0(), v2()));
093
094    values.add(v1());
095    values.add(v2());
096
097    assertGet(k0(), values);
098    assertEquals(size + 2, multimap().size());
099  }
100
101  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
102  public void testPutNullValue_supported() {
103    int size = getNumElements();
104
105    multimap().put(k3(), null);
106
107    assertGet(k3(), Lists.newArrayList((V) null)); // ImmutableList.of can't take null.
108    assertEquals(size + 1, multimap().size());
109  }
110
111  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
112  public void testPutNullValue_unsupported() {
113    assertThrows(NullPointerException.class, () -> multimap().put(k1(), null));
114
115    expectUnchanged();
116  }
117
118  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
119  public void testPutNullKey() {
120    int size = getNumElements();
121
122    multimap().put(null, v3());
123
124    assertGet(null, v3());
125    assertEquals(size + 1, multimap().size());
126  }
127
128  @MapFeature.Require(SUPPORTS_PUT)
129  public void testPutNotPresentKeyPropagatesToGet() {
130    int size = getNumElements();
131    Collection<V> collection = multimap().get(k3());
132    assertEmpty(collection);
133    multimap().put(k3(), v3());
134    assertContains(collection, v3());
135    assertEquals(size + 1, multimap().size());
136  }
137
138  @MapFeature.Require(SUPPORTS_PUT)
139  public void testPutNotPresentKeyPropagatesToEntries() {
140    Collection<Entry<K, V>> entries = multimap().entries();
141    assertFalse(entries.contains(mapEntry(k3(), v3())));
142    multimap().put(k3(), v3());
143    assertContains(entries, mapEntry(k3(), v3()));
144  }
145
146  @CollectionSize.Require(absent = ZERO)
147  @MapFeature.Require(SUPPORTS_PUT)
148  public void testPutPresentKeyPropagatesToEntries() {
149    Collection<Entry<K, V>> entries = multimap().entries();
150    assertFalse(entries.contains(mapEntry(k0(), v3())));
151    multimap().put(k0(), v3());
152    assertContains(entries, mapEntry(k0(), v3()));
153  }
154
155  @MapFeature.Require(SUPPORTS_PUT)
156  @CollectionSize.Require(absent = ZERO)
157  public void testPutPresentKeyPropagatesToGet() {
158    List<K> keys = copyToList(multimap().keySet());
159    for (K key : keys) {
160      resetContainer();
161
162      int size = getNumElements();
163
164      Collection<V> collection = multimap().get(key);
165      Collection<V> expectedCollection = copyToList(collection);
166
167      multimap().put(key, v3());
168      expectedCollection.add(v3());
169      assertEqualIgnoringOrder(expectedCollection, collection);
170      assertEquals(size + 1, multimap().size());
171    }
172  }
173
174  @MapFeature.Require(SUPPORTS_PUT)
175  @CollectionSize.Require(absent = ZERO)
176  public void testPutPresentKeyPropagatesToAsMapGet() {
177    List<K> keys = copyToList(multimap().keySet());
178    for (K key : keys) {
179      resetContainer();
180
181      int size = getNumElements();
182
183      Collection<V> collection = multimap().asMap().get(key);
184      assertNotNull(collection);
185      Collection<V> expectedCollection = copyToList(collection);
186
187      multimap().put(key, v3());
188      expectedCollection.add(v3());
189      assertEqualIgnoringOrder(expectedCollection, collection);
190      assertEquals(size + 1, multimap().size());
191    }
192  }
193
194  @MapFeature.Require(SUPPORTS_PUT)
195  @CollectionSize.Require(absent = ZERO)
196  public void testPutPresentKeyPropagatesToAsMapEntrySet() {
197    List<K> keys = copyToList(multimap().keySet());
198    for (K key : keys) {
199      resetContainer();
200
201      int size = getNumElements();
202
203      Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator();
204      Collection<V> collection = null;
205      while (asMapItr.hasNext()) {
206        Entry<K, Collection<V>> asMapEntry = asMapItr.next();
207        if (key.equals(asMapEntry.getKey())) {
208          collection = asMapEntry.getValue();
209          break;
210        }
211      }
212      assertNotNull(collection);
213      Collection<V> expectedCollection = copyToList(collection);
214
215      multimap().put(key, v3());
216      expectedCollection.add(v3());
217      assertEqualIgnoringOrder(expectedCollection, collection);
218      assertEquals(size + 1, multimap().size());
219    }
220  }
221}