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> 35 extends AbstractMultimapTester<K, V, Multimap<K, V>> { 36 @CollectionSize.Require(absent = ZERO) 37 public void testContainsKeyYes() { 38 assertTrue(multimap().containsKey(sampleKeys().e0)); 39 } 40 41 public void testContainsKeyNo() { 42 assertFalse(multimap().containsKey(sampleKeys().e3)); 43 } 44 45 public void testContainsKeysFromKeySet() { 46 for (K k : multimap().keySet()) { 47 assertTrue(multimap().containsKey(k)); 48 } 49 } 50 51 public void testContainsKeyAgreesWithGet() { 52 for (K k : sampleKeys()) { 53 assertEquals(!multimap().get(k).isEmpty(), multimap().containsKey(k)); 54 } 55 } 56 57 public void testContainsKeyAgreesWithAsMap() { 58 for (K k : sampleKeys()) { 59 assertEquals(multimap().containsKey(k), multimap().asMap().containsKey(k)); 60 } 61 } 62 63 public void testContainsKeyAgreesWithKeySet() { 64 for (K k : sampleKeys()) { 65 assertEquals(multimap().containsKey(k), multimap().keySet().contains(k)); 66 } 67 } 68 69 @MapFeature.Require(ALLOWS_NULL_KEYS) 70 @CollectionSize.Require(absent = ZERO) 71 public void testContainsKeyNullPresent() { 72 initMultimapWithNullKey(); 73 assertTrue(multimap().containsKey(null)); 74 } 75 76 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 77 public void testContainsKeyNullAbsent() { 78 assertFalse(multimap().containsKey(null)); 79 } 80 81 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) 82 public void testContainsKeyNullDisallowed() { 83 try { 84 multimap().containsKey(null); 85 fail("Expected NullPointerException"); 86 } catch (NullPointerException expected) { 87 // success 88 } 89 } 90 } 91