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  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.Multimap;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  
28  /**
29   * Tester for the {@code containsKey} methods of {@code Multimap} and its {@code asMap()} view.
30   *
31   * @author Louis Wasserman
32   */
33  @GwtCompatible
34  public class MultimapContainsKeyTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
35    @CollectionSize.Require(absent = ZERO)
36    public void testContainsKeyYes() {
37      assertTrue(multimap().containsKey(k0()));
38    }
39  
40    public void testContainsKeyNo() {
41      assertFalse(multimap().containsKey(k3()));
42    }
43  
44    public void testContainsKeysFromKeySet() {
45      for (K k : multimap().keySet()) {
46        assertTrue(multimap().containsKey(k));
47      }
48    }
49  
50    public void testContainsKeyAgreesWithGet() {
51      for (K k : sampleKeys()) {
52        assertEquals(!multimap().get(k).isEmpty(), multimap().containsKey(k));
53      }
54    }
55  
56    public void testContainsKeyAgreesWithAsMap() {
57      for (K k : sampleKeys()) {
58        assertEquals(multimap().containsKey(k), multimap().asMap().containsKey(k));
59      }
60    }
61  
62    public void testContainsKeyAgreesWithKeySet() {
63      for (K k : sampleKeys()) {
64        assertEquals(multimap().containsKey(k), multimap().keySet().contains(k));
65      }
66    }
67  
68    @MapFeature.Require(ALLOWS_NULL_KEYS)
69    @CollectionSize.Require(absent = ZERO)
70    public void testContainsKeyNullPresent() {
71      initMultimapWithNullKey();
72      assertTrue(multimap().containsKey(null));
73    }
74  
75    @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
76    public void testContainsKeyNullAbsent() {
77      assertFalse(multimap().containsKey(null));
78    }
79  
80    @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
81    public void testContainsKeyNullDisallowed() {
82      try {
83        multimap().containsKey(null);
84        fail("Expected NullPointerException");
85      } catch (NullPointerException expected) {
86        // success
87      }
88    }
89  }