blob: 6e6ed4f11547e0a5a1a6a3d2a7bee69a35d1a702 [file] [log] [blame]
crazyboblee210bf432009-02-05 06:04:18 +00001/*
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
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
limpbizkit@gmail.com9a227be2010-07-03 15:51:31 +00009package com.google.inject.internal.util;
crazyboblee210bf432009-02-05 06:04:18 +000010
limpbizkit@gmail.com9a227be2010-07-03 15:51:31 +000011import com.google.inject.internal.util.Jsr166HashMap;
crazyboblee210bf432009-02-05 06:04:18 +000012import junit.framework.TestCase;
13
14import java.io.BufferedInputStream;
15import java.io.BufferedOutputStream;
16import java.io.ByteArrayInputStream;
17import java.io.ByteArrayOutputStream;
18import java.io.ObjectInputStream;
19import java.io.ObjectOutputStream;
20import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.Collection;
23import java.util.Iterator;
24import java.util.Map;
25import java.util.Set;
26
27/**
28 * ConcurrentHashMap tests copied from ConcurrentHashMapTest. Useful as a
29 * test case for CustomConcurrentHashMap.
30 */
31public class Jsr166HashMapTest extends TestCase {
32
33 /*
34 * The following two methods and constants were copied from JSR166TestCase.
35 */
36
37 /**
38 * fail with message "should throw exception"
39 */
40 public void shouldThrow() {
41 fail("Should throw exception");
42 }
43
44 /**
45 * fail with message "Unexpected exception"
46 */
47 public void unexpectedException() {
48 fail("Unexpected exception");
49 }
50
51 static final Integer zero = new Integer(0);
52 static final Integer one = new Integer(1);
53 static final Integer two = new Integer(2);
54 static final Integer three = new Integer(3);
55 static final Integer four = new Integer(4);
56 static final Integer five = new Integer(5);
57 static final Integer six = new Integer(6);
58 static final Integer seven = new Integer(7);
59 static final Integer eight = new Integer(8);
60 static final Integer nine = new Integer(9);
61 static final Integer m1 = new Integer(-1);
62 static final Integer m2 = new Integer(-2);
63 static final Integer m3 = new Integer(-3);
64 static final Integer m4 = new Integer(-4);
65 static final Integer m5 = new Integer(-5);
66 static final Integer m6 = new Integer(-6);
67 static final Integer m10 = new Integer(-10);
68
69 /**
70 * Create a map from Integers 1-5 to Strings "A"-"E".
71 */
72 private static Jsr166HashMap map5() {
73 Jsr166HashMap map = new Jsr166HashMap(5);
74 assertTrue(map.isEmpty());
75 map.put(one, "A");
76 map.put(two, "B");
77 map.put(three, "C");
78 map.put(four, "D");
79 map.put(five, "E");
80 assertFalse(map.isEmpty());
81 assertEquals(5, map.size());
82 return map;
83 }
84
85 /**
86 * clear removes all pairs
87 */
88 public void testClear() {
89 Jsr166HashMap map = map5();
90 map.clear();
91 assertEquals(map.size(), 0);
92 }
93
94 /**
95 * Maps with same contents are equal
96 */
97 public void testEquals() {
98 Jsr166HashMap map1 = map5();
99 Jsr166HashMap map2 = map5();
100 assertEquals(map1, map2);
101 assertEquals(map2, map1);
102 map1.clear();
103 assertFalse(map1.equals(map2));
104 assertFalse(map2.equals(map1));
105 }
106
107 /**
108 * containsKey returns true for contained key
109 */
110 public void testContainsKey() {
111 Jsr166HashMap map = map5();
112 assertTrue(map.containsKey(one));
113 assertFalse(map.containsKey(zero));
114 }
115
116 /**
117 * containsValue returns true for held values
118 */
119 public void testContainsValue() {
120 Jsr166HashMap map = map5();
121 assertTrue(map.containsValue("A"));
122 assertFalse(map.containsValue("Z"));
123 }
124
125 /**
126 * get returns the correct element at the given key, or null if not present
127 */
128 public void testGet() {
129 Jsr166HashMap map = map5();
130 assertEquals("A", (String) map.get(one));
131 Jsr166HashMap empty = new Jsr166HashMap();
132 assertNull(map.get("anything"));
133 }
134
135 /**
136 * isEmpty is true of empty map and false for non-empty
137 */
138 public void testIsEmpty() {
139 Jsr166HashMap empty = new Jsr166HashMap();
140 Jsr166HashMap map = map5();
141 assertTrue(empty.isEmpty());
142 assertFalse(map.isEmpty());
143 }
144
145 /**
146 * keySet returns a Set containing all the keys
147 */
148 public void testKeySet() {
149 Jsr166HashMap map = map5();
150 Set s = map.keySet();
151 assertEquals(5, s.size());
152 assertTrue(s.contains(one));
153 assertTrue(s.contains(two));
154 assertTrue(s.contains(three));
155 assertTrue(s.contains(four));
156 assertTrue(s.contains(five));
157 }
158
159 /**
160 * keySet.toArray returns contains all keys
161 */
162 public void testKeySetToArray() {
163 Jsr166HashMap map = map5();
164 Set s = map.keySet();
165 Object[] ar = s.toArray();
166 assertTrue(s.containsAll(Arrays.asList(ar)));
167 assertEquals(5, ar.length);
168 ar[0] = m10;
169 assertFalse(s.containsAll(Arrays.asList(ar)));
170 }
171
172 /**
173 * Values.toArray contains all values
174 */
175 public void testValuesToArray() {
176 Jsr166HashMap map = map5();
177 Collection v = map.values();
178 Object[] ar = v.toArray();
179 ArrayList s = new ArrayList(Arrays.asList(ar));
180 assertEquals(5, ar.length);
181 assertTrue(s.contains("A"));
182 assertTrue(s.contains("B"));
183 assertTrue(s.contains("C"));
184 assertTrue(s.contains("D"));
185 assertTrue(s.contains("E"));
186 }
187
188 /**
189 * entrySet.toArray contains all entries
190 */
191 public void testEntrySetToArray() {
192 Jsr166HashMap map = map5();
193 Set s = map.entrySet();
194 Object[] ar = s.toArray();
195 assertEquals(5, ar.length);
196 for (int i = 0; i < 5; ++i) {
197 assertTrue(map.containsKey(((Map.Entry) (ar[i])).getKey()));
198 assertTrue(map.containsValue(((Map.Entry) (ar[i])).getValue()));
199 }
200 }
201
202 /**
203 * values collection contains all values
204 */
205 public void testValues() {
206 Jsr166HashMap map = map5();
207 Collection s = map.values();
208 assertEquals(5, s.size());
209 assertTrue(s.contains("A"));
210 assertTrue(s.contains("B"));
211 assertTrue(s.contains("C"));
212 assertTrue(s.contains("D"));
213 assertTrue(s.contains("E"));
214 }
215
216 /**
217 * entrySet contains all pairs
218 */
219 public void testEntrySet() {
220 Jsr166HashMap map = map5();
221 Set s = map.entrySet();
222 assertEquals(5, s.size());
223 Iterator it = s.iterator();
224 while (it.hasNext()) {
225 Map.Entry e = (Map.Entry) it.next();
226 assertTrue(
227 (e.getKey().equals(one) && e.getValue().equals("A")) ||
228 (e.getKey().equals(two) && e.getValue().equals("B"))
229 ||
230 (e.getKey().equals(three) && e.getValue()
231 .equals("C")) ||
232 (e.getKey().equals(four) && e.getValue()
233 .equals("D")) ||
234 (e.getKey().equals(five) && e.getValue()
235 .equals("E")));
236 }
237 }
238
239 /**
240 * putAll adds all key-value pairs from the given map
241 */
242 public void testPutAll() {
243 Jsr166HashMap empty = new Jsr166HashMap();
244 Jsr166HashMap map = map5();
245 empty.putAll(map);
246 assertEquals(5, empty.size());
247 assertTrue(empty.containsKey(one));
248 assertTrue(empty.containsKey(two));
249 assertTrue(empty.containsKey(three));
250 assertTrue(empty.containsKey(four));
251 assertTrue(empty.containsKey(five));
252 }
253
254 /**
255 * putIfAbsent works when the given key is not present
256 */
257 public void testPutIfAbsent() {
258 Jsr166HashMap map = map5();
259 map.putIfAbsent(six, "Z");
260 assertTrue(map.containsKey(six));
261 }
262
263 /**
264 * putIfAbsent does not add the pair if the key is already present
265 */
266 public void testPutIfAbsent2() {
267 Jsr166HashMap map = map5();
268 assertEquals("A", map.putIfAbsent(one, "Z"));
269 }
270
271 /**
272 * replace fails when the given key is not present
273 */
274 public void testReplace() {
275 Jsr166HashMap map = map5();
276 assertNull(map.replace(six, "Z"));
277 assertFalse(map.containsKey(six));
278 }
279
280 /**
281 * replace succeeds if the key is already present
282 */
283 public void testReplace2() {
284 Jsr166HashMap map = map5();
285 assertNotNull(map.replace(one, "Z"));
286 assertEquals("Z", map.get(one));
287 }
288
289
290 /**
291 * replace value fails when the given key not mapped to expected value
292 */
293 public void testReplaceValue() {
294 Jsr166HashMap map = map5();
295 assertEquals("A", map.get(one));
296 assertFalse(map.replace(one, "Z", "Z"));
297 assertEquals("A", map.get(one));
298 }
299
300 /**
301 * replace value succeeds when the given key mapped to expected value
302 */
303 public void testReplaceValue2() {
304 Jsr166HashMap map = map5();
305 assertEquals("A", map.get(one));
306 assertTrue(map.replace(one, "A", "Z"));
307 assertEquals("Z", map.get(one));
308 }
309
310
311 /**
312 * remove removes the correct key-value pair from the map
313 */
314 public void testRemove() {
315 Jsr166HashMap map = map5();
316 map.remove(five);
317 assertEquals(4, map.size());
318 assertFalse(map.containsKey(five));
319 }
320
321 /**
322 * remove(key,value) removes only if pair present
323 */
324 public void testRemove2() {
325 Jsr166HashMap map = map5();
326 map.remove(five, "E");
327 assertEquals(4, map.size());
328 assertFalse(map.containsKey(five));
329 map.remove(four, "A");
330 assertEquals(4, map.size());
331 assertTrue(map.containsKey(four));
332
333 }
334
335 /**
336 * size returns the correct values
337 */
338 public void testSize() {
339 Jsr166HashMap map = map5();
340 Jsr166HashMap empty = new Jsr166HashMap();
341 assertEquals(0, empty.size());
342 assertEquals(5, map.size());
343 }
344
345 /**
346 * toString contains toString of elements
347 */
348 public void testToString() {
349 Jsr166HashMap map = map5();
350 String s = map.toString();
351 for (int i = 1; i <= 5; ++i) {
352 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
353 }
354 }
355
356 // Exception tests
357
358 /**
359 * Cannot create with negative capacity
360 */
361 public void testConstructor1() {
362 try {
363 new Jsr166HashMap(-1, 0, 1);
364 shouldThrow();
365 } catch (IllegalArgumentException e) {
366 }
367 }
368
369 /**
370 * Cannot create with negative concurrency level
371 */
372 public void testConstructor2() {
373 try {
374 new Jsr166HashMap(1, 0, -1);
375 shouldThrow();
376 } catch (IllegalArgumentException e) {
377 }
378 }
379
380 /**
381 * Cannot create with only negative capacity
382 */
383 public void testConstructor3() {
384 try {
385 new Jsr166HashMap(-1);
386 shouldThrow();
387 } catch (IllegalArgumentException e) {
388 }
389 }
390
391 /**
392 * get(null) throws NPE
393 */
394 public void testGet_NullPointerException() {
395 try {
396 Jsr166HashMap c = new Jsr166HashMap(5);
397 c.get(null);
398 shouldThrow();
399 } catch (NullPointerException e) {
400 }
401 }
402
403 /**
404 * containsKey(null) throws NPE
405 */
406 public void testContainsKey_NullPointerException() {
407 try {
408 Jsr166HashMap c = new Jsr166HashMap(5);
409 c.containsKey(null);
410 shouldThrow();
411 } catch (NullPointerException e) {
412 }
413 }
414
415 /**
416 * containsValue(null) throws NPE
417 */
418 public void testContainsValue_NullPointerException() {
419 try {
420 Jsr166HashMap c = new Jsr166HashMap(5);
421 c.containsValue(null);
422 shouldThrow();
423 } catch (NullPointerException e) {
424 }
425 }
426
427 /**
428 * put(null,x) throws NPE
429 */
430 public void testPut1_NullPointerException() {
431 try {
432 Jsr166HashMap c = new Jsr166HashMap(5);
433 c.put(null, "whatever");
434 shouldThrow();
435 } catch (NullPointerException e) {
436 }
437 }
438
439 /**
440 * put(x, null) throws NPE
441 */
442 public void testPut2_NullPointerException() {
443 try {
444 Jsr166HashMap c = new Jsr166HashMap(5);
445 c.put("whatever", null);
446 shouldThrow();
447 } catch (NullPointerException e) {
448 }
449 }
450
451 /**
452 * putIfAbsent(null, x) throws NPE
453 */
454 public void testPutIfAbsent1_NullPointerException() {
455 try {
456 Jsr166HashMap c = new Jsr166HashMap(5);
457 c.putIfAbsent(null, "whatever");
458 shouldThrow();
459 } catch (NullPointerException e) {
460 }
461 }
462
463 /**
464 * replace(null, x) throws NPE
465 */
466 public void testReplace_NullPointerException() {
467 try {
468 Jsr166HashMap c = new Jsr166HashMap(5);
469 c.replace(null, "whatever");
470 shouldThrow();
471 } catch (NullPointerException e) {
472 }
473 }
474
475 /**
476 * replace(null, x, y) throws NPE
477 */
478 public void testReplaceValue_NullPointerException() {
479 try {
480 Jsr166HashMap c = new Jsr166HashMap(5);
481 c.replace(null, one, "whatever");
482 shouldThrow();
483 } catch (NullPointerException e) {
484 }
485 }
486
487 /**
488 * putIfAbsent(x, null) throws NPE
489 */
490 public void testPutIfAbsent2_NullPointerException() {
491 try {
492 Jsr166HashMap c = new Jsr166HashMap(5);
493 c.putIfAbsent("whatever", null);
494 shouldThrow();
495 } catch (NullPointerException e) {
496 }
497 }
498
499
500 /**
501 * replace(x, null) throws NPE
502 */
503 public void testReplace2_NullPointerException() {
504 try {
505 Jsr166HashMap c = new Jsr166HashMap(5);
506 c.replace("whatever", null);
507 shouldThrow();
508 } catch (NullPointerException e) {
509 }
510 }
511
512 /**
513 * replace(x, null, y) throws NPE
514 */
515 public void testReplaceValue2_NullPointerException() {
516 try {
517 Jsr166HashMap c = new Jsr166HashMap(5);
518 c.replace("whatever", null, "A");
519 shouldThrow();
520 } catch (NullPointerException e) {
521 }
522 }
523
524 /**
525 * replace(x, y, null) throws NPE
526 */
527 public void testReplaceValue3_NullPointerException() {
528 try {
529 Jsr166HashMap c = new Jsr166HashMap(5);
530 c.replace("whatever", one, null);
531 shouldThrow();
532 } catch (NullPointerException e) {
533 }
534 }
535
536
537 /**
538 * remove(null) throws NPE
539 */
540 public void testRemove1_NullPointerException() {
541 try {
542 Jsr166HashMap c = new Jsr166HashMap(5);
543 c.put("sadsdf", "asdads");
544 c.remove(null);
545 shouldThrow();
546 } catch (NullPointerException e) {
547 }
548 }
549
550 /**
551 * remove(null, x) throws NPE
552 */
553 public void testRemove2_NullPointerException() {
554 try {
555 Jsr166HashMap c = new Jsr166HashMap(5);
556 c.put("sadsdf", "asdads");
557 c.remove(null, "whatever");
558 shouldThrow();
559 } catch (NullPointerException e) {
560 }
561 }
562
563 /**
564 * remove(x, null) returns false
565 */
566 public void testRemove3() {
567 try {
568 Jsr166HashMap c = new Jsr166HashMap(5);
569 c.put("sadsdf", "asdads");
570 assertFalse(c.remove("sadsdf", null));
571 } catch (NullPointerException e) {
572 fail();
573 }
574 }
575
576 /**
577 * A deserialized map equals original
578 */
579 public void testSerialization() {
580 Jsr166HashMap q = map5();
581
582 try {
583 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
584 ObjectOutputStream out =
585 new ObjectOutputStream(new BufferedOutputStream(bout));
586 out.writeObject(q);
587 out.close();
588
589 ByteArrayInputStream bin =
590 new ByteArrayInputStream(bout.toByteArray());
591 ObjectInputStream in =
592 new ObjectInputStream(new BufferedInputStream(bin));
593 Jsr166HashMap r = (Jsr166HashMap) in.readObject();
594 assertEquals(q.size(), r.size());
595 assertTrue(q.equals(r));
596 assertTrue(r.equals(q));
597 } catch (Exception e) {
598 e.printStackTrace();
599 unexpectedException();
600 }
601 }
602
603
604 /**
605 * SetValue of an EntrySet entry sets value in the map.
606 */
607 public void testSetValueWriteThrough() {
608 // Adapted from a bug report by Eric Zoerner
609 Jsr166HashMap map = new Jsr166HashMap(2, 5.0f, 1);
610 assertTrue(map.isEmpty());
611 for (int i = 0; i < 20; i++) {
612 map.put(new Integer(i), new Integer(i));
613 }
614 assertFalse(map.isEmpty());
615 Map.Entry entry1 = (Map.Entry) map.entrySet().iterator().next();
616
617 // assert that entry1 is not 16
618 assertTrue("entry is 16, test not valid",
619 !entry1.getKey().equals(new Integer(16)));
620
621 // remove 16 (a different key) from map
622 // which just happens to cause entry1 to be cloned in map
623 map.remove(new Integer(16));
624 entry1.setValue("XYZ");
625 assertTrue(map.containsValue("XYZ")); // fails
626 }
627
628}