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