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