1 |
dl |
1.1 |
/* |
2 |
dl |
1.9 |
* 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/licenses/publicdomain |
5 |
|
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
|
|
import java.util.Enumeration; |
13 |
dl |
1.2 |
import java.io.*; |
14 |
dl |
1.1 |
|
15 |
dl |
1.3 |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
16 |
dl |
1.1 |
public static void main(String[] args) { |
17 |
|
|
junit.textui.TestRunner.run (suite()); |
18 |
|
|
} |
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(ConcurrentHashMapTest.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
dl |
1.5 |
/** |
24 |
|
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
25 |
|
|
*/ |
26 |
dl |
1.1 |
private static ConcurrentHashMap map5() { |
27 |
|
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
28 |
|
|
assertTrue(map.isEmpty()); |
29 |
|
|
map.put(one, "A"); |
30 |
|
|
map.put(two, "B"); |
31 |
|
|
map.put(three, "C"); |
32 |
|
|
map.put(four, "D"); |
33 |
|
|
map.put(five, "E"); |
34 |
|
|
assertFalse(map.isEmpty()); |
35 |
|
|
assertEquals(5, map.size()); |
36 |
|
|
return map; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
dl |
1.5 |
* clear removes all pairs |
41 |
dl |
1.1 |
*/ |
42 |
dl |
1.5 |
public void testClear() { |
43 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
44 |
|
|
map.clear(); |
45 |
|
|
assertEquals(map.size(), 0); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
dl |
1.5 |
* Maps with same contents are equal |
50 |
|
|
*/ |
51 |
|
|
public void testEquals() { |
52 |
|
|
ConcurrentHashMap map1 = map5(); |
53 |
|
|
ConcurrentHashMap map2 = map5(); |
54 |
|
|
assertEquals(map1, map2); |
55 |
|
|
assertEquals(map2, map1); |
56 |
|
|
map1.clear(); |
57 |
|
|
assertFalse(map1.equals(map2)); |
58 |
|
|
assertFalse(map2.equals(map1)); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* contains returns true for contained value |
63 |
dl |
1.1 |
*/ |
64 |
dl |
1.5 |
public void testContains() { |
65 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
66 |
|
|
assertTrue(map.contains("A")); |
67 |
|
|
assertFalse(map.contains("Z")); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
dl |
1.5 |
* containsKey returns true for contained key |
72 |
dl |
1.1 |
*/ |
73 |
dl |
1.5 |
public void testContainsKey() { |
74 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
75 |
|
|
assertTrue(map.containsKey(one)); |
76 |
dl |
1.5 |
assertFalse(map.containsKey(zero)); |
77 |
dl |
1.1 |
} |
78 |
|
|
|
79 |
|
|
/** |
80 |
dl |
1.5 |
* containsValue returns true for held values |
81 |
dl |
1.1 |
*/ |
82 |
dl |
1.5 |
public void testContainsValue() { |
83 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
84 |
|
|
assertTrue(map.contains("A")); |
85 |
|
|
assertFalse(map.contains("Z")); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
dl |
1.4 |
* enumeration returns an enumeration containing the correct |
90 |
|
|
* elements |
91 |
dl |
1.1 |
*/ |
92 |
dl |
1.5 |
public void testEnumeration() { |
93 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
94 |
|
|
Enumeration e = map.elements(); |
95 |
|
|
int count = 0; |
96 |
|
|
while(e.hasMoreElements()){ |
97 |
|
|
count++; |
98 |
|
|
e.nextElement(); |
99 |
|
|
} |
100 |
|
|
assertEquals(5, count); |
101 |
dl |
1.4 |
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
dl |
1.5 |
* get returns the correct element at the given key, |
105 |
|
|
* or null if not present |
106 |
dl |
1.1 |
*/ |
107 |
dl |
1.5 |
public void testGet() { |
108 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
109 |
|
|
assertEquals("A", (String)map.get(one)); |
110 |
|
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
111 |
dl |
1.5 |
assertNull(map.get("anything")); |
112 |
dl |
1.1 |
} |
113 |
|
|
|
114 |
|
|
/** |
115 |
dl |
1.5 |
* isEmpty is true of empty map and false for non-empty |
116 |
dl |
1.1 |
*/ |
117 |
dl |
1.5 |
public void testIsEmpty() { |
118 |
dl |
1.1 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
119 |
|
|
ConcurrentHashMap map = map5(); |
120 |
|
|
assertTrue(empty.isEmpty()); |
121 |
|
|
assertFalse(map.isEmpty()); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
/** |
125 |
dl |
1.3 |
* keys returns an enumeration containing all the keys from the map |
126 |
dl |
1.1 |
*/ |
127 |
dl |
1.5 |
public void testKeys() { |
128 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
129 |
|
|
Enumeration e = map.keys(); |
130 |
|
|
int count = 0; |
131 |
|
|
while(e.hasMoreElements()){ |
132 |
|
|
count++; |
133 |
|
|
e.nextElement(); |
134 |
|
|
} |
135 |
|
|
assertEquals(5, count); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/** |
139 |
dl |
1.3 |
* keySet returns a Set containing all the keys |
140 |
dl |
1.1 |
*/ |
141 |
dl |
1.5 |
public void testKeySet() { |
142 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
143 |
|
|
Set s = map.keySet(); |
144 |
|
|
assertEquals(5, s.size()); |
145 |
|
|
assertTrue(s.contains(one)); |
146 |
|
|
assertTrue(s.contains(two)); |
147 |
|
|
assertTrue(s.contains(three)); |
148 |
|
|
assertTrue(s.contains(four)); |
149 |
|
|
assertTrue(s.contains(five)); |
150 |
|
|
} |
151 |
|
|
|
152 |
dl |
1.5 |
/** |
153 |
|
|
* values collection contains all values |
154 |
|
|
*/ |
155 |
|
|
public void testValues() { |
156 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
157 |
|
|
Collection s = map.values(); |
158 |
|
|
assertEquals(5, s.size()); |
159 |
|
|
assertTrue(s.contains("A")); |
160 |
|
|
assertTrue(s.contains("B")); |
161 |
|
|
assertTrue(s.contains("C")); |
162 |
|
|
assertTrue(s.contains("D")); |
163 |
|
|
assertTrue(s.contains("E")); |
164 |
|
|
} |
165 |
|
|
|
166 |
dl |
1.5 |
/** |
167 |
|
|
* entrySet contains all pairs |
168 |
|
|
*/ |
169 |
|
|
public void testEntrySet() { |
170 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
171 |
|
|
Set s = map.entrySet(); |
172 |
|
|
assertEquals(5, s.size()); |
173 |
|
|
Iterator it = s.iterator(); |
174 |
|
|
while (it.hasNext()) { |
175 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
176 |
|
|
assertTrue( |
177 |
|
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
178 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
179 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
180 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
181 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
182 |
|
|
} |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
dl |
1.3 |
* putAll adds all key-value pairs from the given map |
187 |
dl |
1.1 |
*/ |
188 |
dl |
1.5 |
public void testPutAll() { |
189 |
dl |
1.1 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
190 |
|
|
ConcurrentHashMap map = map5(); |
191 |
|
|
empty.putAll(map); |
192 |
|
|
assertEquals(5, empty.size()); |
193 |
|
|
assertTrue(empty.containsKey(one)); |
194 |
|
|
assertTrue(empty.containsKey(two)); |
195 |
|
|
assertTrue(empty.containsKey(three)); |
196 |
|
|
assertTrue(empty.containsKey(four)); |
197 |
|
|
assertTrue(empty.containsKey(five)); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
dl |
1.3 |
* putIfAbsent works when the given key is not present |
202 |
dl |
1.1 |
*/ |
203 |
dl |
1.5 |
public void testPutIfAbsent() { |
204 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
205 |
dl |
1.7 |
map.putIfAbsent(six, "Z"); |
206 |
|
|
assertTrue(map.containsKey(six)); |
207 |
dl |
1.1 |
} |
208 |
|
|
|
209 |
|
|
/** |
210 |
dl |
1.3 |
* putIfAbsent does not add the pair if the key is already present |
211 |
dl |
1.1 |
*/ |
212 |
dl |
1.5 |
public void testPutIfAbsent2() { |
213 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
214 |
|
|
assertEquals("A", map.putIfAbsent(one, "Z")); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
/** |
218 |
dl |
1.7 |
* replace fails when the given key is not present |
219 |
|
|
*/ |
220 |
|
|
public void testReplace() { |
221 |
|
|
ConcurrentHashMap map = map5(); |
222 |
dl |
1.8 |
assertNull(map.replace(six, "Z")); |
223 |
dl |
1.7 |
assertFalse(map.containsKey(six)); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
/** |
227 |
|
|
* replace succeeds if the key is already present |
228 |
|
|
*/ |
229 |
|
|
public void testReplace2() { |
230 |
|
|
ConcurrentHashMap map = map5(); |
231 |
dl |
1.8 |
assertNotNull(map.replace(one, "Z")); |
232 |
dl |
1.7 |
assertEquals("Z", map.get(one)); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
|
236 |
|
|
/** |
237 |
|
|
* replace value fails when the given key not mapped to expected value |
238 |
|
|
*/ |
239 |
|
|
public void testReplaceValue() { |
240 |
|
|
ConcurrentHashMap map = map5(); |
241 |
|
|
assertEquals("A", map.get(one)); |
242 |
|
|
assertFalse(map.replace(one, "Z", "Z")); |
243 |
|
|
assertEquals("A", map.get(one)); |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
/** |
247 |
|
|
* replace value succeeds when the given key mapped to expected value |
248 |
|
|
*/ |
249 |
|
|
public void testReplaceValue2() { |
250 |
|
|
ConcurrentHashMap map = map5(); |
251 |
|
|
assertEquals("A", map.get(one)); |
252 |
|
|
assertTrue(map.replace(one, "A", "Z")); |
253 |
|
|
assertEquals("Z", map.get(one)); |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
|
257 |
|
|
/** |
258 |
dl |
1.3 |
* remove removes the correct key-value pair from the map |
259 |
dl |
1.1 |
*/ |
260 |
dl |
1.5 |
public void testRemove() { |
261 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
262 |
|
|
map.remove(five); |
263 |
|
|
assertEquals(4, map.size()); |
264 |
|
|
assertFalse(map.containsKey(five)); |
265 |
|
|
} |
266 |
|
|
|
267 |
dl |
1.5 |
/** |
268 |
|
|
* remove(key,value) removes only if pair present |
269 |
|
|
*/ |
270 |
|
|
public void testRemove2() { |
271 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
272 |
|
|
map.remove(five, "E"); |
273 |
|
|
assertEquals(4, map.size()); |
274 |
|
|
assertFalse(map.containsKey(five)); |
275 |
|
|
map.remove(four, "A"); |
276 |
|
|
assertEquals(4, map.size()); |
277 |
|
|
assertTrue(map.containsKey(four)); |
278 |
|
|
|
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
/** |
282 |
dl |
1.3 |
* size returns the correct values |
283 |
dl |
1.1 |
*/ |
284 |
dl |
1.5 |
public void testSize() { |
285 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
286 |
|
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
287 |
|
|
assertEquals(0, empty.size()); |
288 |
|
|
assertEquals(5, map.size()); |
289 |
|
|
} |
290 |
|
|
|
291 |
dl |
1.5 |
/** |
292 |
|
|
* toString contains toString of elements |
293 |
|
|
*/ |
294 |
|
|
public void testToString() { |
295 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
296 |
|
|
String s = map.toString(); |
297 |
|
|
for (int i = 1; i <= 5; ++i) { |
298 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
299 |
|
|
} |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
// Exception tests |
303 |
|
|
|
304 |
dl |
1.5 |
/** |
305 |
|
|
* Cannot create with negative capacity |
306 |
|
|
*/ |
307 |
|
|
public void testConstructor1() { |
308 |
|
|
try { |
309 |
dl |
1.1 |
new ConcurrentHashMap(-1,0,1); |
310 |
dl |
1.5 |
shouldThrow(); |
311 |
|
|
} catch(IllegalArgumentException e){} |
312 |
dl |
1.1 |
} |
313 |
|
|
|
314 |
dl |
1.5 |
/** |
315 |
|
|
* Cannot create with negative concurrency level |
316 |
|
|
*/ |
317 |
|
|
public void testConstructor2() { |
318 |
|
|
try { |
319 |
dl |
1.1 |
new ConcurrentHashMap(1,0,-1); |
320 |
dl |
1.5 |
shouldThrow(); |
321 |
|
|
} catch(IllegalArgumentException e){} |
322 |
dl |
1.1 |
} |
323 |
|
|
|
324 |
dl |
1.5 |
/** |
325 |
|
|
* Cannot create with only negative capacity |
326 |
|
|
*/ |
327 |
|
|
public void testConstructor3() { |
328 |
|
|
try { |
329 |
dl |
1.1 |
new ConcurrentHashMap(-1); |
330 |
dl |
1.5 |
shouldThrow(); |
331 |
|
|
} catch(IllegalArgumentException e){} |
332 |
dl |
1.1 |
} |
333 |
|
|
|
334 |
dl |
1.5 |
/** |
335 |
|
|
* get(null) throws NPE |
336 |
|
|
*/ |
337 |
|
|
public void testGet_NullPointerException() { |
338 |
|
|
try { |
339 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
340 |
|
|
c.get(null); |
341 |
dl |
1.5 |
shouldThrow(); |
342 |
|
|
} catch(NullPointerException e){} |
343 |
dl |
1.1 |
} |
344 |
|
|
|
345 |
dl |
1.5 |
/** |
346 |
|
|
* containsKey(null) throws NPE |
347 |
|
|
*/ |
348 |
|
|
public void testContainsKey_NullPointerException() { |
349 |
|
|
try { |
350 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
351 |
|
|
c.containsKey(null); |
352 |
dl |
1.5 |
shouldThrow(); |
353 |
|
|
} catch(NullPointerException e){} |
354 |
dl |
1.1 |
} |
355 |
|
|
|
356 |
dl |
1.5 |
/** |
357 |
|
|
* containsValue(null) throws NPE |
358 |
|
|
*/ |
359 |
|
|
public void testContainsValue_NullPointerException() { |
360 |
|
|
try { |
361 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
362 |
|
|
c.containsValue(null); |
363 |
dl |
1.5 |
shouldThrow(); |
364 |
|
|
} catch(NullPointerException e){} |
365 |
dl |
1.1 |
} |
366 |
|
|
|
367 |
dl |
1.5 |
/** |
368 |
|
|
* contains(null) throws NPE |
369 |
|
|
*/ |
370 |
|
|
public void testContains_NullPointerException() { |
371 |
|
|
try { |
372 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
373 |
|
|
c.contains(null); |
374 |
dl |
1.5 |
shouldThrow(); |
375 |
|
|
} catch(NullPointerException e){} |
376 |
dl |
1.1 |
} |
377 |
|
|
|
378 |
dl |
1.5 |
/** |
379 |
|
|
* put(null,x) throws NPE |
380 |
|
|
*/ |
381 |
|
|
public void testPut1_NullPointerException() { |
382 |
|
|
try { |
383 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
384 |
|
|
c.put(null, "whatever"); |
385 |
dl |
1.5 |
shouldThrow(); |
386 |
|
|
} catch(NullPointerException e){} |
387 |
dl |
1.1 |
} |
388 |
|
|
|
389 |
dl |
1.5 |
/** |
390 |
|
|
* put(x, null) throws NPE |
391 |
|
|
*/ |
392 |
|
|
public void testPut2_NullPointerException() { |
393 |
|
|
try { |
394 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
395 |
|
|
c.put("whatever", null); |
396 |
dl |
1.5 |
shouldThrow(); |
397 |
|
|
} catch(NullPointerException e){} |
398 |
dl |
1.1 |
} |
399 |
|
|
|
400 |
dl |
1.5 |
/** |
401 |
|
|
* putIfAbsent(null, x) throws NPE |
402 |
|
|
*/ |
403 |
|
|
public void testPutIfAbsent1_NullPointerException() { |
404 |
|
|
try { |
405 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
406 |
|
|
c.putIfAbsent(null, "whatever"); |
407 |
dl |
1.5 |
shouldThrow(); |
408 |
|
|
} catch(NullPointerException e){} |
409 |
dl |
1.1 |
} |
410 |
|
|
|
411 |
dl |
1.5 |
/** |
412 |
dl |
1.7 |
* replace(null, x) throws NPE |
413 |
|
|
*/ |
414 |
|
|
public void testReplace_NullPointerException() { |
415 |
|
|
try { |
416 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
417 |
|
|
c.replace(null, "whatever"); |
418 |
|
|
shouldThrow(); |
419 |
|
|
} catch(NullPointerException e){} |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
/** |
423 |
|
|
* replace(null, x, y) throws NPE |
424 |
|
|
*/ |
425 |
|
|
public void testReplaceValue_NullPointerException() { |
426 |
|
|
try { |
427 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
428 |
|
|
c.replace(null, one, "whatever"); |
429 |
|
|
shouldThrow(); |
430 |
|
|
} catch(NullPointerException e){} |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
/** |
434 |
dl |
1.5 |
* putIfAbsent(x, null) throws NPE |
435 |
|
|
*/ |
436 |
|
|
public void testPutIfAbsent2_NullPointerException() { |
437 |
|
|
try { |
438 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
439 |
|
|
c.putIfAbsent("whatever", null); |
440 |
dl |
1.7 |
shouldThrow(); |
441 |
|
|
} catch(NullPointerException e){} |
442 |
|
|
} |
443 |
|
|
|
444 |
|
|
|
445 |
|
|
/** |
446 |
|
|
* replace(x, null) throws NPE |
447 |
|
|
*/ |
448 |
|
|
public void testReplace2_NullPointerException() { |
449 |
|
|
try { |
450 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
451 |
|
|
c.replace("whatever", null); |
452 |
|
|
shouldThrow(); |
453 |
|
|
} catch(NullPointerException e){} |
454 |
|
|
} |
455 |
|
|
|
456 |
|
|
/** |
457 |
|
|
* replace(x, null, y) throws NPE |
458 |
|
|
*/ |
459 |
|
|
public void testReplaceValue2_NullPointerException() { |
460 |
|
|
try { |
461 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
462 |
|
|
c.replace("whatever", null, "A"); |
463 |
|
|
shouldThrow(); |
464 |
|
|
} catch(NullPointerException e){} |
465 |
|
|
} |
466 |
|
|
|
467 |
|
|
/** |
468 |
|
|
* replace(x, y, null) throws NPE |
469 |
|
|
*/ |
470 |
|
|
public void testReplaceValue3_NullPointerException() { |
471 |
|
|
try { |
472 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
473 |
|
|
c.replace("whatever", one, null); |
474 |
dl |
1.5 |
shouldThrow(); |
475 |
|
|
} catch(NullPointerException e){} |
476 |
dl |
1.1 |
} |
477 |
|
|
|
478 |
|
|
|
479 |
dl |
1.5 |
/** |
480 |
|
|
* remove(null) throws NPE |
481 |
|
|
*/ |
482 |
|
|
public void testRemove1_NullPointerException() { |
483 |
|
|
try { |
484 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
485 |
|
|
c.put("sadsdf", "asdads"); |
486 |
|
|
c.remove(null); |
487 |
dl |
1.5 |
shouldThrow(); |
488 |
|
|
} catch(NullPointerException e){} |
489 |
dl |
1.1 |
} |
490 |
|
|
|
491 |
dl |
1.5 |
/** |
492 |
|
|
* remove(null, x) throws NPE |
493 |
|
|
*/ |
494 |
|
|
public void testRemove2_NullPointerException() { |
495 |
|
|
try { |
496 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
497 |
|
|
c.put("sadsdf", "asdads"); |
498 |
|
|
c.remove(null, "whatever"); |
499 |
dl |
1.5 |
shouldThrow(); |
500 |
|
|
} catch(NullPointerException e){} |
501 |
dl |
1.1 |
} |
502 |
dl |
1.2 |
|
503 |
dl |
1.5 |
/** |
504 |
|
|
* A deserialized map equals original |
505 |
|
|
*/ |
506 |
dl |
1.2 |
public void testSerialization() { |
507 |
|
|
ConcurrentHashMap q = map5(); |
508 |
|
|
|
509 |
|
|
try { |
510 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
511 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
512 |
|
|
out.writeObject(q); |
513 |
|
|
out.close(); |
514 |
|
|
|
515 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
516 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
517 |
|
|
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
518 |
|
|
assertEquals(q.size(), r.size()); |
519 |
|
|
assertTrue(q.equals(r)); |
520 |
|
|
assertTrue(r.equals(q)); |
521 |
|
|
} catch(Exception e){ |
522 |
|
|
e.printStackTrace(); |
523 |
dl |
1.5 |
unexpectedException(); |
524 |
dl |
1.2 |
} |
525 |
|
|
} |
526 |
dl |
1.6 |
|
527 |
|
|
|
528 |
|
|
/** |
529 |
|
|
* SetValue of an EntrySet entry sets value in the map. |
530 |
|
|
*/ |
531 |
|
|
public void testSetValueWriteThrough() { |
532 |
|
|
// Adapted from a bug report by Eric Zoerner |
533 |
|
|
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
534 |
|
|
assertTrue(map.isEmpty()); |
535 |
|
|
for (int i = 0; i < 20; i++) |
536 |
|
|
map.put(new Integer(i), new Integer(i)); |
537 |
|
|
assertFalse(map.isEmpty()); |
538 |
|
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
539 |
|
|
|
540 |
|
|
// assert that entry1 is not 16 |
541 |
|
|
assertTrue("entry is 16, test not valid", |
542 |
|
|
!entry1.getKey().equals(new Integer(16))); |
543 |
|
|
|
544 |
|
|
// remove 16 (a different key) from map |
545 |
|
|
// which just happens to cause entry1 to be cloned in map |
546 |
|
|
map.remove(new Integer(16)); |
547 |
|
|
entry1.setValue("XYZ"); |
548 |
|
|
assertTrue(map.containsValue("XYZ")); // fails |
549 |
|
|
} |
550 |
dl |
1.1 |
|
551 |
|
|
} |