MultimapAsMapGetTester.java revision 0888a09821a98ac0680fad765217302858e70fa4
1/*
2 * Copyright (C) 2013 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
17package com.google.common.collect.testing.google;
18
19import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
20import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
24import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
25import static org.truth0.Truth.ASSERT;
26
27import com.google.common.annotations.GwtCompatible;
28import com.google.common.collect.Multimap;
29import com.google.common.collect.testing.Helpers;
30import com.google.common.collect.testing.features.CollectionSize;
31import com.google.common.collect.testing.features.MapFeature;
32
33import java.util.Collection;
34
35/**
36 * Tests for {@code Multimap.asMap().get(Object)}.
37 *
38 * @author Louis Wasserman
39 */
40@GwtCompatible
41public class MultimapAsMapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
42
43  @CollectionSize.Require(SEVERAL)
44  @MapFeature.Require(SUPPORTS_REMOVE)
45  public void testPropagatesRemoveToMultimap() {
46    resetContainer(
47        Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
48        Helpers.mapEntry(sampleKeys().e0, sampleValues().e3),
49        Helpers.mapEntry(sampleKeys().e0, sampleValues().e2));
50    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
51    assertTrue(result.remove(sampleValues().e0));
52    assertFalse(multimap().containsEntry(sampleKeys().e0, sampleValues().e0));
53    assertEquals(2, multimap().size());
54  }
55
56  @CollectionSize.Require(absent = ZERO)
57  @MapFeature.Require(SUPPORTS_REMOVE)
58  public void testPropagatesRemoveLastElementToMultimap() {
59    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
60    assertTrue(result.remove(sampleValues().e0));
61    assertGet(sampleKeys().e0);
62  }
63
64  @CollectionSize.Require(absent = ZERO)
65  @MapFeature.Require(SUPPORTS_REMOVE)
66  public void testPropagatesClearToMultimap() {
67    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
68    result.clear();
69    assertGet(sampleKeys().e0);
70    ASSERT.that(result).isEmpty();
71  }
72
73  @CollectionSize.Require(absent = ZERO)
74  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
75  public void testAddNullValue() {
76    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
77    assertTrue(result.add(null));
78    assertTrue(multimap().containsEntry(sampleKeys().e0, null));
79  }
80
81  @CollectionSize.Require(absent = ZERO)
82  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES})
83  public void testRemoveNullValue() {
84    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
85    assertFalse(result.remove(null));
86  }
87
88  @CollectionSize.Require(absent = ZERO)
89  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
90  public void testAddNullValueUnsupported() {
91    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
92    try {
93      result.add(null);
94      fail("Expected NullPointerException");
95    } catch (NullPointerException expected) {}
96  }
97
98  @CollectionSize.Require(absent = ZERO)
99  @MapFeature.Require(SUPPORTS_PUT)
100  public void testPropagatesAddToMultimap() {
101    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
102    result.add(sampleValues().e3);
103    ASSERT.that(multimap().get(sampleKeys().e0))
104        .has().exactly(sampleValues().e0, sampleValues().e3);
105  }
106
107  @CollectionSize.Require(absent = ZERO)
108  @MapFeature.Require({ SUPPORTS_REMOVE, SUPPORTS_PUT })
109  public void testPropagatesRemoveThenAddToMultimap() {
110    int oldSize = getNumElements();
111
112    K k0 = sampleKeys().e0;
113    V v0 = sampleValues().e0;
114
115    Collection<V> result = multimap().asMap().get(k0);
116    assertTrue(result.remove(v0));
117
118    assertFalse(multimap().containsKey(k0));
119    assertFalse(multimap().containsEntry(k0, v0));
120    ASSERT.that(result).isEmpty();
121
122    V v1 = sampleValues().e1;
123    V v2 = sampleValues().e2;
124
125    assertTrue(result.add(v1));
126    assertTrue(result.add(v2));
127
128    ASSERT.that(result).has().exactly(v1, v2);
129    ASSERT.that(multimap().get(k0)).has().exactly(v1, v2);
130    assertTrue(multimap().containsKey(k0));
131    assertFalse(multimap().containsEntry(k0, v0));
132    assertTrue(multimap().containsEntry(k0, v2));
133    assertEquals(oldSize + 1, multimap().size());
134  }
135
136  @CollectionSize.Require(absent = ZERO)
137  @MapFeature.Require(SUPPORTS_REMOVE)
138  public void testReflectsMultimapRemove() {
139    Collection<V> result = multimap().asMap().get(sampleKeys().e0);
140    multimap().removeAll(sampleKeys().e0);
141    ASSERT.that(result).isEmpty();
142  }
143}
144