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