11 |
|
import java.util.Enumeration; |
12 |
|
import java.io.*; |
13 |
|
|
14 |
< |
public class ConcurrentHashMapTest extends TestCase{ |
14 |
> |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
19 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
20 |
|
} |
21 |
|
|
22 |
< |
static final Integer one = new Integer(1); |
23 |
< |
static final Integer two = new Integer(2); |
24 |
< |
static final Integer three = new Integer(3); |
25 |
< |
static final Integer four = new Integer(4); |
26 |
< |
static final Integer five = new Integer(5); |
27 |
< |
|
22 |
> |
/** |
23 |
> |
* Create a map from Integers 1-5 to Strings "A"-"E". |
24 |
> |
*/ |
25 |
|
private static ConcurrentHashMap map5() { |
26 |
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
27 |
|
assertTrue(map.isEmpty()); |
36 |
|
} |
37 |
|
|
38 |
|
/** |
39 |
< |
* Test to verify clear correctly removes all key-element pairs from the map |
39 |
> |
* clear removes all pairs |
40 |
|
*/ |
41 |
< |
public void testClear(){ |
41 |
> |
public void testClear() { |
42 |
|
ConcurrentHashMap map = map5(); |
43 |
|
map.clear(); |
44 |
|
assertEquals(map.size(), 0); |
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
< |
* Test to verify contains gives the appropriate value |
48 |
> |
* Maps with same contents are equal |
49 |
> |
*/ |
50 |
> |
public void testEquals() { |
51 |
> |
ConcurrentHashMap map1 = map5(); |
52 |
> |
ConcurrentHashMap map2 = map5(); |
53 |
> |
assertEquals(map1, map2); |
54 |
> |
assertEquals(map2, map1); |
55 |
> |
map1.clear(); |
56 |
> |
assertFalse(map1.equals(map2)); |
57 |
> |
assertFalse(map2.equals(map1)); |
58 |
> |
} |
59 |
> |
|
60 |
> |
/** |
61 |
> |
* contains returns true for contained value |
62 |
|
*/ |
63 |
< |
public void testContains(){ |
63 |
> |
public void testContains() { |
64 |
|
ConcurrentHashMap map = map5(); |
65 |
|
assertTrue(map.contains("A")); |
66 |
|
assertFalse(map.contains("Z")); |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* Test to verify containsKey gives the appropriate value |
70 |
> |
* containsKey returns true for contained key |
71 |
|
*/ |
72 |
< |
public void testContainsKey(){ |
72 |
> |
public void testContainsKey() { |
73 |
|
ConcurrentHashMap map = map5(); |
74 |
|
assertTrue(map.containsKey(one)); |
75 |
< |
assertFalse(map.containsKey(new Integer(100))); |
75 |
> |
assertFalse(map.containsKey(zero)); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
< |
* Identical to normal contains |
79 |
> |
* containsValue returns true for held values |
80 |
|
*/ |
81 |
< |
public void testContainsValue(){ |
81 |
> |
public void testContainsValue() { |
82 |
|
ConcurrentHashMap map = map5(); |
83 |
|
assertTrue(map.contains("A")); |
84 |
|
assertFalse(map.contains("Z")); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
88 |
< |
* tes to verify enumeration returns an enumeration containing the correct elements |
88 |
> |
* enumeration returns an enumeration containing the correct |
89 |
> |
* elements |
90 |
|
*/ |
91 |
< |
public void testEnumeration(){ |
91 |
> |
public void testEnumeration() { |
92 |
|
ConcurrentHashMap map = map5(); |
93 |
|
Enumeration e = map.elements(); |
94 |
|
int count = 0; |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* Test to verify get returns the correct element at the given index |
103 |
> |
* Clone creates an equal map |
104 |
|
*/ |
105 |
< |
public void testGet(){ |
105 |
> |
public void testClone() { |
106 |
|
ConcurrentHashMap map = map5(); |
107 |
< |
assertEquals("A", (String)map.get(one)); |
107 |
> |
ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone()); |
108 |
> |
assertEquals(map, m2); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* Test to verify get on a nonexistant key returns null |
112 |
> |
* get returns the correct element at the given key, |
113 |
> |
* or null if not present |
114 |
|
*/ |
115 |
< |
public void testGet2(){ |
115 |
> |
public void testGet() { |
116 |
> |
ConcurrentHashMap map = map5(); |
117 |
> |
assertEquals("A", (String)map.get(one)); |
118 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
119 |
< |
assertNull(empty.get("anything")); |
119 |
> |
assertNull(map.get("anything")); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* Simple test to verify isEmpty returns the correct value |
123 |
> |
* isEmpty is true of empty map and false for non-empty |
124 |
|
*/ |
125 |
< |
public void testIsEmpty(){ |
125 |
> |
public void testIsEmpty() { |
126 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
127 |
|
ConcurrentHashMap map = map5(); |
128 |
|
assertTrue(empty.isEmpty()); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* Test to verify keys returns an enumeration containing all the keys from the map |
133 |
> |
* keys returns an enumeration containing all the keys from the map |
134 |
|
*/ |
135 |
< |
public void testKeys(){ |
135 |
> |
public void testKeys() { |
136 |
|
ConcurrentHashMap map = map5(); |
137 |
|
Enumeration e = map.keys(); |
138 |
|
int count = 0; |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* Test to verify keySet returns a Set containing all the keys |
147 |
> |
* keySet returns a Set containing all the keys |
148 |
|
*/ |
149 |
< |
public void testKeySet(){ |
149 |
> |
public void testKeySet() { |
150 |
|
ConcurrentHashMap map = map5(); |
151 |
|
Set s = map.keySet(); |
152 |
|
assertEquals(5, s.size()); |
157 |
|
assertTrue(s.contains(five)); |
158 |
|
} |
159 |
|
|
160 |
< |
public void testValues(){ |
160 |
> |
/** |
161 |
> |
* values collection contains all values |
162 |
> |
*/ |
163 |
> |
public void testValues() { |
164 |
|
ConcurrentHashMap map = map5(); |
165 |
|
Collection s = map.values(); |
166 |
|
assertEquals(5, s.size()); |
171 |
|
assertTrue(s.contains("E")); |
172 |
|
} |
173 |
|
|
174 |
< |
public void testEntrySet(){ |
174 |
> |
/** |
175 |
> |
* entrySet contains all pairs |
176 |
> |
*/ |
177 |
> |
public void testEntrySet() { |
178 |
|
ConcurrentHashMap map = map5(); |
179 |
|
Set s = map.entrySet(); |
180 |
|
assertEquals(5, s.size()); |
191 |
|
} |
192 |
|
|
193 |
|
/** |
194 |
< |
* Test to verify putAll correctly adds all key-value pairs from the given map |
194 |
> |
* putAll adds all key-value pairs from the given map |
195 |
|
*/ |
196 |
< |
public void testPutAll(){ |
196 |
> |
public void testPutAll() { |
197 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
198 |
|
ConcurrentHashMap map = map5(); |
199 |
|
empty.putAll(map); |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* Test to verify putIfAbsent works when the given key is not present |
209 |
> |
* putIfAbsent works when the given key is not present |
210 |
|
*/ |
211 |
< |
public void testPutIfAbsent(){ |
211 |
> |
public void testPutIfAbsent() { |
212 |
|
ConcurrentHashMap map = map5(); |
213 |
|
map.putIfAbsent(new Integer(6), "Z"); |
214 |
|
assertTrue(map.containsKey(new Integer(6))); |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
< |
* Test to verify putIfAbsent does not add the pair if the key is already present |
218 |
> |
* putIfAbsent does not add the pair if the key is already present |
219 |
|
*/ |
220 |
< |
public void testPutIfAbsent2(){ |
220 |
> |
public void testPutIfAbsent2() { |
221 |
|
ConcurrentHashMap map = map5(); |
222 |
|
assertEquals("A", map.putIfAbsent(one, "Z")); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
< |
* Test to verify remove removes the correct key-value pair from the map |
226 |
> |
* remove removes the correct key-value pair from the map |
227 |
|
*/ |
228 |
< |
public void testRemove(){ |
228 |
> |
public void testRemove() { |
229 |
|
ConcurrentHashMap map = map5(); |
230 |
|
map.remove(five); |
231 |
|
assertEquals(4, map.size()); |
232 |
|
assertFalse(map.containsKey(five)); |
233 |
|
} |
234 |
|
|
235 |
< |
public void testRemove2(){ |
235 |
> |
/** |
236 |
> |
* remove(key,value) removes only if pair present |
237 |
> |
*/ |
238 |
> |
public void testRemove2() { |
239 |
|
ConcurrentHashMap map = map5(); |
240 |
|
map.remove(five, "E"); |
241 |
|
assertEquals(4, map.size()); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* Simple test to verify size returns the correct values |
250 |
> |
* size returns the correct values |
251 |
|
*/ |
252 |
< |
public void testSize(){ |
252 |
> |
public void testSize() { |
253 |
|
ConcurrentHashMap map = map5(); |
254 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
255 |
|
assertEquals(0, empty.size()); |
256 |
|
assertEquals(5, map.size()); |
257 |
|
} |
258 |
|
|
259 |
< |
public void testToString(){ |
259 |
> |
/** |
260 |
> |
* toString contains toString of elements |
261 |
> |
*/ |
262 |
> |
public void testToString() { |
263 |
|
ConcurrentHashMap map = map5(); |
264 |
|
String s = map.toString(); |
265 |
|
for (int i = 1; i <= 5; ++i) { |
269 |
|
|
270 |
|
// Exception tests |
271 |
|
|
272 |
< |
public void testConstructor1(){ |
273 |
< |
try{ |
272 |
> |
/** |
273 |
> |
* Cannot create with negative capacity |
274 |
> |
*/ |
275 |
> |
public void testConstructor1() { |
276 |
> |
try { |
277 |
|
new ConcurrentHashMap(-1,0,1); |
278 |
< |
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
279 |
< |
}catch(IllegalArgumentException e){} |
278 |
> |
shouldThrow(); |
279 |
> |
} catch(IllegalArgumentException e){} |
280 |
|
} |
281 |
|
|
282 |
< |
public void testConstructor2(){ |
283 |
< |
try{ |
282 |
> |
/** |
283 |
> |
* Cannot create with negative concurrency level |
284 |
> |
*/ |
285 |
> |
public void testConstructor2() { |
286 |
> |
try { |
287 |
|
new ConcurrentHashMap(1,0,-1); |
288 |
< |
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
289 |
< |
}catch(IllegalArgumentException e){} |
288 |
> |
shouldThrow(); |
289 |
> |
} catch(IllegalArgumentException e){} |
290 |
|
} |
291 |
|
|
292 |
< |
public void testConstructor3(){ |
293 |
< |
try{ |
292 |
> |
/** |
293 |
> |
* Cannot create with only negative capacity |
294 |
> |
*/ |
295 |
> |
public void testConstructor3() { |
296 |
> |
try { |
297 |
|
new ConcurrentHashMap(-1); |
298 |
< |
fail("ConcurrentHashMap(int) should throw Illegal Argument Exception"); |
299 |
< |
}catch(IllegalArgumentException e){} |
298 |
> |
shouldThrow(); |
299 |
> |
} catch(IllegalArgumentException e){} |
300 |
|
} |
301 |
|
|
302 |
< |
public void testGet_NullPointerException(){ |
303 |
< |
try{ |
302 |
> |
/** |
303 |
> |
* get(null) throws NPE |
304 |
> |
*/ |
305 |
> |
public void testGet_NullPointerException() { |
306 |
> |
try { |
307 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
308 |
|
c.get(null); |
309 |
< |
fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception"); |
310 |
< |
}catch(NullPointerException e){} |
309 |
> |
shouldThrow(); |
310 |
> |
} catch(NullPointerException e){} |
311 |
|
} |
312 |
|
|
313 |
< |
public void testContainsKey_NullPointerException(){ |
314 |
< |
try{ |
313 |
> |
/** |
314 |
> |
* containsKey(null) throws NPE |
315 |
> |
*/ |
316 |
> |
public void testContainsKey_NullPointerException() { |
317 |
> |
try { |
318 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
319 |
|
c.containsKey(null); |
320 |
< |
fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception"); |
321 |
< |
}catch(NullPointerException e){} |
320 |
> |
shouldThrow(); |
321 |
> |
} catch(NullPointerException e){} |
322 |
|
} |
323 |
|
|
324 |
< |
public void testContainsValue_NullPointerException(){ |
325 |
< |
try{ |
324 |
> |
/** |
325 |
> |
* containsValue(null) throws NPE |
326 |
> |
*/ |
327 |
> |
public void testContainsValue_NullPointerException() { |
328 |
> |
try { |
329 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
330 |
|
c.containsValue(null); |
331 |
< |
fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception"); |
332 |
< |
}catch(NullPointerException e){} |
331 |
> |
shouldThrow(); |
332 |
> |
} catch(NullPointerException e){} |
333 |
|
} |
334 |
|
|
335 |
< |
public void testContains_NullPointerException(){ |
336 |
< |
try{ |
335 |
> |
/** |
336 |
> |
* contains(null) throws NPE |
337 |
> |
*/ |
338 |
> |
public void testContains_NullPointerException() { |
339 |
> |
try { |
340 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
341 |
|
c.contains(null); |
342 |
< |
fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception"); |
343 |
< |
}catch(NullPointerException e){} |
342 |
> |
shouldThrow(); |
343 |
> |
} catch(NullPointerException e){} |
344 |
|
} |
345 |
|
|
346 |
< |
public void testPut1_NullPointerException(){ |
347 |
< |
try{ |
346 |
> |
/** |
347 |
> |
* put(null,x) throws NPE |
348 |
> |
*/ |
349 |
> |
public void testPut1_NullPointerException() { |
350 |
> |
try { |
351 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
352 |
|
c.put(null, "whatever"); |
353 |
< |
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
354 |
< |
}catch(NullPointerException e){} |
353 |
> |
shouldThrow(); |
354 |
> |
} catch(NullPointerException e){} |
355 |
|
} |
356 |
|
|
357 |
< |
public void testPut2_NullPointerException(){ |
358 |
< |
try{ |
357 |
> |
/** |
358 |
> |
* put(x, null) throws NPE |
359 |
> |
*/ |
360 |
> |
public void testPut2_NullPointerException() { |
361 |
> |
try { |
362 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
363 |
|
c.put("whatever", null); |
364 |
< |
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
365 |
< |
}catch(NullPointerException e){} |
364 |
> |
shouldThrow(); |
365 |
> |
} catch(NullPointerException e){} |
366 |
|
} |
367 |
|
|
368 |
< |
public void testPutIfAbsent1_NullPointerException(){ |
369 |
< |
try{ |
368 |
> |
/** |
369 |
> |
* putIfAbsent(null, x) throws NPE |
370 |
> |
*/ |
371 |
> |
public void testPutIfAbsent1_NullPointerException() { |
372 |
> |
try { |
373 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
374 |
|
c.putIfAbsent(null, "whatever"); |
375 |
< |
fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
376 |
< |
}catch(NullPointerException e){} |
375 |
> |
shouldThrow(); |
376 |
> |
} catch(NullPointerException e){} |
377 |
|
} |
378 |
|
|
379 |
< |
public void testPutIfAbsent2_NullPointerException(){ |
380 |
< |
try{ |
379 |
> |
/** |
380 |
> |
* putIfAbsent(x, null) throws NPE |
381 |
> |
*/ |
382 |
> |
public void testPutIfAbsent2_NullPointerException() { |
383 |
> |
try { |
384 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
385 |
|
c.putIfAbsent("whatever", null); |
386 |
< |
fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
387 |
< |
}catch(NullPointerException e){} |
386 |
> |
shouldThrow(); |
387 |
> |
} catch(NullPointerException e){} |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
< |
public void testRemove1_NullPointerException(){ |
392 |
< |
try{ |
391 |
> |
/** |
392 |
> |
* remove(null) throws NPE |
393 |
> |
*/ |
394 |
> |
public void testRemove1_NullPointerException() { |
395 |
> |
try { |
396 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
397 |
|
c.put("sadsdf", "asdads"); |
398 |
|
c.remove(null); |
399 |
< |
fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione"); |
400 |
< |
}catch(NullPointerException e){} |
399 |
> |
shouldThrow(); |
400 |
> |
} catch(NullPointerException e){} |
401 |
|
} |
402 |
|
|
403 |
< |
public void testRemove2_NullPointerException(){ |
404 |
< |
try{ |
403 |
> |
/** |
404 |
> |
* remove(null, x) throws NPE |
405 |
> |
*/ |
406 |
> |
public void testRemove2_NullPointerException() { |
407 |
> |
try { |
408 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
409 |
|
c.put("sadsdf", "asdads"); |
410 |
|
c.remove(null, "whatever"); |
411 |
< |
fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione"); |
412 |
< |
}catch(NullPointerException e){} |
411 |
> |
shouldThrow(); |
412 |
> |
} catch(NullPointerException e){} |
413 |
|
} |
414 |
|
|
415 |
+ |
/** |
416 |
+ |
* A deserialized map equals original |
417 |
+ |
*/ |
418 |
|
public void testSerialization() { |
419 |
|
ConcurrentHashMap q = map5(); |
420 |
|
|
432 |
|
assertTrue(r.equals(q)); |
433 |
|
} catch(Exception e){ |
434 |
|
e.printStackTrace(); |
435 |
< |
fail("unexpected exception"); |
435 |
> |
unexpectedException(); |
436 |
|
} |
437 |
|
} |
369 |
– |
|
438 |
|
|
439 |
|
} |