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