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.18 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.28 by jsr166, Tue Feb 21 02:04:17 2012 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.util.Enumeration;
13 < import java.io.*;
11 > import java.util.concurrent.ConcurrentHashMap;
12  
13   public class ConcurrentHashMapTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18          return new TestSuite(ConcurrentHashMapTest.class);
19      }
20  
21      /**
22 <     * Create a map from Integers 1-5 to Strings "A"-"E".
22 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
23       */
24      private static ConcurrentHashMap map5() {
25          ConcurrentHashMap map = new ConcurrentHashMap(5);
# Line 37 | Line 35 | public class ConcurrentHashMapTest exten
35      }
36  
37      /**
38 <     *  clear removes all pairs
38 >     * clear removes all pairs
39       */
40      public void testClear() {
41          ConcurrentHashMap map = map5();
42          map.clear();
43 <        assertEquals(map.size(), 0);
43 >        assertEquals(0, map.size());
44      }
45  
46      /**
47 <     *  Maps with same contents are equal
47 >     * Maps with same contents are equal
48       */
49      public void testEquals() {
50          ConcurrentHashMap map1 = map5();
# Line 59 | Line 57 | public class ConcurrentHashMapTest exten
57      }
58  
59      /**
60 <     *  contains returns true for contained value
60 >     * contains returns true for contained value
61       */
62      public void testContains() {
63          ConcurrentHashMap map = map5();
# Line 68 | Line 66 | public class ConcurrentHashMapTest exten
66      }
67  
68      /**
69 <     *  containsKey returns true for contained key
69 >     * containsKey returns true for contained key
70       */
71      public void testContainsKey() {
72          ConcurrentHashMap map = map5();
# Line 77 | Line 75 | public class ConcurrentHashMapTest exten
75      }
76  
77      /**
78 <     *  containsValue returns true for held values
78 >     * containsValue returns true for held values
79       */
80      public void testContainsValue() {
81          ConcurrentHashMap map = map5();
# Line 86 | Line 84 | public class ConcurrentHashMapTest exten
84      }
85  
86      /**
87 <     *   enumeration returns an enumeration containing the correct
88 <     *   elements
87 >     * enumeration returns an enumeration containing the correct
88 >     * elements
89       */
90      public void testEnumeration() {
91          ConcurrentHashMap map = map5();
# Line 101 | Line 99 | public class ConcurrentHashMapTest exten
99      }
100  
101      /**
102 <     *  get returns the correct element at the given key,
103 <     *  or null if not present
102 >     * get returns the correct element at the given key,
103 >     * or null if not present
104       */
105      public void testGet() {
106          ConcurrentHashMap map = map5();
# Line 112 | Line 110 | public class ConcurrentHashMapTest exten
110      }
111  
112      /**
113 <     *  isEmpty is true of empty map and false for non-empty
113 >     * isEmpty is true of empty map and false for non-empty
114       */
115      public void testIsEmpty() {
116          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 122 | Line 120 | public class ConcurrentHashMapTest exten
120      }
121  
122      /**
123 <     *   keys returns an enumeration containing all the keys from the map
123 >     * keys returns an enumeration containing all the keys from the map
124       */
125      public void testKeys() {
126          ConcurrentHashMap map = map5();
# Line 136 | Line 134 | public class ConcurrentHashMapTest exten
134      }
135  
136      /**
137 <     *   keySet returns a Set containing all the keys
137 >     * keySet returns a Set containing all the keys
138       */
139      public void testKeySet() {
140          ConcurrentHashMap map = map5();
# Line 150 | Line 148 | public class ConcurrentHashMapTest exten
148      }
149  
150      /**
151 <     *  keySet.toArray returns contains all keys
151 >     * keySet.toArray returns contains all keys
152       */
153      public void testKeySetToArray() {
154          ConcurrentHashMap map = map5();
# Line 163 | Line 161 | public class ConcurrentHashMapTest exten
161      }
162  
163      /**
164 <     *  Values.toArray contains all values
164 >     * Values.toArray contains all values
165       */
166      public void testValuesToArray() {
167          ConcurrentHashMap map = map5();
# Line 179 | Line 177 | public class ConcurrentHashMapTest exten
177      }
178  
179      /**
180 <     *  entrySet.toArray contains all entries
180 >     * entrySet.toArray contains all entries
181       */
182      public void testEntrySetToArray() {
183          ConcurrentHashMap map = map5();
# Line 226 | Line 224 | public class ConcurrentHashMapTest exten
224      }
225  
226      /**
227 <     *   putAll  adds all key-value pairs from the given map
227 >     * putAll adds all key-value pairs from the given map
228       */
229      public void testPutAll() {
230          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 241 | Line 239 | public class ConcurrentHashMapTest exten
239      }
240  
241      /**
242 <     *   putIfAbsent works when the given key is not present
242 >     * putIfAbsent works when the given key is not present
243       */
244      public void testPutIfAbsent() {
245          ConcurrentHashMap map = map5();
# Line 250 | Line 248 | public class ConcurrentHashMapTest exten
248      }
249  
250      /**
251 <     *   putIfAbsent does not add the pair if the key is already present
251 >     * putIfAbsent does not add the pair if the key is already present
252       */
253      public void testPutIfAbsent2() {
254          ConcurrentHashMap map = map5();
# Line 258 | Line 256 | public class ConcurrentHashMapTest exten
256      }
257  
258      /**
259 <     *   replace fails when the given key is not present
259 >     * replace fails when the given key is not present
260       */
261      public void testReplace() {
262          ConcurrentHashMap map = map5();
# Line 267 | Line 265 | public class ConcurrentHashMapTest exten
265      }
266  
267      /**
268 <     *   replace succeeds if the key is already present
268 >     * replace succeeds if the key is already present
269       */
270      public void testReplace2() {
271          ConcurrentHashMap map = map5();
# Line 275 | Line 273 | public class ConcurrentHashMapTest exten
273          assertEquals("Z", map.get(one));
274      }
275  
278
276      /**
277       * replace value fails when the given key not mapped to expected value
278       */
# Line 296 | Line 293 | public class ConcurrentHashMapTest exten
293          assertEquals("Z", map.get(one));
294      }
295  
299
296      /**
297 <     *   remove removes the correct key-value pair from the map
297 >     * remove removes the correct key-value pair from the map
298       */
299      public void testRemove() {
300          ConcurrentHashMap map = map5();
# Line 318 | Line 314 | public class ConcurrentHashMapTest exten
314          map.remove(four, "A");
315          assertEquals(4, map.size());
316          assertTrue(map.containsKey(four));
321
317      }
318  
319      /**
320 <     *   size returns the correct values
320 >     * size returns the correct values
321       */
322      public void testSize() {
323          ConcurrentHashMap map = map5();
# Line 338 | Line 333 | public class ConcurrentHashMapTest exten
333          ConcurrentHashMap map = map5();
334          String s = map.toString();
335          for (int i = 1; i <= 5; ++i) {
336 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
336 >            assertTrue(s.contains(String.valueOf(i)));
337          }
338      }
339  
# Line 351 | Line 346 | public class ConcurrentHashMapTest exten
346          try {
347              new ConcurrentHashMap(-1,0,1);
348              shouldThrow();
349 <        } catch (IllegalArgumentException e) {}
349 >        } catch (IllegalArgumentException success) {}
350      }
351  
352      /**
# Line 361 | Line 356 | public class ConcurrentHashMapTest exten
356          try {
357              new ConcurrentHashMap(1,0,-1);
358              shouldThrow();
359 <        } catch (IllegalArgumentException e) {}
359 >        } catch (IllegalArgumentException success) {}
360      }
361  
362      /**
# Line 371 | Line 366 | public class ConcurrentHashMapTest exten
366          try {
367              new ConcurrentHashMap(-1);
368              shouldThrow();
369 <        } catch (IllegalArgumentException e) {}
369 >        } catch (IllegalArgumentException success) {}
370      }
371  
372      /**
# Line 382 | Line 377 | public class ConcurrentHashMapTest exten
377              ConcurrentHashMap c = new ConcurrentHashMap(5);
378              c.get(null);
379              shouldThrow();
380 <        } catch (NullPointerException e) {}
380 >        } catch (NullPointerException success) {}
381      }
382  
383      /**
# Line 393 | Line 388 | public class ConcurrentHashMapTest exten
388              ConcurrentHashMap c = new ConcurrentHashMap(5);
389              c.containsKey(null);
390              shouldThrow();
391 <        } catch (NullPointerException e) {}
391 >        } catch (NullPointerException success) {}
392      }
393  
394      /**
# Line 404 | Line 399 | public class ConcurrentHashMapTest exten
399              ConcurrentHashMap c = new ConcurrentHashMap(5);
400              c.containsValue(null);
401              shouldThrow();
402 <        } catch (NullPointerException e) {}
402 >        } catch (NullPointerException success) {}
403      }
404  
405      /**
# Line 415 | Line 410 | public class ConcurrentHashMapTest exten
410              ConcurrentHashMap c = new ConcurrentHashMap(5);
411              c.contains(null);
412              shouldThrow();
413 <        } catch (NullPointerException e) {}
413 >        } catch (NullPointerException success) {}
414      }
415  
416      /**
# Line 426 | Line 421 | public class ConcurrentHashMapTest exten
421              ConcurrentHashMap c = new ConcurrentHashMap(5);
422              c.put(null, "whatever");
423              shouldThrow();
424 <        } catch (NullPointerException e) {}
424 >        } catch (NullPointerException success) {}
425      }
426  
427      /**
# Line 437 | Line 432 | public class ConcurrentHashMapTest exten
432              ConcurrentHashMap c = new ConcurrentHashMap(5);
433              c.put("whatever", null);
434              shouldThrow();
435 <        } catch (NullPointerException e) {}
435 >        } catch (NullPointerException success) {}
436      }
437  
438      /**
# Line 448 | Line 443 | public class ConcurrentHashMapTest exten
443              ConcurrentHashMap c = new ConcurrentHashMap(5);
444              c.putIfAbsent(null, "whatever");
445              shouldThrow();
446 <        } catch (NullPointerException e) {}
446 >        } catch (NullPointerException success) {}
447      }
448  
449      /**
# Line 459 | Line 454 | public class ConcurrentHashMapTest exten
454              ConcurrentHashMap c = new ConcurrentHashMap(5);
455              c.replace(null, "whatever");
456              shouldThrow();
457 <        } catch (NullPointerException e) {}
457 >        } catch (NullPointerException success) {}
458      }
459  
460      /**
# Line 470 | Line 465 | public class ConcurrentHashMapTest exten
465              ConcurrentHashMap c = new ConcurrentHashMap(5);
466              c.replace(null, one, "whatever");
467              shouldThrow();
468 <        } catch (NullPointerException e) {}
468 >        } catch (NullPointerException success) {}
469      }
470  
471      /**
# Line 481 | Line 476 | public class ConcurrentHashMapTest exten
476              ConcurrentHashMap c = new ConcurrentHashMap(5);
477              c.putIfAbsent("whatever", null);
478              shouldThrow();
479 <        } catch (NullPointerException e) {}
479 >        } catch (NullPointerException success) {}
480      }
481  
487
482      /**
483       * replace(x, null) throws NPE
484       */
# Line 493 | Line 487 | public class ConcurrentHashMapTest exten
487              ConcurrentHashMap c = new ConcurrentHashMap(5);
488              c.replace("whatever", null);
489              shouldThrow();
490 <        } catch (NullPointerException e) {}
490 >        } catch (NullPointerException success) {}
491      }
492  
493      /**
# Line 504 | Line 498 | public class ConcurrentHashMapTest exten
498              ConcurrentHashMap c = new ConcurrentHashMap(5);
499              c.replace("whatever", null, "A");
500              shouldThrow();
501 <        } catch (NullPointerException e) {}
501 >        } catch (NullPointerException success) {}
502      }
503  
504      /**
# Line 515 | Line 509 | public class ConcurrentHashMapTest exten
509              ConcurrentHashMap c = new ConcurrentHashMap(5);
510              c.replace("whatever", one, null);
511              shouldThrow();
512 <        } catch (NullPointerException e) {}
512 >        } catch (NullPointerException success) {}
513      }
514  
521
515      /**
516       * remove(null) throws NPE
517       */
# Line 528 | Line 521 | public class ConcurrentHashMapTest exten
521              c.put("sadsdf", "asdads");
522              c.remove(null);
523              shouldThrow();
524 <        } catch (NullPointerException e) {}
524 >        } catch (NullPointerException success) {}
525      }
526  
527      /**
# Line 540 | Line 533 | public class ConcurrentHashMapTest exten
533              c.put("sadsdf", "asdads");
534              c.remove(null, "whatever");
535              shouldThrow();
536 <        } catch (NullPointerException e) {}
536 >        } catch (NullPointerException success) {}
537      }
538  
539      /**
540       * remove(x, null) returns false
541       */
542      public void testRemove3() {
543 <        try {
544 <            ConcurrentHashMap c = new ConcurrentHashMap(5);
545 <            c.put("sadsdf", "asdads");
553 <            assertFalse(c.remove("sadsdf", null));
554 <        } catch (NullPointerException e) {
555 <            fail();
556 <        }
543 >        ConcurrentHashMap c = new ConcurrentHashMap(5);
544 >        c.put("sadsdf", "asdads");
545 >        assertFalse(c.remove("sadsdf", null));
546      }
547  
548      /**
549       * A deserialized map equals original
550       */
551      public void testSerialization() throws Exception {
552 <        ConcurrentHashMap q = map5();
552 >        Map x = map5();
553 >        Map y = serialClone(x);
554  
555 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
556 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
557 <        out.writeObject(q);
558 <        out.close();
569 <
570 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
571 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
572 <        ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
573 <        assertEquals(q.size(), r.size());
574 <        assertTrue(q.equals(r));
575 <        assertTrue(r.equals(q));
555 >        assertTrue(x != y);
556 >        assertEquals(x.size(), y.size());
557 >        assertEquals(x, y);
558 >        assertEquals(y, x);
559      }
560  
578
561      /**
562       * SetValue of an EntrySet entry sets value in the map.
563       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines