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.Helpers.assertContentsAnyOrder; 20 import static com.google.common.collect.testing.Helpers.assertEmpty; 21 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 22 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 23 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 24 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 25 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 26 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 27 28 import com.google.common.annotations.GwtCompatible; 29 import com.google.common.collect.Multimap; 30 import com.google.common.collect.testing.Helpers; 31 import com.google.common.collect.testing.features.CollectionSize; 32 import com.google.common.collect.testing.features.MapFeature; 33 import java.util.Collection; 34 import org.junit.Ignore; 35 36 /** 37 * Tests for {@code Multimap.asMap().get(Object)}. 38 * 39 * @author Louis Wasserman 40 */ 41 @GwtCompatible 42 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 43 public class MultimapAsMapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 44 45 @CollectionSize.Require(SEVERAL) 46 @MapFeature.Require(SUPPORTS_REMOVE) testPropagatesRemoveToMultimap()47 public void testPropagatesRemoveToMultimap() { 48 resetContainer( 49 Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k0(), v2())); 50 Collection<V> result = multimap().asMap().get(k0()); 51 assertTrue(result.remove(v0())); 52 assertFalse(multimap().containsEntry(k0(), v0())); 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(k0()); 60 assertTrue(result.remove(v0())); 61 assertGet(k0()); 62 } 63 64 @CollectionSize.Require(absent = ZERO) 65 @MapFeature.Require(SUPPORTS_REMOVE) testPropagatesClearToMultimap()66 public void testPropagatesClearToMultimap() { 67 Collection<V> result = multimap().asMap().get(k0()); 68 result.clear(); 69 assertGet(k0()); 70 assertEmpty(result); 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(k0()); 77 assertTrue(result.add(null)); 78 assertTrue(multimap().containsEntry(k0(), 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(k0()); 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(k0()); 92 try { 93 result.add(null); 94 fail("Expected NullPointerException"); 95 } catch (NullPointerException expected) { 96 } 97 } 98 99 @CollectionSize.Require(absent = ZERO) 100 @MapFeature.Require(SUPPORTS_PUT) testPropagatesAddToMultimap()101 public void testPropagatesAddToMultimap() { 102 Collection<V> result = multimap().asMap().get(k0()); 103 result.add(v3()); 104 assertContentsAnyOrder(multimap().get(k0()), v0(), v3()); 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 Collection<V> result = multimap().asMap().get(k0()); 113 assertTrue(result.remove(v0())); 114 115 assertFalse(multimap().containsKey(k0())); 116 assertFalse(multimap().containsEntry(k0(), v0())); 117 assertEmpty(result); 118 119 assertTrue(result.add(v1())); 120 assertTrue(result.add(v2())); 121 122 assertContentsAnyOrder(result, v1(), v2()); 123 assertContentsAnyOrder(multimap().get(k0()), v1(), v2()); 124 assertTrue(multimap().containsKey(k0())); 125 assertFalse(multimap().containsEntry(k0(), v0())); 126 assertTrue(multimap().containsEntry(k0(), v2())); 127 assertEquals(oldSize + 1, multimap().size()); 128 } 129 130 @CollectionSize.Require(absent = ZERO) 131 @MapFeature.Require(SUPPORTS_REMOVE) testReflectsMultimapRemove()132 public void testReflectsMultimapRemove() { 133 Collection<V> result = multimap().asMap().get(k0()); 134 multimap().removeAll(k0()); 135 assertEmpty(result); 136 } 137 } 138