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.collect.testing.AbstractMapTester;
031import com.google.common.collect.testing.Helpers;
032import com.google.common.collect.testing.features.CollectionFeature;
033import com.google.common.collect.testing.features.CollectionSize;
034import com.google.common.collect.testing.features.MapFeature;
035import java.lang.reflect.Method;
036import java.util.Iterator;
037import java.util.Map.Entry;
038import java.util.Set;
039import org.junit.Ignore;
040
041/**
042 * Tests {@link java.util.Map#entrySet}.
043 *
044 * @author Louis Wasserman
045 * @param <K> The key type of the map implementation under test.
046 * @param <V> The value type of the map implementation under test.
047 */
048@GwtCompatible
049@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
050public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
051  private enum IncomparableType {
052    INSTANCE;
053  }
054
055  @CollectionSize.Require(ONE)
056  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
057  public void testEntrySetIteratorRemove() {
058    Set<Entry<K, V>> entrySet = getMap().entrySet();
059    Iterator<Entry<K, V>> entryItr = entrySet.iterator();
060    assertEquals(e0(), entryItr.next());
061    entryItr.remove();
062    assertTrue(getMap().isEmpty());
063    assertFalse(entrySet.contains(e0()));
064  }
065
066  public void testContainsEntryWithIncomparableKey() {
067    try {
068      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0())));
069    } catch (ClassCastException acceptable) {
070      // allowed by the spec
071    }
072  }
073
074  public void testContainsEntryWithIncomparableValue() {
075    try {
076      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE)));
077    } catch (ClassCastException acceptable) {
078      // allowed by the spec
079    }
080  }
081
082  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
083  public void testContainsEntryWithNullKeyAbsent() {
084    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0())));
085  }
086
087  @CollectionSize.Require(absent = ZERO)
088  @MapFeature.Require(ALLOWS_NULL_KEYS)
089  public void testContainsEntryWithNullKeyPresent() {
090    initMapWithNullKey();
091    assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
092  }
093
094  @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
095  public void testContainsEntryWithNullValueAbsent() {
096    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null)));
097  }
098
099  @CollectionSize.Require(absent = ZERO)
100  @MapFeature.Require(ALLOWS_NULL_VALUES)
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)
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)
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)
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
145  public static Method getContainsEntryWithIncomparableKeyMethod() {
146    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
147  }
148
149  @GwtIncompatible // reflection
150  public static Method getContainsEntryWithIncomparableValueMethod() {
151    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
152  }
153
154  @GwtIncompatible // reflection
155  public static Method getSetValueMethod() {
156    return Helpers.getMethod(MapEntrySetTester.class, "testSetValue");
157  }
158
159  @GwtIncompatible // reflection
160  public static Method getSetValueWithNullValuesPresentMethod() {
161    return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent");
162  }
163
164  @GwtIncompatible // reflection
165  public static Method getSetValueWithNullValuesAbsentMethod() {
166    return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent");
167  }
168}