blob: 72740e31234251e234779ef22325ce7c57163175 [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7package jsr166;
8
Narayan Kamath8e9a0e92015-04-28 11:40:00 +01009import java.util.AbstractMap;
10import java.util.Map;
11
12import junit.framework.Test;
13import junit.framework.TestSuite;
Calin Juravle8f0d92b2013-08-01 17:26:00 +010014
15public class EntryTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010016 // android-note: Removed because the CTS runner does a bad job of
17 // retrying tests that have suite() declarations.
18 //
19 // public static void main(String[] args) {
20 // main(suite(), args);
21 // }
22 // public static Test suite() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000023 // return new TestSuite(EntryTest.class);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010024 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010025
26 static final String k1 = "1";
27 static final String v1 = "a";
28 static final String k2 = "2";
29 static final String v2 = "b";
30
31 /**
32 * A new SimpleEntry(k, v) holds k, v.
33 */
34 public void testConstructor1() {
35 Map.Entry e = new AbstractMap.SimpleEntry(k1, v1);
36 assertEquals(k1, e.getKey());
37 assertEquals(v1, e.getValue());
38 }
39
40 /**
41 * A new SimpleImmutableEntry(k, v) holds k, v.
42 */
43 public void testConstructor2() {
44 Map.Entry s = new AbstractMap.SimpleImmutableEntry(k1, v1);
45 assertEquals(k1, s.getKey());
46 assertEquals(v1, s.getValue());
47 }
48
49 /**
50 * A new SimpleEntry(entry(k, v)) holds k, v.
51 */
52 public void testConstructor3() {
53 Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1);
54 Map.Entry e = new AbstractMap.SimpleEntry(e2);
55 assertEquals(k1, e.getKey());
56 assertEquals(v1, e.getValue());
57 }
58
59 /**
60 * A new SimpleImmutableEntry(entry(k, v)) holds k, v.
61 */
62 public void testConstructor4() {
63 Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1);
64 Map.Entry s = new AbstractMap.SimpleImmutableEntry(s2);
65 assertEquals(k1, s.getKey());
66 assertEquals(v1, s.getValue());
67 }
68
69 /**
70 * Entries with same key-value pairs are equal and have same
71 * hashcodes
72 */
73 public void testEquals() {
74 Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1);
75 Map.Entry e = new AbstractMap.SimpleEntry(e2);
76 Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1);
77 Map.Entry s = new AbstractMap.SimpleImmutableEntry(s2);
78 assertEquals(e2, e);
79 assertEquals(e2.hashCode(), e.hashCode());
80 assertEquals(s2, s);
81 assertEquals(s2.hashCode(), s.hashCode());
82 assertEquals(e2, s2);
83 assertEquals(e2.hashCode(), s2.hashCode());
84 assertEquals(e, s);
85 assertEquals(e.hashCode(), s.hashCode());
86 }
87
88 /**
89 * Entries with different key-value pairs are not equal
90 */
91 public void testNotEquals() {
92 Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1);
93 Map.Entry e = new AbstractMap.SimpleEntry(k2, v1);
94 assertFalse(e2.equals(e));
95 e = new AbstractMap.SimpleEntry(k1, v2);
96 assertFalse(e2.equals(e));
97 e = new AbstractMap.SimpleEntry(k2, v2);
98 assertFalse(e2.equals(e));
99
100 Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1);
101 Map.Entry s = new AbstractMap.SimpleImmutableEntry(k2, v1);
102 assertFalse(s2.equals(s));
103 s = new AbstractMap.SimpleImmutableEntry(k1, v2);
104 assertFalse(s2.equals(s));
105 s = new AbstractMap.SimpleImmutableEntry(k2, v2);
106 assertFalse(s2.equals(s));
107 }
108
109 /**
110 * getValue returns last setValue for SimpleEntry
111 */
112 public void testSetValue1() {
113 Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1);
114 Map.Entry e = new AbstractMap.SimpleEntry(e2);
115 assertEquals(k1, e.getKey());
116 assertEquals(v1, e.getValue());
117 e.setValue(k2);
118 assertEquals(k2, e.getValue());
119 assertFalse(e2.equals(e));
120 }
121
122 /**
123 * setValue for SimpleImmutableEntry throws UnsupportedOperationException
124 */
125 public void testSetValue2() {
126 Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1);
127 Map.Entry s = new AbstractMap.SimpleImmutableEntry(s2);
128 assertEquals(k1, s.getKey());
129 assertEquals(v1, s.getValue());
130 try {
131 s.setValue(k2);
132 shouldThrow();
133 } catch (UnsupportedOperationException success) {}
134 }
135}