001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.util.junit;
024:
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import com.mchange.v2.util.DoubleWeakHashMap;
029:
030: import junit.framework.TestCase;
031:
032: public class DoubleWeakHashMapJUnitTestCase extends TestCase {
033: public void testGetNeverAdded() {
034: Map m = new DoubleWeakHashMap();
035: assertNull(m.get("foo"));
036: }
037:
038: public void testHardAdds() {
039: Integer a = new Integer(1);
040: Integer b = new Integer(2);
041: Integer c = new Integer(3);
042:
043: String poop = new String("poop");
044: String scoop = new String("scoop");
045: String doop = new String("dcoop");
046:
047: Map m = new DoubleWeakHashMap();
048: m.put(a, poop);
049: m.put(b, scoop);
050: m.put(c, doop);
051: assertEquals("Size should be three, viewed via Map directly.",
052: m.size(), 3);
053: assertEquals("Size should be three, viewed via keySet .", m
054: .keySet().size(), 3);
055: assertEquals(
056: "Size should be three, viewed via values Collection.",
057: m.values().size(), 3);
058:
059: int count = 0;
060: for (Iterator ii = m.keySet().iterator(); ii.hasNext();) {
061: count += ((Integer) ii.next()).intValue();
062: }
063: assertEquals(
064: "Count should be six, viewed via values Collection.",
065: count, 6);
066:
067: Integer d = new Integer(4);
068: m.put(d, poop);
069: m.values().remove(poop);
070: assertEquals(
071: "After removing a doubled value, size should be 2", m
072: .size(), 2);
073: }
074:
075: public void testWeakness() {
076: Integer a = new Integer(1);
077: Integer b = new Integer(2);
078: Integer c = new Integer(3);
079:
080: String poop = new String("poop");
081:
082: Map m = new DoubleWeakHashMap();
083: m.put(a, poop);
084: m.put(b, new Object());
085: m.put(c, new Object());
086:
087: //race condition... b & c might already have been removed... but i doubt it
088: assertEquals(
089: "1) Weak values should not yet have been removed (but not guaranteed! sometimes fails without a defect!)",
090: m.size(), 3);
091:
092: // we are relying that a full, synchronous GC occurs,
093: // which is not guaranteed in all VMs
094: System.gc();
095:
096: // let's see if we can force a deeper gc via a big array creation
097: byte[] bArray = new byte[1024 * 1024];
098:
099: assertEquals(
100: "2) Weak values should have been automatically removed (but not guaranteed! sometimes fails without a defect!)",
101: m.size(), 1);
102:
103: m.put(new Object(), b);
104:
105: //race condition... b & c might already have been removed... but i doubt it
106: assertEquals(
107: "3) Weak key should not yet have been removed (but not guaranteed! sometimes fails without a defect!)",
108: m.size(), 2);
109:
110: System.gc();
111: // let's see if we can force a deeper gc via a big array creation
112: bArray = new byte[1024 * 1024];
113:
114: assertEquals(
115: "4) Weak key should have been automatically removed (but not guaranteed! sometimes fails without a defect!)",
116: m.size(), 1);
117: }
118: }
|