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