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