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 com.google.common.collect.testing.google.ReflectionFreeAssertThrows.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; 035import org.junit.Ignore; 036 037/** 038 * Tests for {@code Multimap.asMap().get(Object)}. 039 * 040 * @author Louis Wasserman 041 */ 042@GwtCompatible 043@Ignore("test runners must not instantiate and run this directly, only via suites we build") 044// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 045@SuppressWarnings("JUnit4ClassUsedInJUnit3") 046public class MultimapAsMapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 047 048 @CollectionSize.Require(SEVERAL) 049 @MapFeature.Require(SUPPORTS_REMOVE) 050 public void testPropagatesRemoveToMultimap() { 051 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v3()), mapEntry(k0(), v2())); 052 Collection<V> result = multimap().asMap().get(k0()); 053 assertTrue(result.remove(v0())); 054 assertFalse(multimap().containsEntry(k0(), v0())); 055 assertEquals(2, multimap().size()); 056 } 057 058 @CollectionSize.Require(absent = ZERO) 059 @MapFeature.Require(SUPPORTS_REMOVE) 060 public void testPropagatesRemoveLastElementToMultimap() { 061 Collection<V> result = multimap().asMap().get(k0()); 062 assertTrue(result.remove(v0())); 063 assertGet(k0()); 064 } 065 066 @CollectionSize.Require(absent = ZERO) 067 @MapFeature.Require(SUPPORTS_REMOVE) 068 public void testPropagatesClearToMultimap() { 069 Collection<V> result = multimap().asMap().get(k0()); 070 result.clear(); 071 assertGet(k0()); 072 assertEmpty(result); 073 } 074 075 @CollectionSize.Require(absent = ZERO) 076 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 077 public void testAddNullValue() { 078 Collection<V> result = multimap().asMap().get(k0()); 079 assertTrue(result.add(null)); 080 assertTrue(multimap().containsEntry(k0(), null)); 081 } 082 083 @CollectionSize.Require(absent = ZERO) 084 @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUE_QUERIES}) 085 public void testRemoveNullValue() { 086 Collection<V> result = multimap().asMap().get(k0()); 087 assertFalse(result.remove(null)); 088 } 089 090 @CollectionSize.Require(absent = ZERO) 091 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 092 public void testAddNullValueUnsupported() { 093 Collection<V> result = multimap().asMap().get(k0()); 094 assertThrows(NullPointerException.class, () -> result.add(null)); 095 } 096 097 @CollectionSize.Require(absent = ZERO) 098 @MapFeature.Require(SUPPORTS_PUT) 099 public void testPropagatesAddToMultimap() { 100 Collection<V> result = multimap().asMap().get(k0()); 101 result.add(v3()); 102 assertContentsAnyOrder(multimap().get(k0()), v0(), v3()); 103 } 104 105 @CollectionSize.Require(absent = ZERO) 106 @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT}) 107 public void testPropagatesRemoveThenAddToMultimap() { 108 int oldSize = getNumElements(); 109 110 Collection<V> result = multimap().asMap().get(k0()); 111 assertTrue(result.remove(v0())); 112 113 assertFalse(multimap().containsKey(k0())); 114 assertFalse(multimap().containsEntry(k0(), v0())); 115 assertEmpty(result); 116 117 assertTrue(result.add(v1())); 118 assertTrue(result.add(v2())); 119 120 assertContentsAnyOrder(result, v1(), v2()); 121 assertContentsAnyOrder(multimap().get(k0()), v1(), v2()); 122 assertTrue(multimap().containsKey(k0())); 123 assertFalse(multimap().containsEntry(k0(), v0())); 124 assertTrue(multimap().containsEntry(k0(), v2())); 125 assertEquals(oldSize + 1, multimap().size()); 126 } 127 128 @CollectionSize.Require(absent = ZERO) 129 @MapFeature.Require(SUPPORTS_REMOVE) 130 public void testReflectsMultimapRemove() { 131 Collection<V> result = multimap().asMap().get(k0()); 132 multimap().removeAll(k0()); 133 assertEmpty(result); 134 } 135}