1 /* 2 * Copyright (C) 2008 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.testers; 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.testing.AbstractMapTester; 25 import com.google.common.collect.testing.WrongType; 26 import com.google.common.collect.testing.features.CollectionSize; 27 import com.google.common.collect.testing.features.MapFeature; 28 29 /** 30 * A generic JUnit test which tests {@code containsKey()} operations on a map. 31 * Can't be invoked directly; please see 32 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 33 * 34 * @author George van den Driessche 35 */ 36 @GwtCompatible 37 public class MapContainsKeyTester<K, V> extends AbstractMapTester<K, V> { 38 @CollectionSize.Require(absent = ZERO) testContains_yes()39 public void testContains_yes() { 40 assertTrue("containsKey(present) should return true", 41 getMap().containsKey(samples.e0.getKey())); 42 } 43 testContains_no()44 public void testContains_no() { 45 assertFalse("containsKey(notPresent) should return false", 46 getMap().containsKey(samples.e3.getKey())); 47 } 48 49 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) testContains_nullNotContainedButAllowed()50 public void testContains_nullNotContainedButAllowed() { 51 assertFalse("containsKey(null) should return false", 52 getMap().containsKey(null)); 53 } 54 55 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) testContains_nullNotContainedAndUnsupported()56 public void testContains_nullNotContainedAndUnsupported() { 57 expectNullKeyMissingWhenNullKeysUnsupported( 58 "containsKey(null) should return false or throw"); 59 } 60 61 @MapFeature.Require(ALLOWS_NULL_KEYS) 62 @CollectionSize.Require(absent = ZERO) testContains_nonNullWhenNullContained()63 public void testContains_nonNullWhenNullContained() { 64 initMapWithNullKey(); 65 assertFalse("containsKey(notPresent) should return false", 66 getMap().containsKey(samples.e3.getKey())); 67 } 68 69 @MapFeature.Require(ALLOWS_NULL_KEYS) 70 @CollectionSize.Require(absent = ZERO) testContains_nullContained()71 public void testContains_nullContained() { 72 initMapWithNullKey(); 73 assertTrue("containsKey(null) should return true", 74 getMap().containsKey(null)); 75 } 76 testContains_wrongType()77 public void testContains_wrongType() { 78 try { 79 //noinspection SuspiciousMethodCalls 80 assertFalse("containsKey(wrongType) should return false or throw", 81 getMap().containsKey(WrongType.VALUE)); 82 } catch (ClassCastException tolerated) { 83 } 84 } 85 } 86