001/* 002 * Copyright (C) 2013 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 020import static com.google.common.collect.testing.features.CollectionSize.ONE; 021import static com.google.common.collect.testing.features.CollectionSize.ZERO; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES; 024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 026import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 027 028import com.google.common.annotations.GwtCompatible; 029import com.google.common.annotations.GwtIncompatible; 030import com.google.common.annotations.J2ktIncompatible; 031import com.google.common.collect.testing.AbstractMapTester; 032import com.google.common.collect.testing.Helpers; 033import com.google.common.collect.testing.features.CollectionFeature; 034import com.google.common.collect.testing.features.CollectionSize; 035import com.google.common.collect.testing.features.MapFeature; 036import java.lang.reflect.Method; 037import java.util.Iterator; 038import java.util.Map.Entry; 039import java.util.Set; 040import org.junit.Ignore; 041 042/** 043 * Tests {@link java.util.Map#entrySet}. 044 * 045 * @author Louis Wasserman 046 * @param <K> The key type of the map implementation under test. 047 * @param <V> The value type of the map implementation under test. 048 */ 049@GwtCompatible 050@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 051@SuppressWarnings("JUnit4ClassUsedInJUnit3") 052public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> { 053 private enum IncomparableType { 054 INSTANCE; 055 } 056 057 @CollectionSize.Require(ONE) 058 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 059 public void testEntrySetIteratorRemove() { 060 Set<Entry<K, V>> entrySet = getMap().entrySet(); 061 Iterator<Entry<K, V>> entryItr = entrySet.iterator(); 062 assertEquals(e0(), entryItr.next()); 063 entryItr.remove(); 064 assertTrue(getMap().isEmpty()); 065 assertFalse(entrySet.contains(e0())); 066 } 067 068 public void testContainsEntryWithIncomparableKey() { 069 try { 070 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0()))); 071 } catch (ClassCastException acceptable) { 072 // allowed by the spec 073 } 074 } 075 076 public void testContainsEntryWithIncomparableValue() { 077 try { 078 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE))); 079 } catch (ClassCastException acceptable) { 080 // allowed by the spec 081 } 082 } 083 084 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 085 public void testContainsEntryWithNullKeyAbsent() { 086 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0()))); 087 } 088 089 @CollectionSize.Require(absent = ZERO) 090 @MapFeature.Require(ALLOWS_NULL_KEYS) 091 public void testContainsEntryWithNullKeyPresent() { 092 initMapWithNullKey(); 093 assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey()))); 094 } 095 096 @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES) 097 public void testContainsEntryWithNullValueAbsent() { 098 assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null))); 099 } 100 101 @CollectionSize.Require(absent = ZERO) 102 @MapFeature.Require(ALLOWS_NULL_VALUES) 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) 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) 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) 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 148 public static Method getContainsEntryWithIncomparableKeyMethod() { 149 return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey"); 150 } 151 152 @J2ktIncompatible 153 @GwtIncompatible // reflection 154 public static Method getContainsEntryWithIncomparableValueMethod() { 155 return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue"); 156 } 157 158 @J2ktIncompatible 159 @GwtIncompatible // reflection 160 public static Method getSetValueMethod() { 161 return Helpers.getMethod(MapEntrySetTester.class, "testSetValue"); 162 } 163 164 @J2ktIncompatible 165 @GwtIncompatible // reflection 166 public static Method getSetValueWithNullValuesPresentMethod() { 167 return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent"); 168 } 169 170 @J2ktIncompatible 171 @GwtIncompatible // reflection 172 public static Method getSetValueWithNullValuesAbsentMethod() { 173 return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent"); 174 } 175}