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