001/* 002 * Copyright (C) 2013 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.collect.testing.google; 016 017import static com.google.common.collect.testing.Helpers.assertContainsAllOf; 018import static com.google.common.collect.testing.Helpers.mapEntry; 019import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 020import static com.google.common.collect.testing.features.CollectionSize.ONE; 021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 024import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 025import static java.lang.Math.max; 026 027import com.google.common.annotations.GwtCompatible; 028import com.google.common.collect.Multimap; 029import com.google.common.collect.Multiset; 030import com.google.common.collect.Multisets; 031import com.google.common.collect.testing.features.CollectionFeature; 032import com.google.common.collect.testing.features.CollectionSize; 033import com.google.common.collect.testing.features.MapFeature; 034import java.util.Iterator; 035import org.junit.Ignore; 036 037/** 038 * Tester for {@code Multimap.entries}. 039 * 040 * @author Louis Wasserman 041 */ 042@GwtCompatible 043@Ignore("test runners must not instantiate and run this directly, only via suites we build") 044// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 045@SuppressWarnings("JUnit4ClassUsedInJUnit3") 046public class MultimapKeysTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 047 @CollectionSize.Require(SEVERAL) 048 public void testKeys() { 049 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k1(), v0())); 050 Multiset<K> keys = multimap().keys(); 051 assertEquals(2, keys.count(k0())); 052 assertEquals(1, keys.count(k1())); 053 assertEquals(3, keys.size()); 054 assertContainsAllOf(keys, k0(), k1()); 055 assertContainsAllOf( 056 keys.entrySet(), Multisets.immutableEntry(k0(), 2), Multisets.immutableEntry(k1(), 1)); 057 } 058 059 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 060 public void testKeysCountAbsentNullKey() { 061 assertEquals(0, multimap().keys().count(null)); 062 } 063 064 @CollectionSize.Require(SEVERAL) 065 @MapFeature.Require(ALLOWS_NULL_KEYS) 066 public void testKeysWithNullKey() { 067 resetContainer(mapEntry((K) null, v0()), mapEntry((K) null, v1()), mapEntry(k1(), v0())); 068 Multiset<K> keys = multimap().keys(); 069 assertEquals(2, keys.count(null)); 070 assertEquals(1, keys.count(k1())); 071 assertEquals(3, keys.size()); 072 assertContainsAllOf(keys, null, k1()); 073 assertContainsAllOf( 074 keys.entrySet(), Multisets.immutableEntry((K) null, 2), Multisets.immutableEntry(k1(), 1)); 075 } 076 077 public void testKeysElementSet() { 078 assertEquals(multimap().keySet(), multimap().keys().elementSet()); 079 } 080 081 @MapFeature.Require(SUPPORTS_REMOVE) 082 public void testKeysRemove() { 083 int original = multimap().keys().remove(k0(), 1); 084 assertEquals(max(original - 1, 0), multimap().get(k0()).size()); 085 } 086 087 @CollectionSize.Require(ONE) 088 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 089 public void testKeysEntrySetIteratorRemove() { 090 Multiset<K> keys = multimap().keys(); 091 Iterator<Multiset.Entry<K>> itr = keys.entrySet().iterator(); 092 assertEquals(Multisets.immutableEntry(k0(), 1), itr.next()); 093 itr.remove(); 094 assertTrue(multimap().isEmpty()); 095 } 096 097 @CollectionSize.Require(SEVERAL) 098 @MapFeature.Require(SUPPORTS_REMOVE) 099 public void testKeysEntrySetRemove() { 100 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k1(), v0())); 101 assertTrue(multimap().keys().entrySet().remove(Multisets.immutableEntry(k0(), 2))); 102 assertEquals(1, multimap().size()); 103 assertTrue(multimap().containsEntry(k1(), v0())); 104 } 105}