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 */
016package com.google.common.collect.testing.google;
017
018import static com.google.common.collect.testing.Helpers.assertContains;
019import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder;
020import static com.google.common.collect.testing.Helpers.assertEmpty;
021import static com.google.common.collect.testing.Helpers.mapEntry;
022import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
023import static com.google.common.collect.testing.features.CollectionSize.ZERO;
024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
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.features.MapFeature.SUPPORTS_REMOVE;
029import static java.util.Collections.singletonList;
030import static org.junit.Assert.assertThrows;
031
032import com.google.common.annotations.GwtCompatible;
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;
037
038/**
039 * Tests for {@link Multimap#get(Object)}.
040 *
041 * @author Louis Wasserman
042 */
043@GwtCompatible
044public class MultimapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
045  public void testGetEmpty() {
046    Collection<V> result = multimap().get(k3());
047    assertEmpty(result);
048    assertEquals(0, result.size());
049  }
050
051  @CollectionSize.Require(absent = ZERO)
052  public void testGetNonEmpty() {
053    Collection<V> result = multimap().get(k0());
054    assertFalse(result.isEmpty());
055    assertContentsAnyOrder(result, v0());
056  }
057
058  @CollectionSize.Require(SEVERAL)
059  public void testGetMultiple() {
060    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v2()));
061    assertGet(k0(), v0(), v1(), v2());
062  }
063
064  public void testGetAbsentKey() {
065    assertGet(k4());
066  }
067
068  @CollectionSize.Require(SEVERAL)
069  @MapFeature.Require(SUPPORTS_REMOVE)
070  public void testPropagatesRemoveToMultimap() {
071    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3()), mapEntry(k0(), v2()));
072    Collection<V> result = multimap().get(k0());
073    assertTrue(result.remove(v0()));
074    assertFalse(multimap().containsEntry(k0(), v0()));
075    assertEquals(2, multimap().size());
076  }
077
078  @CollectionSize.Require(absent = ZERO)
079  @MapFeature.Require(SUPPORTS_REMOVE)
080  public void testPropagatesRemoveLastElementToMultimap() {
081    Collection<V> result = multimap().get(k0());
082    assertTrue(result.remove(v0()));
083    assertGet(k0());
084  }
085
086  @MapFeature.Require(SUPPORTS_PUT)
087  public void testPropagatesAddToMultimap() {
088    Collection<V> result = multimap().get(k0());
089    assertTrue(result.add(v3()));
090    assertTrue(multimap().containsKey(k0()));
091    assertEquals(getNumElements() + 1, multimap().size());
092    assertTrue(multimap().containsEntry(k0(), v3()));
093  }
094
095  @MapFeature.Require(SUPPORTS_PUT)
096  public void testPropagatesAddAllToMultimap() {
097    Collection<V> result = multimap().get(k0());
098    assertTrue(result.addAll(singletonList(v3())));
099    assertTrue(multimap().containsKey(k0()));
100    assertEquals(getNumElements() + 1, multimap().size());
101    assertTrue(multimap().containsEntry(k0(), v3()));
102  }
103
104  @CollectionSize.Require(absent = ZERO)
105  @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT})
106  public void testPropagatesRemoveLastThenAddToMultimap() {
107    int oldSize = getNumElements();
108
109    Collection<V> result = multimap().get(k0());
110    assertTrue(result.remove(v0()));
111
112    assertFalse(multimap().containsKey(k0()));
113    assertFalse(multimap().containsEntry(k0(), v0()));
114    assertEmpty(result);
115
116    assertTrue(result.add(v1()));
117    assertTrue(result.add(v2()));
118
119    assertContentsAnyOrder(result, v1(), v2());
120    assertContentsAnyOrder(multimap().get(k0()), v1(), v2());
121    assertTrue(multimap().containsKey(k0()));
122    assertFalse(multimap().containsEntry(k0(), v0()));
123    assertTrue(multimap().containsEntry(k0(), v2()));
124    assertEquals(oldSize + 1, multimap().size());
125  }
126
127  @MapFeature.Require(ALLOWS_NULL_KEYS)
128  @CollectionSize.Require(absent = ZERO)
129  public void testGetNullPresent() {
130    initMultimapWithNullKey();
131    assertContains(multimap().get(null), getValueForNullKey());
132  }
133
134  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
135  public void testGetNullAbsent() {
136    assertEmpty(multimap().get(null));
137  }
138
139  @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
140  public void testGetNullForbidden() {
141    assertThrows(NullPointerException.class, () -> multimap().get(null));
142  }
143
144  @MapFeature.Require(ALLOWS_NULL_VALUES)
145  @CollectionSize.Require(absent = ZERO)
146  public void testGetWithNullValue() {
147    initMultimapWithNullValue();
148    assertContains(multimap().get(getKeyForNullValue()), null);
149  }
150}