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