View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.Helpers.assertContainsAllOf;
18  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
19  import static com.google.common.collect.testing.features.CollectionSize.ONE;
20  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
23  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.Multimap;
27  import com.google.common.collect.Multiset;
28  import com.google.common.collect.Multisets;
29  import com.google.common.collect.testing.Helpers;
30  import com.google.common.collect.testing.features.CollectionFeature;
31  import com.google.common.collect.testing.features.CollectionSize;
32  import com.google.common.collect.testing.features.MapFeature;
33  import java.util.Iterator;
34  
35  /**
36   * Tester for {@code Multimap.entries}.
37   *
38   * @author Louis Wasserman
39   */
40  @GwtCompatible
41  public class MultimapKeysTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
42    @CollectionSize.Require(SEVERAL)
43    public void testKeys() {
44      resetContainer(
45          Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k1(), v0()));
46      Multiset<K> keys = multimap().keys();
47      assertEquals(2, keys.count(k0()));
48      assertEquals(1, keys.count(k1()));
49      assertEquals(3, keys.size());
50      assertContainsAllOf(keys, k0(), k1());
51      assertContainsAllOf(
52          keys.entrySet(), Multisets.immutableEntry(k0(), 2), Multisets.immutableEntry(k1(), 1));
53    }
54  
55    @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
56    public void testKeysCountAbsentNullKey() {
57      assertEquals(0, multimap().keys().count(null));
58    }
59  
60    @CollectionSize.Require(SEVERAL)
61    @MapFeature.Require(ALLOWS_NULL_KEYS)
62    public void testKeysWithNullKey() {
63      resetContainer(
64          Helpers.mapEntry((K) null, v0()),
65          Helpers.mapEntry((K) null, v1()),
66          Helpers.mapEntry(k1(), v0()));
67      Multiset<K> keys = multimap().keys();
68      assertEquals(2, keys.count(null));
69      assertEquals(1, keys.count(k1()));
70      assertEquals(3, keys.size());
71      assertContainsAllOf(keys, null, k1());
72      assertContainsAllOf(
73          keys.entrySet(), Multisets.immutableEntry((K) null, 2), Multisets.immutableEntry(k1(), 1));
74    }
75  
76    public void testKeysElementSet() {
77      assertEquals(multimap().keySet(), multimap().keys().elementSet());
78    }
79  
80    @MapFeature.Require(SUPPORTS_REMOVE)
81    public void testKeysRemove() {
82      int original = multimap().keys().remove(k0(), 1);
83      assertEquals(Math.max(original - 1, 0), multimap().get(k0()).size());
84    }
85  
86    @CollectionSize.Require(ONE)
87    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
88    public void testKeysEntrySetIteratorRemove() {
89      Multiset<K> keys = multimap().keys();
90      Iterator<Multiset.Entry<K>> itr = keys.entrySet().iterator();
91      assertEquals(Multisets.immutableEntry(k0(), 1), itr.next());
92      itr.remove();
93      assertTrue(multimap().isEmpty());
94    }
95  
96    @CollectionSize.Require(SEVERAL)
97    @MapFeature.Require(SUPPORTS_REMOVE)
98    public void testKeysEntrySetRemove() {
99      resetContainer(
100         Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k1(), v0()));
101     assertTrue(multimap().keys().entrySet().remove(Multisets.immutableEntry(k0(), 2)));
102     assertEquals(1, multimap().size());
103     assertTrue(multimap().containsEntry(k1(), v0()));
104   }
105 }