1 19 20 package org.openide.util; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.NoSuchElementException ; 31 import java.util.Set ; 32 import junit.textui.TestRunner; 33 import org.netbeans.junit.NbTestCase; 34 import org.netbeans.junit.NbTestSuite; 35 36 42 public class EnumerationsTest extends NbTestCase { 43 44 45 public EnumerationsTest(String testName) { 46 super(testName); 47 } 48 49 53 protected Enumeration singleton(Object obj) { 54 return Enumerations.singleton(obj); 55 } 56 protected Enumeration concat(Enumeration en1, Enumeration en2) { 57 return Enumerations.concat(en1, en2); 58 } 59 protected Enumeration concat(Enumeration enumOfEnums) { 60 return Enumerations.concat(enumOfEnums); 61 } 62 protected Enumeration removeDuplicates(Enumeration en) { 63 return Enumerations.removeDuplicates(en); 64 } 65 protected Enumeration empty() { 66 return Enumerations.empty(); 67 } 68 protected Enumeration array(Object [] arr) { 69 return Enumerations.array(arr); 70 } 71 protected Enumeration convert(Enumeration en, final Map map) { 72 class P implements Enumerations.Processor { 73 public Object process(Object obj, Collection nothing) { 74 return map.get(obj); 75 } 76 } 77 78 79 return Enumerations.convert(en, new P()); 80 } 81 protected Enumeration removeNulls(Enumeration en) { 82 return Enumerations.removeNulls(en); 83 } 84 protected Enumeration filter(Enumeration en, final Set filter) { 85 class P implements Enumerations.Processor { 86 public Object process(Object obj, Collection nothing) { 87 return filter.contains(obj) ? obj : null; 88 } 89 } 90 91 return Enumerations.filter(en, new P()); 92 } 93 94 protected Enumeration filter(Enumeration en, final QueueProcess filter) { 95 class P implements Enumerations.Processor { 96 public Object process(Object obj, Collection nothing) { 97 return filter.process(obj, nothing); 98 } 99 } 100 101 return Enumerations.filter(en, new P()); 102 } 103 104 108 protected Enumeration queue(Collection initContent, final QueueProcess process) { 109 class C implements Enumerations.Processor { 110 public Object process(Object object, Collection toAdd) { 111 return process.process(object, toAdd); 112 } 113 } 114 return Enumerations.queue( 115 Collections.enumeration(initContent), 116 new C() 117 ); 118 } 119 120 122 public static interface QueueProcess { 123 public Object process(Object object, Collection toAdd); 124 } 125 126 130 public void testEmptyIsEmpty() { 131 Enumeration e = empty(); 132 assertFalse(e.hasMoreElements()); 133 try { 134 e.nextElement(); 135 fail("No elements"); 136 } catch (NoSuchElementException ex) { 137 } 139 } 140 141 public void testSingleIsSingle() { 142 Enumeration e = singleton(this); 143 assertTrue(e.hasMoreElements()); 144 assertEquals("Returns me", this, e.nextElement()); 145 assertFalse("Now it is empty", e.hasMoreElements()); 146 try { 147 e.nextElement(); 148 fail("No elements"); 149 } catch (NoSuchElementException ex) { 150 } 152 } 153 154 public void testConcatTwoAndArray() { 155 Object [] one = { new Integer (1), new Integer (2), new Integer (3) }; 156 Object [] two = { "1", "2", "3" }; 157 158 ArrayList list = new ArrayList (Arrays.asList(one)); 159 list.addAll(Arrays.asList(two)); 160 161 assertEnums( 162 concat(array(one), array(two)), 163 Collections.enumeration(list) 164 ); 165 } 166 167 public void testConcatTwoAndArrayAndTakeOnlyStrings() { 168 Object [] one = { new Integer (1), new Integer (2), new Integer (3) }; 169 Object [] two = { "1", "2", "3" }; 170 Object [] three = { new Long (1) }; 171 Object [] four = { "Kuk" }; 172 173 ArrayList list = new ArrayList (Arrays.asList(two)); 174 list.addAll(Arrays.asList(four)); 175 176 Enumeration [] alls = { 177 array(one), array(two), array(three), array(four) 178 }; 179 180 assertEnums( 181 filter(concat(array(alls)), new OnlyStrings()), 182 Collections.enumeration(list) 183 ); 184 } 185 186 public void testRemoveDuplicates() { 187 Object [] one = { new Integer (1), new Integer (2), new Integer (3) }; 188 Object [] two = { "1", "2", "3" }; 189 Object [] three = { new Integer (1) }; 190 Object [] four = { "2", "3", "4" }; 191 192 Enumeration [] alls = { 193 array(one), array(two), array(three), array(four) 194 }; 195 196 assertEnums( 197 removeDuplicates(concat(array(alls))), 198 array(new Object [] { new Integer (1), new Integer (2), new Integer (3), "1", "2", "3", "4" }) 199 ); 200 201 } 202 203 public void testRemoveDuplicatesAndGCWorks() { 204 205 206 class WeakEnum implements Enumeration { 207 public Object i1 = new Integer (1); 208 public Object i2 = new Integer (1); 209 210 private int state; 211 212 public boolean hasMoreElements() { 213 return state < 5; 214 } 215 216 public Object nextElement() { 217 switch (state++) { 218 case 0: return i1; 219 case 1: case 2: case 3: return ""; 220 default: return i2; 221 } 222 } 223 } 224 225 WeakEnum weak = new WeakEnum(); 226 Enumeration en = removeDuplicates(weak); 227 228 assertTrue("Has some elements", en.hasMoreElements()); 229 assertEquals("And the first one is get", weak.i1, en.nextElement()); 230 231 try { 232 WeakReference ref = new WeakReference (weak.i1); 233 weak.i1 = null; 234 assertGC("Try hard to GC the first integer", ref); 235 } catch (Throwable tw) { 237 } 239 assertTrue("Next object will be string", en.hasMoreElements()); 240 assertEquals("is empty string", "", en.nextElement()); 241 242 assertFalse("The second integer is however equal to the original i1 and thus" + 243 " the enum should not be there", en.hasMoreElements()); 244 } 245 246 public void testQueueEnum() { 247 class Pr implements QueueProcess { 248 public Object process(Object o, Collection c) { 249 Integer i = (Integer )o; 250 int plus = i.intValue() + 1; 251 if (plus < 10) { 252 c.add(new Integer (plus)); 253 } 254 return i; 255 } 256 } 257 Pr p = new Pr(); 258 259 Enumeration en = queue( 260 Collections.nCopies(1, new Integer (0)), p 261 ); 262 263 for (int i = 0; i < 10; i++) { 264 assertTrue("has next", en.hasMoreElements()); 265 en.nextElement(); 266 } 267 268 assertFalse("No next element", en.hasMoreElements()); 269 } 270 271 public void testFilteringAlsoDoesConvertions() throws Exception { 272 class Pr implements QueueProcess { 273 public Object process(Object o, Collection ignore) { 274 Integer i = (Integer )o; 275 int plus = i.intValue() + 1; 276 return new Integer (plus); 277 } 278 } 279 Pr p = new Pr(); 280 281 Enumeration onetwo = array(new Object [] { new Integer (1), new Integer (2) }); 282 Enumeration twothree = array(new Object [] { new Integer (2), new Integer (3) }); 283 284 assertEnums( 285 filter(onetwo, p), twothree 286 ); 287 } 288 289 290 private static void assertEnums(Enumeration e1, Enumeration e2) { 291 int indx = 0; 292 while (e1.hasMoreElements() && e2.hasMoreElements()) { 293 Object i1 = e1.nextElement(); 294 Object i2 = e2.nextElement(); 295 assertEquals(indx++ + "th: ", i1, i2); 296 } 297 298 if (e1.hasMoreElements()) { 299 fail("first one contains another element: " + e1.nextElement()); 300 } 301 if (e2.hasMoreElements()) { 302 fail("second one contains another element: " + e2.nextElement()); 303 } 304 305 try { 306 e1.nextElement(); 307 fail("First one should throw exception, but nothing happend"); 308 } catch (NoSuchElementException ex) { 309 } 311 312 try { 313 e2.nextElement(); 314 fail("Second one should throw exception, but nothing happend"); 315 } catch (NoSuchElementException ex) { 316 } 318 } 319 320 public void testConvertIntegersToStringRemoveNulls() { 321 Object [] garbage = { new Integer (1), "kuk", "hle", new Integer (5) }; 322 323 assertEnums( 324 removeNulls(convert(array(garbage), new MapIntegers())), 325 array(new Object [] { "1", "5" }) 326 ); 327 } 328 329 public void testQueueEnumerationCanReturnNulls() { 330 Object [] nuls = { null, "NULL" }; 331 332 class P implements QueueProcess { 333 public Object process(Object toRet, Collection toAdd) { 334 if (toRet == null) return null; 335 336 if ("NULL".equals(toRet)) { 337 toAdd.add(null); 338 return null; 339 } 340 341 return null; 342 } 343 } 344 345 assertEnums( 346 array(new Object [] { null, null, null }), 347 queue(Arrays.asList(nuls), new P()) 348 ); 349 } 350 351 353 private static final class OnlyStrings implements Set { 354 public boolean add(Object o) { 355 fail("Should not be every called"); 356 return false; 357 } 358 359 public boolean addAll(Collection c) { 360 fail("Should not be every called"); 361 return false; 362 } 363 364 public void clear() { 365 fail("Should not be every called"); 366 } 367 368 public boolean contains(Object o) { 369 return o instanceof String ; 370 } 371 372 public boolean containsAll(Collection c) { 373 fail("Should not be every called"); 374 return false; 375 } 376 377 public boolean isEmpty() { 378 fail("Should not be every called"); 379 return false; 380 } 381 382 public Iterator iterator() { 383 fail("Should not be every called"); 384 return null; 385 } 386 387 public boolean remove(Object o) { 388 fail("Should not be every called"); 389 return false; 390 } 391 392 public boolean removeAll(Collection c) { 393 fail("Should not be every called"); 394 return false; 395 } 396 397 public boolean retainAll(Collection c) { 398 fail("Should not be every called"); 399 return false; 400 } 401 402 public int size() { 403 fail("Should not be every called"); 404 return 1; 405 } 406 407 public Object [] toArray() { 408 fail("Should not be every called"); 409 return null; 410 } 411 412 public Object [] toArray(Object [] a) { 413 fail("Should not be every called"); 414 return null; 415 } 416 } 417 418 420 private static final class MapIntegers implements Map { 421 public boolean containsKey(Object key) { 422 fail("Should not be every called"); 423 return false; 424 } 425 426 public boolean containsValue(Object value) { 427 fail("Should not be every called"); 428 return false; 429 } 430 431 public Set entrySet() { 432 fail("Should not be every called"); 433 return null; 434 } 435 436 public Object get(Object key) { 437 if (key instanceof Integer ) { 438 return key.toString(); 439 } 440 return null; 441 } 442 443 public Set keySet() { 444 fail("Should not be every called"); 445 return null; 446 } 447 448 public Object put(Object key, Object value) { 449 fail("Should not be every called"); 450 return null; 451 } 452 453 public void putAll(Map t) { 454 fail("Should not be every called"); 455 } 456 457 public Collection values() { 458 fail("Should not be every called"); 459 return null; 460 } 461 462 public void clear() { 463 fail("Should not be every called"); 464 } 465 466 public boolean isEmpty() { 467 fail("Should not be every called"); 468 return false; 469 } 470 471 public Object remove(Object key) { 472 fail("Should not be every called"); 473 return null; 474 } 475 476 public int size() { 477 fail("Should not be every called"); 478 return 1; 479 } 480 481 } 482 } 483 | Popular Tags |