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  package com.google.common.collect.testing.google;
17  
18  import static com.google.common.collect.testing.Helpers.assertContains;
19  import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder;
20  import static com.google.common.collect.testing.Helpers.assertEmpty;
21  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
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_KEY_QUERIES;
25  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
26  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
27  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
28  
29  import com.google.common.annotations.GwtCompatible;
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.Collections;
36  
37  /**
38   * Tests for {@link Multimap#get(Object)}.
39   *
40   * @author Louis Wasserman
41   */
42  @GwtCompatible
43  public class MultimapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
44    public void testGetEmpty() {
45      Collection<V> result = multimap().get(k3());
46      assertEmpty(result);
47      assertEquals(0, result.size());
48    }
49  
50    @CollectionSize.Require(absent = ZERO)
51    public void testGetNonEmpty() {
52      Collection<V> result = multimap().get(k0());
53      assertFalse(result.isEmpty());
54      assertContentsAnyOrder(result, v0());
55    }
56  
57    @CollectionSize.Require(SEVERAL)
58    public void testGetMultiple() {
59      resetContainer(
60          Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k0(), v2()));
61      assertGet(k0(), v0(), v1(), v2());
62    }
63  
64    public void testGetAbsentKey() {
65      assertGet(k4());
66    }
67  
68    @CollectionSize.Require(SEVERAL)
69    @MapFeature.Require(SUPPORTS_REMOVE)
70    public void testPropagatesRemoveToMultimap() {
71      resetContainer(
72          Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k0(), v2()));
73      Collection<V> result = multimap().get(k0());
74      assertTrue(result.remove(v0()));
75      assertFalse(multimap().containsEntry(k0(), v0()));
76      assertEquals(2, multimap().size());
77    }
78  
79    @CollectionSize.Require(absent = ZERO)
80    @MapFeature.Require(SUPPORTS_REMOVE)
81    public void testPropagatesRemoveLastElementToMultimap() {
82      Collection<V> result = multimap().get(k0());
83      assertTrue(result.remove(v0()));
84      assertGet(k0());
85    }
86  
87    @MapFeature.Require(SUPPORTS_PUT)
88    public void testPropagatesAddToMultimap() {
89      Collection<V> result = multimap().get(k0());
90      assertTrue(result.add(v3()));
91      assertTrue(multimap().containsKey(k0()));
92      assertEquals(getNumElements() + 1, multimap().size());
93      assertTrue(multimap().containsEntry(k0(), v3()));
94    }
95  
96    @MapFeature.Require(SUPPORTS_PUT)
97    public void testPropagatesAddAllToMultimap() {
98      Collection<V> result = multimap().get(k0());
99      assertTrue(result.addAll(Collections.singletonList(v3())));
100     assertTrue(multimap().containsKey(k0()));
101     assertEquals(getNumElements() + 1, multimap().size());
102     assertTrue(multimap().containsEntry(k0(), v3()));
103   }
104 
105   @CollectionSize.Require(absent = ZERO)
106   @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT})
107   public void testPropagatesRemoveLastThenAddToMultimap() {
108     int oldSize = getNumElements();
109 
110     Collection<V> result = multimap().get(k0());
111     assertTrue(result.remove(v0()));
112 
113     assertFalse(multimap().containsKey(k0()));
114     assertFalse(multimap().containsEntry(k0(), v0()));
115     assertEmpty(result);
116 
117     assertTrue(result.add(v1()));
118     assertTrue(result.add(v2()));
119 
120     assertContentsAnyOrder(result, v1(), v2());
121     assertContentsAnyOrder(multimap().get(k0()), v1(), v2());
122     assertTrue(multimap().containsKey(k0()));
123     assertFalse(multimap().containsEntry(k0(), v0()));
124     assertTrue(multimap().containsEntry(k0(), v2()));
125     assertEquals(oldSize + 1, multimap().size());
126   }
127 
128   @MapFeature.Require(ALLOWS_NULL_KEYS)
129   @CollectionSize.Require(absent = ZERO)
130   public void testGetNullPresent() {
131     initMultimapWithNullKey();
132     assertContains(multimap().get(null), getValueForNullKey());
133   }
134 
135   @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
136   public void testGetNullAbsent() {
137     assertEmpty(multimap().get(null));
138   }
139 
140   @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
141   public void testGetNullForbidden() {
142     try {
143       multimap().get(null);
144       fail("Expected NullPointerException");
145     } catch (NullPointerException expected) {
146       // success
147     }
148   }
149 
150   @MapFeature.Require(ALLOWS_NULL_VALUES)
151   @CollectionSize.Require(absent = ZERO)
152   public void testGetWithNullValue() {
153     initMultimapWithNullValue();
154     assertContains(multimap().get(getKeyForNullValue()), null);
155   }
156 }