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