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; 037import org.junit.Ignore; 038 039/** 040 * Tester for {@code Multimap.entries}. 041 * 042 * @author Louis Wasserman 043 */ 044@GwtCompatible 045@Ignore("test runners must not instantiate and run this directly, only via suites we build") 046// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 047@SuppressWarnings("JUnit4ClassUsedInJUnit3") 048public class MultimapEntriesTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 049 public void testEntries() { 050 assertEqualIgnoringOrder(getSampleElements(), multimap().entries()); 051 } 052 053 @CollectionSize.Require(absent = ZERO) 054 @MapFeature.Require(ALLOWS_NULL_KEYS) 055 public void testContainsEntryWithNullKeyPresent() { 056 initMultimapWithNullKey(); 057 assertContains(multimap().entries(), mapEntry((K) null, getValueForNullKey())); 058 } 059 060 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 061 public void testContainsEntryWithNullKeyAbsent() { 062 assertFalse(multimap().entries().contains(mapEntry(null, v0()))); 063 } 064 065 @CollectionSize.Require(absent = ZERO) 066 @MapFeature.Require(ALLOWS_NULL_VALUES) 067 public void testContainsEntryWithNullValuePresent() { 068 initMultimapWithNullValue(); 069 assertContains(multimap().entries(), mapEntry(getKeyForNullValue(), (V) null)); 070 } 071 072 @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES) 073 public void testContainsEntryWithNullValueAbsent() { 074 assertFalse(multimap().entries().contains(mapEntry(k0(), null))); 075 } 076 077 @CollectionSize.Require(absent = ZERO) 078 @MapFeature.Require(SUPPORTS_REMOVE) 079 public void testRemovePropagatesToMultimap() { 080 assertTrue(multimap().entries().remove(mapEntry(k0(), v0()))); 081 expectMissing(mapEntry(k0(), v0())); 082 assertEquals(getNumElements() - 1, multimap().size()); 083 assertFalse(multimap().containsEntry(k0(), v0())); 084 } 085 086 @CollectionSize.Require(absent = ZERO) 087 @MapFeature.Require(SUPPORTS_REMOVE) 088 public void testRemoveAllPropagatesToMultimap() { 089 assertTrue(multimap().entries().removeAll(singleton(mapEntry(k0(), v0())))); 090 expectMissing(mapEntry(k0(), v0())); 091 assertEquals(getNumElements() - 1, multimap().size()); 092 assertFalse(multimap().containsEntry(k0(), v0())); 093 } 094 095 @CollectionSize.Require(absent = ZERO) 096 @MapFeature.Require(SUPPORTS_REMOVE) 097 /* 098 * We are comparing Multimaps of the same type, so as long as they have value collections that 099 * implement equals() (as with ListMultimap or SetMultimap, as opposed to a QueueMultimap or 100 * something), our equality check is value-based. 101 */ 102 @SuppressWarnings("UndefinedEquals") 103 public void testRetainAllPropagatesToMultimap() { 104 multimap().entries().retainAll(singleton(mapEntry(k0(), v0()))); 105 assertEquals(getSubjectGenerator().create(mapEntry(k0(), v0())), multimap()); 106 assertEquals(1, multimap().size()); 107 assertTrue(multimap().containsEntry(k0(), v0())); 108 } 109 110 @CollectionSize.Require(ONE) 111 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 112 public void testIteratorRemovePropagatesToMultimap() { 113 Iterator<Entry<K, V>> iterator = multimap().entries().iterator(); 114 assertEquals(mapEntry(k0(), v0()), iterator.next()); 115 iterator.remove(); 116 assertTrue(multimap().isEmpty()); 117 } 118 119 @CollectionSize.Require(absent = ZERO) 120 @MapFeature.Require(SUPPORTS_REMOVE) 121 public void testEntriesRemainValidAfterRemove() { 122 Iterator<Entry<K, V>> iterator = multimap().entries().iterator(); 123 Entry<K, V> entry = iterator.next(); 124 K key = entry.getKey(); 125 V value = entry.getValue(); 126 multimap().removeAll(key); 127 assertEquals(key, entry.getKey()); 128 assertEquals(value, entry.getValue()); 129 } 130}