1 /* 2 * Copyright (C) 2013 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.testers; 18 19 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 20 import static com.google.common.collect.testing.features.CollectionSize.ONE; 21 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 22 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 23 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 24 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 25 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 26 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 27 28 import com.google.common.annotations.GwtCompatible; 29 import com.google.common.annotations.GwtIncompatible; 30 import com.google.common.annotations.J2ktIncompatible; 31 import com.google.common.collect.testing.AbstractMapTester; 32 import com.google.common.collect.testing.Helpers; 33 import com.google.common.collect.testing.features.CollectionFeature; 34 import com.google.common.collect.testing.features.CollectionSize; 35 import com.google.common.collect.testing.features.MapFeature; 36 import java.lang.reflect.Method; 37 import java.util.Iterator; 38 import java.util.Map.Entry; 39 import java.util.Set; 40 import org.junit.Ignore; 41 42 /** 43 * Tests {@link java.util.Map#entrySet}. 44 * 45 * @author Louis Wasserman 46 * @param <K> The key type of the map implementation under test. 47 * @param <V> The value type of the map implementation under test. 48 */ 49 @GwtCompatible 50 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 51 @SuppressWarnings("JUnit4ClassUsedInJUnit3") 52 public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> { 53 private enum IncomparableType { 54 INSTANCE; 55 } 56 57 @CollectionSize.Require(ONE) 58 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) testEntrySetIteratorRemove()59 public void testEntrySetIteratorRemove() { 60 Set<Entry<K, V>> entrySet = getMap().entrySet(); 61 Iterator<Entry<K, V>> entryItr = entrySet.iterator(); 62 assertEquals(e0(), entryItr.next()); 63 entryItr.remove(); 64 assertTrue(getMap().isEmpty()); 65 assertFalse(entrySet.contains(e0())); 66 } 67 testContainsEntryWithIncomparableKey()68 public void testContainsEntryWithIncomparableKey() { 69 try { 70 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0()))); 71 } catch (ClassCastException acceptable) { 72 // allowed by the spec 73 } 74 } 75 testContainsEntryWithIncomparableValue()76 public void testContainsEntryWithIncomparableValue() { 77 try { 78 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE))); 79 } catch (ClassCastException acceptable) { 80 // allowed by the spec 81 } 82 } 83 84 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) testContainsEntryWithNullKeyAbsent()85 public void testContainsEntryWithNullKeyAbsent() { 86 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0()))); 87 } 88 89 @CollectionSize.Require(absent = ZERO) 90 @MapFeature.Require(ALLOWS_NULL_KEYS) testContainsEntryWithNullKeyPresent()91 public void testContainsEntryWithNullKeyPresent() { 92 initMapWithNullKey(); 93 assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey()))); 94 } 95 96 @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES) testContainsEntryWithNullValueAbsent()97 public void testContainsEntryWithNullValueAbsent() { 98 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null))); 99 } 100 101 @CollectionSize.Require(absent = ZERO) 102 @MapFeature.Require(ALLOWS_NULL_VALUES) testContainsEntryWithNullValuePresent()103 public void testContainsEntryWithNullValuePresent() { 104 initMapWithNullValue(); 105 assertTrue(getMap().entrySet().contains(Helpers.mapEntry(getKeyForNullValue(), null))); 106 } 107 108 @MapFeature.Require(SUPPORTS_PUT) 109 @CollectionSize.Require(absent = ZERO) testSetValue()110 public void testSetValue() { 111 for (Entry<K, V> entry : getMap().entrySet()) { 112 if (entry.getKey().equals(k0())) { 113 assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3())); 114 break; 115 } 116 } 117 expectReplacement(entry(k0(), v3())); 118 } 119 120 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 121 @CollectionSize.Require(absent = ZERO) testSetValueWithNullValuesPresent()122 public void testSetValueWithNullValuesPresent() { 123 for (Entry<K, V> entry : getMap().entrySet()) { 124 if (entry.getKey().equals(k0())) { 125 assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(null)); 126 break; 127 } 128 } 129 expectReplacement(entry(k0(), (V) null)); 130 } 131 132 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 133 @CollectionSize.Require(absent = ZERO) testSetValueWithNullValuesAbsent()134 public void testSetValueWithNullValuesAbsent() { 135 for (Entry<K, V> entry : getMap().entrySet()) { 136 try { 137 entry.setValue(null); 138 fail("Expected NullPointerException"); 139 } catch (NullPointerException exception) { 140 break; 141 } 142 } 143 expectUnchanged(); 144 } 145 146 @J2ktIncompatible 147 @GwtIncompatible // reflection getContainsEntryWithIncomparableKeyMethod()148 public static Method getContainsEntryWithIncomparableKeyMethod() { 149 return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey"); 150 } 151 152 @J2ktIncompatible 153 @GwtIncompatible // reflection getContainsEntryWithIncomparableValueMethod()154 public static Method getContainsEntryWithIncomparableValueMethod() { 155 return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue"); 156 } 157 158 @J2ktIncompatible 159 @GwtIncompatible // reflection getSetValueMethod()160 public static Method getSetValueMethod() { 161 return Helpers.getMethod(MapEntrySetTester.class, "testSetValue"); 162 } 163 164 @J2ktIncompatible 165 @GwtIncompatible // reflection getSetValueWithNullValuesPresentMethod()166 public static Method getSetValueWithNullValuesPresentMethod() { 167 return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent"); 168 } 169 170 @J2ktIncompatible 171 @GwtIncompatible // reflection getSetValueWithNullValuesAbsentMethod()172 public static Method getSetValueWithNullValuesAbsentMethod() { 173 return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent"); 174 } 175 } 176