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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines