1 /* 2 * Copyright (C) 2013 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 17 package com.google.common.collect.testing.google; 18 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_VALUES; 22 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 23 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 24 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 25 import static com.google.common.truth.Truth.assertThat; 26 27 import com.google.common.annotations.GwtCompatible; 28 import com.google.common.collect.Multimap; 29 import com.google.common.collect.testing.Helpers; 30 import com.google.common.collect.testing.features.CollectionSize; 31 import com.google.common.collect.testing.features.MapFeature; 32 33 import java.util.Collection; 34 35 /** 36 * Tests for {@code Multimap.asMap().get(Object)}. 37 * 38 * @author Louis Wasserman 39 */ 40 @GwtCompatible 41 public class MultimapAsMapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 42 43 @CollectionSize.Require(SEVERAL) 44 @MapFeature.Require(SUPPORTS_REMOVE) testPropagatesRemoveToMultimap()45 public void testPropagatesRemoveToMultimap() { 46 resetContainer( 47 Helpers.mapEntry(sampleKeys().e0, sampleValues().e0), 48 Helpers.mapEntry(sampleKeys().e0, sampleValues().e3), 49 Helpers.mapEntry(sampleKeys().e0, sampleValues().e2)); 50 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 51 assertTrue(result.remove(sampleValues().e0)); 52 assertFalse(multimap().containsEntry(sampleKeys().e0, sampleValues().e0)); 53 assertEquals(2, multimap().size()); 54 } 55 56 @CollectionSize.Require(absent = ZERO) 57 @MapFeature.Require(SUPPORTS_REMOVE) testPropagatesRemoveLastElementToMultimap()58 public void testPropagatesRemoveLastElementToMultimap() { 59 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 60 assertTrue(result.remove(sampleValues().e0)); 61 assertGet(sampleKeys().e0); 62 } 63 64 @CollectionSize.Require(absent = ZERO) 65 @MapFeature.Require(SUPPORTS_REMOVE) testPropagatesClearToMultimap()66 public void testPropagatesClearToMultimap() { 67 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 68 result.clear(); 69 assertGet(sampleKeys().e0); 70 assertThat(result).isEmpty(); 71 } 72 73 @CollectionSize.Require(absent = ZERO) 74 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) testAddNullValue()75 public void testAddNullValue() { 76 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 77 assertTrue(result.add(null)); 78 assertTrue(multimap().containsEntry(sampleKeys().e0, null)); 79 } 80 81 @CollectionSize.Require(absent = ZERO) 82 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES}) testRemoveNullValue()83 public void testRemoveNullValue() { 84 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 85 assertFalse(result.remove(null)); 86 } 87 88 @CollectionSize.Require(absent = ZERO) 89 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) testAddNullValueUnsupported()90 public void testAddNullValueUnsupported() { 91 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 92 try { 93 result.add(null); 94 fail("Expected NullPointerException"); 95 } catch (NullPointerException expected) {} 96 } 97 98 @CollectionSize.Require(absent = ZERO) 99 @MapFeature.Require(SUPPORTS_PUT) testPropagatesAddToMultimap()100 public void testPropagatesAddToMultimap() { 101 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 102 result.add(sampleValues().e3); 103 assertThat(multimap().get(sampleKeys().e0)) 104 .has().exactly(sampleValues().e0, sampleValues().e3); 105 } 106 107 @CollectionSize.Require(absent = ZERO) 108 @MapFeature.Require({ SUPPORTS_REMOVE, SUPPORTS_PUT }) testPropagatesRemoveThenAddToMultimap()109 public void testPropagatesRemoveThenAddToMultimap() { 110 int oldSize = getNumElements(); 111 112 K k0 = sampleKeys().e0; 113 V v0 = sampleValues().e0; 114 115 Collection<V> result = multimap().asMap().get(k0); 116 assertTrue(result.remove(v0)); 117 118 assertFalse(multimap().containsKey(k0)); 119 assertFalse(multimap().containsEntry(k0, v0)); 120 assertThat(result).isEmpty(); 121 122 V v1 = sampleValues().e1; 123 V v2 = sampleValues().e2; 124 125 assertTrue(result.add(v1)); 126 assertTrue(result.add(v2)); 127 128 assertThat(result).has().exactly(v1, v2); 129 assertThat(multimap().get(k0)).has().exactly(v1, v2); 130 assertTrue(multimap().containsKey(k0)); 131 assertFalse(multimap().containsEntry(k0, v0)); 132 assertTrue(multimap().containsEntry(k0, v2)); 133 assertEquals(oldSize + 1, multimap().size()); 134 } 135 136 @CollectionSize.Require(absent = ZERO) 137 @MapFeature.Require(SUPPORTS_REMOVE) testReflectsMultimapRemove()138 public void testReflectsMultimapRemove() { 139 Collection<V> result = multimap().asMap().get(sampleKeys().e0); 140 multimap().removeAll(sampleKeys().e0); 141 assertThat(result).isEmpty(); 142 } 143 } 144