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.5 by dl, Sat Sep 20 18:20:07 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 210 | Line 211 | public class ConcurrentHashMapTest exten
211       */
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      /**
# Line 223 | Line 224 | public class ConcurrentHashMapTest exten
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() {
# Line 377 | Line 418 | public class ConcurrentHashMapTest exten
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 +    /**
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() {
# Line 389 | Line 452 | public class ConcurrentHashMapTest exten
452  
453  
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() {
# Line 435 | Line 532 | public class ConcurrentHashMapTest exten
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