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.Iterables.getOnlyElement; 018import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder; 019import static com.google.common.collect.testing.Helpers.assertContentsInOrder; 020import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 021import static com.google.common.collect.testing.Helpers.mapEntry; 022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 023import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 024import static com.google.common.collect.testing.features.CollectionSize.ZERO; 025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 026import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 027import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 028import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 029import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows; 030 031import com.google.common.annotations.GwtCompatible; 032import com.google.common.collect.Multimap; 033import com.google.common.collect.testing.features.CollectionFeature; 034import com.google.common.collect.testing.features.CollectionSize; 035import com.google.common.collect.testing.features.MapFeature; 036import java.util.ArrayList; 037import java.util.Collection; 038import java.util.Iterator; 039import java.util.List; 040import java.util.Map.Entry; 041import java.util.Set; 042import org.junit.Ignore; 043 044/** 045 * Tests for {@link Multimap#asMap}. 046 * 047 * @author Louis Wasserman 048 */ 049@GwtCompatible 050@Ignore("test runners must not instantiate and run this directly, only via suites we build") 051// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 052@SuppressWarnings("JUnit4ClassUsedInJUnit3") 053public class MultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 054 public void testAsMapGet() { 055 for (K key : sampleKeys()) { 056 List<V> expectedValues = new ArrayList<>(); 057 for (Entry<K, V> entry : getSampleElements()) { 058 if (entry.getKey().equals(key)) { 059 expectedValues.add(entry.getValue()); 060 } 061 } 062 063 Collection<V> collection = multimap().asMap().get(key); 064 if (expectedValues.isEmpty()) { 065 assertNull(collection); 066 } else { 067 assertEqualIgnoringOrder(expectedValues, collection); 068 } 069 } 070 } 071 072 @CollectionSize.Require(absent = ZERO) 073 @MapFeature.Require(ALLOWS_NULL_KEYS) 074 public void testAsMapGetNullKeyPresent() { 075 initMultimapWithNullKey(); 076 assertContentsAnyOrder(multimap().asMap().get(null), getValueForNullKey()); 077 } 078 079 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 080 public void testAsMapGetNullKeyAbsent() { 081 assertNull(multimap().asMap().get(null)); 082 } 083 084 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) 085 public void testAsMapGetNullKeyUnsupported() { 086 assertThrows(NullPointerException.class, () -> multimap().asMap().get(null)); 087 } 088 089 @CollectionSize.Require(absent = ZERO) 090 @MapFeature.Require(SUPPORTS_REMOVE) 091 public void testAsMapRemove() { 092 assertContentsInOrder(multimap().asMap().remove(k0()), v0()); 093 assertGet(k0()); 094 assertEquals(getNumElements() - 1, multimap().size()); 095 } 096 097 @CollectionSize.Require(SEVERAL) 098 @MapFeature.Require(SUPPORTS_PUT) 099 public void testAsMapEntrySetReflectsPutSameKey() { 100 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3())); 101 102 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 103 Collection<V> valueCollection = getOnlyElement(asMapEntrySet).getValue(); 104 assertContentsAnyOrder(valueCollection, v0(), v3()); 105 assertTrue(multimap().put(k0(), v4())); 106 assertContentsAnyOrder(valueCollection, v0(), v3(), v4()); 107 } 108 109 @CollectionSize.Require(SEVERAL) 110 @MapFeature.Require(SUPPORTS_PUT) 111 public void testAsMapEntrySetReflectsPutDifferentKey() { 112 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3())); 113 114 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 115 assertTrue(multimap().put(k1(), v4())); 116 assertEquals(2, asMapEntrySet.size()); 117 } 118 119 @CollectionSize.Require(SEVERAL) 120 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 121 public void testAsMapEntrySetRemovePropagatesToMultimap() { 122 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3())); 123 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 124 Entry<K, Collection<V>> asMapEntry0 = getOnlyElement(asMapEntrySet); 125 assertTrue(multimap().put(k1(), v4())); 126 assertTrue(asMapEntrySet.remove(asMapEntry0)); 127 assertEquals(1, multimap().size()); 128 assertContentsInOrder(multimap().keySet(), k1()); 129 } 130 131 @CollectionSize.Require(SEVERAL) 132 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 133 public void testAsMapEntrySetIteratorRemovePropagatesToMultimap() { 134 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3())); 135 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 136 Iterator<Entry<K, Collection<V>>> asMapEntryItr = asMapEntrySet.iterator(); 137 asMapEntryItr.next(); 138 asMapEntryItr.remove(); 139 assertTrue(multimap().isEmpty()); 140 } 141}