001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2001, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.util;
018:
019: // J2SE dependencies
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Map;
023: import java.util.Random;
024:
025: // JUnit dependencies
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: /**
031: * Test the {@link WeakHashSet}. A standard {@link HashSet} object
032: * is used for comparaison purpose.
033: *
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/util/WeakValueHashMapTest.java $
035: * @version $Id: WeakValueHashMapTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
036: * @author Martin Desruisseaux
037: */
038: public final class WeakValueHashMapTest extends TestCase {
039: /**
040: * The size of the test sets to be created.
041: */
042: private static final int SAMPLE_SIZE = 500;
043:
044: /**
045: * Run the suit from the command line.
046: */
047: public static void main(final String[] args) {
048: org.geotools.util.logging.Logging.GEOTOOLS
049: .forceMonolineConsoleOutput();
050: junit.textui.TestRunner.run(suite());
051: }
052:
053: /**
054: * Returns the test suite.
055: */
056: public static Test suite() {
057: return new TestSuite(WeakValueHashMapTest.class);
058: }
059:
060: /**
061: * Constructs a test case with the given name.
062: */
063: public WeakValueHashMapTest(final String name) {
064: super (name);
065: }
066:
067: /**
068: * Tests the {@link WeakValueHashMap} using strong references.
069: * The tested {@link WeakValueHashMap} should behave like a
070: * standard {@link Map} object.
071: */
072: public void testStrongReferences() {
073: final Random random = new Random();
074: for (int pass = 0; pass < 4; pass++) {
075: final WeakValueHashMap weakMap = new WeakValueHashMap();
076: final HashMap strongMap = new HashMap();
077: for (int i = 0; i < SAMPLE_SIZE; i++) {
078: final Integer key = new Integer(random
079: .nextInt(SAMPLE_SIZE));
080: final Integer value = new Integer(random
081: .nextInt(SAMPLE_SIZE));
082: assertEquals("containsKey:",
083: strongMap.containsKey(key), weakMap
084: .containsKey(key));
085: if (false) {
086: // Can't test this one, since 'WeakValueHashMap.entrySet()' is not implemented.
087: assertEquals("containsValue:", strongMap
088: .containsValue(value), weakMap
089: .containsValue(value));
090: }
091: assertSame("get:", strongMap.get(key), weakMap.get(key));
092: if (random.nextBoolean()) {
093: // Test addition.
094: assertSame("put:", strongMap.put(key, value),
095: weakMap.put(key, value));
096: } else {
097: // Test remove
098: assertSame("remove:", strongMap.remove(key),
099: weakMap.remove(key));
100: }
101: assertEquals("equals:", strongMap, weakMap);
102: }
103: }
104: }
105:
106: /**
107: * Tests the {@link WeakValueHashMap} using weak references.
108: * In this test, we have to keep in mind than some elements
109: * in {@code weakMap} may disaspear at any time.
110: */
111: public void testWeakReferences() throws InterruptedException {
112: final Random random = new Random();
113: for (int pass = 0; pass < 2; pass++) {
114: final WeakValueHashMap weakMap = new WeakValueHashMap();
115: final HashMap strongMap = new HashMap();
116: for (int i = 0; i < SAMPLE_SIZE; i++) {
117: final Integer key = new Integer(random
118: .nextInt(SAMPLE_SIZE));
119: final Integer value = new Integer(random
120: .nextInt(SAMPLE_SIZE));
121: if (random.nextBoolean()) {
122: /*
123: * Test addition.
124: */
125: final Object weakPrevious = weakMap.put(key, value);
126: final Object strongPrevious = strongMap.put(key,
127: value);
128: if (weakPrevious == null) {
129: // If the element was not in the WeakValueHashMap (i.e. if the garbage
130: // collector has cleared it), then it must not been in HashMap neither
131: // (otherwise GC should not have cleared it).
132: assertNull("put:", strongPrevious);
133: } else {
134: assertNotSame(value, weakPrevious);
135: }
136: if (strongPrevious != null) {
137: // Note: If 'strongPrevious==null', 'weakPrevious' may not
138: // be null if GC has not collected its entry yet.
139: assertSame("put:", strongPrevious, weakPrevious);
140: }
141: } else {
142: /*
143: * Test remove
144: */
145: final Object weakPrevious = weakMap.get(key);
146: final Object strongPrevious = strongMap.remove(key);
147: if (strongPrevious != null) {
148: assertSame("remove:", strongPrevious,
149: weakPrevious);
150: }
151: }
152: if (false) {
153: // Can't test this one, since 'WeakValueHashMap.entrySet()' is not implemented.
154: assertTrue("containsAll:", weakMap.entrySet()
155: .containsAll(strongMap.entrySet()));
156: }
157: }
158: // Do our best to lets GC finish its work.
159: for (int i = 0; i < 4; i++) {
160: Thread.sleep(50);
161: System.gc();
162: }
163: assertTrue("equals:", strongMap.equals(weakMap));
164: }
165: }
166: }
|