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