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