ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentHashMapTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 00:31:57 2003 UTC vs.
Revision 1.9 by dl, Sat Dec 27 19:26:43 2003 UTC

# Line 1 | Line 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.
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 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 19 | Line 20 | public class ConcurrentHashMapTest exten
20          return new TestSuite(ConcurrentHashMapTest.class);
21      }
22  
23 +    /**
24 +     * Create a map from Integers 1-5 to Strings "A"-"E".
25 +     */
26      private static ConcurrentHashMap map5() {  
27          ConcurrentHashMap map = new ConcurrentHashMap(5);
28          assertTrue(map.isEmpty());
# Line 33 | Line 37 | public class ConcurrentHashMapTest exten
37      }
38  
39      /**
40 <     *   clear  removes all key-element pairs from the map
40 >     *  clear removes all pairs
41       */
42 <    public void testClear(){
42 >    public void testClear() {
43          ConcurrentHashMap map = map5();
44          map.clear();
45          assertEquals(map.size(), 0);
46      }
47  
48      /**
49 <     *   contains gives the appropriate value
49 >     *  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       */
64 <    public void testContains(){
64 >    public void testContains() {
65          ConcurrentHashMap map = map5();
66          assertTrue(map.contains("A"));
67          assertFalse(map.contains("Z"));
68      }
69      
70      /**
71 <     *   containsKey gives the appropriate value
71 >     *  containsKey returns true for contained key
72       */
73 <    public void testContainsKey(){
73 >    public void testContainsKey() {
74          ConcurrentHashMap map = map5();
75          assertTrue(map.containsKey(one));
76 <        assertFalse(map.containsKey(new Integer(100)));
76 >        assertFalse(map.containsKey(zero));
77      }
78  
79      /**
80 <     *  Identical to normal contains
80 >     *  containsValue returns true for held values
81       */
82 <    public void testContainsValue(){
82 >    public void testContainsValue() {
83          ConcurrentHashMap map = map5();
84          assertTrue(map.contains("A"));
85          assertFalse(map.contains("Z"));
# Line 72 | Line 89 | public class ConcurrentHashMapTest exten
89       *   enumeration returns an enumeration containing the correct
90       *   elements
91       */
92 <    public void testEnumeration(){
92 >    public void testEnumeration() {
93          ConcurrentHashMap map = map5();
94          Enumeration e = map.elements();
95          int count = 0;
# Line 86 | Line 103 | public class ConcurrentHashMapTest exten
103      /**
104       *   Clone creates an equal map
105       */
106 <    public void testClone(){
106 >    public void testClone() {
107          ConcurrentHashMap map = map5();
108          ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone());
109          assertEquals(map, m2);
110      }
111  
112      /**
113 <     *   get returns the correct element at the given index
113 >     *  get returns the correct element at the given key,
114 >     *  or null if not present
115       */
116 <    public void testGet(){
116 >    public void testGet() {
117          ConcurrentHashMap map = map5();
118          assertEquals("A", (String)map.get(one));
101    }
102
103    /**
104     *   get on a nonexistant key returns null
105     */
106    public void testGet2(){
119          ConcurrentHashMap empty = new ConcurrentHashMap();
120 <        assertNull(empty.get("anything"));
120 >        assertNull(map.get("anything"));
121      }
122  
123      /**
124 <     *  Simple test to verify isEmpty returns the correct value
124 >     *  isEmpty is true of empty map and false for non-empty
125       */
126 <    public void testIsEmpty(){
126 >    public void testIsEmpty() {
127          ConcurrentHashMap empty = new ConcurrentHashMap();
128          ConcurrentHashMap map = map5();
129          assertTrue(empty.isEmpty());
# Line 121 | Line 133 | public class ConcurrentHashMapTest exten
133      /**
134       *   keys returns an enumeration containing all the keys from the map
135       */
136 <    public void testKeys(){
136 >    public void testKeys() {
137          ConcurrentHashMap map = map5();
138          Enumeration e = map.keys();
139          int count = 0;
# Line 135 | Line 147 | public class ConcurrentHashMapTest exten
147      /**
148       *   keySet returns a Set containing all the keys
149       */
150 <    public void testKeySet(){
150 >    public void testKeySet() {
151          ConcurrentHashMap map = map5();
152          Set s = map.keySet();
153          assertEquals(5, s.size());
# Line 146 | Line 158 | public class ConcurrentHashMapTest exten
158          assertTrue(s.contains(five));
159      }
160  
161 <    public void testValues(){
161 >    /**
162 >     * values collection contains all values
163 >     */
164 >    public void testValues() {
165          ConcurrentHashMap map = map5();
166          Collection s = map.values();
167          assertEquals(5, s.size());
# Line 157 | Line 172 | public class ConcurrentHashMapTest exten
172          assertTrue(s.contains("E"));
173      }
174  
175 <    public void testEntrySet(){
175 >    /**
176 >     * entrySet contains all pairs
177 >     */
178 >    public void testEntrySet() {
179          ConcurrentHashMap map = map5();
180          Set s = map.entrySet();
181          assertEquals(5, s.size());
# Line 176 | Line 194 | public class ConcurrentHashMapTest exten
194      /**
195       *   putAll  adds all key-value pairs from the given map
196       */
197 <    public void testPutAll(){
197 >    public void testPutAll() {
198          ConcurrentHashMap empty = new ConcurrentHashMap();
199          ConcurrentHashMap map = map5();
200          empty.putAll(map);
# Line 191 | Line 209 | public class ConcurrentHashMapTest exten
209      /**
210       *   putIfAbsent works when the given key is not present
211       */
212 <    public void testPutIfAbsent(){
212 >    public void testPutIfAbsent() {
213          ConcurrentHashMap map = map5();
214 <        map.putIfAbsent(new Integer(6), "Z");
215 <        assertTrue(map.containsKey(new Integer(6)));
214 >        map.putIfAbsent(six, "Z");
215 >        assertTrue(map.containsKey(six));
216      }
217  
218      /**
219       *   putIfAbsent does not add the pair if the key is already present
220       */
221 <    public void testPutIfAbsent2(){
221 >    public void testPutIfAbsent2() {
222          ConcurrentHashMap map = map5();
223          assertEquals("A", map.putIfAbsent(one, "Z"));
224      }
225  
226      /**
227 +     *   replace fails when the given key is not present
228 +     */
229 +    public void testReplace() {
230 +        ConcurrentHashMap map = map5();
231 +        assertNull(map.replace(six, "Z"));
232 +        assertFalse(map.containsKey(six));
233 +    }
234 +
235 +    /**
236 +     *   replace succeeds if the key is already present
237 +     */
238 +    public void testReplace2() {
239 +        ConcurrentHashMap map = map5();
240 +        assertNotNull(map.replace(one, "Z"));
241 +        assertEquals("Z", map.get(one));
242 +    }
243 +
244 +
245 +    /**
246 +     * replace value fails when the given key not mapped to expected value
247 +     */
248 +    public void testReplaceValue() {
249 +        ConcurrentHashMap map = map5();
250 +        assertEquals("A", map.get(one));
251 +        assertFalse(map.replace(one, "Z", "Z"));
252 +        assertEquals("A", map.get(one));
253 +    }
254 +
255 +    /**
256 +     * replace value succeeds when the given key mapped to expected value
257 +     */
258 +    public void testReplaceValue2() {
259 +        ConcurrentHashMap map = map5();
260 +        assertEquals("A", map.get(one));
261 +        assertTrue(map.replace(one, "A", "Z"));
262 +        assertEquals("Z", map.get(one));
263 +    }
264 +
265 +
266 +    /**
267       *   remove removes the correct key-value pair from the map
268       */
269 <    public void testRemove(){
269 >    public void testRemove() {
270          ConcurrentHashMap map = map5();
271          map.remove(five);
272          assertEquals(4, map.size());
273          assertFalse(map.containsKey(five));
274      }
275  
276 <    public void testRemove2(){
276 >    /**
277 >     * remove(key,value) removes only if pair present
278 >     */
279 >    public void testRemove2() {
280          ConcurrentHashMap map = map5();
281          map.remove(five, "E");
282          assertEquals(4, map.size());
# Line 229 | Line 290 | public class ConcurrentHashMapTest exten
290      /**
291       *   size returns the correct values
292       */
293 <    public void testSize(){
293 >    public void testSize() {
294          ConcurrentHashMap map = map5();
295          ConcurrentHashMap empty = new ConcurrentHashMap();
296          assertEquals(0, empty.size());
297          assertEquals(5, map.size());
298      }
299  
300 <    public void testToString(){
300 >    /**
301 >     * toString contains toString of elements
302 >     */
303 >    public void testToString() {
304          ConcurrentHashMap map = map5();
305          String s = map.toString();
306          for (int i = 1; i <= 5; ++i) {
# Line 246 | Line 310 | public class ConcurrentHashMapTest exten
310  
311      // Exception tests
312      
313 <    public void testConstructor1(){
314 <        try{
313 >    /**
314 >     * Cannot create with negative capacity
315 >     */
316 >    public void testConstructor1() {
317 >        try {
318              new ConcurrentHashMap(-1,0,1);
319 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
320 <        }catch(IllegalArgumentException e){}
319 >            shouldThrow();
320 >        } catch(IllegalArgumentException e){}
321      }
322  
323 <    public void testConstructor2(){
324 <        try{
323 >    /**
324 >     * Cannot create with negative concurrency level
325 >     */
326 >    public void testConstructor2() {
327 >        try {
328              new ConcurrentHashMap(1,0,-1);
329 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
330 <        }catch(IllegalArgumentException e){}
329 >            shouldThrow();
330 >        } catch(IllegalArgumentException e){}
331      }
332  
333 <    public void testConstructor3(){
334 <        try{
333 >    /**
334 >     * Cannot create with only negative capacity
335 >     */
336 >    public void testConstructor3() {
337 >        try {
338              new ConcurrentHashMap(-1);
339 <            fail("ConcurrentHashMap(int) should throw Illegal Argument Exception");
340 <        }catch(IllegalArgumentException e){}
339 >            shouldThrow();
340 >        } catch(IllegalArgumentException e){}
341      }
342  
343 <    public void testGet_NullPointerException(){
344 <        try{
343 >    /**
344 >     * get(null) throws NPE
345 >     */
346 >    public void testGet_NullPointerException() {
347 >        try {
348              ConcurrentHashMap c = new ConcurrentHashMap(5);
349              c.get(null);
350 <            fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception");
351 <        }catch(NullPointerException e){}
350 >            shouldThrow();
351 >        } catch(NullPointerException e){}
352      }
353  
354 <    public void testContainsKey_NullPointerException(){
355 <        try{
354 >    /**
355 >     * containsKey(null) throws NPE
356 >     */
357 >    public void testContainsKey_NullPointerException() {
358 >        try {
359              ConcurrentHashMap c = new ConcurrentHashMap(5);
360              c.containsKey(null);
361 <            fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception");
362 <        }catch(NullPointerException e){}
361 >            shouldThrow();
362 >        } catch(NullPointerException e){}
363      }
364  
365 <    public void testContainsValue_NullPointerException(){
366 <        try{
365 >    /**
366 >     * containsValue(null) throws NPE
367 >     */
368 >    public void testContainsValue_NullPointerException() {
369 >        try {
370              ConcurrentHashMap c = new ConcurrentHashMap(5);
371              c.containsValue(null);
372 <            fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception");
373 <        }catch(NullPointerException e){}
372 >            shouldThrow();
373 >        } catch(NullPointerException e){}
374      }
375  
376 <    public void testContains_NullPointerException(){
377 <        try{
376 >    /**
377 >     * contains(null) throws NPE
378 >     */
379 >    public void testContains_NullPointerException() {
380 >        try {
381              ConcurrentHashMap c = new ConcurrentHashMap(5);
382              c.contains(null);
383 <            fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception");
384 <        }catch(NullPointerException e){}
383 >            shouldThrow();
384 >        } catch(NullPointerException e){}
385      }
386  
387 <    public void testPut1_NullPointerException(){
388 <        try{
387 >    /**
388 >     * put(null,x) throws NPE
389 >     */
390 >    public void testPut1_NullPointerException() {
391 >        try {
392              ConcurrentHashMap c = new ConcurrentHashMap(5);
393              c.put(null, "whatever");
394 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
395 <        }catch(NullPointerException e){}
394 >            shouldThrow();
395 >        } catch(NullPointerException e){}
396      }
397  
398 <    public void testPut2_NullPointerException(){
399 <        try{
398 >    /**
399 >     * put(x, null) throws NPE
400 >     */
401 >    public void testPut2_NullPointerException() {
402 >        try {
403              ConcurrentHashMap c = new ConcurrentHashMap(5);
404              c.put("whatever", null);
405 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
406 <        }catch(NullPointerException e){}
405 >            shouldThrow();
406 >        } catch(NullPointerException e){}
407      }
408  
409 <    public void testPutIfAbsent1_NullPointerException(){
410 <        try{
409 >    /**
410 >     * putIfAbsent(null, x) throws NPE
411 >     */
412 >    public void testPutIfAbsent1_NullPointerException() {
413 >        try {
414              ConcurrentHashMap c = new ConcurrentHashMap(5);
415              c.putIfAbsent(null, "whatever");
416 <            fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
417 <        }catch(NullPointerException e){}
416 >            shouldThrow();
417 >        } catch(NullPointerException e){}
418 >    }
419 >
420 >    /**
421 >     * replace(null, x) throws NPE
422 >     */
423 >    public void testReplace_NullPointerException() {
424 >        try {
425 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
426 >            c.replace(null, "whatever");
427 >            shouldThrow();
428 >        } catch(NullPointerException e){}
429      }
430  
431 <    public void testPutIfAbsent2_NullPointerException(){
432 <        try{
431 >    /**
432 >     * replace(null, x, y) throws NPE
433 >     */
434 >    public void testReplaceValue_NullPointerException() {
435 >        try {
436 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
437 >            c.replace(null, one, "whatever");
438 >            shouldThrow();
439 >        } catch(NullPointerException e){}
440 >    }
441 >
442 >    /**
443 >     * putIfAbsent(x, null) throws NPE
444 >     */
445 >    public void testPutIfAbsent2_NullPointerException() {
446 >        try {
447              ConcurrentHashMap c = new ConcurrentHashMap(5);
448              c.putIfAbsent("whatever", null);
449 <            fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
450 <        }catch(NullPointerException e){}
449 >            shouldThrow();
450 >        } catch(NullPointerException e){}
451      }
452  
453  
454 <    public void testRemove1_NullPointerException(){
455 <        try{
454 >    /**
455 >     * replace(x, null) throws NPE
456 >     */
457 >    public void testReplace2_NullPointerException() {
458 >        try {
459 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
460 >            c.replace("whatever", null);
461 >            shouldThrow();
462 >        } catch(NullPointerException e){}
463 >    }
464 >
465 >    /**
466 >     * replace(x, null, y) throws NPE
467 >     */
468 >    public void testReplaceValue2_NullPointerException() {
469 >        try {
470 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
471 >            c.replace("whatever", null, "A");
472 >            shouldThrow();
473 >        } catch(NullPointerException e){}
474 >    }
475 >
476 >    /**
477 >     * replace(x, y, null) throws NPE
478 >     */
479 >    public void testReplaceValue3_NullPointerException() {
480 >        try {
481 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
482 >            c.replace("whatever", one, null);
483 >            shouldThrow();
484 >        } catch(NullPointerException e){}
485 >    }
486 >
487 >
488 >    /**
489 >     * remove(null) throws NPE
490 >     */
491 >    public void testRemove1_NullPointerException() {
492 >        try {
493              ConcurrentHashMap c = new ConcurrentHashMap(5);
494              c.put("sadsdf", "asdads");
495              c.remove(null);
496 <            fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione");
497 <        }catch(NullPointerException e){}
496 >            shouldThrow();
497 >        } catch(NullPointerException e){}
498      }
499  
500 <    public void testRemove2_NullPointerException(){
501 <        try{
500 >    /**
501 >     * remove(null, x) throws NPE
502 >     */
503 >    public void testRemove2_NullPointerException() {
504 >        try {
505              ConcurrentHashMap c = new ConcurrentHashMap(5);
506              c.put("sadsdf", "asdads");
507              c.remove(null, "whatever");
508 <            fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione");
509 <        }catch(NullPointerException e){}
508 >            shouldThrow();
509 >        } catch(NullPointerException e){}
510      }
511  
512 +    /**
513 +     * A deserialized map equals original
514 +     */
515      public void testSerialization() {
516          ConcurrentHashMap q = map5();
517  
# Line 367 | Line 529 | public class ConcurrentHashMapTest exten
529              assertTrue(r.equals(q));
530          } catch(Exception e){
531              e.printStackTrace();
532 <            fail("unexpected exception");
532 >            unexpectedException();
533          }
534      }
535  
536 +
537 +    /**
538 +     * SetValue of an EntrySet entry sets value in the map.
539 +     */
540 +    public void testSetValueWriteThrough() {
541 +        // Adapted from a bug report by Eric Zoerner
542 +        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
543 +        assertTrue(map.isEmpty());
544 +        for (int i = 0; i < 20; i++)
545 +            map.put(new Integer(i), new Integer(i));
546 +        assertFalse(map.isEmpty());
547 +        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
548 +        
549 +        // assert that entry1 is not 16
550 +        assertTrue("entry is 16, test not valid",
551 +                   !entry1.getKey().equals(new Integer(16)));
552 +        
553 +        // remove 16 (a different key) from map
554 +        // which just happens to cause entry1 to be cloned in map
555 +        map.remove(new Integer(16));
556 +        entry1.setValue("XYZ");
557 +        assertTrue(map.containsValue("XYZ")); // fails
558 +    }
559      
560   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines