Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 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;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import java.util.Collections;
     24 import java.util.Map.Entry;
     25 
     26 /**
     27  * Tests for {@code AbstractMapEntry}.
     28  *
     29  * @author Mike Bostock
     30  */
     31 @GwtCompatible
     32 public class AbstractMapEntryTest extends TestCase {
     33   private static final String NK = null;
     34   private static final Integer NV = null;
     35 
     36   private static <K, V> Entry<K, V> entry(final K key, final V value) {
     37     return new AbstractMapEntry<K, V>() {
     38         @Override public K getKey() {
     39           return key;
     40         }
     41         @Override public V getValue() {
     42           return value;
     43         }
     44       };
     45   }
     46 
     47   private static <K, V> Entry<K, V> control(K key, V value) {
     48     return Collections.singletonMap(key, value).entrySet().iterator().next();
     49   }
     50 
     51   public void testToString() {
     52     assertEquals("foo=1", entry("foo", 1).toString());
     53   }
     54 
     55   public void testToStringNull() {
     56     assertEquals("null=1", entry(NK, 1).toString());
     57     assertEquals("foo=null", entry("foo", NV).toString());
     58     assertEquals("null=null", entry(NK, NV).toString());
     59   }
     60 
     61   public void testEquals() {
     62     Entry<String, Integer> foo1 = entry("foo", 1);
     63     assertEquals(foo1, foo1);
     64     assertEquals(control("foo", 1), foo1);
     65     assertEquals(control("bar", 2), entry("bar", 2));
     66     assertFalse(control("foo", 1).equals(entry("foo", 2)));
     67     assertFalse(foo1.equals(control("bar", 1)));
     68     assertFalse(foo1.equals(new Object()));
     69     assertFalse(foo1.equals(null));
     70   }
     71 
     72   public void testEqualsNull() {
     73     assertEquals(control(NK, 1), entry(NK, 1));
     74     assertEquals(control("bar", NV), entry("bar", NV));
     75     assertFalse(control(NK, 1).equals(entry(NK, 2)));
     76     assertFalse(entry(NK, 1).equals(control("bar", 1)));
     77     assertFalse(entry(NK, 1).equals(new Object()));
     78     assertFalse(entry(NK, 1).equals(null));
     79   }
     80 
     81   public void testHashCode() {
     82     assertEquals(control("foo", 1).hashCode(), entry("foo", 1).hashCode());
     83     assertEquals(control("bar", 2).hashCode(), entry("bar", 2).hashCode());
     84   }
     85 
     86   public void testHashCodeNull() {
     87     assertEquals(control(NK, 1).hashCode(), entry(NK, 1).hashCode());
     88     assertEquals(control("bar", NV).hashCode(), entry("bar", NV).hashCode());
     89     assertEquals(control(NK, NV).hashCode(), entry(NK, NV).hashCode());
     90   }
     91 }
     92