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.38 by jsr166, Tue Sep 24 17:03:14 2013 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.*;
10   import java.util.*;
11 < import java.util.concurrent.*;
11 < import java.util.Enumeration;
11 > import java.util.concurrent.ConcurrentHashMap;
12  
13 < public class ConcurrentHashMapTest extends TestCase{
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);
18 >        return new TestSuite(ConcurrentHashMapTest.class);
19      }
20  
21 <    static final Integer one = new Integer(1);
22 <    static final Integer two = new Integer(2);
23 <    static final Integer three = new Integer(3);
24 <    static final Integer four = new Integer(4);
25 <    static final Integer five = new Integer(5);
26 <
27 <    private static ConcurrentHashMap map5() {  
28 <        ConcurrentHashMap map = new ConcurrentHashMap(5);
21 >    /**
22 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
23 >     */
24 >    private static ConcurrentHashMap<Integer, String> map5() {
25 >        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
26          assertTrue(map.isEmpty());
27 <        map.put(one, "A");
28 <        map.put(two, "B");
29 <        map.put(three, "C");
30 <        map.put(four, "D");
31 <        map.put(five, "E");
27 >        map.put(one, "A");
28 >        map.put(two, "B");
29 >        map.put(three, "C");
30 >        map.put(four, "D");
31 >        map.put(five, "E");
32          assertFalse(map.isEmpty());
33          assertEquals(5, map.size());
34 <        return map;
34 >        return map;
35 >    }
36 >
37 >    /** Re-implement Integer.compare for old java versions */
38 >    static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; }
39 >
40 >    // classes for testing Comparable fallbacks
41 >    static class BI implements Comparable<BI> {
42 >        private final int value;
43 >        BI(int value) { this.value = value; }
44 >        public int compareTo(BI other) {
45 >            return compare(value, other.value);
46 >        }
47 >        public boolean equals(Object x) {
48 >            return (x instanceof BI) && ((BI)x).value == value;
49 >        }
50 >        public int hashCode() { return 42; }
51 >    }
52 >    static class CI extends BI { CI(int value) { super(value); } }
53 >    static class DI extends BI { DI(int value) { super(value); } }
54 >
55 >    static class BS implements Comparable<BS> {
56 >        private final String value;
57 >        BS(String value) { this.value = value; }
58 >        public int compareTo(BS other) {
59 >            return value.compareTo(other.value);
60 >        }
61 >        public boolean equals(Object x) {
62 >            return (x instanceof BS) && value.equals(((BS)x).value);
63 >        }
64 >        public int hashCode() { return 42; }
65 >    }
66 >
67 >    static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
68 >        implements Comparable<LexicographicList<E>> {
69 >        LexicographicList(Collection<E> c) { super(c); }
70 >        LexicographicList(E e) { super(Collections.singleton(e)); }
71 >        public int compareTo(LexicographicList<E> other) {
72 >            int common = Math.min(size(), other.size());
73 >            int r = 0;
74 >            for (int i = 0; i < common; i++) {
75 >                if ((r = get(i).compareTo(other.get(i))) != 0)
76 >                    break;
77 >            }
78 >            if (r == 0)
79 >                r = compare(size(), other.size());
80 >            return r;
81 >        }
82 >        private static final long serialVersionUID = 0;
83 >    }
84 >
85 >    static class CollidingObject {
86 >        final String value;
87 >        CollidingObject(final String value) { this.value = value; }
88 >        public int hashCode() { return this.value.hashCode() & 1; }
89 >        public boolean equals(final Object obj) {
90 >            return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
91 >        }
92 >    }
93 >
94 >    static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
95 >        ComparableCollidingObject(final String value) { super(value); }
96 >        public int compareTo(final ComparableCollidingObject o) {
97 >            return value.compareTo(o.value);
98 >        }
99      }
100  
101      /**
102 <     *  Test to verify clear correctly removes all key-element pairs from the map
102 >     * Inserted elements that are subclasses of the same Comparable
103 >     * class are found.
104       */
105 <    public void testClear(){
106 <        ConcurrentHashMap map = map5();
107 <        map.clear();
108 <        assertEquals(map.size(), 0);
105 >    public void testComparableFamily() {
106 >        ConcurrentHashMap<BI, Boolean> m =
107 >            new ConcurrentHashMap<BI, Boolean>();
108 >        for (int i = 0; i < 1000; i++) {
109 >            assertTrue(m.put(new CI(i), true) == null);
110 >        }
111 >        for (int i = 0; i < 1000; i++) {
112 >            assertTrue(m.containsKey(new CI(i)));
113 >            assertTrue(m.containsKey(new DI(i)));
114 >        }
115      }
116  
117      /**
118 <     *  Test to verify contains gives the appropriate value
118 >     * Elements of classes with erased generic type parameters based
119 >     * on Comparable can be inserted and found.
120       */
121 <    public void testContains(){
122 <        ConcurrentHashMap map = map5();
123 <        assertTrue(map.contains("A"));
124 <        assertFalse(map.contains("Z"));
121 >    public void testGenericComparable() {
122 >        ConcurrentHashMap<Object, Boolean> m =
123 >            new ConcurrentHashMap<Object, Boolean>();
124 >        for (int i = 0; i < 1000; i++) {
125 >            BI bi = new BI(i);
126 >            BS bs = new BS(String.valueOf(i));
127 >            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
128 >            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
129 >            assertTrue(m.putIfAbsent(bis, true) == null);
130 >            assertTrue(m.containsKey(bis));
131 >            if (m.putIfAbsent(bss, true) == null)
132 >                assertTrue(m.containsKey(bss));
133 >            assertTrue(m.containsKey(bis));
134 >        }
135 >        for (int i = 0; i < 1000; i++) {
136 >            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
137 >        }
138 >    }
139 >
140 >    /**
141 >     * Elements of non-comparable classes equal to those of classes
142 >     * with erased generic type parameters based on Comparable can be
143 >     * inserted and found.
144 >     */
145 >    public void testGenericComparable2() {
146 >        ConcurrentHashMap<Object, Boolean> m =
147 >            new ConcurrentHashMap<Object, Boolean>();
148 >        for (int i = 0; i < 1000; i++) {
149 >            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
150 >        }
151 >
152 >        for (int i = 0; i < 1000; i++) {
153 >            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
154 >            assertTrue(m.containsKey(bis));
155 >        }
156      }
157 <    
157 >
158      /**
159 <     *  Test to verify containsKey gives the appropriate value
159 >     * Mixtures of instances of comparable and non-comparable classes
160 >     * can be inserted and found.
161       */
162 <    public void testContainsKey(){
162 >    public void testMixedComparable() {
163 >        int size = 1200;        // makes measured test run time -> 35ms
164 >        ConcurrentHashMap<Object, Object> map =
165 >            new ConcurrentHashMap<Object, Object>();
166 >        Random rng = new Random();
167 >        for (int i = 0; i < size; i++) {
168 >            Object x;
169 >            switch (rng.nextInt(4)) {
170 >            case 0:
171 >                x = new Object();
172 >                break;
173 >            case 1:
174 >                x = new CollidingObject(Integer.toString(i));
175 >                break;
176 >            default:
177 >                x = new ComparableCollidingObject(Integer.toString(i));
178 >            }
179 >            assertNull(map.put(x, x));
180 >        }
181 >        int count = 0;
182 >        for (Object k : map.keySet()) {
183 >            assertEquals(map.get(k), k);
184 >            ++count;
185 >        }
186 >        assertEquals(count, size);
187 >        assertEquals(map.size(), size);
188 >        for (Object k : map.keySet()) {
189 >            assertEquals(map.put(k, k), k);
190 >        }
191 >    }
192 >
193 >    /**
194 >     * clear removes all pairs
195 >     */
196 >    public void testClear() {
197          ConcurrentHashMap map = map5();
198 <        assertTrue(map.containsKey(one));
199 <        assertFalse(map.containsKey(new Integer(100)));
198 >        map.clear();
199 >        assertEquals(0, map.size());
200      }
201  
202      /**
203 <     *  Identical to normal contains
203 >     * Maps with same contents are equal
204       */
205 <    public void testContainsValue(){
205 >    public void testEquals() {
206 >        ConcurrentHashMap map1 = map5();
207 >        ConcurrentHashMap map2 = map5();
208 >        assertEquals(map1, map2);
209 >        assertEquals(map2, map1);
210 >        map1.clear();
211 >        assertFalse(map1.equals(map2));
212 >        assertFalse(map2.equals(map1));
213 >    }
214 >
215 >    /**
216 >     * hashCode() equals sum of each key.hashCode ^ value.hashCode
217 >     */
218 >    public void testHashCode() {
219 >        ConcurrentHashMap<Integer,String> map = map5();
220 >        int sum = 0;
221 >        for (Map.Entry<Integer,String> e : map.entrySet())
222 >            sum += e.getKey().hashCode() ^ e.getValue().hashCode();
223 >        assertEquals(sum, map.hashCode());
224 >    }
225 >
226 >    /**
227 >     * contains returns true for contained value
228 >     */
229 >    public void testContains() {
230          ConcurrentHashMap map = map5();
231 <        assertTrue(map.contains("A"));
231 >        assertTrue(map.contains("A"));
232          assertFalse(map.contains("Z"));
233      }
234  
235      /**
236 <     *  tes to verify enumeration returns an enumeration containing the correct elements
236 >     * containsKey returns true for contained key
237 >     */
238 >    public void testContainsKey() {
239 >        ConcurrentHashMap map = map5();
240 >        assertTrue(map.containsKey(one));
241 >        assertFalse(map.containsKey(zero));
242 >    }
243 >
244 >    /**
245 >     * containsValue returns true for held values
246       */
247 <    public void testEnumeration(){
247 >    public void testContainsValue() {
248          ConcurrentHashMap map = map5();
249 <        Enumeration e = map.elements();
250 <        int count = 0;
83 <        while(e.hasMoreElements()){
84 <            count++;
85 <            e.nextElement();
86 <        }
87 <        assertEquals(5, count);
249 >        assertTrue(map.containsValue("A"));
250 >        assertFalse(map.containsValue("Z"));
251      }
252  
253      /**
254 <     *  Test to verify get returns the correct element at the given index
254 >     * enumeration returns an enumeration containing the correct
255 >     * elements
256       */
257 <    public void testGet(){
257 >    public void testEnumeration() {
258          ConcurrentHashMap map = map5();
259 <        assertEquals("A", (String)map.get(one));
259 >        Enumeration e = map.elements();
260 >        int count = 0;
261 >        while (e.hasMoreElements()) {
262 >            count++;
263 >            e.nextElement();
264 >        }
265 >        assertEquals(5, count);
266      }
267  
268      /**
269 <     *  Test to verify get on a nonexistant key returns null
269 >     * get returns the correct element at the given key,
270 >     * or null if not present
271       */
272 <    public void testGet2(){
272 >    public void testGet() {
273 >        ConcurrentHashMap map = map5();
274 >        assertEquals("A", (String)map.get(one));
275          ConcurrentHashMap empty = new ConcurrentHashMap();
276 <        assertNull(empty.get("anything"));
276 >        assertNull(map.get("anything"));
277      }
278  
279      /**
280 <     *  Simple test to verify isEmpty returns the correct value
280 >     * isEmpty is true of empty map and false for non-empty
281       */
282 <    public void testIsEmpty(){
282 >    public void testIsEmpty() {
283          ConcurrentHashMap empty = new ConcurrentHashMap();
284          ConcurrentHashMap map = map5();
285 <        assertTrue(empty.isEmpty());
285 >        assertTrue(empty.isEmpty());
286          assertFalse(map.isEmpty());
287      }
288  
289      /**
290 <     *  Test to verify keys returns an enumeration containing all the keys from the map
290 >     * keys returns an enumeration containing all the keys from the map
291 >     */
292 >    public void testKeys() {
293 >        ConcurrentHashMap map = map5();
294 >        Enumeration e = map.keys();
295 >        int count = 0;
296 >        while (e.hasMoreElements()) {
297 >            count++;
298 >            e.nextElement();
299 >        }
300 >        assertEquals(5, count);
301 >    }
302 >
303 >    /**
304 >     * keySet returns a Set containing all the keys
305 >     */
306 >    public void testKeySet() {
307 >        ConcurrentHashMap map = map5();
308 >        Set s = map.keySet();
309 >        assertEquals(5, s.size());
310 >        assertTrue(s.contains(one));
311 >        assertTrue(s.contains(two));
312 >        assertTrue(s.contains(three));
313 >        assertTrue(s.contains(four));
314 >        assertTrue(s.contains(five));
315 >    }
316 >
317 >    /**
318 >     * keySet.toArray returns contains all keys
319 >     */
320 >    public void testKeySetToArray() {
321 >        ConcurrentHashMap map = map5();
322 >        Set s = map.keySet();
323 >        Object[] ar = s.toArray();
324 >        assertTrue(s.containsAll(Arrays.asList(ar)));
325 >        assertEquals(5, ar.length);
326 >        ar[0] = m10;
327 >        assertFalse(s.containsAll(Arrays.asList(ar)));
328 >    }
329 >
330 >    /**
331 >     * Values.toArray contains all values
332       */
333 <    public void testKeys(){
333 >    public void testValuesToArray() {
334          ConcurrentHashMap map = map5();
335 <        Enumeration e = map.keys();
336 <        int count = 0;
337 <        while(e.hasMoreElements()){
338 <            count++;
339 <            e.nextElement();
340 <        }
341 <        assertEquals(5, count);
335 >        Collection v = map.values();
336 >        Object[] ar = v.toArray();
337 >        ArrayList s = new ArrayList(Arrays.asList(ar));
338 >        assertEquals(5, ar.length);
339 >        assertTrue(s.contains("A"));
340 >        assertTrue(s.contains("B"));
341 >        assertTrue(s.contains("C"));
342 >        assertTrue(s.contains("D"));
343 >        assertTrue(s.contains("E"));
344      }
345  
346      /**
347 <     *  Test to verify keySet returns a Set containing all the keys
347 >     * entrySet.toArray contains all entries
348       */
349 <    public void testKeySet(){
349 >    public void testEntrySetToArray() {
350          ConcurrentHashMap map = map5();
351 <        Set s = map.keySet();
352 <        assertEquals(5, s.size());
353 <        assertTrue(s.contains(one));
354 <        assertTrue(s.contains(two));
355 <        assertTrue(s.contains(three));
356 <        assertTrue(s.contains(four));
357 <        assertTrue(s.contains(five));
351 >        Set s = map.entrySet();
352 >        Object[] ar = s.toArray();
353 >        assertEquals(5, ar.length);
354 >        for (int i = 0; i < 5; ++i) {
355 >            assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
356 >            assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
357 >        }
358      }
359  
360 <    public void testValues(){
360 >    /**
361 >     * values collection contains all values
362 >     */
363 >    public void testValues() {
364          ConcurrentHashMap map = map5();
365 <        Collection s = map.values();
366 <        assertEquals(5, s.size());
367 <        assertTrue(s.contains("A"));
368 <        assertTrue(s.contains("B"));
369 <        assertTrue(s.contains("C"));
370 <        assertTrue(s.contains("D"));
371 <        assertTrue(s.contains("E"));
365 >        Collection s = map.values();
366 >        assertEquals(5, s.size());
367 >        assertTrue(s.contains("A"));
368 >        assertTrue(s.contains("B"));
369 >        assertTrue(s.contains("C"));
370 >        assertTrue(s.contains("D"));
371 >        assertTrue(s.contains("E"));
372      }
373  
374 <    public void testEntrySet(){
374 >    /**
375 >     * entrySet contains all pairs
376 >     */
377 >    public void testEntrySet() {
378          ConcurrentHashMap map = map5();
379 <        Set s = map.entrySet();
380 <        assertEquals(5, s.size());
379 >        Set s = map.entrySet();
380 >        assertEquals(5, s.size());
381          Iterator it = s.iterator();
382          while (it.hasNext()) {
383              Map.Entry e = (Map.Entry) it.next();
384 <            assertTrue(
384 >            assertTrue(
385                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
386                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
387                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 169 | Line 391 | public class ConcurrentHashMapTest exten
391      }
392  
393      /**
394 <     *  Test to verify putAll correctly adds all key-value pairs from the given map
394 >     * putAll adds all key-value pairs from the given map
395       */
396 <    public void testPutAll(){
396 >    public void testPutAll() {
397          ConcurrentHashMap empty = new ConcurrentHashMap();
398          ConcurrentHashMap map = map5();
399 <        empty.putAll(map);
400 <        assertEquals(5, empty.size());
401 <        assertTrue(empty.containsKey(one));
402 <        assertTrue(empty.containsKey(two));
403 <        assertTrue(empty.containsKey(three));
404 <        assertTrue(empty.containsKey(four));
405 <        assertTrue(empty.containsKey(five));
399 >        empty.putAll(map);
400 >        assertEquals(5, empty.size());
401 >        assertTrue(empty.containsKey(one));
402 >        assertTrue(empty.containsKey(two));
403 >        assertTrue(empty.containsKey(three));
404 >        assertTrue(empty.containsKey(four));
405 >        assertTrue(empty.containsKey(five));
406      }
407  
408      /**
409 <     *  Test to verify putIfAbsent works when the given key is not present
409 >     * putIfAbsent works when the given key is not present
410       */
411 <    public void testPutIfAbsent(){
411 >    public void testPutIfAbsent() {
412          ConcurrentHashMap map = map5();
413 <        map.putIfAbsent(new Integer(6), "Z");
414 <        assertTrue(map.containsKey(new Integer(6)));
413 >        map.putIfAbsent(six, "Z");
414 >        assertTrue(map.containsKey(six));
415      }
416  
417      /**
418 <     *  Test to verify putIfAbsent does not add the pair if the key is already present
418 >     * putIfAbsent does not add the pair if the key is already present
419       */
420 <    public void testPutIfAbsent2(){
420 >    public void testPutIfAbsent2() {
421          ConcurrentHashMap map = map5();
422          assertEquals("A", map.putIfAbsent(one, "Z"));
423      }
424  
425      /**
426 <     *  Test to verify remove removes the correct key-value pair from the map
426 >     * replace fails when the given key is not present
427 >     */
428 >    public void testReplace() {
429 >        ConcurrentHashMap map = map5();
430 >        assertNull(map.replace(six, "Z"));
431 >        assertFalse(map.containsKey(six));
432 >    }
433 >
434 >    /**
435 >     * replace succeeds if the key is already present
436 >     */
437 >    public void testReplace2() {
438 >        ConcurrentHashMap map = map5();
439 >        assertNotNull(map.replace(one, "Z"));
440 >        assertEquals("Z", map.get(one));
441 >    }
442 >
443 >    /**
444 >     * replace value fails when the given key not mapped to expected value
445 >     */
446 >    public void testReplaceValue() {
447 >        ConcurrentHashMap map = map5();
448 >        assertEquals("A", map.get(one));
449 >        assertFalse(map.replace(one, "Z", "Z"));
450 >        assertEquals("A", map.get(one));
451 >    }
452 >
453 >    /**
454 >     * replace value succeeds when the given key mapped to expected value
455       */
456 <    public void testRemove(){
456 >    public void testReplaceValue2() {
457          ConcurrentHashMap map = map5();
458 <        map.remove(five);
459 <        assertEquals(4, map.size());
460 <        assertFalse(map.containsKey(five));
458 >        assertEquals("A", map.get(one));
459 >        assertTrue(map.replace(one, "A", "Z"));
460 >        assertEquals("Z", map.get(one));
461      }
462  
463 <    public void testRemove2(){
463 >    /**
464 >     * remove removes the correct key-value pair from the map
465 >     */
466 >    public void testRemove() {
467          ConcurrentHashMap map = map5();
468 <        map.remove(five, "E");
469 <        assertEquals(4, map.size());
470 <        assertFalse(map.containsKey(five));
471 <        map.remove(four, "A");
219 <        assertEquals(4, map.size());
220 <        assertTrue(map.containsKey(four));
468 >        map.remove(five);
469 >        assertEquals(4, map.size());
470 >        assertFalse(map.containsKey(five));
471 >    }
472  
473 +    /**
474 +     * remove(key,value) removes only if pair present
475 +     */
476 +    public void testRemove2() {
477 +        ConcurrentHashMap map = map5();
478 +        map.remove(five, "E");
479 +        assertEquals(4, map.size());
480 +        assertFalse(map.containsKey(five));
481 +        map.remove(four, "A");
482 +        assertEquals(4, map.size());
483 +        assertTrue(map.containsKey(four));
484      }
485  
486      /**
487 <     *  Simple test to verify size returns the correct values
487 >     * size returns the correct values
488       */
489 <    public void testSize(){
489 >    public void testSize() {
490          ConcurrentHashMap map = map5();
491          ConcurrentHashMap empty = new ConcurrentHashMap();
492 <        assertEquals(0, empty.size());
493 <        assertEquals(5, map.size());
492 >        assertEquals(0, empty.size());
493 >        assertEquals(5, map.size());
494      }
495  
496 <    public void testToString(){
496 >    /**
497 >     * toString contains toString of elements
498 >     */
499 >    public void testToString() {
500          ConcurrentHashMap map = map5();
501          String s = map.toString();
502          for (int i = 1; i <= 5; ++i) {
503 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
503 >            assertTrue(s.contains(String.valueOf(i)));
504          }
505 <    }        
505 >    }
506  
507      // Exception tests
243    
244    public void testConstructor1(){
245        try{
246            new ConcurrentHashMap(-1,0,1);
247            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
248        }catch(IllegalArgumentException e){}
249    }
250
251    public void testConstructor2(){
252        try{
253            new ConcurrentHashMap(1,0,-1);
254            fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
255        }catch(IllegalArgumentException e){}
256    }
508  
509 <    public void testConstructor3(){
510 <        try{
509 >    /**
510 >     * Cannot create with only negative capacity
511 >     */
512 >    public void testConstructor1() {
513 >        try {
514              new ConcurrentHashMap(-1);
515 <            fail("ConcurrentHashMap(int) should throw Illegal Argument Exception");
516 <        }catch(IllegalArgumentException e){}
515 >            shouldThrow();
516 >        } catch (IllegalArgumentException success) {}
517 >    }
518 >
519 >    /**
520 >     * Constructor (initialCapacity, loadFactor) throws
521 >     * IllegalArgumentException if either argument is negative
522 >      */
523 >    public void testConstructor2() {
524 >        try {
525 >            new ConcurrentHashMap(-1, .75f);
526 >            shouldThrow();
527 >        } catch (IllegalArgumentException e) {}
528 >
529 >        try {
530 >            new ConcurrentHashMap(16, -1);
531 >            shouldThrow();
532 >        } catch (IllegalArgumentException e) {}
533 >    }
534 >
535 >    /**
536 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
537 >     * throws IllegalArgumentException if any argument is negative
538 >     */
539 >    public void testConstructor3() {
540 >        try {
541 >            new ConcurrentHashMap(-1, .75f, 1);
542 >            shouldThrow();
543 >        } catch (IllegalArgumentException e) {}
544 >
545 >        try {
546 >            new ConcurrentHashMap(16, -1, 1);
547 >            shouldThrow();
548 >        } catch (IllegalArgumentException e) {}
549 >
550 >        try {
551 >            new ConcurrentHashMap(16, .75f, -1);
552 >            shouldThrow();
553 >        } catch (IllegalArgumentException e) {}
554      }
555  
556 <    public void testGet_NullPointerException(){
557 <        try{
556 >    /**
557 >     * ConcurrentHashMap(map) throws NullPointerException if the given
558 >     * map is null
559 >     */
560 >    public void testConstructor4() {
561 >        try {
562 >            new ConcurrentHashMap(null);
563 >            shouldThrow();
564 >        } catch (NullPointerException e) {}
565 >    }
566 >
567 >    /**
568 >     * ConcurrentHashMap(map) creates a new map with the same mappings
569 >     * as the given map
570 >     */
571 >    public void testConstructor5() {
572 >        ConcurrentHashMap map1 = map5();
573 >        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
574 >        assertTrue(map2.equals(map1));
575 >        map2.put(one, "F");
576 >        assertFalse(map2.equals(map1));
577 >    }
578 >
579 >
580 >    /**
581 >     * get(null) throws NPE
582 >     */
583 >    public void testGet_NullPointerException() {
584 >        try {
585              ConcurrentHashMap c = new ConcurrentHashMap(5);
586              c.get(null);
587 <            fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception");
588 <        }catch(NullPointerException e){}
587 >            shouldThrow();
588 >        } catch (NullPointerException success) {}
589      }
590  
591 <    public void testContainsKey_NullPointerException(){
592 <        try{
591 >    /**
592 >     * containsKey(null) throws NPE
593 >     */
594 >    public void testContainsKey_NullPointerException() {
595 >        try {
596              ConcurrentHashMap c = new ConcurrentHashMap(5);
597              c.containsKey(null);
598 <            fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception");
599 <        }catch(NullPointerException e){}
598 >            shouldThrow();
599 >        } catch (NullPointerException success) {}
600      }
601  
602 <    public void testContainsValue_NullPointerException(){
603 <        try{
602 >    /**
603 >     * containsValue(null) throws NPE
604 >     */
605 >    public void testContainsValue_NullPointerException() {
606 >        try {
607              ConcurrentHashMap c = new ConcurrentHashMap(5);
608              c.containsValue(null);
609 <            fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception");
610 <        }catch(NullPointerException e){}
609 >            shouldThrow();
610 >        } catch (NullPointerException success) {}
611      }
612  
613 <    public void testContains_NullPointerException(){
614 <        try{
613 >    /**
614 >     * contains(null) throws NPE
615 >     */
616 >    public void testContains_NullPointerException() {
617 >        try {
618              ConcurrentHashMap c = new ConcurrentHashMap(5);
619              c.contains(null);
620 <            fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception");
621 <        }catch(NullPointerException e){}
620 >            shouldThrow();
621 >        } catch (NullPointerException success) {}
622      }
623  
624 <    public void testPut1_NullPointerException(){
625 <        try{
624 >    /**
625 >     * put(null,x) throws NPE
626 >     */
627 >    public void testPut1_NullPointerException() {
628 >        try {
629              ConcurrentHashMap c = new ConcurrentHashMap(5);
630              c.put(null, "whatever");
631 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
632 <        }catch(NullPointerException e){}
631 >            shouldThrow();
632 >        } catch (NullPointerException success) {}
633      }
634  
635 <    public void testPut2_NullPointerException(){
636 <        try{
635 >    /**
636 >     * put(x, null) throws NPE
637 >     */
638 >    public void testPut2_NullPointerException() {
639 >        try {
640              ConcurrentHashMap c = new ConcurrentHashMap(5);
641              c.put("whatever", null);
642 <            fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
643 <        }catch(NullPointerException e){}
642 >            shouldThrow();
643 >        } catch (NullPointerException success) {}
644      }
645  
646 <    public void testPutIfAbsent1_NullPointerException(){
647 <        try{
646 >    /**
647 >     * putIfAbsent(null, x) throws NPE
648 >     */
649 >    public void testPutIfAbsent1_NullPointerException() {
650 >        try {
651              ConcurrentHashMap c = new ConcurrentHashMap(5);
652              c.putIfAbsent(null, "whatever");
653 <            fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
654 <        }catch(NullPointerException e){}
653 >            shouldThrow();
654 >        } catch (NullPointerException success) {}
655      }
656  
657 <    public void testPutIfAbsent2_NullPointerException(){
658 <        try{
657 >    /**
658 >     * replace(null, x) throws NPE
659 >     */
660 >    public void testReplace_NullPointerException() {
661 >        try {
662 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
663 >            c.replace(null, "whatever");
664 >            shouldThrow();
665 >        } catch (NullPointerException success) {}
666 >    }
667 >
668 >    /**
669 >     * replace(null, x, y) throws NPE
670 >     */
671 >    public void testReplaceValue_NullPointerException() {
672 >        try {
673 >            ConcurrentHashMap c = new ConcurrentHashMap(5);
674 >            c.replace(null, one, "whatever");
675 >            shouldThrow();
676 >        } catch (NullPointerException success) {}
677 >    }
678 >
679 >    /**
680 >     * putIfAbsent(x, null) throws NPE
681 >     */
682 >    public void testPutIfAbsent2_NullPointerException() {
683 >        try {
684              ConcurrentHashMap c = new ConcurrentHashMap(5);
685              c.putIfAbsent("whatever", null);
686 <            fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
687 <        }catch(NullPointerException e){}
686 >            shouldThrow();
687 >        } catch (NullPointerException success) {}
688      }
689  
690 +    /**
691 +     * replace(x, null) throws NPE
692 +     */
693 +    public void testReplace2_NullPointerException() {
694 +        try {
695 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
696 +            c.replace("whatever", null);
697 +            shouldThrow();
698 +        } catch (NullPointerException success) {}
699 +    }
700 +
701 +    /**
702 +     * replace(x, null, y) throws NPE
703 +     */
704 +    public void testReplaceValue2_NullPointerException() {
705 +        try {
706 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
707 +            c.replace("whatever", null, "A");
708 +            shouldThrow();
709 +        } catch (NullPointerException success) {}
710 +    }
711 +
712 +    /**
713 +     * replace(x, y, null) throws NPE
714 +     */
715 +    public void testReplaceValue3_NullPointerException() {
716 +        try {
717 +            ConcurrentHashMap c = new ConcurrentHashMap(5);
718 +            c.replace("whatever", one, null);
719 +            shouldThrow();
720 +        } catch (NullPointerException success) {}
721 +    }
722  
723 <    public void testRemove1_NullPointerException(){
724 <        try{
723 >    /**
724 >     * remove(null) throws NPE
725 >     */
726 >    public void testRemove1_NullPointerException() {
727 >        try {
728              ConcurrentHashMap c = new ConcurrentHashMap(5);
729              c.put("sadsdf", "asdads");
730              c.remove(null);
731 <            fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione");
732 <        }catch(NullPointerException e){}
731 >            shouldThrow();
732 >        } catch (NullPointerException success) {}
733      }
734  
735 <    public void testRemove2_NullPointerException(){
736 <        try{
735 >    /**
736 >     * remove(null, x) throws NPE
737 >     */
738 >    public void testRemove2_NullPointerException() {
739 >        try {
740              ConcurrentHashMap c = new ConcurrentHashMap(5);
741              c.put("sadsdf", "asdads");
742              c.remove(null, "whatever");
743 <            fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione");
744 <        }catch(NullPointerException e){}
743 >            shouldThrow();
744 >        } catch (NullPointerException success) {}
745      }
746 <    
746 >
747 >    /**
748 >     * remove(x, null) returns false
749 >     */
750 >    public void testRemove3() {
751 >        ConcurrentHashMap c = new ConcurrentHashMap(5);
752 >        c.put("sadsdf", "asdads");
753 >        assertFalse(c.remove("sadsdf", null));
754 >    }
755 >
756 >    /**
757 >     * A deserialized map equals original
758 >     */
759 >    public void testSerialization() throws Exception {
760 >        Map x = map5();
761 >        Map y = serialClone(x);
762 >
763 >        assertNotSame(x, y);
764 >        assertEquals(x.size(), y.size());
765 >        assertEquals(x, y);
766 >        assertEquals(y, x);
767 >    }
768 >
769 >    /**
770 >     * SetValue of an EntrySet entry sets value in the map.
771 >     */
772 >    public void testSetValueWriteThrough() {
773 >        // Adapted from a bug report by Eric Zoerner
774 >        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
775 >        assertTrue(map.isEmpty());
776 >        for (int i = 0; i < 20; i++)
777 >            map.put(new Integer(i), new Integer(i));
778 >        assertFalse(map.isEmpty());
779 >        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
780 >        // Unless it happens to be first (in which case remainder of
781 >        // test is skipped), remove a possibly-colliding key from map
782 >        // which, under some implementations, may cause entry1 to be
783 >        // cloned in map
784 >        if (!entry1.getKey().equals(new Integer(16))) {
785 >            map.remove(new Integer(16));
786 >            entry1.setValue("XYZ");
787 >            assertTrue(map.containsValue("XYZ")); // fails if write-through broken
788 >        }
789 >    }
790 >
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines