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 import org.junit.Ignore; 35 36 /** 37 * Tester for {@code Multimap.entries}. 38 * 39 * @author Louis Wasserman 40 */ 41 @GwtCompatible 42 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 43 public class MultimapKeysTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 44 @CollectionSize.Require(SEVERAL) testKeys()45 public void testKeys() { 46 resetContainer( 47 Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k1(), v0())); 48 Multiset<K> keys = multimap().keys(); 49 assertEquals(2, keys.count(k0())); 50 assertEquals(1, keys.count(k1())); 51 assertEquals(3, keys.size()); 52 assertContainsAllOf(keys, k0(), k1()); 53 assertContainsAllOf( 54 keys.entrySet(), Multisets.immutableEntry(k0(), 2), Multisets.immutableEntry(k1(), 1)); 55 } 56 57 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) testKeysCountAbsentNullKey()58 public void testKeysCountAbsentNullKey() { 59 assertEquals(0, multimap().keys().count(null)); 60 } 61 62 @CollectionSize.Require(SEVERAL) 63 @MapFeature.Require(ALLOWS_NULL_KEYS) testKeysWithNullKey()64 public void testKeysWithNullKey() { 65 resetContainer( 66 Helpers.mapEntry((K) null, v0()), 67 Helpers.mapEntry((K) null, v1()), 68 Helpers.mapEntry(k1(), v0())); 69 Multiset<K> keys = multimap().keys(); 70 assertEquals(2, keys.count(null)); 71 assertEquals(1, keys.count(k1())); 72 assertEquals(3, keys.size()); 73 assertContainsAllOf(keys, null, k1()); 74 assertContainsAllOf( 75 keys.entrySet(), Multisets.immutableEntry((K) null, 2), Multisets.immutableEntry(k1(), 1)); 76 } 77 testKeysElementSet()78 public void testKeysElementSet() { 79 assertEquals(multimap().keySet(), multimap().keys().elementSet()); 80 } 81 82 @MapFeature.Require(SUPPORTS_REMOVE) testKeysRemove()83 public void testKeysRemove() { 84 int original = multimap().keys().remove(k0(), 1); 85 assertEquals(Math.max(original - 1, 0), multimap().get(k0()).size()); 86 } 87 88 @CollectionSize.Require(ONE) 89 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) testKeysEntrySetIteratorRemove()90 public void testKeysEntrySetIteratorRemove() { 91 Multiset<K> keys = multimap().keys(); 92 Iterator<Multiset.Entry<K>> itr = keys.entrySet().iterator(); 93 assertEquals(Multisets.immutableEntry(k0(), 1), itr.next()); 94 itr.remove(); 95 assertTrue(multimap().isEmpty()); 96 } 97 98 @CollectionSize.Require(SEVERAL) 99 @MapFeature.Require(SUPPORTS_REMOVE) testKeysEntrySetRemove()100 public void testKeysEntrySetRemove() { 101 resetContainer( 102 Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k1(), v0())); 103 assertTrue(multimap().keys().entrySet().remove(Multisets.immutableEntry(k0(), 2))); 104 assertEquals(1, multimap().size()); 105 assertTrue(multimap().containsEntry(k1(), v0())); 106 } 107 } 108