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. 045@SuppressWarnings("JUnit4ClassUsedInJUnit3") 046public class MultimapGetTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 047 public void testGetEmpty() { 048 Collection<V> result = multimap().get(k3()); 049 assertEmpty(result); 050 assertEquals(0, result.size()); 051 } 052 053 @CollectionSize.Require(absent = ZERO) 054 public void testGetNonEmpty() { 055 Collection<V> result = multimap().get(k0()); 056 assertFalse(result.isEmpty()); 057 assertContentsAnyOrder(result, v0()); 058 } 059 060 @CollectionSize.Require(SEVERAL) 061 public void testGetMultiple() { 062 resetContainer( 063 Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k0(), v2())); 064 assertGet(k0(), v0(), v1(), v2()); 065 } 066 067 public void testGetAbsentKey() { 068 assertGet(k4()); 069 } 070 071 @CollectionSize.Require(SEVERAL) 072 @MapFeature.Require(SUPPORTS_REMOVE) 073 public void testPropagatesRemoveToMultimap() { 074 resetContainer( 075 Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3()), Helpers.mapEntry(k0(), v2())); 076 Collection<V> result = multimap().get(k0()); 077 assertTrue(result.remove(v0())); 078 assertFalse(multimap().containsEntry(k0(), v0())); 079 assertEquals(2, multimap().size()); 080 } 081 082 @CollectionSize.Require(absent = ZERO) 083 @MapFeature.Require(SUPPORTS_REMOVE) 084 public void testPropagatesRemoveLastElementToMultimap() { 085 Collection<V> result = multimap().get(k0()); 086 assertTrue(result.remove(v0())); 087 assertGet(k0()); 088 } 089 090 @MapFeature.Require(SUPPORTS_PUT) 091 public void testPropagatesAddToMultimap() { 092 Collection<V> result = multimap().get(k0()); 093 assertTrue(result.add(v3())); 094 assertTrue(multimap().containsKey(k0())); 095 assertEquals(getNumElements() + 1, multimap().size()); 096 assertTrue(multimap().containsEntry(k0(), v3())); 097 } 098 099 @MapFeature.Require(SUPPORTS_PUT) 100 public void testPropagatesAddAllToMultimap() { 101 Collection<V> result = multimap().get(k0()); 102 assertTrue(result.addAll(Collections.singletonList(v3()))); 103 assertTrue(multimap().containsKey(k0())); 104 assertEquals(getNumElements() + 1, multimap().size()); 105 assertTrue(multimap().containsEntry(k0(), v3())); 106 } 107 108 @CollectionSize.Require(absent = ZERO) 109 @MapFeature.Require({SUPPORTS_REMOVE, SUPPORTS_PUT}) 110 public void testPropagatesRemoveLastThenAddToMultimap() { 111 int oldSize = getNumElements(); 112 113 Collection<V> result = multimap().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 @MapFeature.Require(ALLOWS_NULL_KEYS) 132 @CollectionSize.Require(absent = ZERO) 133 public void testGetNullPresent() { 134 initMultimapWithNullKey(); 135 assertContains(multimap().get(null), getValueForNullKey()); 136 } 137 138 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 139 public void testGetNullAbsent() { 140 assertEmpty(multimap().get(null)); 141 } 142 143 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) 144 public void testGetNullForbidden() { 145 try { 146 multimap().get(null); 147 fail("Expected NullPointerException"); 148 } catch (NullPointerException expected) { 149 // success 150 } 151 } 152 153 @MapFeature.Require(ALLOWS_NULL_VALUES) 154 @CollectionSize.Require(absent = ZERO) 155 public void testGetWithNullValue() { 156 initMultimapWithNullValue(); 157 assertContains(multimap().get(getKeyForNullValue()), null); 158 } 159}