ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/EntryTest.java
Revision: 1.9
Committed: Sun May 29 13:45:35 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +1 -1 lines
Log Message:
various test case improvements

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     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     * 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 jsr166 1.6 assertFalse(e2.equals(e));
89 dl 1.1 e = new AbstractMap.SimpleEntry(k1, v2);
90 jsr166 1.6 assertFalse(e2.equals(e));
91 dl 1.1 e = new AbstractMap.SimpleEntry(k2, v2);
92 jsr166 1.6 assertFalse(e2.equals(e));
93 dl 1.1
94     Map.Entry s2 = new AbstractMap.SimpleImmutableEntry(k1, v1);
95     Map.Entry s = new AbstractMap.SimpleImmutableEntry(k2, v1);
96 jsr166 1.6 assertFalse(s2.equals(s));
97 dl 1.1 s = new AbstractMap.SimpleImmutableEntry(k1, v2);
98 jsr166 1.6 assertFalse(s2.equals(s));
99 dl 1.1 s = new AbstractMap.SimpleImmutableEntry(k2, v2);
100 jsr166 1.6 assertFalse(s2.equals(s));
101 dl 1.1 }
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 jsr166 1.6 assertFalse(e2.equals(e));
114 dl 1.1 }
115    
116     /**
117     * setValue for SimpleImmutableEntry throws UnsupportedOperationException
118     */
119 jsr166 1.9 public void testSetValue2() {
120 dl 1.1 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 jsr166 1.4 shouldThrow();
127 dl 1.1 } catch (UnsupportedOperationException success) {}
128     }
129     }