1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.common.collect.testing.google; 17 18 import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder; 19 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 20 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 22 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 23 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 24 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 25 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 26 import static org.truth0.Truth.ASSERT; 27 28 import com.google.common.annotations.GwtCompatible; 29 import com.google.common.collect.Multimap; 30 import com.google.common.collect.testing.Helpers; 31 import com.google.common.collect.testing.features.CollectionSize; 32 import com.google.common.collect.testing.features.MapFeature; 33 34 import java.util.Collection; 35 import java.util.Collections; 36 37 /** 38 * Tests for {@link Multimap#get(Object)}. 39 * 40 * @author Louis Wasserman 41 */ 42 @GwtCompatible 43 public class MultimapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { testGetEmpty()44 public void testGetEmpty() { 45 Collection<V> result = multimap().get(sampleKeys().e3); 46 assertTrue(result.isEmpty()); 47 assertEquals(0, result.size()); 48 } 49 50 @CollectionSize.Require(absent = ZERO) testGetNonEmpty()51 public void testGetNonEmpty() { 52 Collection<V> result = multimap().get(sampleKeys().e0); 53 assertFalse(result.isEmpty()); 54 assertContentsAnyOrder(result, sampleValues().e0); 55 } 56 57 @CollectionSize.Require(SEVERAL) testGetMultiple()58 public void testGetMultiple() { 59 resetContainer( 60 Helpers.mapEntry(sampleKeys().e0, sampleValues().e0), 61 Helpers.mapEntry(sampleKeys().e0, sampleValues().e1), 62 Helpers.mapEntry(sampleKeys().e0, sampleValues().e2)); 63 assertGet(sampleKeys().e0, 64 sampleValues().e0, 65 sampleValues().e1, 66 sampleValues().e2); 67 } 68 testGetAbsentKey()69 public void testGetAbsentKey() { 70 assertGet(sampleKeys().e4); 71 } 72 73 @CollectionSize.Require(SEVERAL) 74 @MapFeature.Require(SUPPORTS_REMOVE) testPropagatesRemoveToMultimap()75 public void testPropagatesRemoveToMultimap() { 76 resetContainer( 77 Helpers.mapEntry(sampleKeys().e0, sampleValues().e0), 78 Helpers.mapEntry(sampleKeys().e0, sampleValues().e3), 79 Helpers.mapEntry(sampleKeys().e0, sampleValues().e2)); 80 Collection<V> result = multimap().get(sampleKeys().e0); 81 assertTrue(result.remove(sampleValues().e0)); 82 assertFalse(multimap().containsEntry(sampleKeys().e0, sampleValues().e0)); 83 assertEquals(2, multimap().size()); 84 } 85 86 @CollectionSize.Require(absent = ZERO) 87 @MapFeature.Require(SUPPORTS_REMOVE) testPropagatesRemoveLastElementToMultimap()88 public void testPropagatesRemoveLastElementToMultimap() { 89 Collection<V> result = multimap().get(sampleKeys().e0); 90 assertTrue(result.remove(sampleValues().e0)); 91 assertGet(sampleKeys().e0); 92 } 93 94 @MapFeature.Require(SUPPORTS_PUT) testPropagatesAddToMultimap()95 public void testPropagatesAddToMultimap() { 96 Collection<V> result = multimap().get(sampleKeys().e0); 97 assertTrue(result.add(sampleValues().e3)); 98 assertTrue(multimap().containsKey(sampleKeys().e0)); 99 assertEquals(getNumElements() + 1, multimap().size()); 100 assertTrue(multimap().containsEntry(sampleKeys().e0, sampleValues().e3)); 101 } 102 103 @MapFeature.Require(SUPPORTS_PUT) testPropagatesAddAllToMultimap()104 public void testPropagatesAddAllToMultimap() { 105 Collection<V> result = multimap().get(sampleKeys().e0); 106 assertTrue(result.addAll(Collections.singletonList(sampleValues().e3))); 107 assertTrue(multimap().containsKey(sampleKeys().e0)); 108 assertEquals(getNumElements() + 1, multimap().size()); 109 assertTrue(multimap().containsEntry(sampleKeys().e0, sampleValues().e3)); 110 } 111 112 @CollectionSize.Require(absent = ZERO) 113 @MapFeature.Require({ SUPPORTS_REMOVE, SUPPORTS_PUT }) testPropagatesRemoveLastThenAddToMultimap()114 public void testPropagatesRemoveLastThenAddToMultimap() { 115 int oldSize = getNumElements(); 116 117 K k0 = sampleKeys().e0; 118 V v0 = sampleValues().e0; 119 120 Collection<V> result = multimap().get(k0); 121 assertTrue(result.remove(v0)); 122 123 assertFalse(multimap().containsKey(k0)); 124 assertFalse(multimap().containsEntry(k0, v0)); 125 ASSERT.that(result).isEmpty(); 126 127 V v1 = sampleValues().e1; 128 V v2 = sampleValues().e2; 129 130 assertTrue(result.add(v1)); 131 assertTrue(result.add(v2)); 132 133 ASSERT.that(result).has().exactly(v1, v2); 134 ASSERT.that(multimap().get(k0)).has().exactly(v1, v2); 135 assertTrue(multimap().containsKey(k0)); 136 assertFalse(multimap().containsEntry(k0, v0)); 137 assertTrue(multimap().containsEntry(k0, v2)); 138 assertEquals(oldSize + 1, multimap().size()); 139 } 140 141 @MapFeature.Require(ALLOWS_NULL_KEYS) 142 @CollectionSize.Require(absent = ZERO) testGetNullPresent()143 public void testGetNullPresent() { 144 initMultimapWithNullKey(); 145 ASSERT.that(multimap().get(null)).has().item(getValueForNullKey()); 146 } 147 148 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) testGetNullAbsent()149 public void testGetNullAbsent() { 150 ASSERT.that(multimap().get(null)).isEmpty(); 151 } 152 153 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) testGetNullForbidden()154 public void testGetNullForbidden() { 155 try { 156 multimap().get(null); 157 fail("Expected NullPointerException"); 158 } catch (NullPointerException expected) { 159 // success 160 } 161 } 162 163 @MapFeature.Require(ALLOWS_NULL_VALUES) 164 @CollectionSize.Require(absent = ZERO) testGetWithNullValue()165 public void testGetWithNullValue() { 166 initMultimapWithNullValue(); 167 ASSERT.that(multimap().get(getKeyForNullValue())) 168 .has().item(null); 169 } 170 } 171