001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software 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: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.common.collections;
023:
024: import java.lang.ref.Reference;
025: import java.lang.ref.ReferenceQueue;
026: import java.lang.ref.SoftReference;
027: import java.math.BigInteger;
028: import java.util.ArrayList;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.Random;
032:
033: import junit.framework.TestCase;
034: import org.jboss.util.collection.SoftSet;
035: import org.jboss.util.collection.SoftValueHashMap;
036:
037: /**
038: * Tests of the SoftReference based jboss common collection classes
039: *
040: * @author Scott.Stark@jboss.org
041: * @version $Revision: 57211 $
042: */
043: public class SoftReferenceUnitTest extends TestCase {
044: public SoftReferenceUnitTest(String name) {
045: super (name);
046: }
047:
048: /**
049: * Tests of the SoftSet
050: * @throws Exception
051: */
052: public void testSoftValueSet() throws Exception {
053: SoftSet set = new SoftSet();
054: StringBuffer akey = new StringBuffer("Key#");
055: for (int n = 0; n < 1000; n++) {
056: String key = "Key#" + n;
057: set.add(key);
058: assertTrue("set has key", set.contains(key));
059: akey.setLength(4);
060: akey.append("" + n);
061: String key2 = akey.toString();
062: assertEquals(key, key2);
063: assertEquals(key.hashCode(), key2.hashCode());
064: assertTrue("set has akey", set.contains(key2));
065: }
066: assertEquals("Size == 1000", 1000, set.size());
067: assertEquals("Set.isEmpty is false", false, set.isEmpty());
068: String[] keys = new String[1000];
069: set.toArray(keys);
070: for (int n = 0; n < 1000; n++) {
071: String key = keys[n];
072: assertTrue("set has key", set.contains(key));
073: }
074:
075: HashSet set2 = new HashSet();
076: set2.add("Key#1000");
077: assertFalse("set has not Key#1000", set.contains("Key#1000"));
078: assertTrue("Key#1000 was added", set.addAll(set2));
079: assertEquals("Size == 1001", 1001, set.size());
080: assertTrue("Key#1000 was removed", set.removeAll(set2));
081: assertEquals("Size == 1000", 1000, set.size());
082: set.add("Key#1000");
083: assertTrue("Key#1000 was removed", set.retainAll(set2));
084: assertEquals("Size == 1", 1, set.size());
085: assertTrue("set contains [Key#1000]", set.containsAll(set2));
086:
087: set.clear();
088: assertEquals("Size == 0", 0, set.size());
089: assertEquals("Size is empty", true, set.isEmpty());
090: for (int n = 0; n < 1000; n++) {
091: String key = keys[n];
092: set.add(key);
093: assertTrue("set has key", set.contains(key));
094: }
095:
096: for (int n = 0; n < 1000; n++) {
097: String key = "Key#" + n;
098: set.remove(key);
099: assertFalse("key was removed", set.contains(key));
100: }
101:
102: for (int n = 0; n < 1000; n++) {
103: String key = "Key#" + n;
104: set.add(key);
105: assertTrue("set has key", set.contains(key));
106: }
107: Iterator iter = set.iterator();
108: while (iter.hasNext()) {
109: Object o = iter.next();
110: assertTrue("o instanceof String", o instanceof String);
111: }
112:
113: forceSoftRefCollection();
114: assertEquals("Size == 0 after gc", 0, set.size());
115: }
116:
117: /**
118: * Tests of the SoftValueHashMap
119: * @throws Exception
120: */
121: public void testSoftValueHashMap() throws Exception {
122: SoftValueHashMap map = new SoftValueHashMap();
123: for (int n = 0; n < 1000; n++) {
124: String key = "Key#" + n;
125: String value = "Value#" + n;
126: map.put(key, value);
127: }
128: assertEquals("Size == 1000", 1000, map.size());
129: forceSoftRefCollection();
130: assertEquals("Size == 0 after gc", 0, map.size());
131: }
132:
133: private void forceSoftRefCollection() {
134: ReferenceQueue queue = new ReferenceQueue();
135: SoftReference reference = new SoftReference(new Object(), queue);
136:
137: ArrayList list = new ArrayList();
138: try {
139: Random rnd = new Random();
140: for (int i = 0; true; i++) {
141: BigInteger bi = new BigInteger(16384, rnd);
142: list.add(bi);
143: if (i % 1000 == 0) {
144: Reference ref;
145: if ((ref = queue.poll()) != null) {
146: System.out
147: .println("Break as the soft reference has been queued: "
148: + ref);
149: break;
150: }
151: }
152: }
153: } catch (Throwable e) {
154: }
155: }
156: }
|