ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/EntryTest.java
Revision: 1.11
Committed: Wed Dec 31 19:05:42 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +5 -2 lines
Log Message:
no wildcard imports

File Contents

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