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.
051public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
052  private enum IncomparableType {
053    INSTANCE;
054  }
055
056  @CollectionSize.Require(ONE)
057  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
058  public void testEntrySetIteratorRemove() {
059    Set<Entry<K, V>> entrySet = getMap().entrySet();
060    Iterator<Entry<K, V>> entryItr = entrySet.iterator();
061    assertEquals(e0(), entryItr.next());
062    entryItr.remove();
063    assertTrue(getMap().isEmpty());
064    assertFalse(entrySet.contains(e0()));
065  }
066
067  public void testContainsEntryWithIncomparableKey() {
068    try {
069      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0())));
070    } catch (ClassCastException acceptable) {
071      // allowed by the spec
072    }
073  }
074
075  public void testContainsEntryWithIncomparableValue() {
076    try {
077      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE)));
078    } catch (ClassCastException acceptable) {
079      // allowed by the spec
080    }
081  }
082
083  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
084  public void testContainsEntryWithNullKeyAbsent() {
085    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0())));
086  }
087
088  @CollectionSize.Require(absent = ZERO)
089  @MapFeature.Require(ALLOWS_NULL_KEYS)
090  public void testContainsEntryWithNullKeyPresent() {
091    initMapWithNullKey();
092    assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
093  }
094
095  @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
096  public void testContainsEntryWithNullValueAbsent() {
097    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null)));
098  }
099
100  @CollectionSize.Require(absent = ZERO)
101  @MapFeature.Require(ALLOWS_NULL_VALUES)
102  public void testContainsEntryWithNullValuePresent() {
103    initMapWithNullValue();
104    assertTrue(getMap().entrySet().contains(Helpers.mapEntry(getKeyForNullValue(), null)));
105  }
106
107  @MapFeature.Require(SUPPORTS_PUT)
108  @CollectionSize.Require(absent = ZERO)
109  public void testSetValue() {
110    for (Entry<K, V> entry : getMap().entrySet()) {
111      if (entry.getKey().equals(k0())) {
112        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
113        break;
114      }
115    }
116    expectReplacement(entry(k0(), v3()));
117  }
118
119  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
120  @CollectionSize.Require(absent = ZERO)
121  public void testSetValueWithNullValuesPresent() {
122    for (Entry<K, V> entry : getMap().entrySet()) {
123      if (entry.getKey().equals(k0())) {
124        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(null));
125        break;
126      }
127    }
128    expectReplacement(entry(k0(), (V) null));
129  }
130
131  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
132  @CollectionSize.Require(absent = ZERO)
133  public void testSetValueWithNullValuesAbsent() {
134    for (Entry<K, V> entry : getMap().entrySet()) {
135      try {
136        entry.setValue(null);
137        fail("Expected NullPointerException");
138      } catch (NullPointerException exception) {
139        break;
140      }
141    }
142    expectUnchanged();
143  }
144
145  @J2ktIncompatible
146  @GwtIncompatible // reflection
147  public static Method getContainsEntryWithIncomparableKeyMethod() {
148    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
149  }
150
151  @J2ktIncompatible
152  @GwtIncompatible // reflection
153  public static Method getContainsEntryWithIncomparableValueMethod() {
154    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
155  }
156
157  @J2ktIncompatible
158  @GwtIncompatible // reflection
159  public static Method getSetValueMethod() {
160    return Helpers.getMethod(MapEntrySetTester.class, "testSetValue");
161  }
162
163  @J2ktIncompatible
164  @GwtIncompatible // reflection
165  public static Method getSetValueWithNullValuesPresentMethod() {
166    return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent");
167  }
168
169  @J2ktIncompatible
170  @GwtIncompatible // reflection
171  public static Method getSetValueWithNullValuesAbsentMethod() {
172    return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent");
173  }
174}