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