001/* 002 * Copyright (C) 2013 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.google; 018 019import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder; 020import static com.google.common.collect.testing.Helpers.assertEmpty; 021import static com.google.common.collect.testing.Helpers.mapEntry; 022import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 023import static com.google.common.collect.testing.features.CollectionSize.ZERO; 024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 026import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 027import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 028import static org.junit.Assert.assertThrows; 029 030import com.google.common.annotations.GwtCompatible; 031import com.google.common.collect.Multimap; 032import com.google.common.collect.testing.features.CollectionSize; 033import com.google.common.collect.testing.features.MapFeature; 034import java.util.Collection; 035 036/** 037 * Tests for {@code Multimap.asMap().get(Object)}. 038 * 039 * @author Louis Wasserman 040 */ 041@GwtCompatible 042public class MultimapAsMapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 043 044 @CollectionSize.Require(SEVERAL) 045 @MapFeature.Require(SUPPORTS_REMOVE) 046 public void testPropagatesRemoveToMultimap() { 047 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3()), mapEntry(k0(), v2())); 048 Collection<V> result = multimap().asMap().get(k0()); 049 assertTrue(result.remove(v0())); 050 assertFalse(multimap().containsEntry(k0(), v0())); 051 assertEquals(2, multimap().size()); 052 } 053 054 @CollectionSize.Require(absent = ZERO) 055 @MapFeature.Require(SUPPORTS_REMOVE) 056 public void testPropagatesRemoveLastElementToMultimap() { 057 Collection<V> result = multimap().asMap().get(k0()); 058 assertTrue(result.remove(v0())); 059 assertGet(k0()); 060 } 061 062 @CollectionSize.Require(absent = ZERO) 063 @MapFeature.Require(SUPPORTS_REMOVE) 064 public void testPropagatesClearToMultimap() { 065 Collection<V> result = multimap().asMap().get(k0()); 066 result.clear(); 067 assertGet(k0()); 068 assertEmpty(result); 069 } 070 071 @CollectionSize.Require(absent = ZERO) 072 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 073 public void testAddNullValue() { 074 Collection<V> result = multimap().asMap().get(k0()); 075 assertTrue(result.add(null)); 076 assertTrue(multimap().containsEntry(k0(), null)); 077 } 078 079 @CollectionSize.Require(absent = ZERO) 080 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES}) 081 public void testRemoveNullValue() { 082 Collection<V> result = multimap().asMap().get(k0()); 083 assertFalse(result.remove(null)); 084 } 085 086 @CollectionSize.Require(absent = ZERO) 087 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 088 public void testAddNullValueUnsupported() { 089 Collection<V> result = multimap().asMap().get(k0()); 090 assertThrows(NullPointerException.class, () -> result.add(null)); 091 } 092 093 @CollectionSize.Require(absent = ZERO) 094 @MapFeature.Require(SUPPORTS_PUT) 095 public void testPropagatesAddToMultimap() { 096 Collection<V> result = multimap().asMap().get(k0()); 097 result.add(v3()); 098 assertContentsAnyOrder(multimap().get(k0()), v0(), v3()); 099 } 100 101 @CollectionSize.Require(absent = ZERO) 102 @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT}) 103 public void testPropagatesRemoveThenAddToMultimap() { 104 int oldSize = getNumElements(); 105 106 Collection<V> result = multimap().asMap().get(k0()); 107 assertTrue(result.remove(v0())); 108 109 assertFalse(multimap().containsKey(k0())); 110 assertFalse(multimap().containsEntry(k0(), v0())); 111 assertEmpty(result); 112 113 assertTrue(result.add(v1())); 114 assertTrue(result.add(v2())); 115 116 assertContentsAnyOrder(result, v1(), v2()); 117 assertContentsAnyOrder(multimap().get(k0()), v1(), v2()); 118 assertTrue(multimap().containsKey(k0())); 119 assertFalse(multimap().containsEntry(k0(), v0())); 120 assertTrue(multimap().containsEntry(k0(), v2())); 121 assertEquals(oldSize + 1, multimap().size()); 122 } 123 124 @CollectionSize.Require(absent = ZERO) 125 @MapFeature.Require(SUPPORTS_REMOVE) 126 public void testReflectsMultimapRemove() { 127 Collection<V> result = multimap().asMap().get(k0()); 128 multimap().removeAll(k0()); 129 assertEmpty(result); 130 } 131}