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