Home | History | Annotate | Download | only in testers
      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 
     27 import com.google.common.annotations.GwtCompatible;
     28 import com.google.common.annotations.GwtIncompatible;
     29 import com.google.common.collect.testing.AbstractMapTester;
     30 import com.google.common.collect.testing.Helpers;
     31 import com.google.common.collect.testing.features.CollectionFeature;
     32 import com.google.common.collect.testing.features.CollectionSize;
     33 import com.google.common.collect.testing.features.MapFeature;
     34 
     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(samples.e0, entryItr.next());
     59     entryItr.remove();
     60     assertTrue(getMap().isEmpty());
     61     assertFalse(entrySet.contains(samples.e0));
     62   }
     63 
     64   public void testContainsEntryWithIncomparableKey() {
     65     assertFalse(getMap()
     66         .entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, samples.e0.getValue())));
     67   }
     68 
     69   public void testContainsEntryWithIncomparableValue() {
     70     assertFalse(getMap()
     71         .entrySet().contains(Helpers.mapEntry(samples.e0.getKey(), IncomparableType.INSTANCE)));
     72   }
     73 
     74   @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
     75   public void testContainsEntryWithNullKeyAbsent() {
     76     assertFalse(getMap()
     77         .entrySet().contains(Helpers.mapEntry(null, samples.e0.getValue())));
     78   }
     79 
     80   @CollectionSize.Require(absent = ZERO)
     81   @MapFeature.Require(ALLOWS_NULL_KEYS)
     82   public void testContainsEntryWithNullKeyPresent() {
     83     initMapWithNullKey();
     84     assertTrue(getMap()
     85         .entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
     86   }
     87 
     88   @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
     89   public void testContainsEntryWithNullValueAbsent() {
     90     assertFalse(getMap()
     91         .entrySet().contains(Helpers.mapEntry(samples.e0.getKey(), null)));
     92   }
     93 
     94   @CollectionSize.Require(absent = ZERO)
     95   @MapFeature.Require(ALLOWS_NULL_VALUES)
     96   public void testContainsEntryWithNullValuePresent() {
     97     initMapWithNullValue();
     98     assertTrue(getMap()
     99         .entrySet().contains(Helpers.mapEntry(getKeyForNullValue(), null)));
    100   }
    101 
    102   @GwtIncompatible("reflection")
    103   public static Method getContainsEntryWithIncomparableKeyMethod() {
    104     return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
    105   }
    106 
    107   @GwtIncompatible("reflection")
    108   public static Method getContainsEntryWithIncomparableValueMethod() {
    109     return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
    110   }
    111 }
    112