1 |
/* |
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 |
|
7 |
import junit.framework.*; |
8 |
import java.util.*; |
9 |
|
10 |
public class EntryTest extends JSR166TestCase { |
11 |
public static void main(String[] args) { |
12 |
junit.textui.TestRunner.run(suite()); |
13 |
} |
14 |
public static Test suite() { |
15 |
return new TestSuite(EntryTest.class); |
16 |
} |
17 |
|
18 |
static final String k1 = "1"; |
19 |
static final String v1 = "a"; |
20 |
static final String k2 = "2"; |
21 |
static final String v2 = "b"; |
22 |
|
23 |
/** |
24 |
* A new SimpleEntry(k, v) holds k, v. |
25 |
*/ |
26 |
public void testConstructor1() { |
27 |
Map.Entry e = new AbstractMap.SimpleEntry(k1, v1); |
28 |
assertEquals(k1, e.getKey()); |
29 |
assertEquals(v1, e.getValue()); |
30 |
} |
31 |
|
32 |
/** |
33 |
* A new SimpleImmutableEntry(k, v) holds k, v. |
34 |
*/ |
35 |
public void testConstructor2() { |
36 |
Map.Entry s = new AbstractMap.SimpleImmutableEntry(k1, v1); |
37 |
assertEquals(k1, s.getKey()); |
38 |
assertEquals(v1, s.getValue()); |
39 |
} |
40 |
|
41 |
/** |
42 |
* A new SimpleEntry(entry(k, v)) holds k, v. |
43 |
*/ |
44 |
public void testConstructor3() { |
45 |
Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1); |
46 |
Map.Entry e = new AbstractMap.SimpleEntry(e2); |
47 |
assertEquals(k1, e.getKey()); |
48 |
assertEquals(v1, e.getValue()); |
49 |
} |
50 |
|
51 |
/** |
52 |
* A new SimpleImmutableEntry(entry(k, v)) holds k, v. |
53 |
*/ |
54 |
public void testConstructor4() { |
55 |
Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1); |
56 |
Map.Entry s = new AbstractMap.SimpleImmutableEntry(s2); |
57 |
assertEquals(k1, s.getKey()); |
58 |
assertEquals(v1, s.getValue()); |
59 |
} |
60 |
|
61 |
/** |
62 |
* Entries with same key-value pairs are equal and have same |
63 |
* hashcodes |
64 |
*/ |
65 |
public void testEquals() { |
66 |
Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1); |
67 |
Map.Entry e = new AbstractMap.SimpleEntry(e2); |
68 |
Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1); |
69 |
Map.Entry s = new AbstractMap.SimpleImmutableEntry(s2); |
70 |
assertEquals(e2, e); |
71 |
assertEquals(e2.hashCode(), e.hashCode()); |
72 |
assertEquals(s2, s); |
73 |
assertEquals(s2.hashCode(), s.hashCode()); |
74 |
assertEquals(e2, s2); |
75 |
assertEquals(e2.hashCode(), s2.hashCode()); |
76 |
assertEquals(e, s); |
77 |
assertEquals(e.hashCode(), s.hashCode()); |
78 |
} |
79 |
|
80 |
/** |
81 |
* Entries with different key-value pairs are not equal |
82 |
*/ |
83 |
public void testNotEquals() { |
84 |
Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1); |
85 |
Map.Entry e = new AbstractMap.SimpleEntry(k2, v1); |
86 |
assertFalse(e2.equals(e)); |
87 |
e = new AbstractMap.SimpleEntry(k1, v2); |
88 |
assertFalse(e2.equals(e)); |
89 |
e = new AbstractMap.SimpleEntry(k2, v2); |
90 |
assertFalse(e2.equals(e)); |
91 |
|
92 |
Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1); |
93 |
Map.Entry s = new AbstractMap.SimpleImmutableEntry(k2, v1); |
94 |
assertFalse(s2.equals(s)); |
95 |
s = new AbstractMap.SimpleImmutableEntry(k1, v2); |
96 |
assertFalse(s2.equals(s)); |
97 |
s = new AbstractMap.SimpleImmutableEntry(k2, v2); |
98 |
assertFalse(s2.equals(s)); |
99 |
} |
100 |
|
101 |
/** |
102 |
* getValue returns last setValue for SimpleEntry |
103 |
*/ |
104 |
public void testSetValue1() { |
105 |
Map.Entry e2 = new AbstractMap.SimpleEntry(k1, v1); |
106 |
Map.Entry e = new AbstractMap.SimpleEntry(e2); |
107 |
assertEquals(k1, e.getKey()); |
108 |
assertEquals(v1, e.getValue()); |
109 |
e.setValue(k2); |
110 |
assertEquals(k2, e.getValue()); |
111 |
assertFalse(e2.equals(e)); |
112 |
} |
113 |
|
114 |
/** |
115 |
* setValue for SimpleImmutableEntry throws UnsupportedOperationException |
116 |
*/ |
117 |
public void testSetValue2() { |
118 |
Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1); |
119 |
Map.Entry s = new AbstractMap.SimpleImmutableEntry(s2); |
120 |
assertEquals(k1, s.getKey()); |
121 |
assertEquals(v1, s.getValue()); |
122 |
try { |
123 |
s.setValue(k2); |
124 |
shouldThrow(); |
125 |
} catch (UnsupportedOperationException success) {} |
126 |
} |
127 |
} |