KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > util > junit > DoubleWeakHashMapJUnitTestCase


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.util.junit;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import com.mchange.v2.util.DoubleWeakHashMap;
30
31 import junit.framework.TestCase;
32
33 public class DoubleWeakHashMapJUnitTestCase extends TestCase
34 {
35     public void testGetNeverAdded()
36     {
37         Map JavaDoc m = new DoubleWeakHashMap();
38         assertNull( m.get("foo") );
39     }
40     
41     public void testHardAdds()
42     {
43         Integer JavaDoc a = new Integer JavaDoc(1);
44         Integer JavaDoc b = new Integer JavaDoc(2);
45         Integer JavaDoc c = new Integer JavaDoc(3);
46         
47         String JavaDoc poop = new String JavaDoc("poop");
48         String JavaDoc scoop = new String JavaDoc("scoop");
49         String JavaDoc doop = new String JavaDoc("dcoop");
50         
51         Map JavaDoc m = new DoubleWeakHashMap();
52         m.put(a, poop);
53         m.put(b, scoop);
54         m.put(c, doop);
55         assertEquals("Size should be three, viewed via Map directly.", m.size(), 3);
56         assertEquals("Size should be three, viewed via keySet .", m.keySet().size(), 3);
57         assertEquals("Size should be three, viewed via values Collection.", m.values().size(), 3);
58         
59         int count = 0;
60         for (Iterator JavaDoc ii = m.keySet().iterator(); ii.hasNext();)
61         {
62             count += ((Integer JavaDoc) ii.next()).intValue();
63         }
64         assertEquals("Count should be six, viewed via values Collection.", count, 6);
65         
66         Integer JavaDoc d = new Integer JavaDoc(4);
67         m.put(d, poop);
68         m.values().remove(poop);
69         assertEquals("After removing a doubled value, size should be 2", m.size(), 2);
70     }
71     
72     public void testWeakness()
73     {
74         Integer JavaDoc a = new Integer JavaDoc(1);
75         Integer JavaDoc b = new Integer JavaDoc(2);
76         Integer JavaDoc c = new Integer JavaDoc(3);
77         
78         String JavaDoc poop = new String JavaDoc("poop");
79
80         Map JavaDoc m = new DoubleWeakHashMap();
81         m.put(a, poop);
82         m.put(b, new Object JavaDoc());
83         m.put(c, new Object JavaDoc());
84         
85         //race condition... b & c might already have been removed... but i doubt it
86
assertEquals("1) Weak values should not yet have been removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 3);
87         
88         // we are relying that a full, synchronous GC occurs,
89
// which is not guaranteed in all VMs
90
System.gc();
91         
92         // let's see if we can force a deeper gc via a big array creation
93
byte[] bArray = new byte[1024 * 1024];
94         
95         assertEquals("2) Weak values should have been automatically removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 1);
96         
97         m.put( new Object JavaDoc(), b);
98         
99         //race condition... b & c might already have been removed... but i doubt it
100
assertEquals("3) Weak key should not yet have been removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 2);
101
102         System.gc();
103         // let's see if we can force a deeper gc via a big array creation
104
bArray = new byte[1024 * 1024];
105
106         assertEquals("4) Weak key should have been automatically removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 1);
107     }
108 }
109
Popular Tags