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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
import java.util.Enumeration; |
12 |
import java.io.*; |
13 |
|
14 |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
15 |
public static void main(String[] args) { |
16 |
junit.textui.TestRunner.run (suite()); |
17 |
} |
18 |
public static Test suite() { |
19 |
return new TestSuite(ConcurrentHashMapTest.class); |
20 |
} |
21 |
|
22 |
private static ConcurrentHashMap map5() { |
23 |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
24 |
assertTrue(map.isEmpty()); |
25 |
map.put(one, "A"); |
26 |
map.put(two, "B"); |
27 |
map.put(three, "C"); |
28 |
map.put(four, "D"); |
29 |
map.put(five, "E"); |
30 |
assertFalse(map.isEmpty()); |
31 |
assertEquals(5, map.size()); |
32 |
return map; |
33 |
} |
34 |
|
35 |
/** |
36 |
* clear removes all key-element pairs from the map |
37 |
*/ |
38 |
public void testClear(){ |
39 |
ConcurrentHashMap map = map5(); |
40 |
map.clear(); |
41 |
assertEquals(map.size(), 0); |
42 |
} |
43 |
|
44 |
/** |
45 |
* contains gives the appropriate value |
46 |
*/ |
47 |
public void testContains(){ |
48 |
ConcurrentHashMap map = map5(); |
49 |
assertTrue(map.contains("A")); |
50 |
assertFalse(map.contains("Z")); |
51 |
} |
52 |
|
53 |
/** |
54 |
* containsKey gives the appropriate value |
55 |
*/ |
56 |
public void testContainsKey(){ |
57 |
ConcurrentHashMap map = map5(); |
58 |
assertTrue(map.containsKey(one)); |
59 |
assertFalse(map.containsKey(new Integer(100))); |
60 |
} |
61 |
|
62 |
/** |
63 |
* Identical to normal contains |
64 |
*/ |
65 |
public void testContainsValue(){ |
66 |
ConcurrentHashMap map = map5(); |
67 |
assertTrue(map.contains("A")); |
68 |
assertFalse(map.contains("Z")); |
69 |
} |
70 |
|
71 |
/** |
72 |
* enumeration returns an enumeration containing the correct |
73 |
* elements |
74 |
*/ |
75 |
public void testEnumeration(){ |
76 |
ConcurrentHashMap map = map5(); |
77 |
Enumeration e = map.elements(); |
78 |
int count = 0; |
79 |
while(e.hasMoreElements()){ |
80 |
count++; |
81 |
e.nextElement(); |
82 |
} |
83 |
assertEquals(5, count); |
84 |
} |
85 |
|
86 |
/** |
87 |
* Clone creates an equal map |
88 |
*/ |
89 |
public void testClone(){ |
90 |
ConcurrentHashMap map = map5(); |
91 |
ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone()); |
92 |
assertEquals(map, m2); |
93 |
} |
94 |
|
95 |
/** |
96 |
* get returns the correct element at the given index |
97 |
*/ |
98 |
public void testGet(){ |
99 |
ConcurrentHashMap map = map5(); |
100 |
assertEquals("A", (String)map.get(one)); |
101 |
} |
102 |
|
103 |
/** |
104 |
* get on a nonexistant key returns null |
105 |
*/ |
106 |
public void testGet2(){ |
107 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
108 |
assertNull(empty.get("anything")); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Simple test to verify isEmpty returns the correct value |
113 |
*/ |
114 |
public void testIsEmpty(){ |
115 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
116 |
ConcurrentHashMap map = map5(); |
117 |
assertTrue(empty.isEmpty()); |
118 |
assertFalse(map.isEmpty()); |
119 |
} |
120 |
|
121 |
/** |
122 |
* keys returns an enumeration containing all the keys from the map |
123 |
*/ |
124 |
public void testKeys(){ |
125 |
ConcurrentHashMap map = map5(); |
126 |
Enumeration e = map.keys(); |
127 |
int count = 0; |
128 |
while(e.hasMoreElements()){ |
129 |
count++; |
130 |
e.nextElement(); |
131 |
} |
132 |
assertEquals(5, count); |
133 |
} |
134 |
|
135 |
/** |
136 |
* keySet returns a Set containing all the keys |
137 |
*/ |
138 |
public void testKeySet(){ |
139 |
ConcurrentHashMap map = map5(); |
140 |
Set s = map.keySet(); |
141 |
assertEquals(5, s.size()); |
142 |
assertTrue(s.contains(one)); |
143 |
assertTrue(s.contains(two)); |
144 |
assertTrue(s.contains(three)); |
145 |
assertTrue(s.contains(four)); |
146 |
assertTrue(s.contains(five)); |
147 |
} |
148 |
|
149 |
public void testValues(){ |
150 |
ConcurrentHashMap map = map5(); |
151 |
Collection s = map.values(); |
152 |
assertEquals(5, s.size()); |
153 |
assertTrue(s.contains("A")); |
154 |
assertTrue(s.contains("B")); |
155 |
assertTrue(s.contains("C")); |
156 |
assertTrue(s.contains("D")); |
157 |
assertTrue(s.contains("E")); |
158 |
} |
159 |
|
160 |
public void testEntrySet(){ |
161 |
ConcurrentHashMap map = map5(); |
162 |
Set s = map.entrySet(); |
163 |
assertEquals(5, s.size()); |
164 |
Iterator it = s.iterator(); |
165 |
while (it.hasNext()) { |
166 |
Map.Entry e = (Map.Entry) it.next(); |
167 |
assertTrue( |
168 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
169 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
170 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
171 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
172 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
173 |
} |
174 |
} |
175 |
|
176 |
/** |
177 |
* putAll adds all key-value pairs from the given map |
178 |
*/ |
179 |
public void testPutAll(){ |
180 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
181 |
ConcurrentHashMap map = map5(); |
182 |
empty.putAll(map); |
183 |
assertEquals(5, empty.size()); |
184 |
assertTrue(empty.containsKey(one)); |
185 |
assertTrue(empty.containsKey(two)); |
186 |
assertTrue(empty.containsKey(three)); |
187 |
assertTrue(empty.containsKey(four)); |
188 |
assertTrue(empty.containsKey(five)); |
189 |
} |
190 |
|
191 |
/** |
192 |
* putIfAbsent works when the given key is not present |
193 |
*/ |
194 |
public void testPutIfAbsent(){ |
195 |
ConcurrentHashMap map = map5(); |
196 |
map.putIfAbsent(new Integer(6), "Z"); |
197 |
assertTrue(map.containsKey(new Integer(6))); |
198 |
} |
199 |
|
200 |
/** |
201 |
* putIfAbsent does not add the pair if the key is already present |
202 |
*/ |
203 |
public void testPutIfAbsent2(){ |
204 |
ConcurrentHashMap map = map5(); |
205 |
assertEquals("A", map.putIfAbsent(one, "Z")); |
206 |
} |
207 |
|
208 |
/** |
209 |
* remove removes the correct key-value pair from the map |
210 |
*/ |
211 |
public void testRemove(){ |
212 |
ConcurrentHashMap map = map5(); |
213 |
map.remove(five); |
214 |
assertEquals(4, map.size()); |
215 |
assertFalse(map.containsKey(five)); |
216 |
} |
217 |
|
218 |
public void testRemove2(){ |
219 |
ConcurrentHashMap map = map5(); |
220 |
map.remove(five, "E"); |
221 |
assertEquals(4, map.size()); |
222 |
assertFalse(map.containsKey(five)); |
223 |
map.remove(four, "A"); |
224 |
assertEquals(4, map.size()); |
225 |
assertTrue(map.containsKey(four)); |
226 |
|
227 |
} |
228 |
|
229 |
/** |
230 |
* size returns the correct values |
231 |
*/ |
232 |
public void testSize(){ |
233 |
ConcurrentHashMap map = map5(); |
234 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
235 |
assertEquals(0, empty.size()); |
236 |
assertEquals(5, map.size()); |
237 |
} |
238 |
|
239 |
public void testToString(){ |
240 |
ConcurrentHashMap map = map5(); |
241 |
String s = map.toString(); |
242 |
for (int i = 1; i <= 5; ++i) { |
243 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
244 |
} |
245 |
} |
246 |
|
247 |
// Exception tests |
248 |
|
249 |
public void testConstructor1(){ |
250 |
try{ |
251 |
new ConcurrentHashMap(-1,0,1); |
252 |
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
253 |
}catch(IllegalArgumentException e){} |
254 |
} |
255 |
|
256 |
public void testConstructor2(){ |
257 |
try{ |
258 |
new ConcurrentHashMap(1,0,-1); |
259 |
fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception"); |
260 |
}catch(IllegalArgumentException e){} |
261 |
} |
262 |
|
263 |
public void testConstructor3(){ |
264 |
try{ |
265 |
new ConcurrentHashMap(-1); |
266 |
fail("ConcurrentHashMap(int) should throw Illegal Argument Exception"); |
267 |
}catch(IllegalArgumentException e){} |
268 |
} |
269 |
|
270 |
public void testGet_NullPointerException(){ |
271 |
try{ |
272 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
273 |
c.get(null); |
274 |
fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception"); |
275 |
}catch(NullPointerException e){} |
276 |
} |
277 |
|
278 |
public void testContainsKey_NullPointerException(){ |
279 |
try{ |
280 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
281 |
c.containsKey(null); |
282 |
fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception"); |
283 |
}catch(NullPointerException e){} |
284 |
} |
285 |
|
286 |
public void testContainsValue_NullPointerException(){ |
287 |
try{ |
288 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
289 |
c.containsValue(null); |
290 |
fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception"); |
291 |
}catch(NullPointerException e){} |
292 |
} |
293 |
|
294 |
public void testContains_NullPointerException(){ |
295 |
try{ |
296 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
297 |
c.contains(null); |
298 |
fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception"); |
299 |
}catch(NullPointerException e){} |
300 |
} |
301 |
|
302 |
public void testPut1_NullPointerException(){ |
303 |
try{ |
304 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
305 |
c.put(null, "whatever"); |
306 |
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
307 |
}catch(NullPointerException e){} |
308 |
} |
309 |
|
310 |
public void testPut2_NullPointerException(){ |
311 |
try{ |
312 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
313 |
c.put("whatever", null); |
314 |
fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception"); |
315 |
}catch(NullPointerException e){} |
316 |
} |
317 |
|
318 |
public void testPutIfAbsent1_NullPointerException(){ |
319 |
try{ |
320 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
321 |
c.putIfAbsent(null, "whatever"); |
322 |
fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
323 |
}catch(NullPointerException e){} |
324 |
} |
325 |
|
326 |
public void testPutIfAbsent2_NullPointerException(){ |
327 |
try{ |
328 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
329 |
c.putIfAbsent("whatever", null); |
330 |
fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception"); |
331 |
}catch(NullPointerException e){} |
332 |
} |
333 |
|
334 |
|
335 |
public void testRemove1_NullPointerException(){ |
336 |
try{ |
337 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
338 |
c.put("sadsdf", "asdads"); |
339 |
c.remove(null); |
340 |
fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione"); |
341 |
}catch(NullPointerException e){} |
342 |
} |
343 |
|
344 |
public void testRemove2_NullPointerException(){ |
345 |
try{ |
346 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
347 |
c.put("sadsdf", "asdads"); |
348 |
c.remove(null, "whatever"); |
349 |
fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione"); |
350 |
}catch(NullPointerException e){} |
351 |
} |
352 |
|
353 |
public void testSerialization() { |
354 |
ConcurrentHashMap q = map5(); |
355 |
|
356 |
try { |
357 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
358 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
359 |
out.writeObject(q); |
360 |
out.close(); |
361 |
|
362 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
363 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
364 |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
365 |
assertEquals(q.size(), r.size()); |
366 |
assertTrue(q.equals(r)); |
367 |
assertTrue(r.equals(q)); |
368 |
} catch(Exception e){ |
369 |
e.printStackTrace(); |
370 |
fail("unexpected exception"); |
371 |
} |
372 |
} |
373 |
|
374 |
|
375 |
} |