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.assertContains; 018import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 019import static com.google.common.collect.testing.Helpers.mapEntry; 020import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 021import static com.google.common.collect.testing.features.CollectionSize.ONE; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 026import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 027import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 028import static java.util.Collections.singleton; 029 030import com.google.common.annotations.GwtCompatible; 031import com.google.common.collect.Multimap; 032import com.google.common.collect.testing.features.CollectionFeature; 033import com.google.common.collect.testing.features.CollectionSize; 034import com.google.common.collect.testing.features.MapFeature; 035import java.util.Iterator; 036import java.util.Map.Entry; 037 038/** 039 * Tester for {@code Multimap.entries}. 040 * 041 * @author Louis Wasserman 042 */ 043@GwtCompatible 044public class MultimapEntriesTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 045 public void testEntries() { 046 assertEqualIgnoringOrder(getSampleElements(), multimap().entries()); 047 } 048 049 @CollectionSize.Require(absent = ZERO) 050 @MapFeature.Require(ALLOWS_NULL_KEYS) 051 public void testContainsEntryWithNullKeyPresent() { 052 initMultimapWithNullKey(); 053 assertContains(multimap().entries(), mapEntry((K) null, getValueForNullKey())); 054 } 055 056 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 057 public void testContainsEntryWithNullKeyAbsent() { 058 assertFalse(multimap().entries().contains(mapEntry(null, v0()))); 059 } 060 061 @CollectionSize.Require(absent = ZERO) 062 @MapFeature.Require(ALLOWS_NULL_VALUES) 063 public void testContainsEntryWithNullValuePresent() { 064 initMultimapWithNullValue(); 065 assertContains(multimap().entries(), mapEntry(getKeyForNullValue(), (V) null)); 066 } 067 068 @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES) 069 public void testContainsEntryWithNullValueAbsent() { 070 assertFalse(multimap().entries().contains(mapEntry(k0(), null))); 071 } 072 073 @CollectionSize.Require(absent = ZERO) 074 @MapFeature.Require(SUPPORTS_REMOVE) 075 public void testRemovePropagatesToMultimap() { 076 assertTrue(multimap().entries().remove(mapEntry(k0(), v0()))); 077 expectMissing(mapEntry(k0(), v0())); 078 assertEquals(getNumElements() - 1, multimap().size()); 079 assertFalse(multimap().containsEntry(k0(), v0())); 080 } 081 082 @CollectionSize.Require(absent = ZERO) 083 @MapFeature.Require(SUPPORTS_REMOVE) 084 public void testRemoveAllPropagatesToMultimap() { 085 assertTrue(multimap().entries().removeAll(singleton(mapEntry(k0(), v0())))); 086 expectMissing(mapEntry(k0(), v0())); 087 assertEquals(getNumElements() - 1, multimap().size()); 088 assertFalse(multimap().containsEntry(k0(), v0())); 089 } 090 091 @CollectionSize.Require(absent = ZERO) 092 @MapFeature.Require(SUPPORTS_REMOVE) 093 /* 094 * We are comparing Multimaps of the same type, so as long as they have value collections that 095 * implement equals() (as with ListMultimap or SetMultimap, as opposed to a QueueMultimap or 096 * something), our equality check is value-based. 097 */ 098 @SuppressWarnings("UndefinedEquals") 099 public void testRetainAllPropagatesToMultimap() { 100 multimap().entries().retainAll(singleton(mapEntry(k0(), v0()))); 101 assertEquals(getSubjectGenerator().create(mapEntry(k0(), v0())), multimap()); 102 assertEquals(1, multimap().size()); 103 assertTrue(multimap().containsEntry(k0(), v0())); 104 } 105 106 @CollectionSize.Require(ONE) 107 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 108 public void testIteratorRemovePropagatesToMultimap() { 109 Iterator<Entry<K, V>> iterator = multimap().entries().iterator(); 110 assertEquals(mapEntry(k0(), v0()), iterator.next()); 111 iterator.remove(); 112 assertTrue(multimap().isEmpty()); 113 } 114 115 @CollectionSize.Require(absent = ZERO) 116 @MapFeature.Require(SUPPORTS_REMOVE) 117 public void testEntriesRemainValidAfterRemove() { 118 Iterator<Entry<K, V>> iterator = multimap().entries().iterator(); 119 Entry<K, V> entry = iterator.next(); 120 K key = entry.getKey(); 121 V value = entry.getValue(); 122 multimap().removeAll(key); 123 assertEquals(key, entry.getKey()); 124 assertEquals(value, entry.getValue()); 125 } 126}