1 /* 2 * Copyright (C) 2012 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.features.CollectionSize.ZERO; 20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 22 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 23 import static com.google.common.truth.Truth.assertThat; 24 25 import com.google.common.annotations.GwtCompatible; 26 import com.google.common.collect.ImmutableList; 27 import com.google.common.collect.Lists; 28 import com.google.common.collect.Multimap; 29 import com.google.common.collect.testing.Helpers; 30 import com.google.common.collect.testing.features.CollectionSize; 31 import com.google.common.collect.testing.features.MapFeature; 32 33 import java.util.Collection; 34 import java.util.Iterator; 35 import java.util.List; 36 import java.util.Map.Entry; 37 38 /** 39 * Tester for {@link Multimap#put}. 40 * 41 * @author Louis Wasserman 42 */ 43 @GwtCompatible 44 public class MultimapPutTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 45 @MapFeature.Require(absent = SUPPORTS_PUT) testPutUnsupported()46 public void testPutUnsupported() { 47 try { 48 multimap().put(sampleKeys().e3, sampleValues().e3); 49 fail("Expected UnsupportedOperationException"); 50 } catch (UnsupportedOperationException expected) {} 51 } 52 53 @MapFeature.Require(SUPPORTS_PUT) testPutEmpty()54 public void testPutEmpty() { 55 int size = getNumElements(); 56 57 K key = sampleKeys().e3; 58 V value = sampleValues().e3; 59 60 assertGet(key, ImmutableList.<V>of()); 61 62 assertTrue(multimap().put(key, value)); 63 64 assertGet(key, value); 65 assertEquals(size + 1, multimap().size()); 66 } 67 68 @MapFeature.Require(SUPPORTS_PUT) 69 @CollectionSize.Require(absent = ZERO) testPutPresent()70 public void testPutPresent() { 71 int size = getNumElements(); 72 73 K key = sampleKeys().e0; 74 V oldValue = sampleValues().e0; 75 V newValue = sampleValues().e3; 76 77 assertGet(key, oldValue); 78 79 assertTrue(multimap().put(key, newValue)); 80 81 assertGet(key, oldValue, newValue); 82 assertEquals(size + 1, multimap().size()); 83 } 84 85 @MapFeature.Require(SUPPORTS_PUT) testPutTwoElements()86 public void testPutTwoElements() { 87 int size = getNumElements(); 88 89 K key = sampleKeys().e0; 90 V v1 = sampleValues().e3; 91 V v2 = sampleValues().e4; 92 93 List<V> values = Helpers.copyToList(multimap().get(key)); 94 95 assertTrue(multimap().put(key, v1)); 96 assertTrue(multimap().put(key, v2)); 97 98 values.add(v1); 99 values.add(v2); 100 101 assertGet(key, values); 102 assertEquals(size + 2, multimap().size()); 103 } 104 105 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) testPutNullValue_supported()106 public void testPutNullValue_supported() { 107 int size = getNumElements(); 108 109 multimap().put(sampleKeys().e3, null); 110 111 assertGet(sampleKeys().e3, Lists.newArrayList((V) null)); // ImmutableList.of can't take null. 112 assertEquals(size + 1, multimap().size()); 113 } 114 115 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) testPutNullValue_unsupported()116 public void testPutNullValue_unsupported() { 117 try { 118 multimap().put(sampleKeys().e1, null); 119 fail(); 120 } catch (NullPointerException expected) { 121 } 122 123 expectUnchanged(); 124 } 125 126 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) testPutNullKey()127 public void testPutNullKey() { 128 int size = getNumElements(); 129 130 multimap().put(null, sampleValues().e3); 131 132 assertGet(null, sampleValues().e3); 133 assertEquals(size + 1, multimap().size()); 134 } 135 136 @MapFeature.Require(SUPPORTS_PUT) testPutNotPresentKeyPropagatesToGet()137 public void testPutNotPresentKeyPropagatesToGet() { 138 int size = getNumElements(); 139 Collection<V> collection = multimap().get(sampleKeys().e3); 140 assertThat(collection).isEmpty(); 141 multimap().put(sampleKeys().e3, sampleValues().e3); 142 assertThat(collection).has().item(sampleValues().e3); 143 assertEquals(size + 1, multimap().size()); 144 } 145 146 @MapFeature.Require(SUPPORTS_PUT) testPutNotPresentKeyPropagatesToEntries()147 public void testPutNotPresentKeyPropagatesToEntries() { 148 Collection<Entry<K, V>> entries = multimap().entries(); 149 assertFalse(entries.contains(Helpers.mapEntry(sampleKeys().e3, sampleValues().e3))); 150 multimap().put(sampleKeys().e3, sampleValues().e3); 151 assertThat(entries).has().allOf(Helpers.mapEntry(sampleKeys().e3, sampleValues().e3)); 152 } 153 154 @CollectionSize.Require(absent = ZERO) 155 @MapFeature.Require(SUPPORTS_PUT) testPutPresentKeyPropagatesToEntries()156 public void testPutPresentKeyPropagatesToEntries() { 157 Collection<Entry<K, V>> entries = multimap().entries(); 158 assertFalse(entries.contains(Helpers.mapEntry(sampleKeys().e0, sampleValues().e3))); 159 multimap().put(sampleKeys().e0, sampleValues().e3); 160 assertThat(entries).has().allOf(Helpers.mapEntry(sampleKeys().e0, sampleValues().e3)); 161 } 162 163 @MapFeature.Require(SUPPORTS_PUT) 164 @CollectionSize.Require(absent = ZERO) testPutPresentKeyPropagatesToGet()165 public void testPutPresentKeyPropagatesToGet() { 166 List<K> keys = Helpers.copyToList(multimap().keySet()); 167 for (K key : keys) { 168 resetContainer(); 169 170 int size = getNumElements(); 171 172 Collection<V> collection = multimap().get(key); 173 Collection<V> expectedCollection = Helpers.copyToList(collection); 174 175 multimap().put(key, sampleValues().e3); 176 expectedCollection.add(sampleValues().e3); 177 assertThat(collection).has().exactlyAs(expectedCollection); 178 assertEquals(size + 1, multimap().size()); 179 } 180 } 181 182 @MapFeature.Require(SUPPORTS_PUT) 183 @CollectionSize.Require(absent = ZERO) testPutPresentKeyPropagatesToAsMapGet()184 public void testPutPresentKeyPropagatesToAsMapGet() { 185 List<K> keys = Helpers.copyToList(multimap().keySet()); 186 for (K key : keys) { 187 resetContainer(); 188 189 int size = getNumElements(); 190 191 Collection<V> collection = multimap().asMap().get(key); 192 assertNotNull(collection); 193 Collection<V> expectedCollection = Helpers.copyToList(collection); 194 195 multimap().put(key, sampleValues().e3); 196 expectedCollection.add(sampleValues().e3); 197 assertThat(collection).has().exactlyAs(expectedCollection); 198 assertEquals(size + 1, multimap().size()); 199 } 200 } 201 202 @MapFeature.Require(SUPPORTS_PUT) 203 @CollectionSize.Require(absent = ZERO) testPutPresentKeyPropagatesToAsMapEntrySet()204 public void testPutPresentKeyPropagatesToAsMapEntrySet() { 205 List<K> keys = Helpers.copyToList(multimap().keySet()); 206 for (K key : keys) { 207 resetContainer(); 208 209 int size = getNumElements(); 210 211 Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator(); 212 Collection<V> collection = null; 213 while (asMapItr.hasNext()) { 214 Entry<K, Collection<V>> asMapEntry = asMapItr.next(); 215 if (key.equals(asMapEntry.getKey())) { 216 collection = asMapEntry.getValue(); 217 break; 218 } 219 } 220 assertNotNull(collection); 221 Collection<V> expectedCollection = Helpers.copyToList(collection); 222 223 multimap().put(key, sampleValues().e3); 224 expectedCollection.add(sampleValues().e3); 225 assertThat(collection).has().exactlyAs(expectedCollection); 226 assertEquals(size + 1, multimap().size()); 227 } 228 } 229 } 230