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 15package com.google.common.collect.testing.google; 16 17import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 18import static com.google.common.collect.testing.features.CollectionSize.ZERO; 19import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 21import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 22 23import com.google.common.annotations.GwtCompatible; 24import com.google.common.collect.Multimap; 25import com.google.common.collect.testing.features.CollectionFeature; 26import com.google.common.collect.testing.features.CollectionSize; 27import com.google.common.collect.testing.features.MapFeature; 28 29import java.util.Iterator; 30import java.util.Map; 31 32/** 33 * Tester for {@code Multimap.keySet}. 34 * 35 * @author Louis Wasserman 36 */ 37@GwtCompatible 38public class MultimapKeySetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 39 public void testKeySet() { 40 for (Map.Entry<K, V> entry : getSampleElements()) { 41 assertTrue(multimap().keySet().contains(entry.getKey())); 42 } 43 } 44 45 @CollectionSize.Require(absent = ZERO) 46 @MapFeature.Require(ALLOWS_NULL_KEYS) 47 public void testKeySetContainsNullKeyPresent() { 48 initMultimapWithNullKey(); 49 assertTrue(multimap().keySet().contains(null)); 50 } 51 52 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 53 public void testKeySetContainsNullKeyAbsent() { 54 assertFalse(multimap().keySet().contains(null)); 55 } 56 57 @MapFeature.Require(SUPPORTS_REMOVE) 58 public void testKeySetRemovePropagatesToMultimap() { 59 int key0Count = multimap().get(sampleKeys().e0).size(); 60 assertEquals(key0Count > 0, multimap().keySet().remove(sampleKeys().e0)); 61 assertEquals(getNumElements() - key0Count, multimap().size()); 62 assertGet(sampleKeys().e0); 63 } 64 65 @CollectionSize.Require(absent = ZERO) 66 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 67 public void testKeySetIteratorRemove() { 68 int key0Count = multimap().get(sampleKeys().e0).size(); 69 Iterator<K> keyItr = multimap().keySet().iterator(); 70 while (keyItr.hasNext()) { 71 if (keyItr.next().equals(sampleKeys().e0)) { 72 keyItr.remove(); 73 } 74 } 75 assertEquals(getNumElements() - key0Count, multimap().size()); 76 assertGet(sampleKeys().e0); 77 } 78} 79