001/* 002 * Copyright (C) 2012 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 */ 016package com.google.common.collect.testing.google; 017 018import static com.google.common.collect.testing.Helpers.assertContains; 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_KEYS; 024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 026import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 027import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 028 029import com.google.common.annotations.GwtCompatible; 030import com.google.common.collect.Multimap; 031import com.google.common.collect.testing.Helpers; 032import com.google.common.collect.testing.features.CollectionSize; 033import com.google.common.collect.testing.features.MapFeature; 034import java.util.Collection; 035import java.util.Collections; 036import org.junit.Ignore; 037 038/** 039 * Tests for {@link Multimap#get(Object)}. 040 * 041 * @author Louis Wasserman 042 */ 043@GwtCompatible 044@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 045public class MultimapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 046 public void testGetEmpty() { 047 Collection<V> result = multimap().get(k3()); 048 assertEmpty(result); 049 assertEquals(0, result.size()); 050 } 051 052 @CollectionSize.Require(absent = ZERO) 053 public void testGetNonEmpty() { 054 Collection<V> result = multimap().get(k0()); 055 assertFalse(result.isEmpty()); 056 assertContentsAnyOrder(result, v0()); 057 } 058 059 @CollectionSize.Require(SEVERAL) 060 public void testGetMultiple() { 061 resetContainer( 062 Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k0(), v2())); 063 assertGet(k0(), v0(), v1(), v2()); 064 } 065 066 public void testGetAbsentKey() { 067 assertGet(k4()); 068 } 069 070 @CollectionSize.Require(SEVERAL) 071 @MapFeature.Require(SUPPORTS_REMOVE) 072 public void testPropagatesRemoveToMultimap() { 073 resetContainer( 074 Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k0(), v2())); 075 Collection<V> result = multimap().get(k0()); 076 assertTrue(result.remove(v0())); 077 assertFalse(multimap().containsEntry(k0(), v0())); 078 assertEquals(2, multimap().size()); 079 } 080 081 @CollectionSize.Require(absent = ZERO) 082 @MapFeature.Require(SUPPORTS_REMOVE) 083 public void testPropagatesRemoveLastElementToMultimap() { 084 Collection<V> result = multimap().get(k0()); 085 assertTrue(result.remove(v0())); 086 assertGet(k0()); 087 } 088 089 @MapFeature.Require(SUPPORTS_PUT) 090 public void testPropagatesAddToMultimap() { 091 Collection<V> result = multimap().get(k0()); 092 assertTrue(result.add(v3())); 093 assertTrue(multimap().containsKey(k0())); 094 assertEquals(getNumElements() + 1, multimap().size()); 095 assertTrue(multimap().containsEntry(k0(), v3())); 096 } 097 098 @MapFeature.Require(SUPPORTS_PUT) 099 public void testPropagatesAddAllToMultimap() { 100 Collection<V> result = multimap().get(k0()); 101 assertTrue(result.addAll(Collections.singletonList(v3()))); 102 assertTrue(multimap().containsKey(k0())); 103 assertEquals(getNumElements() + 1, multimap().size()); 104 assertTrue(multimap().containsEntry(k0(), v3())); 105 } 106 107 @CollectionSize.Require(absent = ZERO) 108 @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT}) 109 public void testPropagatesRemoveLastThenAddToMultimap() { 110 int oldSize = getNumElements(); 111 112 Collection<V> result = multimap().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 @MapFeature.Require(ALLOWS_NULL_KEYS) 131 @CollectionSize.Require(absent = ZERO) 132 public void testGetNullPresent() { 133 initMultimapWithNullKey(); 134 assertContains(multimap().get(null), getValueForNullKey()); 135 } 136 137 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 138 public void testGetNullAbsent() { 139 assertEmpty(multimap().get(null)); 140 } 141 142 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) 143 public void testGetNullForbidden() { 144 try { 145 multimap().get(null); 146 fail("Expected NullPointerException"); 147 } catch (NullPointerException expected) { 148 // success 149 } 150 } 151 152 @MapFeature.Require(ALLOWS_NULL_VALUES) 153 @CollectionSize.Require(absent = ZERO) 154 public void testGetWithNullValue() { 155 initMultimapWithNullValue(); 156 assertContains(multimap().get(getKeyForNullValue()), null); 157 } 158}