View Javadoc
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  
17  package com.google.common.collect.testing.google;
18  
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_VALUES;
24  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
25  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
26  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
27  
28  import com.google.common.annotations.GwtCompatible;
29  import com.google.common.collect.Multimap;
30  import com.google.common.collect.testing.Helpers;
31  import com.google.common.collect.testing.features.CollectionSize;
32  import com.google.common.collect.testing.features.MapFeature;
33  import java.util.Collection;
34  
35  /**
36   * Tests for {@code Multimap.asMap().get(Object)}.
37   *
38   * @author Louis Wasserman
39   */
40  @GwtCompatible
41  public 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(k0(), v0()), Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k0(), v2()));
48      Collection<V> result = multimap().asMap().get(k0());
49      assertTrue(result.remove(v0()));
50      assertFalse(multimap().containsEntry(k0(), v0()));
51      assertEquals(2, multimap().size());
52    }
53  
54    @CollectionSize.Require(absent = ZERO)
55    @MapFeature.Require(SUPPORTS_REMOVE)
56    public void testPropagatesRemoveLastElementToMultimap() {
57      Collection<V> result = multimap().asMap().get(k0());
58      assertTrue(result.remove(v0()));
59      assertGet(k0());
60    }
61  
62    @CollectionSize.Require(absent = ZERO)
63    @MapFeature.Require(SUPPORTS_REMOVE)
64    public void testPropagatesClearToMultimap() {
65      Collection<V> result = multimap().asMap().get(k0());
66      result.clear();
67      assertGet(k0());
68      assertEmpty(result);
69    }
70  
71    @CollectionSize.Require(absent = ZERO)
72    @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
73    public void testAddNullValue() {
74      Collection<V> result = multimap().asMap().get(k0());
75      assertTrue(result.add(null));
76      assertTrue(multimap().containsEntry(k0(), null));
77    }
78  
79    @CollectionSize.Require(absent = ZERO)
80    @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES})
81    public void testRemoveNullValue() {
82      Collection<V> result = multimap().asMap().get(k0());
83      assertFalse(result.remove(null));
84    }
85  
86    @CollectionSize.Require(absent = ZERO)
87    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
88    public void testAddNullValueUnsupported() {
89      Collection<V> result = multimap().asMap().get(k0());
90      try {
91        result.add(null);
92        fail("Expected NullPointerException");
93      } catch (NullPointerException expected) {
94      }
95    }
96  
97    @CollectionSize.Require(absent = ZERO)
98    @MapFeature.Require(SUPPORTS_PUT)
99    public void testPropagatesAddToMultimap() {
100     Collection<V> result = multimap().asMap().get(k0());
101     result.add(v3());
102     assertContentsAnyOrder(multimap().get(k0()), v0(), v3());
103   }
104 
105   @CollectionSize.Require(absent = ZERO)
106   @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT})
107   public void testPropagatesRemoveThenAddToMultimap() {
108     int oldSize = getNumElements();
109 
110     Collection<V> result = multimap().asMap().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   @CollectionSize.Require(absent = ZERO)
129   @MapFeature.Require(SUPPORTS_REMOVE)
130   public void testReflectsMultimapRemove() {
131     Collection<V> result = multimap().asMap().get(k0());
132     multimap().removeAll(k0());
133     assertEmpty(result);
134   }
135 }