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; 035 036/** 037 * Tester for {@code Multimap.entries}. 038 * 039 * @author Louis Wasserman 040 */ 041@GwtCompatible 042public class MultimapKeysTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 043 @CollectionSize.Require(SEVERAL) 044 public void testKeys() { 045 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k1(), v0())); 046 Multiset<K> keys = multimap().keys(); 047 assertEquals(2, keys.count(k0())); 048 assertEquals(1, keys.count(k1())); 049 assertEquals(3, keys.size()); 050 assertContainsAllOf(keys, k0(), k1()); 051 assertContainsAllOf( 052 keys.entrySet(), Multisets.immutableEntry(k0(), 2), Multisets.immutableEntry(k1(), 1)); 053 } 054 055 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 056 public void testKeysCountAbsentNullKey() { 057 assertEquals(0, multimap().keys().count(null)); 058 } 059 060 @CollectionSize.Require(SEVERAL) 061 @MapFeature.Require(ALLOWS_NULL_KEYS) 062 public void testKeysWithNullKey() { 063 resetContainer(mapEntry(null, v0()), mapEntry(null, v1()), mapEntry(k1(), v0())); 064 Multiset<K> keys = multimap().keys(); 065 assertEquals(2, keys.count(null)); 066 assertEquals(1, keys.count(k1())); 067 assertEquals(3, keys.size()); 068 assertContainsAllOf(keys, null, k1()); 069 assertContainsAllOf( 070 keys.entrySet(), Multisets.immutableEntry((K) null, 2), Multisets.immutableEntry(k1(), 1)); 071 } 072 073 public void testKeysElementSet() { 074 assertEquals(multimap().keySet(), multimap().keys().elementSet()); 075 } 076 077 @MapFeature.Require(SUPPORTS_REMOVE) 078 public void testKeysRemove() { 079 int original = multimap().keys().remove(k0(), 1); 080 assertEquals(max(original - 1, 0), multimap().get(k0()).size()); 081 } 082 083 @CollectionSize.Require(ONE) 084 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 085 public void testKeysEntrySetIteratorRemove() { 086 Multiset<K> keys = multimap().keys(); 087 Iterator<Multiset.Entry<K>> itr = keys.entrySet().iterator(); 088 assertEquals(Multisets.immutableEntry(k0(), 1), itr.next()); 089 itr.remove(); 090 assertTrue(multimap().isEmpty()); 091 } 092 093 @CollectionSize.Require(SEVERAL) 094 @MapFeature.Require(SUPPORTS_REMOVE) 095 public void testKeysEntrySetRemove() { 096 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k1(), v0())); 097 assertTrue(multimap().keys().entrySet().remove(Multisets.immutableEntry(k0(), 2))); 098 assertEquals(1, multimap().size()); 099 assertTrue(multimap().containsEntry(k1(), v0())); 100 } 101}