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.HashSet;
021: import java.util.Random;
022: import java.util.Set;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: /**
029: * Test the {@link CanonicalSet}. A standard {@link HashSet} object
030: * is used for comparaison purpose.
031: *
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/util/CanonicalSetTest.java $
033: * @version $Id: CanonicalSetTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
034: * @author Martin Desruisseaux
035: */
036: public final class CanonicalSetTest extends TestCase {
037: /**
038: * Run the suit from the command line.
039: */
040: public static void main(final String[] args) {
041: org.geotools.util.logging.Logging.GEOTOOLS
042: .forceMonolineConsoleOutput();
043: junit.textui.TestRunner.run(suite());
044: }
045:
046: /**
047: * Returns the test suite.
048: */
049: public static Test suite() {
050: return new TestSuite(CanonicalSetTest.class);
051: }
052:
053: /**
054: * Constructs a test case with the given name.
055: */
056: public CanonicalSetTest(final String name) {
057: super (name);
058: }
059:
060: /**
061: * Test the {@link CanonicalSet} using strong references.
062: * The tested {@link CanonicalSet} should behave like a
063: * standard {@link Set} object.
064: */
065: public void testStrongReferences() {
066: final Random random = new Random();
067: for (int pass = 0; pass < 20; pass++) {
068: final CanonicalSet weakSet = new CanonicalSet();
069: final HashSet strongSet = new HashSet();
070: for (int i = 0; i < 1000; i++) {
071: final Integer value = new Integer(random.nextInt(500));
072: if (random.nextBoolean()) {
073: /*
074: * Test addition.
075: */
076: final boolean weakModified = weakSet.add(value);
077: final boolean strongModified = strongSet.add(value);
078: assertEquals("add:", strongModified, weakModified);
079: if (strongModified) {
080: assertSame("get:", value, weakSet.get(value));
081: } else {
082: assertEquals("get:", value, weakSet.get(value));
083: }
084: } else {
085: /*
086: * Test remove
087: */
088: final boolean weakModified = weakSet.remove(value);
089: final boolean strongModified = strongSet
090: .remove(value);
091: assertEquals("remove:", strongModified,
092: weakModified);
093: assertNull("get:", weakSet.get(value));
094: }
095: assertEquals("contains:", strongSet.contains(value),
096: weakSet.contains(value));
097: assertEquals("equals:", strongSet, weakSet);
098: }
099: }
100: }
101:
102: /**
103: * Test the {@link CanonicalSet} using weak references.
104: * In this test, we have to keep in mind than some elements
105: * in <code>weakSet</code> may disaspear at any time!
106: */
107: public void testWeakReferences() throws InterruptedException {
108: final Random random = new Random();
109: for (int pass = 0; pass < 2; pass++) {
110: final CanonicalSet weakSet = new CanonicalSet();
111: final HashSet strongSet = new HashSet();
112: for (int i = 0; i < 500; i++) {
113: final Integer value = new Integer(random.nextInt(500));
114: if (random.nextBoolean()) {
115: /*
116: * Test addition.
117: */
118: final boolean weakModified = weakSet.add(value);
119: final boolean strongModified = strongSet.add(value);
120: if (weakModified) {
121: // If the element was not in the CanonicalSet (i.e. if the garbage
122: // collector has cleared it), then it must not been in HashSet neither
123: // (otherwise GC should not have cleared it).
124: assertTrue("add:", strongModified);
125: } else {
126: assertTrue(value != weakSet.get(value));
127: if (strongModified) {
128: // If the element was already in the CanonicalSet but not in the
129: // HashSet, this is because GC has not cleared it yet. Replace it
130: // by 'value', because if we don't it may be cleared later and the
131: // "contains" test below will fails.
132: //
133: // Note: we don't test if 'remove' below returns 'true', because GC
134: // may have already done its work since the few previous lines!
135: weakSet.remove(value);
136: assertTrue(weakSet.add(value));
137: assertSame(value, weakSet.get(value));
138: }
139: }
140: } else {
141: /*
142: * Test remove
143: */
144: final boolean c = weakSet.contains(value);
145: if (strongSet.remove(value)) {
146: assertTrue("contains:", c);
147: }
148: }
149: assertTrue("containsAll:", weakSet
150: .containsAll(strongSet));
151: }
152: // Do our best to lets GC finish its work.
153: for (int i = 0; i < 4; i++) {
154: Thread.sleep(50);
155: System.gc();
156: }
157: assertEquals("equals:", strongSet, weakSet);
158: }
159: }
160: }
|