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.Helpers.getMethod;
020import static com.google.common.collect.testing.Helpers.mapEntry;
021import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
022import static com.google.common.collect.testing.features.CollectionSize.ONE;
023import static com.google.common.collect.testing.features.CollectionSize.ZERO;
024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
025import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
026import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
027import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
028import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
029import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
030
031import com.google.common.annotations.GwtCompatible;
032import com.google.common.annotations.GwtIncompatible;
033import com.google.common.annotations.J2ktIncompatible;
034import com.google.common.collect.testing.AbstractMapTester;
035import com.google.common.collect.testing.features.CollectionFeature;
036import com.google.common.collect.testing.features.CollectionSize;
037import com.google.common.collect.testing.features.MapFeature;
038import java.lang.reflect.Method;
039import java.util.Iterator;
040import java.util.Map.Entry;
041import java.util.Set;
042import org.junit.Ignore;
043
044/**
045 * Tests {@link java.util.Map#entrySet}.
046 *
047 * @author Louis Wasserman
048 * @param <K> The key type of the map implementation under test.
049 * @param <V> The value type of the map implementation under test.
050 */
051@GwtCompatible
052@Ignore("test runners must not instantiate and run this directly, only via suites we build")
053// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
054@SuppressWarnings("JUnit4ClassUsedInJUnit3")
055public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
056  private enum IncomparableType {
057    INSTANCE;
058  }
059
060  @CollectionSize.Require(ONE)
061  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
062  public void testEntrySetIteratorRemove() {
063    Set<Entry<K, V>> entrySet = getMap().entrySet();
064    Iterator<Entry<K, V>> entryItr = entrySet.iterator();
065    assertEquals(e0(), entryItr.next());
066    entryItr.remove();
067    assertTrue(getMap().isEmpty());
068    assertFalse(entrySet.contains(e0()));
069  }
070
071  public void testContainsEntryWithIncomparableKey() {
072    try {
073      assertFalse(getMap().entrySet().contains(mapEntry(IncomparableType.INSTANCE, v0())));
074    } catch (ClassCastException acceptable) {
075      // allowed by the spec
076    }
077  }
078
079  public void testContainsEntryWithIncomparableValue() {
080    try {
081      assertFalse(getMap().entrySet().contains(mapEntry(k0(), IncomparableType.INSTANCE)));
082    } catch (ClassCastException acceptable) {
083      // allowed by the spec
084    }
085  }
086
087  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
088  public void testContainsEntryWithNullKeyAbsent() {
089    assertFalse(getMap().entrySet().contains(mapEntry(null, v0())));
090  }
091
092  @CollectionSize.Require(absent = ZERO)
093  @MapFeature.Require(ALLOWS_NULL_KEYS)
094  public void testContainsEntryWithNullKeyPresent() {
095    initMapWithNullKey();
096    assertTrue(getMap().entrySet().contains(mapEntry(null, getValueForNullKey())));
097  }
098
099  @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
100  public void testContainsEntryWithNullValueAbsent() {
101    assertFalse(getMap().entrySet().contains(mapEntry(k0(), null)));
102  }
103
104  @CollectionSize.Require(absent = ZERO)
105  @MapFeature.Require(ALLOWS_NULL_VALUES)
106  public void testContainsEntryWithNullValuePresent() {
107    initMapWithNullValue();
108    assertTrue(getMap().entrySet().contains(mapEntry(getKeyForNullValue(), null)));
109  }
110
111  @MapFeature.Require(SUPPORTS_PUT)
112  @CollectionSize.Require(absent = ZERO)
113  public void testSetValue() {
114    for (Entry<K, V> entry : getMap().entrySet()) {
115      if (entry.getKey().equals(k0())) {
116        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
117        break;
118      }
119    }
120    expectReplacement(entry(k0(), v3()));
121  }
122
123  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
124  @CollectionSize.Require(absent = ZERO)
125  public void testSetValueWithNullValuesPresent() {
126    for (Entry<K, V> entry : getMap().entrySet()) {
127      if (entry.getKey().equals(k0())) {
128        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(null));
129        break;
130      }
131    }
132    expectReplacement(entry(k0(), (V) null));
133  }
134
135  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
136  @CollectionSize.Require(absent = ZERO)
137  public void testSetValueWithNullValuesAbsent() {
138    for (Entry<K, V> entry : getMap().entrySet()) {
139      assertThrows(NullPointerException.class, () -> entry.setValue(null));
140      break;
141    }
142    expectUnchanged();
143  }
144
145  @J2ktIncompatible
146  @GwtIncompatible // reflection
147  public static Method getContainsEntryWithIncomparableKeyMethod() {
148    return getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
149  }
150
151  @J2ktIncompatible
152  @GwtIncompatible // reflection
153  public static Method getContainsEntryWithIncomparableValueMethod() {
154    return getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
155  }
156
157  @J2ktIncompatible
158  @GwtIncompatible // reflection
159  public static Method getSetValueMethod() {
160    return getMethod(MapEntrySetTester.class, "testSetValue");
161  }
162
163  @J2ktIncompatible
164  @GwtIncompatible // reflection
165  public static Method getSetValueWithNullValuesPresentMethod() {
166    return getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent");
167  }
168
169  @J2ktIncompatible
170  @GwtIncompatible // reflection
171  public static Method getSetValueWithNullValuesAbsentMethod() {
172    return getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent");
173  }
174}