1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.util.Enumeration; |
13 |
< |
import java.io.*; |
11 |
> |
import java.util.concurrent.ConcurrentHashMap; |
12 |
|
|
13 |
|
public class ConcurrentHashMapTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
23 |
|
*/ |
24 |
|
private static ConcurrentHashMap map5() { |
25 |
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
< |
* clear removes all pairs |
38 |
> |
* clear removes all pairs |
39 |
|
*/ |
40 |
|
public void testClear() { |
41 |
|
ConcurrentHashMap map = map5(); |
42 |
|
map.clear(); |
43 |
< |
assertEquals(map.size(), 0); |
43 |
> |
assertEquals(0, map.size()); |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
< |
* Maps with same contents are equal |
47 |
> |
* Maps with same contents are equal |
48 |
|
*/ |
49 |
|
public void testEquals() { |
50 |
|
ConcurrentHashMap map1 = map5(); |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* contains returns true for contained value |
60 |
> |
* contains returns true for contained value |
61 |
|
*/ |
62 |
|
public void testContains() { |
63 |
|
ConcurrentHashMap map = map5(); |
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* containsKey returns true for contained key |
69 |
> |
* containsKey returns true for contained key |
70 |
|
*/ |
71 |
|
public void testContainsKey() { |
72 |
|
ConcurrentHashMap map = map5(); |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* containsValue returns true for held values |
78 |
> |
* containsValue returns true for held values |
79 |
|
*/ |
80 |
|
public void testContainsValue() { |
81 |
|
ConcurrentHashMap map = map5(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* enumeration returns an enumeration containing the correct |
88 |
< |
* elements |
87 |
> |
* enumeration returns an enumeration containing the correct |
88 |
> |
* elements |
89 |
|
*/ |
90 |
|
public void testEnumeration() { |
91 |
|
ConcurrentHashMap map = map5(); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* get returns the correct element at the given key, |
103 |
< |
* or null if not present |
102 |
> |
* get returns the correct element at the given key, |
103 |
> |
* or null if not present |
104 |
|
*/ |
105 |
|
public void testGet() { |
106 |
|
ConcurrentHashMap map = map5(); |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
< |
* isEmpty is true of empty map and false for non-empty |
113 |
> |
* isEmpty is true of empty map and false for non-empty |
114 |
|
*/ |
115 |
|
public void testIsEmpty() { |
116 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* keys returns an enumeration containing all the keys from the map |
123 |
> |
* keys returns an enumeration containing all the keys from the map |
124 |
|
*/ |
125 |
|
public void testKeys() { |
126 |
|
ConcurrentHashMap map = map5(); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* keySet returns a Set containing all the keys |
137 |
> |
* keySet returns a Set containing all the keys |
138 |
|
*/ |
139 |
|
public void testKeySet() { |
140 |
|
ConcurrentHashMap map = map5(); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* keySet.toArray returns contains all keys |
151 |
> |
* keySet.toArray returns contains all keys |
152 |
|
*/ |
153 |
|
public void testKeySetToArray() { |
154 |
|
ConcurrentHashMap map = map5(); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* Values.toArray contains all values |
164 |
> |
* Values.toArray contains all values |
165 |
|
*/ |
166 |
|
public void testValuesToArray() { |
167 |
|
ConcurrentHashMap map = map5(); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
< |
* entrySet.toArray contains all entries |
180 |
> |
* entrySet.toArray contains all entries |
181 |
|
*/ |
182 |
|
public void testEntrySetToArray() { |
183 |
|
ConcurrentHashMap map = map5(); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* putAll adds all key-value pairs from the given map |
227 |
> |
* putAll adds all key-value pairs from the given map |
228 |
|
*/ |
229 |
|
public void testPutAll() { |
230 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
< |
* putIfAbsent works when the given key is not present |
242 |
> |
* putIfAbsent works when the given key is not present |
243 |
|
*/ |
244 |
|
public void testPutIfAbsent() { |
245 |
|
ConcurrentHashMap map = map5(); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
< |
* putIfAbsent does not add the pair if the key is already present |
251 |
> |
* putIfAbsent does not add the pair if the key is already present |
252 |
|
*/ |
253 |
|
public void testPutIfAbsent2() { |
254 |
|
ConcurrentHashMap map = map5(); |
256 |
|
} |
257 |
|
|
258 |
|
/** |
259 |
< |
* replace fails when the given key is not present |
259 |
> |
* replace fails when the given key is not present |
260 |
|
*/ |
261 |
|
public void testReplace() { |
262 |
|
ConcurrentHashMap map = map5(); |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
< |
* replace succeeds if the key is already present |
268 |
> |
* replace succeeds if the key is already present |
269 |
|
*/ |
270 |
|
public void testReplace2() { |
271 |
|
ConcurrentHashMap map = map5(); |
273 |
|
assertEquals("Z", map.get(one)); |
274 |
|
} |
275 |
|
|
278 |
– |
|
276 |
|
/** |
277 |
|
* replace value fails when the given key not mapped to expected value |
278 |
|
*/ |
293 |
|
assertEquals("Z", map.get(one)); |
294 |
|
} |
295 |
|
|
299 |
– |
|
296 |
|
/** |
297 |
< |
* remove removes the correct key-value pair from the map |
297 |
> |
* remove removes the correct key-value pair from the map |
298 |
|
*/ |
299 |
|
public void testRemove() { |
300 |
|
ConcurrentHashMap map = map5(); |
314 |
|
map.remove(four, "A"); |
315 |
|
assertEquals(4, map.size()); |
316 |
|
assertTrue(map.containsKey(four)); |
321 |
– |
|
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* size returns the correct values |
320 |
> |
* size returns the correct values |
321 |
|
*/ |
322 |
|
public void testSize() { |
323 |
|
ConcurrentHashMap map = map5(); |
333 |
|
ConcurrentHashMap map = map5(); |
334 |
|
String s = map.toString(); |
335 |
|
for (int i = 1; i <= 5; ++i) { |
336 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
336 |
> |
assertTrue(s.contains(String.valueOf(i))); |
337 |
|
} |
338 |
|
} |
339 |
|
|
479 |
|
} catch (NullPointerException success) {} |
480 |
|
} |
481 |
|
|
487 |
– |
|
482 |
|
/** |
483 |
|
* replace(x, null) throws NPE |
484 |
|
*/ |
512 |
|
} catch (NullPointerException success) {} |
513 |
|
} |
514 |
|
|
521 |
– |
|
515 |
|
/** |
516 |
|
* remove(null) throws NPE |
517 |
|
*/ |
549 |
|
* A deserialized map equals original |
550 |
|
*/ |
551 |
|
public void testSerialization() throws Exception { |
552 |
< |
ConcurrentHashMap q = map5(); |
552 |
> |
Map x = map5(); |
553 |
> |
Map y = serialClone(x); |
554 |
|
|
555 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
556 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
557 |
< |
out.writeObject(q); |
558 |
< |
out.close(); |
565 |
< |
|
566 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
567 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
568 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
569 |
< |
assertEquals(q.size(), r.size()); |
570 |
< |
assertTrue(q.equals(r)); |
571 |
< |
assertTrue(r.equals(q)); |
555 |
> |
assertTrue(x != y); |
556 |
> |
assertEquals(x.size(), y.size()); |
557 |
> |
assertEquals(x, y); |
558 |
> |
assertEquals(y, x); |
559 |
|
} |
560 |
|
|
574 |
– |
|
561 |
|
/** |
562 |
|
* SetValue of an EntrySet entry sets value in the map. |
563 |
|
*/ |