1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.*; |
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
import java.util.Enumeration; |
12 |
dl |
1.2 |
import java.io.*; |
13 |
dl |
1.1 |
|
14 |
dl |
1.3 |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
15 |
dl |
1.1 |
public static void main(String[] args) { |
16 |
|
|
junit.textui.TestRunner.run (suite()); |
17 |
|
|
} |
18 |
|
|
public static Test suite() { |
19 |
|
|
return new TestSuite(ConcurrentHashMapTest.class); |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
private static ConcurrentHashMap map5() { |
23 |
|
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
24 |
|
|
assertTrue(map.isEmpty()); |
25 |
|
|
map.put(one, "A"); |
26 |
|
|
map.put(two, "B"); |
27 |
|
|
map.put(three, "C"); |
28 |
|
|
map.put(four, "D"); |
29 |
|
|
map.put(five, "E"); |
30 |
|
|
assertFalse(map.isEmpty()); |
31 |
|
|
assertEquals(5, map.size()); |
32 |
|
|
return map; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
/** |
36 |
dl |
1.3 |
* clear removes all key-element pairs from the map |
37 |
dl |
1.1 |
*/ |
38 |
|
|
public void testClear(){ |
39 |
|
|
ConcurrentHashMap map = map5(); |
40 |
|
|
map.clear(); |
41 |
|
|
assertEquals(map.size(), 0); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
/** |
45 |
dl |
1.3 |
* contains gives the appropriate value |
46 |
dl |
1.1 |
*/ |
47 |
|
|
public void testContains(){ |
48 |
|
|
ConcurrentHashMap map = map5(); |
49 |
|
|
assertTrue(map.contains("A")); |
50 |
|
|
assertFalse(map.contains("Z")); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
/** |
54 |
dl |
1.3 |
* containsKey gives the appropriate value |
55 |
dl |
1.1 |
*/ |
56 |
|
|
public void testContainsKey(){ |
57 |
|
|
ConcurrentHashMap map = map5(); |
58 |
|
|
assertTrue(map.containsKey(one)); |
59 |
|
|
assertFalse(map.containsKey(new Integer(100))); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* Identical to normal contains |
64 |
|
|
*/ |
65 |
|
|
public void testContainsValue(){ |
66 |
|
|
ConcurrentHashMap map = map5(); |
67 |
|
|
assertTrue(map.contains("A")); |
68 |
|
|
assertFalse(map.contains("Z")); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* tes to verify enumeration returns an enumeration containing the correct elements |
73 |
|
|
*/ |
74 |
|
|
public void testEnumeration(){ |
75 |
|
|
ConcurrentHashMap map = map5(); |
76 |
|
|
Enumeration e = map.elements(); |
77 |
|
|
int count = 0; |
78 |
|
|
while(e.hasMoreElements()){ |
79 |
|
|
count++; |
80 |
|
|
e.nextElement(); |
81 |
|
|
} |
82 |
|
|
assertEquals(5, count); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
/** |
86 |
dl |
1.3 |
* get returns the correct element at the given index |
87 |
dl |
1.1 |
*/ |
88 |
|
|
public void testGet(){ |
89 |
|
|
ConcurrentHashMap map = map5(); |
90 |
|
|
assertEquals("A", (String)map.get(one)); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
dl |
1.3 |
* get on a nonexistant key returns null |
95 |
dl |
1.1 |
*/ |
96 |
|
|
public void testGet2(){ |
97 |
|
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
98 |
|
|
assertNull(empty.get("anything")); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/** |
102 |
|
|
* Simple test to verify isEmpty returns the correct value |
103 |
|
|
*/ |
104 |
|
|
public void testIsEmpty(){ |
105 |
|
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
106 |
|
|
ConcurrentHashMap map = map5(); |
107 |
|
|
assertTrue(empty.isEmpty()); |
108 |
|
|
assertFalse(map.isEmpty()); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
dl |
1.3 |
* keys returns an enumeration containing all the keys from the map |
113 |
dl |
1.1 |
*/ |
114 |
|
|
public void testKeys(){ |
115 |
|
|
ConcurrentHashMap map = map5(); |
116 |
|
|
Enumeration e = map.keys(); |
117 |
|
|
int count = 0; |
118 |
|
|
while(e.hasMoreElements()){ |
119 |
|
|
count++; |
120 |
|
|
e.nextElement(); |
121 |
|
|
} |
122 |
|
|
assertEquals(5, count); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
/** |
126 |
dl |
1.3 |
* keySet returns a Set containing all the keys |
127 |
dl |
1.1 |
*/ |
128 |
|
|
public void testKeySet(){ |
129 |
|
|
ConcurrentHashMap map = map5(); |
130 |
|
|
Set s = map.keySet(); |
131 |
|
|
assertEquals(5, s.size()); |
132 |
|
|
assertTrue(s.contains(one)); |
133 |
|
|
assertTrue(s.contains(two)); |
134 |
|
|
assertTrue(s.contains(three)); |
135 |
|
|
assertTrue(s.contains(four)); |
136 |
|
|
assertTrue(s.contains(five)); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
public void testValues(){ |
140 |
|
|
ConcurrentHashMap map = map5(); |
141 |
|
|
Collection s = map.values(); |
142 |
|
|
assertEquals(5, s.size()); |
143 |
|
|
assertTrue(s.contains("A")); |
144 |
|
|
assertTrue(s.contains("B")); |
145 |
|
|
assertTrue(s.contains("C")); |
146 |
|
|
assertTrue(s.contains("D")); |
147 |
|
|
assertTrue(s.contains("E")); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
public void testEntrySet(){ |
151 |
|
|
ConcurrentHashMap map = map5(); |
152 |
|
|
Set s = map.entrySet(); |
153 |
|
|
assertEquals(5, s.size()); |
154 |
|
|
Iterator it = s.iterator(); |
155 |
|
|
while (it.hasNext()) { |
156 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
157 |
|
|
assertTrue( |
158 |
|
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
159 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
160 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
161 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
162 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
/** |
167 |
dl |
1.3 |
* putAll adds all key-value pairs from the given map |
168 |
dl |
1.1 |
*/ |
169 |
|
|
public void testPutAll(){ |
170 |
|
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
171 |
|
|
ConcurrentHashMap map = map5(); |
172 |
|
|
empty.putAll(map); |
173 |
|
|
assertEquals(5, empty.size()); |
174 |
|
|
assertTrue(empty.containsKey(one)); |
175 |
|
|
assertTrue(empty.containsKey(two)); |
176 |
|
|
assertTrue(empty.containsKey(three)); |
177 |
|
|
assertTrue(empty.containsKey(four)); |
178 |
|
|
assertTrue(empty.containsKey(five)); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
/** |
182 |
dl |
1.3 |
* putIfAbsent works when the given key is not present |
183 |
dl |
1.1 |
*/ |
184 |
|
|
public void testPutIfAbsent(){ |
185 |
|
|
ConcurrentHashMap map = map5(); |
186 |
|
|
map.putIfAbsent(new Integer(6), "Z"); |
187 |
|
|
assertTrue(map.containsKey(new Integer(6))); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
/** |
191 |
dl |
1.3 |
* putIfAbsent does not add the pair if the key is already present |
192 |
dl |
1.1 |
*/ |
193 |
|
|
public void testPutIfAbsent2(){ |
194 |
|
|
ConcurrentHashMap map = map5(); |
195 |
|
|
assertEquals("A", map.putIfAbsent(one, "Z")); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
/** |
199 |
dl |
1.3 |
* remove removes the correct key-value pair from the map |
200 |
dl |
1.1 |
*/ |
201 |
|
|
public void testRemove(){ |
202 |
|
|
ConcurrentHashMap map = map5(); |
203 |
|
|
map.remove(five); |
204 |
|
|
assertEquals(4, map.size()); |
205 |
|
|
assertFalse(map.containsKey(five)); |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
public void testRemove2(){ |
209 |
|
|
ConcurrentHashMap map = map5(); |
210 |
|
|
map.remove(five, "E"); |
211 |
|
|
assertEquals(4, map.size()); |
212 |
|
|
assertFalse(map.containsKey(five)); |
213 |
|
|
map.remove(four, "A"); |
214 |
|
|
assertEquals(4, map.size()); |
215 |
|
|
assertTrue(map.containsKey(four)); |
216 |
|
|
|
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
/** |
220 |
dl |
1.3 |
* size returns the correct values |
221 |
dl |
1.1 |
*/ |
222 |
|
|
public void testSize(){ |
223 |
|
|
ConcurrentHashMap map = map5(); |
224 |
|
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
225 |
|
|
assertEquals(0, empty.size()); |
226 |
|
|
assertEquals(5, map.size()); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
public void testToString(){ |
230 |
|
|
ConcurrentHashMap map = map5(); |
231 |
|
|
String s = map.toString(); |
232 |
|
|
for (int i = 1; i <= 5; ++i) { |
233 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
234 |
|
|
} |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
// Exception tests |
238 |
|
|
|
239 |
|
|
public void testConstructor1(){ |
240 |
|
|
try{ |
241 |
|
|
new ConcurrentHashMap(-1,0,1); |
242 |
|
|
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
243 |
|
|
}catch(IllegalArgumentException e){} |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
public void testConstructor2(){ |
247 |
|
|
try{ |
248 |
|
|
new ConcurrentHashMap(1,0,-1); |
249 |
|
|
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
250 |
|
|
}catch(IllegalArgumentException e){} |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
public void testConstructor3(){ |
254 |
|
|
try{ |
255 |
|
|
new ConcurrentHashMap(-1); |
256 |
|
|
fail("ConcurrentHashMap(int) should throw Illegal Argument Exception"); |
257 |
|
|
}catch(IllegalArgumentException e){} |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
public void testGet_NullPointerException(){ |
261 |
|
|
try{ |
262 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
263 |
|
|
c.get(null); |
264 |
|
|
fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception"); |
265 |
|
|
}catch(NullPointerException e){} |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
public void testContainsKey_NullPointerException(){ |
269 |
|
|
try{ |
270 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
271 |
|
|
c.containsKey(null); |
272 |
|
|
fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception"); |
273 |
|
|
}catch(NullPointerException e){} |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
public void testContainsValue_NullPointerException(){ |
277 |
|
|
try{ |
278 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
279 |
|
|
c.containsValue(null); |
280 |
|
|
fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception"); |
281 |
|
|
}catch(NullPointerException e){} |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
public void testContains_NullPointerException(){ |
285 |
|
|
try{ |
286 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
287 |
|
|
c.contains(null); |
288 |
|
|
fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception"); |
289 |
|
|
}catch(NullPointerException e){} |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
public void testPut1_NullPointerException(){ |
293 |
|
|
try{ |
294 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
295 |
|
|
c.put(null, "whatever"); |
296 |
|
|
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
297 |
|
|
}catch(NullPointerException e){} |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
public void testPut2_NullPointerException(){ |
301 |
|
|
try{ |
302 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
303 |
|
|
c.put("whatever", null); |
304 |
|
|
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
305 |
|
|
}catch(NullPointerException e){} |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
public void testPutIfAbsent1_NullPointerException(){ |
309 |
|
|
try{ |
310 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
311 |
|
|
c.putIfAbsent(null, "whatever"); |
312 |
|
|
fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
313 |
|
|
}catch(NullPointerException e){} |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
public void testPutIfAbsent2_NullPointerException(){ |
317 |
|
|
try{ |
318 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
319 |
|
|
c.putIfAbsent("whatever", null); |
320 |
|
|
fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
321 |
|
|
}catch(NullPointerException e){} |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
|
325 |
|
|
public void testRemove1_NullPointerException(){ |
326 |
|
|
try{ |
327 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
328 |
|
|
c.put("sadsdf", "asdads"); |
329 |
|
|
c.remove(null); |
330 |
|
|
fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione"); |
331 |
|
|
}catch(NullPointerException e){} |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
public void testRemove2_NullPointerException(){ |
335 |
|
|
try{ |
336 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
337 |
|
|
c.put("sadsdf", "asdads"); |
338 |
|
|
c.remove(null, "whatever"); |
339 |
|
|
fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione"); |
340 |
|
|
}catch(NullPointerException e){} |
341 |
|
|
} |
342 |
dl |
1.2 |
|
343 |
|
|
public void testSerialization() { |
344 |
|
|
ConcurrentHashMap q = map5(); |
345 |
|
|
|
346 |
|
|
try { |
347 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
348 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
349 |
|
|
out.writeObject(q); |
350 |
|
|
out.close(); |
351 |
|
|
|
352 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
353 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
354 |
|
|
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
355 |
|
|
assertEquals(q.size(), r.size()); |
356 |
|
|
assertTrue(q.equals(r)); |
357 |
|
|
assertTrue(r.equals(q)); |
358 |
|
|
} catch(Exception e){ |
359 |
|
|
e.printStackTrace(); |
360 |
|
|
fail("unexpected exception"); |
361 |
|
|
} |
362 |
|
|
} |
363 |
|
|
|
364 |
dl |
1.1 |
|
365 |
|
|
} |