KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > CompactMapTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.util;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25
26 /**
27  * Test of CompactMap correctness.
28  *
29  * @author mmetelka
30  */

31 public class CompactMapTest extends NbTestCase {
32
33     public CompactMapTest(java.lang.String JavaDoc testName) {
34         super(testName);
35     }
36
37     @SuppressWarnings JavaDoc("unchecked")
38     public void test() {
39         CompactMap testMap = new CompactMap();
40         Object JavaDoc key1 = new NamedObject("key1");
41         Object JavaDoc key2 = new NamedObject("key2");
42         Object JavaDoc val1 = new NamedObject("val1");
43         Object JavaDoc val2 = new NamedObject("val2");
44         
45         assertEquals(0, testMap.size());
46         assertEquals(null, testMap.get(key1));
47         testMap.put(key1, val1);
48         assertEquals(1, testMap.size());
49         assertEquals(val1, testMap.get(key1));
50         testMap.put(key2, val2);
51         assertEquals(2, testMap.size());
52         assertEquals(val2, testMap.get(key2));
53         assertEquals(val1, testMap.get(key1));
54         
55         Iterator JavaDoc it = testMap.entrySet().iterator();
56         assertTrue(it.hasNext());
57         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
58         boolean wasKey2;
59         if (entry.getKey() == key2) {
60             assertEquals(key2, entry.getKey());
61             assertEquals(val2, entry.getValue());
62             wasKey2 = true;
63         } else { // key1
64
assertEquals(key1, entry.getKey());
65             assertEquals(val1, entry.getValue());
66             wasKey2 = false;
67         }
68         assertTrue(it.hasNext());
69         entry = (Map.Entry JavaDoc)it.next();
70         if (wasKey2) {
71             assertEquals(key1, entry.getKey());
72             assertEquals(val1, entry.getValue());
73         } else {
74             assertEquals(key2, entry.getKey());
75             assertEquals(val2, entry.getValue());
76         }
77
78         assertEquals(val1, testMap.put(key1, val2));
79         assertEquals(val2, testMap.get(key2));
80         assertEquals(val2, testMap.remove(key1));
81         assertEquals(val2, testMap.get(key2));
82
83         it = testMap.entrySet().iterator();
84         assertTrue(it.hasNext());
85         entry = (Map.Entry JavaDoc)it.next();
86         assertEquals(key2, entry.getKey());
87         assertEquals(val2, entry.getValue());
88
89         Entry e1 = new Entry(key1);
90         e1.setValue(val1);
91         assertEquals(null, testMap.putEntry(e1));
92         assertEquals(val1, testMap.get(key1));
93
94         Entry e2 = new Entry(key2);
95         e2.setValue(val1);
96         CompactMap.MapEntry e = testMap.putEntry(e2);
97         assertEquals(key2, e.getKey());
98         assertEquals(val2, e.getValue());
99         
100         CompactMap.MapEntry mapEntry = testMap.getFirstEntry(0);
101         assertEquals(mapEntry, e1);
102         
103         // Test MapEntry.hashCode() and equals()
104
Entry ehe = new Entry(mapEntry.getKey());
105         ehe.setValue(mapEntry.getValue());
106         assertEquals(mapEntry.hashCode(), ehe.hashCode());
107         assertEquals(mapEntry, ehe);
108         
109         // Clear the map
110
testMap.clear();
111         assertEquals(0, testMap.size());
112
113         // Insert 5 mappings to check extending of the bucket table
114
Object JavaDoc key3 = new NamedObject("key3");
115         Object JavaDoc key4 = new NamedObject("key4");
116         Object JavaDoc key5 = new NamedObject("key5");
117         Object JavaDoc val3 = new NamedObject("val3");
118         Object JavaDoc val4 = new NamedObject("val4");
119         Object JavaDoc val5 = new NamedObject("val5");
120         testMap.put(key1, val1);
121         testMap.put(key2, val2);
122         testMap.put(key3, val3);
123         testMap.put(key4, val4);
124         testMap.put(key5, val5);
125         assertEquals(5, testMap.size());
126         assertEquals(val2, testMap.get(key2));
127         assertEquals(val1, testMap.get(key1));
128         assertEquals(val3, testMap.get(key3));
129         assertEquals(val4, testMap.get(key4));
130         assertEquals(val5, testMap.get(key5));
131
132     }
133     
134     private static final class Entry extends CompactMap.MapEntry {
135         
136         private final Object JavaDoc key;
137         
138         private Object JavaDoc value;
139         
140         public Entry(Object JavaDoc key) {
141             this.key = key;
142         }
143
144         public Object JavaDoc setValue(Object JavaDoc value) {
145             Object JavaDoc oldValue = this.value;
146             this.value = value;
147             return oldValue;
148         }
149
150         public Object JavaDoc getValue() {
151             return value;
152         }
153
154         public Object JavaDoc getKey() {
155             return key;
156         }
157
158         protected int valueHashCode() {
159             return (value != null) ? value.hashCode() : 0;
160         }
161         
162         protected boolean valueEquals(Object JavaDoc value2) {
163             return (value == value2 || (value != null && value.equals(value2)));
164         }
165         
166     }
167     
168     private static final class NamedObject extends Object JavaDoc {
169         
170         private final String JavaDoc name;
171         
172         public NamedObject(String JavaDoc name) {
173             this.name = name;
174         }
175         
176         public String JavaDoc toString() {
177             return name;
178         }
179
180     }
181     
182 }
183
Popular Tags