View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
20  import static com.google.common.collect.testing.features.CollectionSize.ONE;
21  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
23  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
24  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
25  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
26  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
27  
28  import com.google.common.annotations.GwtCompatible;
29  import com.google.common.annotations.GwtIncompatible;
30  import com.google.common.collect.testing.AbstractMapTester;
31  import com.google.common.collect.testing.Helpers;
32  import com.google.common.collect.testing.features.CollectionFeature;
33  import com.google.common.collect.testing.features.CollectionSize;
34  import com.google.common.collect.testing.features.MapFeature;
35  import java.lang.reflect.Method;
36  import java.util.Iterator;
37  import java.util.Map.Entry;
38  import java.util.Set;
39  
40  /**
41   * Tests {@link java.util.Map#entrySet}.
42   *
43   * @author Louis Wasserman
44   * @param <K> The key type of the map implementation under test.
45   * @param <V> The value type of the map implementation under test.
46   */
47  @GwtCompatible
48  public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
49    private enum IncomparableType {
50      INSTANCE;
51    }
52  
53    @CollectionSize.Require(ONE)
54    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
55    public void testEntrySetIteratorRemove() {
56      Set<Entry<K, V>> entrySet = getMap().entrySet();
57      Iterator<Entry<K, V>> entryItr = entrySet.iterator();
58      assertEquals(e0(), entryItr.next());
59      entryItr.remove();
60      assertTrue(getMap().isEmpty());
61      assertFalse(entrySet.contains(e0()));
62    }
63  
64    public void testContainsEntryWithIncomparableKey() {
65      try {
66        assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0())));
67      } catch (ClassCastException acceptable) {
68        // allowed by the spec
69      }
70    }
71  
72    public void testContainsEntryWithIncomparableValue() {
73      try {
74        assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE)));
75      } catch (ClassCastException acceptable) {
76        // allowed by the spec
77      }
78    }
79  
80    @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
81    public void testContainsEntryWithNullKeyAbsent() {
82      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0())));
83    }
84  
85    @CollectionSize.Require(absent = ZERO)
86    @MapFeature.Require(ALLOWS_NULL_KEYS)
87    public void testContainsEntryWithNullKeyPresent() {
88      initMapWithNullKey();
89      assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
90    }
91  
92    @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
93    public void testContainsEntryWithNullValueAbsent() {
94      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null)));
95    }
96  
97    @CollectionSize.Require(absent = ZERO)
98    @MapFeature.Require(ALLOWS_NULL_VALUES)
99    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 }