1 22 23 package org.jboss.test.common.collections; 24 25 import java.lang.ref.Reference ; 26 import java.lang.ref.ReferenceQueue ; 27 import java.lang.ref.SoftReference ; 28 import java.math.BigInteger ; 29 import java.util.ArrayList ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Random ; 33 34 import junit.framework.TestCase; 35 import org.jboss.util.collection.SoftSet; 36 import org.jboss.util.collection.SoftValueHashMap; 37 38 44 public class SoftReferenceUnitTest extends TestCase 45 { 46 public SoftReferenceUnitTest(String name) 47 { 48 super(name); 49 } 50 51 55 public void testSoftValueSet() throws Exception 56 { 57 SoftSet set = new SoftSet(); 58 StringBuffer akey = new StringBuffer ("Key#"); 59 for(int n = 0; n < 1000; n ++) 60 { 61 String key = "Key#"+n; 62 set.add(key); 63 assertTrue("set has key", set.contains(key)); 64 akey.setLength(4); 65 akey.append(""+n); 66 String key2 = akey.toString(); 67 assertEquals(key, key2); 68 assertEquals(key.hashCode(), key2.hashCode()); 69 assertTrue("set has akey", set.contains(key2)); 70 } 71 assertEquals("Size == 1000", 1000, set.size()); 72 assertEquals("Set.isEmpty is false", false, set.isEmpty()); 73 String [] keys = new String [1000]; 74 set.toArray(keys); 75 for(int n = 0; n < 1000; n ++) 76 { 77 String key = keys[n]; 78 assertTrue("set has key", set.contains(key)); 79 } 80 81 HashSet set2 = new HashSet (); 82 set2.add("Key#1000"); 83 assertFalse("set has not Key#1000", set.contains("Key#1000")); 84 assertTrue("Key#1000 was added", set.addAll(set2)); 85 assertEquals("Size == 1001", 1001, set.size()); 86 assertTrue("Key#1000 was removed", set.removeAll(set2)); 87 assertEquals("Size == 1000", 1000, set.size()); 88 set.add("Key#1000"); 89 assertTrue("Key#1000 was removed", set.retainAll(set2)); 90 assertEquals("Size == 1", 1, set.size()); 91 assertTrue("set contains [Key#1000]", set.containsAll(set2)); 92 93 set.clear(); 94 assertEquals("Size == 0", 0, set.size()); 95 assertEquals("Size is empty", true, set.isEmpty()); 96 for(int n = 0; n < 1000; n ++) 97 { 98 String key = keys[n]; 99 set.add(key); 100 assertTrue("set has key", set.contains(key)); 101 } 102 103 for(int n = 0; n < 1000; n ++) 104 { 105 String key = "Key#"+n; 106 set.remove(key); 107 assertFalse("key was removed", set.contains(key)); 108 } 109 110 for(int n = 0; n < 1000; n ++) 111 { 112 String key = "Key#"+n; 113 set.add(key); 114 assertTrue("set has key", set.contains(key)); 115 } 116 Iterator iter = set.iterator(); 117 while( iter.hasNext() ) 118 { 119 Object o = iter.next(); 120 assertTrue("o instanceof String", o instanceof String ); 121 } 122 123 forceSoftRefCollection(); 124 assertEquals("Size == 0 after gc", 0, set.size()); 125 } 126 127 131 public void testSoftValueHashMap() throws Exception 132 { 133 SoftValueHashMap map = new SoftValueHashMap(); 134 for(int n = 0; n < 1000; n ++) 135 { 136 String key = "Key#"+n; 137 String value = "Value#"+n; 138 map.put(key, value); 139 } 140 assertEquals("Size == 1000", 1000, map.size()); 141 forceSoftRefCollection(); 142 assertEquals("Size == 0 after gc", 0, map.size()); 143 } 144 145 private void forceSoftRefCollection() 146 { 147 ReferenceQueue queue = new ReferenceQueue (); 148 SoftReference reference = new SoftReference (new Object (), queue); 149 150 ArrayList list = new ArrayList (); 151 try 152 { 153 Random rnd = new Random (); 154 for(int i = 0; true; i ++) 155 { 156 BigInteger bi = new BigInteger (16384, rnd); 157 list.add(bi); 158 if (i%1000==0) 159 { 160 Reference ref; 161 if ( (ref = queue.poll()) != null) 162 { 163 System.out.println("Break as the soft reference has been queued: "+ref); 164 break; 165 } 166 } 167 } 168 } 169 catch (Throwable e) 170 { 171 } 172 } 173 } 174 | Popular Tags |