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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.24 by jsr166, Fri May 27 19:26:42 2011 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/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 11 | Line 12 | import java.util.concurrent.*;
12   import java.util.Enumeration;
13   import java.io.*;
14  
15 < public class ConcurrentHashMapTest extends JSR166TestCase{
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 <    private static ConcurrentHashMap map5() {  
24 <        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 <     *   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);
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 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 <     *   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;
78 <        while(e.hasMoreElements()){
79 <            count++;
80 <            e.nextElement();
81 <        }
82 <        assertEquals(5, count);
84 >        assertTrue(map.containsValue("A"));
85 >        assertFalse(map.containsValue("Z"));
86      }
87  
88      /**
89 <     *   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 <     *   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 <     *   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 <     *   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"));
147 <        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 164 | Line 226 | public class ConcurrentHashMapTest exten
226      }
227  
228      /**
229 <     *   putAll  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 <     *   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 <     *   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 <     *   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());
205 <        assertFalse(map.containsKey(five));
274 >        assertNotNull(map.replace(one, "Z"));
275 >        assertEquals("Z", map.get(one));
276      }
277  
278 <    public void testRemove2(){
278 >    /**
279 >     * replace value fails when the given key not mapped to expected value
280 >     */
281 >    public void testReplaceValue() {
282 >        ConcurrentHashMap map = map5();
283 >        assertEquals("A", map.get(one));
284 >        assertFalse(map.replace(one, "Z", "Z"));
285 >        assertEquals("A", map.get(one));
286 >    }
287 >
288 >    /**
289 >     * replace value succeeds when the given key mapped to expected value
290 >     */
291 >    public void testReplaceValue2() {
292 >        ConcurrentHashMap map = map5();
293 >        assertEquals("A", map.get(one));
294 >        assertTrue(map.replace(one, "A", "Z"));
295 >        assertEquals("Z", map.get(one));
296 >    }
297 >
298 >    /**
299 >     * remove removes the correct key-value pair from the map
300 >     */
301 >    public void testRemove() {
302          ConcurrentHashMap map = map5();
303 <        map.remove(five, "E");
304 <        assertEquals(4, map.size());
305 <        assertFalse(map.containsKey(five));
306 <        map.remove(four, "A");
214 <        assertEquals(4, map.size());
215 <        assertTrue(map.containsKey(four));
303 >        map.remove(five);
304 >        assertEquals(4, map.size());
305 >        assertFalse(map.containsKey(five));
306 >    }
307  
308 +    /**
309 +     * remove(key,value) removes only if pair present
310 +     */
311 +    public void testRemove2() {
312 +        ConcurrentHashMap map = map5();
313 +        map.remove(five, "E");
314 +        assertEquals(4, map.size());
315 +        assertFalse(map.containsKey(five));
316 +        map.remove(four, "A");
317 +        assertEquals(4, map.size());
318 +        assertTrue(map.containsKey(four));
319      }
320  
321      /**
322 <     *   size returns the correct values
322 >     * size returns the correct values
323       */
324 <    public void testSize(){
324 >    public void testSize() {
325          ConcurrentHashMap map = map5();
326          ConcurrentHashMap empty = new ConcurrentHashMap();
327 <        assertEquals(0, empty.size());
328 <        assertEquals(5, map.size());
327 >        assertEquals(0, empty.size());
328 >        assertEquals(5, map.size());
329      }
330  
331 <    public void testToString(){
331 >    /**
332 >     * toString contains toString of elements
333 >     */
334 >    public void testToString() {
335          ConcurrentHashMap map = map5();
336          String s = map.toString();
337          for (int i = 1; i <= 5; ++i) {
338 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
338 >            assertTrue(s.contains(String.valueOf(i)));
339          }
340 <    }        
340 >    }
341  
342      // Exception tests
343 <    
344 <    public void testConstructor1(){
345 <        try{
343 >
344 >    /**
345 >     * Cannot create with negative capacity
346 >     */
347 >    public void testConstructor1() {
348 >        try {
349              new ConcurrentHashMap(-1,0,1);
350 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
351 <        }catch(IllegalArgumentException e){}
350 >            shouldThrow();
351 >        } catch (IllegalArgumentException success) {}
352      }
353  
354 <    public void testConstructor2(){
355 <        try{
354 >    /**
355 >     * Cannot create with negative concurrency level
356 >     */
357 >    public void testConstructor2() {
358 >        try {
359              new ConcurrentHashMap(1,0,-1);
360 <            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
361 <        }catch(IllegalArgumentException e){}
360 >            shouldThrow();
361 >        } catch (IllegalArgumentException success) {}
362      }
363  
364 <    public void testConstructor3(){
365 <        try{
364 >    /**
365 >     * Cannot create with only negative capacity
366 >     */
367 >    public void testConstructor3() {
368 >        try {
369              new ConcurrentHashMap(-1);
370 <            fail("ConcurrentHashMap(int) should throw Illegal Argument Exception");
371 <        }catch(IllegalArgumentException e){}
370 >            shouldThrow();
371 >        } catch (IllegalArgumentException success) {}
372      }
373  
374 <    public void testGet_NullPointerException(){
375 <        try{
374 >    /**
375 >     * get(null) throws NPE
376 >     */
377 >    public void testGet_NullPointerException() {
378 >        try {
379              ConcurrentHashMap c = new ConcurrentHashMap(5);
380              c.get(null);
381 <            fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception");
382 <        }catch(NullPointerException e){}
381 >            shouldThrow();
382 >        } catch (NullPointerException success) {}
383      }
384  
385 <    public void testContainsKey_NullPointerException(){
386 <        try{
385 >    /**
386 >     * containsKey(null) throws NPE
387 >     */
388 >    public void testContainsKey_NullPointerException() {
389 >        try {
390              ConcurrentHashMap c = new ConcurrentHashMap(5);
391              c.containsKey(null);
392 <            fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception");
393 <        }catch(NullPointerException e){}
392 >            shouldThrow();
393 >        } catch (NullPointerException success) {}
394      }
395  
396 <    public void testContainsValue_NullPointerException(){
397 <        try{
396 >    /**
397 >     * containsValue(null) throws NPE
398 >     */
399 >    public void testContainsValue_NullPointerException() {
400 >        try {
401              ConcurrentHashMap c = new ConcurrentHashMap(5);
402              c.containsValue(null);
403 <            fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception");
404 <        }catch(NullPointerException e){}
403 >            shouldThrow();
404 >        } catch (NullPointerException success) {}
405      }
406  
407 <    public void testContains_NullPointerException(){
408 <        try{
407 >    /**
408 >     * contains(null) throws NPE
409 >     */
410 >    public void testContains_NullPointerException() {
411 >        try {
412              ConcurrentHashMap c = new ConcurrentHashMap(5);
413              c.contains(null);
414 <            fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception");
415 <        }catch(NullPointerException e){}
414 >            shouldThrow();
415 >        } catch (NullPointerException success) {}
416      }
417  
418 <    public void testPut1_NullPointerException(){
419 <        try{
418 >    /**
419 >     * put(null,x) throws NPE
420 >     */
421 >    public void testPut1_NullPointerException() {
422 >        try {
423              ConcurrentHashMap c = new ConcurrentHashMap(5);
424              c.put(null, "whatever");
425 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
426 <        }catch(NullPointerException e){}
425 >            shouldThrow();
426 >        } catch (NullPointerException success) {}
427      }
428  
429 <    public void testPut2_NullPointerException(){
430 <        try{
429 >    /**
430 >     * put(x, null) throws NPE
431 >     */
432 >    public void testPut2_NullPointerException() {
433 >        try {
434              ConcurrentHashMap c = new ConcurrentHashMap(5);
435              c.put("whatever", null);
436 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
437 <        }catch(NullPointerException e){}
436 >            shouldThrow();
437 >        } catch (NullPointerException success) {}
438      }
439  
440 <    public void testPutIfAbsent1_NullPointerException(){
441 <        try{
440 >    /**
441 >     * putIfAbsent(null, x) throws NPE
442 >     */
443 >    public void testPutIfAbsent1_NullPointerException() {
444 >        try {
445              ConcurrentHashMap c = new ConcurrentHashMap(5);
446              c.putIfAbsent(null, "whatever");
447 <            fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
448 <        }catch(NullPointerException e){}
447 >            shouldThrow();
448 >        } catch (NullPointerException success) {}
449      }
450  
451 <    public void testPutIfAbsent2_NullPointerException(){
452 <        try{
451 >    /**
452 >     * replace(null, x) throws NPE
453 >     */
454 >    public void testReplace_NullPointerException() {
455 >        try {
456 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
457 >            c.replace(null, "whatever");
458 >            shouldThrow();
459 >        } catch (NullPointerException success) {}
460 >    }
461 >
462 >    /**
463 >     * replace(null, x, y) throws NPE
464 >     */
465 >    public void testReplaceValue_NullPointerException() {
466 >        try {
467 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
468 >            c.replace(null, one, "whatever");
469 >            shouldThrow();
470 >        } catch (NullPointerException success) {}
471 >    }
472 >
473 >    /**
474 >     * putIfAbsent(x, null) throws NPE
475 >     */
476 >    public void testPutIfAbsent2_NullPointerException() {
477 >        try {
478              ConcurrentHashMap c = new ConcurrentHashMap(5);
479              c.putIfAbsent("whatever", null);
480 <            fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
481 <        }catch(NullPointerException e){}
480 >            shouldThrow();
481 >        } catch (NullPointerException success) {}
482 >    }
483 >
484 >    /**
485 >     * replace(x, null) throws NPE
486 >     */
487 >    public void testReplace2_NullPointerException() {
488 >        try {
489 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
490 >            c.replace("whatever", null);
491 >            shouldThrow();
492 >        } catch (NullPointerException success) {}
493      }
494  
495 +    /**
496 +     * replace(x, null, y) throws NPE
497 +     */
498 +    public void testReplaceValue2_NullPointerException() {
499 +        try {
500 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
501 +            c.replace("whatever", null, "A");
502 +            shouldThrow();
503 +        } catch (NullPointerException success) {}
504 +    }
505 +
506 +    /**
507 +     * replace(x, y, null) throws NPE
508 +     */
509 +    public void testReplaceValue3_NullPointerException() {
510 +        try {
511 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
512 +            c.replace("whatever", one, null);
513 +            shouldThrow();
514 +        } catch (NullPointerException success) {}
515 +    }
516  
517 <    public void testRemove1_NullPointerException(){
518 <        try{
517 >    /**
518 >     * remove(null) throws NPE
519 >     */
520 >    public void testRemove1_NullPointerException() {
521 >        try {
522              ConcurrentHashMap c = new ConcurrentHashMap(5);
523              c.put("sadsdf", "asdads");
524              c.remove(null);
525 <            fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione");
526 <        }catch(NullPointerException e){}
525 >            shouldThrow();
526 >        } catch (NullPointerException success) {}
527      }
528  
529 <    public void testRemove2_NullPointerException(){
530 <        try{
529 >    /**
530 >     * remove(null, x) throws NPE
531 >     */
532 >    public void testRemove2_NullPointerException() {
533 >        try {
534              ConcurrentHashMap c = new ConcurrentHashMap(5);
535              c.put("sadsdf", "asdads");
536              c.remove(null, "whatever");
537 <            fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione");
538 <        }catch(NullPointerException e){}
537 >            shouldThrow();
538 >        } catch (NullPointerException success) {}
539 >    }
540 >
541 >    /**
542 >     * remove(x, null) returns false
543 >     */
544 >    public void testRemove3() {
545 >        ConcurrentHashMap c = new ConcurrentHashMap(5);
546 >        c.put("sadsdf", "asdads");
547 >        assertFalse(c.remove("sadsdf", null));
548      }
549  
550 <    public void testSerialization() {
550 >    /**
551 >     * A deserialized map equals original
552 >     */
553 >    public void testSerialization() throws Exception {
554          ConcurrentHashMap q = map5();
555  
556 <        try {
557 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
558 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
559 <            out.writeObject(q);
560 <            out.close();
561 <
562 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
563 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
564 <            ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
565 <            assertEquals(q.size(), r.size());
566 <            assertTrue(q.equals(r));
567 <            assertTrue(r.equals(q));
568 <        } catch(Exception e){
569 <            e.printStackTrace();
570 <            fail("unexpected exception");
571 <        }
556 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
557 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
558 >        out.writeObject(q);
559 >        out.close();
560 >
561 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
562 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
563 >        ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
564 >        assertEquals(q.size(), r.size());
565 >        assertTrue(q.equals(r));
566 >        assertTrue(r.equals(q));
567 >    }
568 >
569 >    /**
570 >     * SetValue of an EntrySet entry sets value in the map.
571 >     */
572 >    public void testSetValueWriteThrough() {
573 >        // Adapted from a bug report by Eric Zoerner
574 >        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
575 >        assertTrue(map.isEmpty());
576 >        for (int i = 0; i < 20; i++)
577 >            map.put(new Integer(i), new Integer(i));
578 >        assertFalse(map.isEmpty());
579 >        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
580 >
581 >        // assert that entry1 is not 16
582 >        assertTrue("entry is 16, test not valid",
583 >                   !entry1.getKey().equals(new Integer(16)));
584 >
585 >        // remove 16 (a different key) from map
586 >        // which just happens to cause entry1 to be cloned in map
587 >        map.remove(new Integer(16));
588 >        entry1.setValue("XYZ");
589 >        assertTrue(map.containsValue("XYZ")); // fails
590      }
591  
364    
592   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines