KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > util > test > ReferenceValueMapTestCase


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ReferenceValueMapTestCase.java,v 1.3 2002/11/08 05:06:28 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.util.test;
12
13 import com.triactive.jdo.util.ReferenceValueMap;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18 import junit.framework.TestCase;
19 import org.apache.log4j.Category;
20
21
22 /**
23  * Abstract base class for tests that test the functionality of a
24  * {@link ReferenceValueMap}.
25  *
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  * @version $Revision: 1.3 $
28  */

29
30 public abstract class ReferenceValueMapTestCase extends TestCase
31 {
32     private static final Category LOG = Category.getInstance(ReferenceValueMapTestCase.class);
33
34     private static final int NUM_TEST_ENTRIES = 50;
35
36
37     /**
38      * Used by the JUnit framework to construct tests. Normally, programmers
39      * would never explicitly use this constructor.
40      *
41      * @param name Name of the <tt>TestCase</tt>.
42      */

43
44     public ReferenceValueMapTestCase(String JavaDoc name)
45     {
46         super(name);
47     }
48
49
50     protected abstract ReferenceValueMap newReferenceValueMap();
51
52
53     public void testMemoryReclamation()
54     {
55         ReferenceValueMap map = newReferenceValueMap();
56         Runtime JavaDoc rt = Runtime.getRuntime();
57
58         rt.gc();
59
60         int added = 0;
61         int size;
62         long freeMem;
63
64         /*
65          * Fill the map with entries until some references get cleared. GC
66          * should cause references to be cleared well before memory fills up.
67          */

68         do
69         {
70             freeMem = rt.freeMemory();
71             String JavaDoc key = "" + added;
72             Object JavaDoc value = new Integer JavaDoc(added++);
73
74             map.put(key, value);
75
76             size = map.size();
77         } while (size == added);
78
79         assertTrue(size < added);
80
81         LOG.info((added - size) + " entries out of " + added + " cleared when free memory was " + (int)(freeMem / 1024) + "KB");
82     }
83
84
85     public void testBasicFunction()
86     {
87         ReferenceValueMap map = newReferenceValueMap();
88         String JavaDoc[] keyArray = new String JavaDoc[NUM_TEST_ENTRIES];
89         Integer JavaDoc[] valueArray = new Integer JavaDoc[NUM_TEST_ENTRIES];
90
91         for (int i = 0; i < keyArray.length; i++)
92         {
93             keyArray[i] = "" + i;
94             valueArray[i] = new Integer JavaDoc(i);
95
96             map.put(keyArray[i], valueArray[i]);
97         }
98
99         checkMapContents(map, keyArray, valueArray);
100
101         Map map2 = (Map)map.clone();
102         map.clear();
103
104         assertEquals(0, map.size());
105
106         map.putAll(map2);
107
108         checkMapContents(map, keyArray, valueArray);
109
110         assertEquals(NUM_TEST_ENTRIES, map.size());
111     }
112
113
114     /**
115      * Tests Map.get(), Map.containsKey(), Map.containsValue(), Map.entrySet(),
116      * Map.keySet(), Map.values()
117      */

118
119     protected void checkMapContents(Map map, String JavaDoc[] keyArray, Integer JavaDoc[] valueArray)
120     {
121         assertEquals(keyArray.length, map.size());
122
123         for (int i = 0; i < keyArray.length; i++)
124             assertTrue(map.containsKey(keyArray[i]));
125
126         assertTrue(!map.containsKey("bitbucket"));
127
128         for (int i = 0; i < valueArray.length; i++)
129             assertTrue(map.containsValue(valueArray[i]));
130
131         assertTrue(!map.containsValue("bitbucket"));
132
133         for (int i = 0; i < keyArray.length; i++)
134             assertEquals(i, ((Integer JavaDoc)map.get(keyArray[i])).intValue());
135
136         Set JavaDoc set = map.entrySet();
137         Iterator JavaDoc it = set.iterator();
138
139         while (it.hasNext())
140         {
141             Map.Entry entry = (Map.Entry)it.next();
142             String JavaDoc s = (String JavaDoc)entry.getKey();
143             Integer JavaDoc i = (Integer JavaDoc)entry.getValue();
144             assertTrue(map.containsKey(s));
145             assertTrue(map.containsValue(i));
146         }
147
148         assertTrue(map.keySet().containsAll(Arrays.asList(keyArray)));
149         assertTrue(map.values().containsAll(Arrays.asList(valueArray)));
150     }
151 }
152
Popular Tags