MultimapGetTester.java revision 7dd252788645e940eada959bdde927426e2531c9
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 */ 16package com.google.common.collect.testing.google; 17 18import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder; 19import static com.google.common.collect.testing.features.CollectionSize.ZERO; 20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_QUERIES; 22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 24import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 25import static org.truth0.Truth.ASSERT; 26 27import com.google.common.annotations.GwtCompatible; 28import com.google.common.collect.Multimap; 29import com.google.common.collect.testing.features.CollectionSize; 30import com.google.common.collect.testing.features.MapFeature; 31 32import java.util.Collection; 33 34/** 35 * Tests for {@link Multimap#get(Object)}. 36 * 37 * @author Louis Wasserman 38 */ 39@GwtCompatible 40public class MultimapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 41 public void testGetEmpty() { 42 Collection<V> result = multimap().get(sampleKeys().e3); 43 assertTrue(result.isEmpty()); 44 assertEquals(0, result.size()); 45 } 46 47 @CollectionSize.Require(absent = ZERO) 48 public void testGetNonEmpty() { 49 Collection<V> result = multimap().get(sampleKeys().e0); 50 assertFalse(result.isEmpty()); 51 assertContentsAnyOrder(result, sampleValues().e0); 52 } 53 54 @CollectionSize.Require(absent = ZERO) 55 @MapFeature.Require(SUPPORTS_REMOVE) 56 public void testGetPropagatesRemove() { 57 Collection<V> result = multimap().get(sampleKeys().e0); 58 assertTrue(result.remove(sampleValues().e0)); 59 assertFalse(multimap().containsKey(sampleKeys().e0)); 60 assertTrue(result.isEmpty()); 61 assertTrue(multimap().get(sampleKeys().e0).isEmpty()); 62 } 63 64 @CollectionSize.Require(absent = ZERO) 65 @MapFeature.Require({ SUPPORTS_REMOVE, SUPPORTS_PUT }) 66 public void testGetRemoveThenAddPropagates() { 67 int oldSize = getNumElements(); 68 69 K k0 = sampleKeys().e0; 70 V v0 = sampleValues().e0; 71 72 Collection<V> result = multimap().get(k0); 73 assertTrue(result.remove(v0)); 74 75 assertFalse(multimap().containsKey(k0)); 76 assertFalse(multimap().containsEntry(k0, v0)); 77 ASSERT.<V, Collection<V>>that(result).isEmpty(); 78 79 V v1 = sampleValues().e1; 80 V v2 = sampleValues().e2; 81 82 assertTrue(result.add(v1)); 83 assertTrue(result.add(v2)); 84 85 ASSERT.<V, Collection<V>>that(result).has().allOf(v1, v2); 86 ASSERT.<V, Collection<V>>that(multimap().get(k0)).has().allOf(v1, v2); 87 assertTrue(multimap().containsKey(k0)); 88 assertFalse(multimap().containsEntry(k0, v0)); 89 assertTrue(multimap().containsEntry(k0, v2)); 90 assertEquals(oldSize + 1, multimap().size()); 91 } 92 93 @MapFeature.Require(ALLOWS_NULL_KEYS) 94 @CollectionSize.Require(absent = ZERO) 95 public void testGetNullPresent() { 96 initMultimapWithNullKey(); 97 ASSERT.<V, Collection<V>>that(multimap().get(null)).has().item(getValueForNullKey()); 98 } 99 100 @MapFeature.Require(ALLOWS_NULL_QUERIES) 101 public void testGetNullAbsent() { 102 ASSERT.<V, Collection<V>>that(multimap().get(null)).isEmpty(); 103 } 104 105 @MapFeature.Require(absent = ALLOWS_NULL_QUERIES) 106 public void testGetNullForbidden() { 107 try { 108 multimap().get(null); 109 fail("Expected NullPointerException"); 110 } catch (NullPointerException expected) { 111 // success 112 } 113 } 114 115 @MapFeature.Require(ALLOWS_NULL_VALUES) 116 @CollectionSize.Require(absent = ZERO) 117 public void testGetWithNullValue() { 118 initMultimapWithNullValue(); 119 ASSERT.<V, Collection<V>>that(multimap().get(getKeyForNullValue())) 120 .has().item(null); 121 } 122} 123