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.base.Preconditions.checkState; 019import static com.google.common.collect.testing.Helpers.assertContainsAllOf; 020import static com.google.common.collect.testing.features.CollectionSize.ZERO; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 024import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows; 025import static java.util.Collections.singletonList; 026 027import com.google.common.annotations.GwtCompatible; 028import com.google.common.collect.ImmutableSet; 029import com.google.common.collect.Iterators; 030import com.google.common.collect.Lists; 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 java.util.Collections; 036import java.util.Iterator; 037import org.junit.Ignore; 038 039/** 040 * Tests for {@link Multimap#putAll(Object, Iterable)}. 041 * 042 * @author Louis Wasserman 043 */ 044@GwtCompatible 045@Ignore("test runners must not instantiate and run this directly, only via suites we build") 046// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 047@SuppressWarnings("JUnit4ClassUsedInJUnit3") 048public class MultimapPutIterableTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 049 @CollectionSize.Require(absent = ZERO) 050 @MapFeature.Require(SUPPORTS_PUT) 051 public void testPutAllNonEmptyIterableOnPresentKey() { 052 assertTrue( 053 multimap() 054 .putAll( 055 k0(), 056 new Iterable<V>() { 057 @Override 058 public Iterator<V> iterator() { 059 return Lists.newArrayList(v3(), v4()).iterator(); 060 } 061 })); 062 assertGet(k0(), v0(), v3(), v4()); 063 } 064 065 @CollectionSize.Require(absent = ZERO) 066 @MapFeature.Require(SUPPORTS_PUT) 067 public void testPutAllNonEmptyCollectionOnPresentKey() { 068 assertTrue(multimap().putAll(k0(), Lists.newArrayList(v3(), v4()))); 069 assertGet(k0(), v0(), v3(), v4()); 070 } 071 072 @MapFeature.Require(SUPPORTS_PUT) 073 public void testPutAllNonEmptyIterableOnAbsentKey() { 074 assertTrue( 075 multimap() 076 .putAll( 077 k3(), 078 new Iterable<V>() { 079 @Override 080 public Iterator<V> iterator() { 081 return Lists.newArrayList(v3(), v4()).iterator(); 082 } 083 })); 084 assertGet(k3(), v3(), v4()); 085 } 086 087 @MapFeature.Require(SUPPORTS_PUT) 088 public void testPutAllNonEmptyCollectionOnAbsentKey() { 089 assertTrue(multimap().putAll(k3(), Lists.newArrayList(v3(), v4()))); 090 assertGet(k3(), v3(), v4()); 091 } 092 093 @CollectionSize.Require(absent = ZERO) 094 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 095 public void testPutAllNullValueOnPresentKey_supported() { 096 assertTrue(multimap().putAll(k0(), Lists.newArrayList(v3(), null))); 097 assertGet(k0(), v0(), v3(), null); 098 } 099 100 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 101 public void testPutAllNullValueOnAbsentKey_supported() { 102 assertTrue(multimap().putAll(k3(), Lists.newArrayList(v3(), null))); 103 assertGet(k3(), v3(), null); 104 } 105 106 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 107 public void testPutAllNullValueSingle_unsupported() { 108 multimap().putAll(k1(), Lists.newArrayList((V) null)); 109 expectUnchanged(); 110 } 111 112 // In principle, it would be nice to apply these two tests to keys with existing values, too. 113 114 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 115 public void testPutAllNullValueNullLast_unsupported() { 116 int size = getNumElements(); 117 118 assertThrows( 119 NullPointerException.class, () -> multimap().putAll(k3(), Lists.newArrayList(v3(), null))); 120 121 Collection<V> values = multimap().get(k3()); 122 if (values.size() == 0) { 123 expectUnchanged(); 124 // Be extra thorough in case internal state was corrupted by the expected null. 125 assertEquals(Lists.newArrayList(), Lists.newArrayList(values)); 126 assertEquals(size, multimap().size()); 127 } else { 128 assertEquals(Lists.newArrayList(v3()), Lists.newArrayList(values)); 129 assertEquals(size + 1, multimap().size()); 130 } 131 } 132 133 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 134 public void testPutAllNullValueNullFirst_unsupported() { 135 int size = getNumElements(); 136 137 assertThrows( 138 NullPointerException.class, () -> multimap().putAll(k3(), Lists.newArrayList(null, v3()))); 139 140 /* 141 * In principle, a Multimap implementation could add e3 first before failing on the null. But 142 * that seems unlikely enough to be worth complicating the test over, especially if there's any 143 * chance that a permissive test could mask a bug. 144 */ 145 expectUnchanged(); 146 // Be extra thorough in case internal state was corrupted by the expected null. 147 assertEquals(Lists.newArrayList(), Lists.newArrayList(multimap().get(k3()))); 148 assertEquals(size, multimap().size()); 149 } 150 151 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) 152 public void testPutAllOnPresentNullKey() { 153 assertTrue(multimap().putAll(null, Lists.newArrayList(v3(), v4()))); 154 assertGet(null, v3(), v4()); 155 } 156 157 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS) 158 public void testPutAllNullForbidden() { 159 assertThrows(NullPointerException.class, () -> multimap().putAll(null, singletonList(v3()))); 160 } 161 162 @MapFeature.Require(SUPPORTS_PUT) 163 public void testPutAllEmptyCollectionOnAbsentKey() { 164 assertFalse(multimap().putAll(k3(), Collections.<V>emptyList())); 165 expectUnchanged(); 166 } 167 168 @MapFeature.Require(SUPPORTS_PUT) 169 public void testPutAllEmptyIterableOnAbsentKey() { 170 Iterable<V> iterable = 171 new Iterable<V>() { 172 @Override 173 public Iterator<V> iterator() { 174 return ImmutableSet.<V>of().iterator(); 175 } 176 }; 177 178 assertFalse(multimap().putAll(k3(), iterable)); 179 expectUnchanged(); 180 } 181 182 @CollectionSize.Require(absent = ZERO) 183 @MapFeature.Require(SUPPORTS_PUT) 184 public void testPutAllEmptyIterableOnPresentKey() { 185 multimap().putAll(k0(), Collections.<V>emptyList()); 186 expectUnchanged(); 187 } 188 189 @MapFeature.Require(SUPPORTS_PUT) 190 public void testPutAllOnlyCallsIteratorOnce() { 191 Iterable<V> iterable = 192 new Iterable<V>() { 193 private boolean calledIteratorAlready = false; 194 195 @Override 196 public Iterator<V> iterator() { 197 checkState(!calledIteratorAlready); 198 calledIteratorAlready = true; 199 return Iterators.forArray(v3()); 200 } 201 }; 202 203 multimap().putAll(k3(), iterable); 204 } 205 206 @MapFeature.Require(SUPPORTS_PUT) 207 public void testPutAllPropagatesToGet() { 208 Collection<V> getCollection = multimap().get(k0()); 209 int getCollectionSize = getCollection.size(); 210 assertTrue(multimap().putAll(k0(), Lists.newArrayList(v3(), v4()))); 211 assertEquals(getCollectionSize + 2, getCollection.size()); 212 assertContainsAllOf(getCollection, v3(), v4()); 213 } 214}