Home | History | Annotate | Download | only in testers
      1 /*
      2  * Copyright (C) 2008 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.MapFeature.ALLOWS_NULL_KEYS;
     20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
     21 
     22 import com.google.common.annotations.GwtCompatible;
     23 import com.google.common.collect.testing.AbstractMapTester;
     24 import com.google.common.collect.testing.Helpers;
     25 import com.google.common.collect.testing.features.CollectionSize;
     26 import com.google.common.collect.testing.features.MapFeature;
     27 
     28 import java.util.Collection;
     29 import java.util.HashMap;
     30 import java.util.Map;
     31 import java.util.Map.Entry;
     32 
     33 /**
     34  * Tests {@link java.util.Map#equals}.
     35  *
     36  * @author George van den Driessche
     37  * @author Chris Povirk
     38  */
     39 @GwtCompatible
     40 public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> {
     41   public void testEquals_otherMapWithSameEntries() {
     42     assertTrue(
     43         "A Map should equal any other Map containing the same entries.",
     44         getMap().equals(newHashMap(getSampleEntries())));
     45   }
     46 
     47   @CollectionSize.Require(absent = CollectionSize.ZERO)
     48   public void testEquals_otherMapWithDifferentEntries() {
     49     Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1));
     50     Entry<K, V> e3 = getSubjectGenerator().samples().e3;
     51     other.put(e3.getKey(), e3.getValue());
     52     assertFalse(
     53         "A Map should not equal another Map containing different entries.",
     54         getMap().equals(other)
     55     );
     56   }
     57 
     58   @CollectionSize.Require(absent = CollectionSize.ZERO)
     59   @MapFeature.Require(ALLOWS_NULL_KEYS)
     60   public void testEquals_containingNullKey() {
     61     Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
     62     entries.add(entry(null, samples.e3.getValue()));
     63 
     64     resetContainer(getSubjectGenerator().create(entries.toArray()));
     65     assertTrue("A Map should equal any other Map containing the same entries,"
     66         + " even if some keys are null.",
     67         getMap().equals(newHashMap(entries)));
     68   }
     69 
     70   @CollectionSize.Require(absent = CollectionSize.ZERO)
     71   public void testEquals_otherContainsNullKey() {
     72     Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
     73     entries.add(entry(null, samples.e3.getValue()));
     74     Map<K, V> other = newHashMap(entries);
     75 
     76     assertFalse(
     77         "Two Maps should not be equal if exactly one of them contains a null "
     78         + "key.",
     79         getMap().equals(other));
     80   }
     81 
     82   @CollectionSize.Require(absent = CollectionSize.ZERO)
     83   @MapFeature.Require(ALLOWS_NULL_VALUES)
     84   public void testEquals_containingNullValue() {
     85     Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
     86     entries.add(entry(samples.e3.getKey(), null));
     87 
     88     resetContainer(getSubjectGenerator().create(entries.toArray()));
     89     assertTrue("A Map should equal any other Map containing the same entries,"
     90         + " even if some values are null.",
     91         getMap().equals(newHashMap(entries)));
     92   }
     93 
     94   @CollectionSize.Require(absent = CollectionSize.ZERO)
     95   public void testEquals_otherContainsNullValue() {
     96     Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
     97     entries.add(entry(samples.e3.getKey(), null));
     98     Map<K, V> other = newHashMap(entries);
     99 
    100     assertFalse(
    101         "Two Maps should not be equal if exactly one of them contains a null "
    102         + "value.",
    103         getMap().equals(other));
    104   }
    105 
    106   @CollectionSize.Require(absent = CollectionSize.ZERO)
    107   public void testEquals_smallerMap() {
    108     Collection<Map.Entry<K, V>> fewerEntries
    109         = getSampleEntries(getNumEntries() - 1);
    110     assertFalse("Maps of different sizes should not be equal.",
    111         getMap().equals(newHashMap(fewerEntries)));
    112   }
    113 
    114   public void testEquals_largerMap() {
    115     Collection<Map.Entry<K, V>> moreEntries
    116         = getSampleEntries(getNumEntries() + 1);
    117     assertFalse("Maps of different sizes should not be equal.",
    118         getMap().equals(newHashMap(moreEntries)));
    119   }
    120 
    121   public void testEquals_list() {
    122     assertFalse("A List should never equal a Map.",
    123         getMap().equals(Helpers.copyToList(getMap().entrySet())));
    124   }
    125 
    126   private static <K, V> HashMap<K, V> newHashMap(
    127       Collection<? extends Map.Entry<? extends K, ? extends V>> entries) {
    128     HashMap<K, V> map = new HashMap<K, V>();
    129     for (Map.Entry<? extends K, ? extends V> entry : entries) {
    130       map.put(entry.getKey(), entry.getValue());
    131     }
    132     return map;
    133   }
    134 }
    135