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