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;
039
040/**
041 * Tests {@link java.util.Map#entrySet}.
042 *
043 * @author Louis Wasserman
044 * @param <K> The key type of the map implementation under test.
045 * @param <V> The value type of the map implementation under test.
046 */
047@GwtCompatible
048public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
049  private enum IncomparableType {
050    INSTANCE;
051  }
052
053  @CollectionSize.Require(ONE)
054  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
055  public void testEntrySetIteratorRemove() {
056    Set<Entry<K, V>> entrySet = getMap().entrySet();
057    Iterator<Entry<K, V>> entryItr = entrySet.iterator();
058    assertEquals(e0(), entryItr.next());
059    entryItr.remove();
060    assertTrue(getMap().isEmpty());
061    assertFalse(entrySet.contains(e0()));
062  }
063
064  public void testContainsEntryWithIncomparableKey() {
065    try {
066      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0())));
067    } catch (ClassCastException acceptable) {
068      // allowed by the spec
069    }
070  }
071
072  public void testContainsEntryWithIncomparableValue() {
073    try {
074      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE)));
075    } catch (ClassCastException acceptable) {
076      // allowed by the spec
077    }
078  }
079
080  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
081  public void testContainsEntryWithNullKeyAbsent() {
082    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0())));
083  }
084
085  @CollectionSize.Require(absent = ZERO)
086  @MapFeature.Require(ALLOWS_NULL_KEYS)
087  public void testContainsEntryWithNullKeyPresent() {
088    initMapWithNullKey();
089    assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
090  }
091
092  @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
093  public void testContainsEntryWithNullValueAbsent() {
094    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null)));
095  }
096
097  @CollectionSize.Require(absent = ZERO)
098  @MapFeature.Require(ALLOWS_NULL_VALUES)
099  public void testContainsEntryWithNullValuePresent() {
100    initMapWithNullValue();
101    assertTrue(getMap().entrySet().contains(Helpers.mapEntry(getKeyForNullValue(), null)));
102  }
103
104  @MapFeature.Require(SUPPORTS_PUT)
105  @CollectionSize.Require(absent = ZERO)
106  public void testSetValue() {
107    for (Entry<K, V> entry : getMap().entrySet()) {
108      if (entry.getKey().equals(k0())) {
109        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
110        break;
111      }
112    }
113    expectReplacement(entry(k0(), v3()));
114  }
115
116  @GwtIncompatible // reflection
117  public static Method getContainsEntryWithIncomparableKeyMethod() {
118    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
119  }
120
121  @GwtIncompatible // reflection
122  public static Method getContainsEntryWithIncomparableValueMethod() {
123    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
124  }
125
126  @GwtIncompatible // reflection
127  public static Method getSetValueMethod() {
128    return Helpers.getMethod(MapEntrySetTester.class, "testSetValue");
129  }
130}