Coverage Summary for Class: MultimapAsMapTester (com.google.common.collect.testing.google)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| MultimapAsMapTester | 0% (0/1) | 0% (0/10) | 0% (0/54) |
1 /* 2 * Copyright (C) 2013 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.collect.testing.google; 16 17 import static com.google.common.collect.testing.Helpers.assertContentsAnyOrder; 18 import static com.google.common.collect.testing.Helpers.assertContentsInOrder; 19 import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 20 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 21 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 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_KEY_QUERIES; 25 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 26 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 27 28 import com.google.common.annotations.GwtCompatible; 29 import com.google.common.collect.Iterables; 30 import com.google.common.collect.Multimap; 31 import com.google.common.collect.testing.Helpers; 32 import com.google.common.collect.testing.features.CollectionFeature; 33 import com.google.common.collect.testing.features.CollectionSize; 34 import com.google.common.collect.testing.features.MapFeature; 35 import java.util.ArrayList; 36 import java.util.Collection; 37 import java.util.Iterator; 38 import java.util.List; 39 import java.util.Map.Entry; 40 import java.util.Set; 41 import org.junit.Ignore; 42 43 /** 44 * Tests for {@link Multimap#asMap}. 45 * 46 * @author Louis Wasserman 47 */ 48 @GwtCompatible 49 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 50 public class MultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 51 public void testAsMapGet() { 52 for (K key : sampleKeys()) { 53 List<V> expectedValues = new ArrayList<>(); 54 for (Entry<K, V> entry : getSampleElements()) { 55 if (entry.getKey().equals(key)) { 56 expectedValues.add(entry.getValue()); 57 } 58 } 59 60 Collection<V> collection = multimap().asMap().get(key); 61 if (expectedValues.isEmpty()) { 62 assertNull(collection); 63 } else { 64 assertEqualIgnoringOrder(expectedValues, collection); 65 } 66 } 67 } 68 69 @CollectionSize.Require(absent = ZERO) 70 @MapFeature.Require(ALLOWS_NULL_KEYS) 71 public void testAsMapGetNullKeyPresent() { 72 initMultimapWithNullKey(); 73 assertContentsAnyOrder(multimap().asMap().get(null), getValueForNullKey()); 74 } 75 76 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 77 public void testAsMapGetNullKeyAbsent() { 78 assertNull(multimap().asMap().get(null)); 79 } 80 81 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) 82 public void testAsMapGetNullKeyUnsupported() { 83 try { 84 multimap().asMap().get(null); 85 fail("Expected NullPointerException"); 86 } catch (NullPointerException expected) { 87 } 88 } 89 90 @CollectionSize.Require(absent = ZERO) 91 @MapFeature.Require(SUPPORTS_REMOVE) 92 public void testAsMapRemove() { 93 assertContentsInOrder(multimap().asMap().remove(k0()), v0()); 94 assertGet(k0()); 95 assertEquals(getNumElements() - 1, multimap().size()); 96 } 97 98 @CollectionSize.Require(SEVERAL) 99 @MapFeature.Require(SUPPORTS_PUT) 100 public void testAsMapEntrySetReflectsPutSameKey() { 101 resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3())); 102 103 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 104 Collection<V> valueCollection = Iterables.getOnlyElement(asMapEntrySet).getValue(); 105 assertContentsAnyOrder(valueCollection, v0(), v3()); 106 assertTrue(multimap().put(k0(), v4())); 107 assertContentsAnyOrder(valueCollection, v0(), v3(), v4()); 108 } 109 110 @CollectionSize.Require(SEVERAL) 111 @MapFeature.Require(SUPPORTS_PUT) 112 public void testAsMapEntrySetReflectsPutDifferentKey() { 113 resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3())); 114 115 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 116 assertTrue(multimap().put(k1(), v4())); 117 assertEquals(2, asMapEntrySet.size()); 118 } 119 120 @CollectionSize.Require(SEVERAL) 121 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 122 public void testAsMapEntrySetRemovePropagatesToMultimap() { 123 resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3())); 124 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 125 Entry<K, Collection<V>> asMapEntry0 = Iterables.getOnlyElement(asMapEntrySet); 126 assertTrue(multimap().put(k1(), v4())); 127 assertTrue(asMapEntrySet.remove(asMapEntry0)); 128 assertEquals(1, multimap().size()); 129 assertContentsInOrder(multimap().keySet(), k1()); 130 } 131 132 @CollectionSize.Require(SEVERAL) 133 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 134 public void testAsMapEntrySetIteratorRemovePropagatesToMultimap() { 135 resetContainer(Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v3())); 136 Set<Entry<K, Collection<V>>> asMapEntrySet = multimap().asMap().entrySet(); 137 Iterator<Entry<K, Collection<V>>> asMapEntryItr = asMapEntrySet.iterator(); 138 asMapEntryItr.next(); 139 asMapEntryItr.remove(); 140 assertTrue(multimap().isEmpty()); 141 } 142 }