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.21 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.46 by jsr166, Sun May 24 01:23:17 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
9 > import java.util.ArrayList;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Collections;
13   import java.util.Enumeration;
14 < import java.io.*;
14 > import java.util.Iterator;
15 > import java.util.Map;
16 > import java.util.Random;
17 > import java.util.Set;
18 > import java.util.concurrent.ConcurrentHashMap;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ConcurrentHashMapTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27      public static Test suite() {
28          return new TestSuite(ConcurrentHashMapTest.class);
29      }
30  
31      /**
32 <     * Create a map from Integers 1-5 to Strings "A"-"E".
32 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
33       */
34 <    private static ConcurrentHashMap map5() {
35 <        ConcurrentHashMap map = new ConcurrentHashMap(5);
34 >    private static ConcurrentHashMap<Integer, String> map5() {
35 >        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
36          assertTrue(map.isEmpty());
37          map.put(one, "A");
38          map.put(two, "B");
# Line 36 | Line 44 | public class ConcurrentHashMapTest exten
44          return map;
45      }
46  
47 +    /** Re-implement Integer.compare for old java versions */
48 +    static int compare(int x, int y) {
49 +        return (x < y) ? -1 : (x > y) ? 1 : 0;
50 +    }
51 +
52 +    // classes for testing Comparable fallbacks
53 +    static class BI implements Comparable<BI> {
54 +        private final int value;
55 +        BI(int value) { this.value = value; }
56 +        public int compareTo(BI other) {
57 +            return compare(value, other.value);
58 +        }
59 +        public boolean equals(Object x) {
60 +            return (x instanceof BI) && ((BI)x).value == value;
61 +        }
62 +        public int hashCode() { return 42; }
63 +    }
64 +    static class CI extends BI { CI(int value) { super(value); } }
65 +    static class DI extends BI { DI(int value) { super(value); } }
66 +
67 +    static class BS implements Comparable<BS> {
68 +        private final String value;
69 +        BS(String value) { this.value = value; }
70 +        public int compareTo(BS other) {
71 +            return value.compareTo(other.value);
72 +        }
73 +        public boolean equals(Object x) {
74 +            return (x instanceof BS) && value.equals(((BS)x).value);
75 +        }
76 +        public int hashCode() { return 42; }
77 +    }
78 +
79 +    static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
80 +        implements Comparable<LexicographicList<E>> {
81 +        LexicographicList(Collection<E> c) { super(c); }
82 +        LexicographicList(E e) { super(Collections.singleton(e)); }
83 +        public int compareTo(LexicographicList<E> other) {
84 +            int common = Math.min(size(), other.size());
85 +            int r = 0;
86 +            for (int i = 0; i < common; i++) {
87 +                if ((r = get(i).compareTo(other.get(i))) != 0)
88 +                    break;
89 +            }
90 +            if (r == 0)
91 +                r = compare(size(), other.size());
92 +            return r;
93 +        }
94 +        private static final long serialVersionUID = 0;
95 +    }
96 +
97 +    static class CollidingObject {
98 +        final String value;
99 +        CollidingObject(final String value) { this.value = value; }
100 +        public int hashCode() { return this.value.hashCode() & 1; }
101 +        public boolean equals(final Object obj) {
102 +            return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
103 +        }
104 +    }
105 +
106 +    static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
107 +        ComparableCollidingObject(final String value) { super(value); }
108 +        public int compareTo(final ComparableCollidingObject o) {
109 +            return value.compareTo(o.value);
110 +        }
111 +    }
112 +
113 +    /**
114 +     * Inserted elements that are subclasses of the same Comparable
115 +     * class are found.
116 +     */
117 +    public void testComparableFamily() {
118 +        int size = 500;         // makes measured test run time -> 60ms
119 +        ConcurrentHashMap<BI, Boolean> m =
120 +            new ConcurrentHashMap<BI, Boolean>();
121 +        for (int i = 0; i < size; i++) {
122 +            assertTrue(m.put(new CI(i), true) == null);
123 +        }
124 +        for (int i = 0; i < size; i++) {
125 +            assertTrue(m.containsKey(new CI(i)));
126 +            assertTrue(m.containsKey(new DI(i)));
127 +        }
128 +    }
129 +
130      /**
131 <     *  clear removes all pairs
131 >     * Elements of classes with erased generic type parameters based
132 >     * on Comparable can be inserted and found.
133 >     */
134 >    public void testGenericComparable() {
135 >        int size = 120;         // makes measured test run time -> 60ms
136 >        ConcurrentHashMap<Object, Boolean> m =
137 >            new ConcurrentHashMap<Object, Boolean>();
138 >        for (int i = 0; i < size; i++) {
139 >            BI bi = new BI(i);
140 >            BS bs = new BS(String.valueOf(i));
141 >            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
142 >            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
143 >            assertTrue(m.putIfAbsent(bis, true) == null);
144 >            assertTrue(m.containsKey(bis));
145 >            if (m.putIfAbsent(bss, true) == null)
146 >                assertTrue(m.containsKey(bss));
147 >            assertTrue(m.containsKey(bis));
148 >        }
149 >        for (int i = 0; i < size; i++) {
150 >            assertTrue(m.containsKey(Collections.singletonList(new BI(i))));
151 >        }
152 >    }
153 >
154 >    /**
155 >     * Elements of non-comparable classes equal to those of classes
156 >     * with erased generic type parameters based on Comparable can be
157 >     * inserted and found.
158 >     */
159 >    public void testGenericComparable2() {
160 >        int size = 500;         // makes measured test run time -> 60ms
161 >        ConcurrentHashMap<Object, Boolean> m =
162 >            new ConcurrentHashMap<Object, Boolean>();
163 >        for (int i = 0; i < size; i++) {
164 >            m.put(Collections.singletonList(new BI(i)), true);
165 >        }
166 >
167 >        for (int i = 0; i < size; i++) {
168 >            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
169 >            assertTrue(m.containsKey(bis));
170 >        }
171 >    }
172 >
173 >    /**
174 >     * Mixtures of instances of comparable and non-comparable classes
175 >     * can be inserted and found.
176 >     */
177 >    public void testMixedComparable() {
178 >        int size = 1200;        // makes measured test run time -> 35ms
179 >        ConcurrentHashMap<Object, Object> map =
180 >            new ConcurrentHashMap<Object, Object>();
181 >        Random rng = new Random();
182 >        for (int i = 0; i < size; i++) {
183 >            Object x;
184 >            switch (rng.nextInt(4)) {
185 >            case 0:
186 >                x = new Object();
187 >                break;
188 >            case 1:
189 >                x = new CollidingObject(Integer.toString(i));
190 >                break;
191 >            default:
192 >                x = new ComparableCollidingObject(Integer.toString(i));
193 >            }
194 >            assertNull(map.put(x, x));
195 >        }
196 >        int count = 0;
197 >        for (Object k : map.keySet()) {
198 >            assertEquals(map.get(k), k);
199 >            ++count;
200 >        }
201 >        assertEquals(count, size);
202 >        assertEquals(map.size(), size);
203 >        for (Object k : map.keySet()) {
204 >            assertEquals(map.put(k, k), k);
205 >        }
206 >    }
207 >
208 >    /**
209 >     * clear removes all pairs
210       */
211      public void testClear() {
212          ConcurrentHashMap map = map5();
213          map.clear();
214 <        assertEquals(map.size(), 0);
214 >        assertEquals(0, map.size());
215      }
216  
217      /**
218 <     *  Maps with same contents are equal
218 >     * Maps with same contents are equal
219       */
220      public void testEquals() {
221          ConcurrentHashMap map1 = map5();
# Line 59 | Line 228 | public class ConcurrentHashMapTest exten
228      }
229  
230      /**
231 <     *  contains returns true for contained value
231 >     * hashCode() equals sum of each key.hashCode ^ value.hashCode
232 >     */
233 >    public void testHashCode() {
234 >        ConcurrentHashMap<Integer,String> map = map5();
235 >        int sum = 0;
236 >        for (Map.Entry<Integer,String> e : map.entrySet())
237 >            sum += e.getKey().hashCode() ^ e.getValue().hashCode();
238 >        assertEquals(sum, map.hashCode());
239 >    }
240 >
241 >    /**
242 >     * contains returns true for contained value
243       */
244      public void testContains() {
245          ConcurrentHashMap map = map5();
# Line 68 | Line 248 | public class ConcurrentHashMapTest exten
248      }
249  
250      /**
251 <     *  containsKey returns true for contained key
251 >     * containsKey returns true for contained key
252       */
253      public void testContainsKey() {
254          ConcurrentHashMap map = map5();
# Line 77 | Line 257 | public class ConcurrentHashMapTest exten
257      }
258  
259      /**
260 <     *  containsValue returns true for held values
260 >     * containsValue returns true for held values
261       */
262      public void testContainsValue() {
263          ConcurrentHashMap map = map5();
# Line 86 | Line 266 | public class ConcurrentHashMapTest exten
266      }
267  
268      /**
269 <     *   enumeration returns an enumeration containing the correct
270 <     *   elements
269 >     * enumeration returns an enumeration containing the correct
270 >     * elements
271       */
272      public void testEnumeration() {
273          ConcurrentHashMap map = map5();
# Line 101 | Line 281 | public class ConcurrentHashMapTest exten
281      }
282  
283      /**
284 <     *  get returns the correct element at the given key,
285 <     *  or null if not present
284 >     * get returns the correct element at the given key,
285 >     * or null if not present
286       */
287      public void testGet() {
288          ConcurrentHashMap map = map5();
289          assertEquals("A", (String)map.get(one));
290          ConcurrentHashMap empty = new ConcurrentHashMap();
291          assertNull(map.get("anything"));
292 +        assertNull(empty.get("anything"));
293      }
294  
295      /**
296 <     *  isEmpty is true of empty map and false for non-empty
296 >     * isEmpty is true of empty map and false for non-empty
297       */
298      public void testIsEmpty() {
299          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 122 | Line 303 | public class ConcurrentHashMapTest exten
303      }
304  
305      /**
306 <     *   keys returns an enumeration containing all the keys from the map
306 >     * keys returns an enumeration containing all the keys from the map
307       */
308      public void testKeys() {
309          ConcurrentHashMap map = map5();
# Line 136 | Line 317 | public class ConcurrentHashMapTest exten
317      }
318  
319      /**
320 <     *   keySet returns a Set containing all the keys
320 >     * keySet returns a Set containing all the keys
321       */
322      public void testKeySet() {
323          ConcurrentHashMap map = map5();
# Line 150 | Line 331 | public class ConcurrentHashMapTest exten
331      }
332  
333      /**
334 <     *  keySet.toArray returns contains all keys
334 >     * keySet.toArray returns contains all keys
335       */
336      public void testKeySetToArray() {
337          ConcurrentHashMap map = map5();
# Line 163 | Line 344 | public class ConcurrentHashMapTest exten
344      }
345  
346      /**
347 <     *  Values.toArray contains all values
347 >     * Values.toArray contains all values
348       */
349      public void testValuesToArray() {
350          ConcurrentHashMap map = map5();
# Line 179 | Line 360 | public class ConcurrentHashMapTest exten
360      }
361  
362      /**
363 <     *  entrySet.toArray contains all entries
363 >     * entrySet.toArray contains all entries
364       */
365      public void testEntrySetToArray() {
366          ConcurrentHashMap map = map5();
# Line 226 | Line 407 | public class ConcurrentHashMapTest exten
407      }
408  
409      /**
410 <     *   putAll  adds all key-value pairs from the given map
410 >     * putAll adds all key-value pairs from the given map
411       */
412      public void testPutAll() {
413          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 241 | Line 422 | public class ConcurrentHashMapTest exten
422      }
423  
424      /**
425 <     *   putIfAbsent works when the given key is not present
425 >     * putIfAbsent works when the given key is not present
426       */
427      public void testPutIfAbsent() {
428          ConcurrentHashMap map = map5();
# Line 250 | Line 431 | public class ConcurrentHashMapTest exten
431      }
432  
433      /**
434 <     *   putIfAbsent does not add the pair if the key is already present
434 >     * putIfAbsent does not add the pair if the key is already present
435       */
436      public void testPutIfAbsent2() {
437          ConcurrentHashMap map = map5();
# Line 258 | Line 439 | public class ConcurrentHashMapTest exten
439      }
440  
441      /**
442 <     *   replace fails when the given key is not present
442 >     * replace fails when the given key is not present
443       */
444      public void testReplace() {
445          ConcurrentHashMap map = map5();
# Line 267 | Line 448 | public class ConcurrentHashMapTest exten
448      }
449  
450      /**
451 <     *   replace succeeds if the key is already present
451 >     * replace succeeds if the key is already present
452       */
453      public void testReplace2() {
454          ConcurrentHashMap map = map5();
# Line 275 | Line 456 | public class ConcurrentHashMapTest exten
456          assertEquals("Z", map.get(one));
457      }
458  
278
459      /**
460       * replace value fails when the given key not mapped to expected value
461       */
# Line 296 | Line 476 | public class ConcurrentHashMapTest exten
476          assertEquals("Z", map.get(one));
477      }
478  
299
479      /**
480 <     *   remove removes the correct key-value pair from the map
480 >     * remove removes the correct key-value pair from the map
481       */
482      public void testRemove() {
483          ConcurrentHashMap map = map5();
# Line 321 | Line 500 | public class ConcurrentHashMapTest exten
500      }
501  
502      /**
503 <     *   size returns the correct values
503 >     * size returns the correct values
504       */
505      public void testSize() {
506          ConcurrentHashMap map = map5();
# Line 337 | Line 516 | public class ConcurrentHashMapTest exten
516          ConcurrentHashMap map = map5();
517          String s = map.toString();
518          for (int i = 1; i <= 5; ++i) {
519 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
519 >            assertTrue(s.contains(String.valueOf(i)));
520          }
521      }
522  
523      // Exception tests
524  
525      /**
526 <     * Cannot create with negative capacity
526 >     * Cannot create with only negative capacity
527       */
528      public void testConstructor1() {
529          try {
530 <            new ConcurrentHashMap(-1,0,1);
530 >            new ConcurrentHashMap(-1);
531              shouldThrow();
532          } catch (IllegalArgumentException success) {}
533      }
534  
535      /**
536 <     * Cannot create with negative concurrency level
537 <     */
536 >     * Constructor (initialCapacity, loadFactor) throws
537 >     * IllegalArgumentException if either argument is negative
538 >      */
539      public void testConstructor2() {
540          try {
541 <            new ConcurrentHashMap(1,0,-1);
541 >            new ConcurrentHashMap(-1, .75f);
542 >            shouldThrow();
543 >        } catch (IllegalArgumentException success) {}
544 >
545 >        try {
546 >            new ConcurrentHashMap(16, -1);
547              shouldThrow();
548          } catch (IllegalArgumentException success) {}
549      }
550  
551      /**
552 <     * Cannot create with only negative capacity
552 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
553 >     * throws IllegalArgumentException if any argument is negative
554       */
555      public void testConstructor3() {
556          try {
557 <            new ConcurrentHashMap(-1);
557 >            new ConcurrentHashMap(-1, .75f, 1);
558 >            shouldThrow();
559 >        } catch (IllegalArgumentException success) {}
560 >
561 >        try {
562 >            new ConcurrentHashMap(16, -1, 1);
563              shouldThrow();
564          } catch (IllegalArgumentException success) {}
565 +
566 +        try {
567 +            new ConcurrentHashMap(16, .75f, -1);
568 +            shouldThrow();
569 +        } catch (IllegalArgumentException success) {}
570 +    }
571 +
572 +    /**
573 +     * ConcurrentHashMap(map) throws NullPointerException if the given
574 +     * map is null
575 +     */
576 +    public void testConstructor4() {
577 +        try {
578 +            new ConcurrentHashMap(null);
579 +            shouldThrow();
580 +        } catch (NullPointerException success) {}
581 +    }
582 +
583 +    /**
584 +     * ConcurrentHashMap(map) creates a new map with the same mappings
585 +     * as the given map
586 +     */
587 +    public void testConstructor5() {
588 +        ConcurrentHashMap map1 = map5();
589 +        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
590 +        assertTrue(map2.equals(map1));
591 +        map2.put(one, "F");
592 +        assertFalse(map2.equals(map1));
593      }
594  
595      /**
596       * get(null) throws NPE
597       */
598      public void testGet_NullPointerException() {
599 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
600          try {
381            ConcurrentHashMap c = new ConcurrentHashMap(5);
601              c.get(null);
602              shouldThrow();
603          } catch (NullPointerException success) {}
# Line 388 | Line 607 | public class ConcurrentHashMapTest exten
607       * containsKey(null) throws NPE
608       */
609      public void testContainsKey_NullPointerException() {
610 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
611          try {
392            ConcurrentHashMap c = new ConcurrentHashMap(5);
612              c.containsKey(null);
613              shouldThrow();
614          } catch (NullPointerException success) {}
# Line 399 | Line 618 | public class ConcurrentHashMapTest exten
618       * containsValue(null) throws NPE
619       */
620      public void testContainsValue_NullPointerException() {
621 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
622          try {
403            ConcurrentHashMap c = new ConcurrentHashMap(5);
623              c.containsValue(null);
624              shouldThrow();
625          } catch (NullPointerException success) {}
# Line 410 | Line 629 | public class ConcurrentHashMapTest exten
629       * contains(null) throws NPE
630       */
631      public void testContains_NullPointerException() {
632 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
633          try {
414            ConcurrentHashMap c = new ConcurrentHashMap(5);
634              c.contains(null);
635              shouldThrow();
636          } catch (NullPointerException success) {}
# Line 421 | Line 640 | public class ConcurrentHashMapTest exten
640       * put(null,x) throws NPE
641       */
642      public void testPut1_NullPointerException() {
643 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
644          try {
425            ConcurrentHashMap c = new ConcurrentHashMap(5);
645              c.put(null, "whatever");
646              shouldThrow();
647          } catch (NullPointerException success) {}
# Line 432 | Line 651 | public class ConcurrentHashMapTest exten
651       * put(x, null) throws NPE
652       */
653      public void testPut2_NullPointerException() {
654 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
655          try {
436            ConcurrentHashMap c = new ConcurrentHashMap(5);
656              c.put("whatever", null);
657              shouldThrow();
658          } catch (NullPointerException success) {}
# Line 443 | Line 662 | public class ConcurrentHashMapTest exten
662       * putIfAbsent(null, x) throws NPE
663       */
664      public void testPutIfAbsent1_NullPointerException() {
665 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
666          try {
447            ConcurrentHashMap c = new ConcurrentHashMap(5);
667              c.putIfAbsent(null, "whatever");
668              shouldThrow();
669          } catch (NullPointerException success) {}
# Line 454 | Line 673 | public class ConcurrentHashMapTest exten
673       * replace(null, x) throws NPE
674       */
675      public void testReplace_NullPointerException() {
676 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
677          try {
458            ConcurrentHashMap c = new ConcurrentHashMap(5);
678              c.replace(null, "whatever");
679              shouldThrow();
680          } catch (NullPointerException success) {}
# Line 465 | Line 684 | public class ConcurrentHashMapTest exten
684       * replace(null, x, y) throws NPE
685       */
686      public void testReplaceValue_NullPointerException() {
687 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
688          try {
469            ConcurrentHashMap c = new ConcurrentHashMap(5);
689              c.replace(null, one, "whatever");
690              shouldThrow();
691          } catch (NullPointerException success) {}
# Line 476 | Line 695 | public class ConcurrentHashMapTest exten
695       * putIfAbsent(x, null) throws NPE
696       */
697      public void testPutIfAbsent2_NullPointerException() {
698 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
699          try {
480            ConcurrentHashMap c = new ConcurrentHashMap(5);
700              c.putIfAbsent("whatever", null);
701              shouldThrow();
702          } catch (NullPointerException success) {}
703      }
704  
486
705      /**
706       * replace(x, null) throws NPE
707       */
708      public void testReplace2_NullPointerException() {
709 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
710          try {
492            ConcurrentHashMap c = new ConcurrentHashMap(5);
711              c.replace("whatever", null);
712              shouldThrow();
713          } catch (NullPointerException success) {}
# Line 499 | Line 717 | public class ConcurrentHashMapTest exten
717       * replace(x, null, y) throws NPE
718       */
719      public void testReplaceValue2_NullPointerException() {
720 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
721          try {
503            ConcurrentHashMap c = new ConcurrentHashMap(5);
722              c.replace("whatever", null, "A");
723              shouldThrow();
724          } catch (NullPointerException success) {}
# Line 510 | Line 728 | public class ConcurrentHashMapTest exten
728       * replace(x, y, null) throws NPE
729       */
730      public void testReplaceValue3_NullPointerException() {
731 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
732          try {
514            ConcurrentHashMap c = new ConcurrentHashMap(5);
733              c.replace("whatever", one, null);
734              shouldThrow();
735          } catch (NullPointerException success) {}
736      }
737  
520
738      /**
739       * remove(null) throws NPE
740       */
741      public void testRemove1_NullPointerException() {
742 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
743 +        c.put("sadsdf", "asdads");
744          try {
526            ConcurrentHashMap c = new ConcurrentHashMap(5);
527            c.put("sadsdf", "asdads");
745              c.remove(null);
746              shouldThrow();
747          } catch (NullPointerException success) {}
# Line 534 | Line 751 | public class ConcurrentHashMapTest exten
751       * remove(null, x) throws NPE
752       */
753      public void testRemove2_NullPointerException() {
754 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
755 +        c.put("sadsdf", "asdads");
756          try {
538            ConcurrentHashMap c = new ConcurrentHashMap(5);
539            c.put("sadsdf", "asdads");
757              c.remove(null, "whatever");
758              shouldThrow();
759          } catch (NullPointerException success) {}
# Line 555 | Line 772 | public class ConcurrentHashMapTest exten
772       * A deserialized map equals original
773       */
774      public void testSerialization() throws Exception {
775 <        ConcurrentHashMap q = map5();
775 >        Map x = map5();
776 >        Map y = serialClone(x);
777  
778 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
779 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
780 <        out.writeObject(q);
781 <        out.close();
564 <
565 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
566 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
567 <        ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
568 <        assertEquals(q.size(), r.size());
569 <        assertTrue(q.equals(r));
570 <        assertTrue(r.equals(q));
778 >        assertNotSame(x, y);
779 >        assertEquals(x.size(), y.size());
780 >        assertEquals(x, y);
781 >        assertEquals(y, x);
782      }
783  
573
784      /**
785       * SetValue of an EntrySet entry sets value in the map.
786       */
# Line 582 | Line 792 | public class ConcurrentHashMapTest exten
792              map.put(new Integer(i), new Integer(i));
793          assertFalse(map.isEmpty());
794          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
795 <
796 <        // assert that entry1 is not 16
797 <        assertTrue("entry is 16, test not valid",
798 <                   !entry1.getKey().equals(new Integer(16)));
799 <
800 <        // remove 16 (a different key) from map
801 <        // which just happens to cause entry1 to be cloned in map
802 <        map.remove(new Integer(16));
803 <        entry1.setValue("XYZ");
594 <        assertTrue(map.containsValue("XYZ")); // fails
795 >        // Unless it happens to be first (in which case remainder of
796 >        // test is skipped), remove a possibly-colliding key from map
797 >        // which, under some implementations, may cause entry1 to be
798 >        // cloned in map
799 >        if (!entry1.getKey().equals(new Integer(16))) {
800 >            map.remove(new Integer(16));
801 >            entry1.setValue("XYZ");
802 >            assertTrue(map.containsValue("XYZ")); // fails if write-through broken
803 >        }
804      }
805  
806   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines