001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: ReferenceValueMapTestCase.java,v 1.3 2002/11/08 05:06:28 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.util.test;
012:
013: import com.triactive.jdo.util.ReferenceValueMap;
014: import java.util.Arrays;
015: import java.util.Iterator;
016: import java.util.Map;
017: import java.util.Set;
018: import junit.framework.TestCase;
019: import org.apache.log4j.Category;
020:
021: /**
022: * Abstract base class for tests that test the functionality of a
023: * {@link ReferenceValueMap}.
024: *
025: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
026: * @version $Revision: 1.3 $
027: */
028:
029: public abstract class ReferenceValueMapTestCase extends TestCase {
030: private static final Category LOG = Category
031: .getInstance(ReferenceValueMapTestCase.class);
032:
033: private static final int NUM_TEST_ENTRIES = 50;
034:
035: /**
036: * Used by the JUnit framework to construct tests. Normally, programmers
037: * would never explicitly use this constructor.
038: *
039: * @param name Name of the <tt>TestCase</tt>.
040: */
041:
042: public ReferenceValueMapTestCase(String name) {
043: super (name);
044: }
045:
046: protected abstract ReferenceValueMap newReferenceValueMap();
047:
048: public void testMemoryReclamation() {
049: ReferenceValueMap map = newReferenceValueMap();
050: Runtime rt = Runtime.getRuntime();
051:
052: rt.gc();
053:
054: int added = 0;
055: int size;
056: long freeMem;
057:
058: /*
059: * Fill the map with entries until some references get cleared. GC
060: * should cause references to be cleared well before memory fills up.
061: */
062: do {
063: freeMem = rt.freeMemory();
064: String key = "" + added;
065: Object value = new Integer(added++);
066:
067: map.put(key, value);
068:
069: size = map.size();
070: } while (size == added);
071:
072: assertTrue(size < added);
073:
074: LOG.info((added - size) + " entries out of " + added
075: + " cleared when free memory was "
076: + (int) (freeMem / 1024) + "KB");
077: }
078:
079: public void testBasicFunction() {
080: ReferenceValueMap map = newReferenceValueMap();
081: String[] keyArray = new String[NUM_TEST_ENTRIES];
082: Integer[] valueArray = new Integer[NUM_TEST_ENTRIES];
083:
084: for (int i = 0; i < keyArray.length; i++) {
085: keyArray[i] = "" + i;
086: valueArray[i] = new Integer(i);
087:
088: map.put(keyArray[i], valueArray[i]);
089: }
090:
091: checkMapContents(map, keyArray, valueArray);
092:
093: Map map2 = (Map) map.clone();
094: map.clear();
095:
096: assertEquals(0, map.size());
097:
098: map.putAll(map2);
099:
100: checkMapContents(map, keyArray, valueArray);
101:
102: assertEquals(NUM_TEST_ENTRIES, map.size());
103: }
104:
105: /**
106: * Tests Map.get(), Map.containsKey(), Map.containsValue(), Map.entrySet(),
107: * Map.keySet(), Map.values()
108: */
109:
110: protected void checkMapContents(Map map, String[] keyArray,
111: Integer[] valueArray) {
112: assertEquals(keyArray.length, map.size());
113:
114: for (int i = 0; i < keyArray.length; i++)
115: assertTrue(map.containsKey(keyArray[i]));
116:
117: assertTrue(!map.containsKey("bitbucket"));
118:
119: for (int i = 0; i < valueArray.length; i++)
120: assertTrue(map.containsValue(valueArray[i]));
121:
122: assertTrue(!map.containsValue("bitbucket"));
123:
124: for (int i = 0; i < keyArray.length; i++)
125: assertEquals(i, ((Integer) map.get(keyArray[i])).intValue());
126:
127: Set set = map.entrySet();
128: Iterator it = set.iterator();
129:
130: while (it.hasNext()) {
131: Map.Entry entry = (Map.Entry) it.next();
132: String s = (String) entry.getKey();
133: Integer i = (Integer) entry.getValue();
134: assertTrue(map.containsKey(s));
135: assertTrue(map.containsValue(i));
136: }
137:
138: assertTrue(map.keySet().containsAll(Arrays.asList(keyArray)));
139: assertTrue(map.values().containsAll(Arrays.asList(valueArray)));
140: }
141: }
|