1 21 22 package org.jacorb.test.notification.util; 23 24 import java.lang.ref.ReferenceQueue ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Arrays ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 import org.jacorb.notification.util.WeakHashSet; 36 37 41 public class WeakHashSetTest extends TestCase 42 { 43 private Set objectUnderTest_; 44 45 48 protected void setUp() throws Exception 49 { 50 super.setUp(); 51 52 objectUnderTest_ = new WeakHashSet(); 53 } 54 55 60 public WeakHashSetTest(String name) 61 { 62 super(name); 63 } 64 65 public void testSize() 66 { 67 assertEquals(0, objectUnderTest_.size()); 68 Object o1 = new Object (); 69 objectUnderTest_.add(o1); 70 assertEquals(1, objectUnderTest_.size()); 71 } 72 73 public void testIsEmpty() 74 { 75 assertTrue(objectUnderTest_.isEmpty()); 76 Object o1 = new Object (); 77 objectUnderTest_.add(o1); 78 assertFalse(objectUnderTest_.isEmpty()); 79 } 80 81 public void testContains() 82 { 83 Object o1 = new Object (); 84 assertFalse(objectUnderTest_.contains(o1)); 85 objectUnderTest_.add(o1); 86 assertTrue(objectUnderTest_.contains(o1)); 87 } 88 89 public void testIterator() 90 { 91 Iterator i = objectUnderTest_.iterator(); 92 assertFalse(i.hasNext()); 93 94 Object o1 = new Object (); 95 Object o2 = new Object (); 96 objectUnderTest_.add(o1); 97 objectUnderTest_.add(o2); 98 99 i = objectUnderTest_.iterator(); 100 assertTrue(i.hasNext()); 101 assertNotNull(i.next()); 102 assertNotNull(i.next()); 103 assertFalse(i.hasNext()); 104 } 105 106 109 public void testToArray() 110 { 111 Object [] array = objectUnderTest_.toArray(); 112 113 assertEquals(0, array.length); 114 115 Object o1 = new Object (); 116 objectUnderTest_.add(o1); 117 118 array = objectUnderTest_.toArray(); 119 120 assertEquals(1, array.length); 121 assertEquals(o1, array[0]); 122 } 123 124 127 public void testToArrayObjectArray() 128 { 129 String s1 = "text"; 130 131 objectUnderTest_.add(s1); 132 133 String [] array = (String []) objectUnderTest_.toArray(new String [0]); 134 assertEquals(1, array.length); 135 assertEquals("text", array[0]); 136 } 137 138 public void testAdd() 139 { 140 Object o1 = new Object (); 141 142 assertTrue(objectUnderTest_.add(o1)); 143 assertFalse(objectUnderTest_.add(o1)); 144 } 145 146 public void testRemove() 147 { 148 Object o1 = new Object (); 149 150 objectUnderTest_.add(o1); 151 assertTrue(objectUnderTest_.contains(o1)); 152 assertTrue(objectUnderTest_.remove(o1)); 153 assertFalse(objectUnderTest_.remove(o1)); 154 assertFalse(objectUnderTest_.contains(o1)); 155 } 156 157 public void testContainsAll() 158 { 159 Object o1 = new Object (); 160 Object o2 = new Object (); 161 162 List all = Arrays.asList(new Object [] { o1, o2 }); 163 164 assertFalse(objectUnderTest_.containsAll(all)); 165 166 objectUnderTest_.add(o1); 167 168 assertFalse(objectUnderTest_.containsAll(all)); 169 170 objectUnderTest_.add(o2); 171 172 assertTrue(objectUnderTest_.containsAll(all)); 173 } 174 175 public void testAddAll() 176 { 177 Object o1 = new Object (); 178 Object o2 = new Object (); 179 180 List all = Arrays.asList(new Object [] { o1, o2 }); 181 182 assertFalse(objectUnderTest_.contains(o1)); 183 assertFalse(objectUnderTest_.contains(o2)); 184 185 assertTrue(objectUnderTest_.addAll(all)); 186 assertFalse(objectUnderTest_.addAll(all)); 187 188 assertTrue(objectUnderTest_.contains(o1)); 189 assertTrue(objectUnderTest_.contains(o2)); 190 } 191 192 public void testRetainAll() 193 { 194 Object o1 = new Object (); 195 Object o2 = new Object (); 196 197 List all = Arrays.asList(new Object [] { o1 }); 198 199 objectUnderTest_.add(o1); 200 objectUnderTest_.add(o2); 201 202 assertTrue(objectUnderTest_.contains(o1)); 203 assertTrue(objectUnderTest_.contains(o2)); 204 205 assertTrue(objectUnderTest_.retainAll(all)); 206 assertFalse(objectUnderTest_.retainAll(all)); 207 208 assertTrue(objectUnderTest_.contains(o1)); 209 assertFalse(objectUnderTest_.contains(o2)); 210 } 211 212 public void testRemoveAll() 213 { 214 Object o1 = new Object (); 215 Object o2 = new Object (); 216 217 List all = Arrays.asList(new Object [] { o1 }); 218 219 objectUnderTest_.add(o1); 220 objectUnderTest_.add(o2); 221 222 assertTrue(objectUnderTest_.contains(o1)); 223 assertTrue(objectUnderTest_.contains(o2)); 224 225 assertTrue(objectUnderTest_.removeAll(all)); 226 assertFalse(objectUnderTest_.removeAll(all)); 227 228 assertFalse(objectUnderTest_.contains(o1)); 229 assertTrue(objectUnderTest_.contains(o2)); 230 } 231 232 public void testClear() 233 { 234 Object o1 = new Object (); 235 236 assertTrue(objectUnderTest_.isEmpty()); 237 238 objectUnderTest_.add(o1); 239 240 assertFalse(objectUnderTest_.isEmpty()); 241 242 objectUnderTest_.clear(); 243 244 assertTrue(objectUnderTest_.isEmpty()); 245 } 246 247 public void testIsReallyWeak() 248 { 249 long now = System.currentTimeMillis(); 250 251 Object o1 = new Object (); 252 ReferenceQueue queue = new ReferenceQueue (); 253 254 new WeakReference (o1, queue); 255 256 objectUnderTest_.add(o1); 257 258 o1 = null; 259 260 while (queue.poll() == null && System.currentTimeMillis() < now + 5000) 261 { 262 System.gc(); 263 } 264 265 assertTrue(objectUnderTest_.isEmpty()); 266 } 267 268 269 public static Test suite() 270 { 271 return new TestSuite(WeakHashSetTest.class); 272 } 273 } | Popular Tags |