1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList; 19 import java.util.Collection; 20 import java.util.Enumeration; 21 import java.util.HashMap; 22 import java.util.HashSet; 23 import java.util.Iterator; 24 import java.util.LinkedList; 25 import java.util.List; 26 import java.util.Map; 27 import java.util.Set; 28 import java.util.SortedMap; 29 import java.util.TreeMap; 30 import java.util.Vector; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 36 import org.apache.commons.collections.bag.HashBag; 37 import org.apache.commons.collections.buffer.BoundedFifoBuffer; 38 import org.apache.commons.collections.collection.AbstractTestCollection; 39 import org.apache.commons.collections.collection.PredicatedCollection; 40 import org.apache.commons.collections.collection.SynchronizedCollection; 41 import org.apache.commons.collections.collection.TransformedCollection; 42 import org.apache.commons.collections.collection.UnmodifiableCollection; 43 44 55 public class TestCollectionUtils extends TestCase { 56 57 public TestCollectionUtils(String testName) { 58 super(testName); 59 } 60 61 public static Test suite() { 62 return new TestSuite(TestCollectionUtils.class); 63 } 64 65 public static void main(String args[]) { 66 String[] testCaseName = { TestCollectionUtils.class.getName() }; 67 junit.textui.TestRunner.main(testCaseName); 68 } 69 70 private Collection collectionA = null; 71 private Collection collectionB = null; 72 73 public void setUp() { 74 collectionA = new ArrayList(); 75 collectionA.add("a"); 76 collectionA.add("b"); 77 collectionA.add("b"); 78 collectionA.add("c"); 79 collectionA.add("c"); 80 collectionA.add("c"); 81 collectionA.add("d"); 82 collectionA.add("d"); 83 collectionA.add("d"); 84 collectionA.add("d"); 85 collectionB = new LinkedList(); 86 collectionB.add("e"); 87 collectionB.add("d"); 88 collectionB.add("d"); 89 collectionB.add("c"); 90 collectionB.add("c"); 91 collectionB.add("c"); 92 collectionB.add("b"); 93 collectionB.add("b"); 94 collectionB.add("b"); 95 collectionB.add("b"); 96 97 } 98 99 public void testGetCardinalityMap() { 100 Map freq = CollectionUtils.getCardinalityMap(collectionA); 101 assertEquals(new Integer(1),freq.get("a")); 102 assertEquals(new Integer(2),freq.get("b")); 103 assertEquals(new Integer(3),freq.get("c")); 104 assertEquals(new Integer(4),freq.get("d")); 105 assertNull(freq.get("e")); 106 107 freq = CollectionUtils.getCardinalityMap(collectionB); 108 assertNull(freq.get("a")); 109 assertEquals(new Integer(4),freq.get("b")); 110 assertEquals(new Integer(3),freq.get("c")); 111 assertEquals(new Integer(2),freq.get("d")); 112 assertEquals(new Integer(1),freq.get("e")); 113 } 114 115 public void testCardinality() { 116 assertEquals(1, CollectionUtils.cardinality("a", collectionA)); 117 assertEquals(2, CollectionUtils.cardinality("b", collectionA)); 118 assertEquals(3, CollectionUtils.cardinality("c", collectionA)); 119 assertEquals(4, CollectionUtils.cardinality("d", collectionA)); 120 assertEquals(0, CollectionUtils.cardinality("e", collectionA)); 121 122 assertEquals(0, CollectionUtils.cardinality("a", collectionB)); 123 assertEquals(4, CollectionUtils.cardinality("b", collectionB)); 124 assertEquals(3, CollectionUtils.cardinality("c", collectionB)); 125 assertEquals(2, CollectionUtils.cardinality("d", collectionB)); 126 assertEquals(1, CollectionUtils.cardinality("e", collectionB)); 127 128 Set set = new HashSet(); 129 set.add("A"); 130 set.add("C"); 131 set.add("E"); 132 set.add("E"); 133 assertEquals(1, CollectionUtils.cardinality("A", set)); 134 assertEquals(0, CollectionUtils.cardinality("B", set)); 135 assertEquals(1, CollectionUtils.cardinality("C", set)); 136 assertEquals(0, CollectionUtils.cardinality("D", set)); 137 assertEquals(1, CollectionUtils.cardinality("E", set)); 138 139 Bag bag = new HashBag(); 140 bag.add("A", 3); 141 bag.add("C"); 142 bag.add("E"); 143 bag.add("E"); 144 assertEquals(3, CollectionUtils.cardinality("A", bag)); 145 assertEquals(0, CollectionUtils.cardinality("B", bag)); 146 assertEquals(1, CollectionUtils.cardinality("C", bag)); 147 assertEquals(0, CollectionUtils.cardinality("D", bag)); 148 assertEquals(2, CollectionUtils.cardinality("E", bag)); 149 } 150 151 public void testCardinalityOfNull() { 152 List list = new ArrayList(); 153 assertEquals(0,CollectionUtils.cardinality(null,list)); 154 { 155 Map freq = CollectionUtils.getCardinalityMap(list); 156 assertNull(freq.get(null)); 157 } 158 list.add("A"); 159 assertEquals(0,CollectionUtils.cardinality(null,list)); 160 { 161 Map freq = CollectionUtils.getCardinalityMap(list); 162 assertNull(freq.get(null)); 163 } 164 list.add(null); 165 assertEquals(1,CollectionUtils.cardinality(null,list)); 166 { 167 Map freq = CollectionUtils.getCardinalityMap(list); 168 assertEquals(new Integer(1),freq.get(null)); 169 } 170 list.add("B"); 171 assertEquals(1,CollectionUtils.cardinality(null,list)); 172 { 173 Map freq = CollectionUtils.getCardinalityMap(list); 174 assertEquals(new Integer(1),freq.get(null)); 175 } 176 list.add(null); 177 assertEquals(2,CollectionUtils.cardinality(null,list)); 178 { 179 Map freq = CollectionUtils.getCardinalityMap(list); 180 assertEquals(new Integer(2),freq.get(null)); 181 } 182 list.add("B"); 183 assertEquals(2,CollectionUtils.cardinality(null,list)); 184 { 185 Map freq = CollectionUtils.getCardinalityMap(list); 186 assertEquals(new Integer(2),freq.get(null)); 187 } 188 list.add(null); 189 assertEquals(3,CollectionUtils.cardinality(null,list)); 190 { 191 Map freq = CollectionUtils.getCardinalityMap(list); 192 assertEquals(new Integer(3),freq.get(null)); 193 } 194 } 195 196 public void testContainsAny() { 197 Collection empty = new ArrayList(0); 198 Collection one = new ArrayList(1); 199 one.add("1"); 200 Collection two = new ArrayList(1); 201 two.add("2"); 202 Collection three = new ArrayList(1); 203 three.add("3"); 204 Collection odds = new ArrayList(2); 205 odds.add("1"); 206 odds.add("3"); 207 208 assertTrue("containsAny({1},{1,3}) should return true.", 209 CollectionUtils.containsAny(one,odds)); 210 assertTrue("containsAny({1,3},{1}) should return true.", 211 CollectionUtils.containsAny(odds,one)); 212 assertTrue("containsAny({3},{1,3}) should return true.", 213 CollectionUtils.containsAny(three,odds)); 214 assertTrue("containsAny({1,3},{3}) should return true.", 215 CollectionUtils.containsAny(odds,three)); 216 assertTrue("containsAny({2},{2}) should return true.", 217 CollectionUtils.containsAny(two,two)); 218 assertTrue("containsAny({1,3},{1,3}) should return true.", 219 CollectionUtils.containsAny(odds,odds)); 220 221 assertTrue("containsAny({2},{1,3}) should return false.", 222 !CollectionUtils.containsAny(two,odds)); 223 assertTrue("containsAny({1,3},{2}) should return false.", 224 !CollectionUtils.containsAny(odds,two)); 225 assertTrue("containsAny({1},{3}) should return false.", 226 !CollectionUtils.containsAny(one,three)); 227 assertTrue("containsAny({3},{1}) should return false.", 228 !CollectionUtils.containsAny(three,one)); 229 assertTrue("containsAny({1,3},{}) should return false.", 230 !CollectionUtils.containsAny(odds,empty)); 231 assertTrue("containsAny({},{1,3}) should return false.", 232 !CollectionUtils.containsAny(empty,odds)); 233 assertTrue("containsAny({},{}) should return false.", 234 !CollectionUtils.containsAny(empty,empty)); 235 } 236 237 public void testUnion() { 238 Collection col = CollectionUtils.union(collectionA,collectionB); 239 Map freq = CollectionUtils.getCardinalityMap(col); 240 assertEquals(new Integer(1),freq.get("a")); 241 assertEquals(new Integer(4),freq.get("b")); 242 assertEquals(new Integer(3),freq.get("c")); 243 assertEquals(new Integer(4),freq.get("d")); 244 assertEquals(new Integer(1),freq.get("e")); 245 246 Collection col2 = CollectionUtils.union(collectionB,collectionA); 247 Map freq2 = CollectionUtils.getCardinalityMap(col2); 248 assertEquals(new Integer(1),freq2.get("a")); 249 assertEquals(new Integer(4),freq2.get("b")); 250 assertEquals(new Integer(3),freq2.get("c")); 251 assertEquals(new Integer(4),freq2.get("d")); 252 assertEquals(new Integer(1),freq2.get("e")); 253 } 254 255 public void testIntersection() { 256 Collection col = CollectionUtils.intersection(collectionA,collectionB); 257 Map freq = CollectionUtils.getCardinalityMap(col); 258 assertNull(freq.get("a")); 259 assertEquals(new Integer(2),freq.get("b")); 260 assertEquals(new Integer(3),freq.get("c")); 261 assertEquals(new Integer(2),freq.get("d")); 262 assertNull(freq.get("e")); 263 264 Collection col2 = CollectionUtils.intersection(collectionB,collectionA); 265 Map freq2 = CollectionUtils.getCardinalityMap(col2); 266 assertNull(freq2.get("a")); 267 assertEquals(new Integer(2),freq2.get("b")); 268 assertEquals(new Integer(3),freq2.get("c")); 269 assertEquals(new Integer(2),freq2.get("d")); 270 assertNull(freq2.get("e")); 271 } 272 273 public void testDisjunction() { 274 Collection col = CollectionUtils.disjunction(collectionA,collectionB); 275 Map freq = CollectionUtils.getCardinalityMap(col); 276 assertEquals(new Integer(1),freq.get("a")); 277 assertEquals(new Integer(2),freq.get("b")); 278 assertNull(freq.get("c")); 279 assertEquals(new Integer(2),freq.get("d")); 280 assertEquals(new Integer(1),freq.get("e")); 281 282 Collection col2 = CollectionUtils.disjunction(collectionB,collectionA); 283 Map freq2 = CollectionUtils.getCardinalityMap(col2); 284 assertEquals(new Integer(1),freq2.get("a")); 285 assertEquals(new Integer(2),freq2.get("b")); 286 assertNull(freq2.get("c")); 287 assertEquals(new Integer(2),freq2.get("d")); 288 assertEquals(new Integer(1),freq2.get("e")); 289 } 290 291 public void testDisjunctionAsUnionMinusIntersection() { 292 Collection dis = CollectionUtils.disjunction(collectionA,collectionB); 293 Collection un = CollectionUtils.union(collectionA,collectionB); 294 Collection inter = CollectionUtils.intersection(collectionA,collectionB); 295 assertTrue(CollectionUtils.isEqualCollection(dis,CollectionUtils.subtract(un,inter))); 296 } 297 298 public void testDisjunctionAsSymmetricDifference() { 299 Collection dis = CollectionUtils.disjunction(collectionA,collectionB); 300 Collection amb = CollectionUtils.subtract(collectionA,collectionB); 301 Collection bma = CollectionUtils.subtract(collectionB,collectionA); 302 assertTrue(CollectionUtils.isEqualCollection(dis,CollectionUtils.union(amb,bma))); 303 } 304 305 public void testSubtract() { 306 Collection col = CollectionUtils.subtract(collectionA,collectionB); 307 Map freq = CollectionUtils.getCardinalityMap(col); 308 assertEquals(new Integer(1),freq.get("a")); 309 assertNull(freq.get("b")); 310 assertNull(freq.get("c")); 311 assertEquals(new Integer(2),freq.get("d")); 312 assertNull(freq.get("e")); 313 314 Collection col2 = CollectionUtils.subtract(collectionB,collectionA); 315 Map freq2 = CollectionUtils.getCardinalityMap(col2); 316 assertEquals(new Integer(1),freq2.get("e")); 317 assertNull(freq2.get("d")); 318 assertNull(freq2.get("c")); 319 assertEquals(new Integer(2),freq2.get("b")); 320 assertNull(freq2.get("a")); 321 } 322 323 public void testIsSubCollectionOfSelf() { 324 assertTrue(CollectionUtils.isSubCollection(collectionA,collectionA)); 325 assertTrue(CollectionUtils.isSubCollection(collectionB,collectionB)); 326 } 327 328 public void testIsSubCollection() { 329 assertTrue(!CollectionUtils.isSubCollection(collectionA,collectionB)); 330 assertTrue(!CollectionUtils.isSubCollection(collectionB,collectionA)); 331 } 332 333 public void testIsSubCollection2() { 334 Collection c = new ArrayList(); 335 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 336 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 337 c.add("a"); 338 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 339 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 340 c.add("b"); 341 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 342 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 343 c.add("b"); 344 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 345 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 346 c.add("c"); 347 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 348 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 349 c.add("c"); 350 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 351 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 352 c.add("c"); 353 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 354 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 355 c.add("d"); 356 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 357 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 358 c.add("d"); 359 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 360 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 361 c.add("d"); 362 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 363 assertTrue(!CollectionUtils.isSubCollection(collectionA,c)); 364 c.add("d"); 365 assertTrue(CollectionUtils.isSubCollection(c,collectionA)); 366 assertTrue(CollectionUtils.isSubCollection(collectionA,c)); 367 c.add("e"); 368 assertTrue(!CollectionUtils.isSubCollection(c,collectionA)); 369 assertTrue(CollectionUtils.isSubCollection(collectionA,c)); 370 } 371 372 public void testIsEqualCollectionToSelf() { 373 assertTrue(CollectionUtils.isEqualCollection(collectionA,collectionA)); 374 assertTrue(CollectionUtils.isEqualCollection(collectionB,collectionB)); 375 } 376 377 public void testIsEqualCollection() { 378 assertTrue(!CollectionUtils.isEqualCollection(collectionA,collectionB)); 379 assertTrue(!CollectionUtils.isEqualCollection(collectionB,collectionA)); 380 } 381 382 public void testIsEqualCollection2() { 383 Collection a = new ArrayList(); 384 Collection b = new ArrayList(); 385 assertTrue(CollectionUtils.isEqualCollection(a,b)); 386 assertTrue(CollectionUtils.isEqualCollection(b,a)); 387 a.add("1"); 388 assertTrue(!CollectionUtils.isEqualCollection(a,b)); 389 assertTrue(!CollectionUtils.isEqualCollection(b,a)); 390 b.add("1"); 391 assertTrue(CollectionUtils.isEqualCollection(a,b)); 392 assertTrue(CollectionUtils.isEqualCollection(b,a)); 393 a.add("2"); 394 assertTrue(!CollectionUtils.isEqualCollection(a,b)); 395 assertTrue(!CollectionUtils.isEqualCollection(b,a)); 396 b.add("2"); 397 assertTrue(CollectionUtils.isEqualCollection(a,b)); 398 assertTrue(CollectionUtils.isEqualCollection(b,a)); 399 a.add("1"); 400 assertTrue(!CollectionUtils.isEqualCollection(a,b)); 401 assertTrue(!CollectionUtils.isEqualCollection(b,a)); 402 b.add("1"); 403 assertTrue(CollectionUtils.isEqualCollection(a,b)); 404 assertTrue(CollectionUtils.isEqualCollection(b,a)); 405 } 406 407 public void testIsProperSubCollection() { 408 Collection a = new ArrayList(); 409 Collection b = new ArrayList(); 410 assertTrue(!CollectionUtils.isProperSubCollection(a,b)); 411 b.add("1"); 412 assertTrue(CollectionUtils.isProperSubCollection(a,b)); 413 assertTrue(!CollectionUtils.isProperSubCollection(b,a)); 414 assertTrue(!CollectionUtils.isProperSubCollection(b,b)); 415 assertTrue(!CollectionUtils.isProperSubCollection(a,a)); 416 a.add("1"); 417 a.add("2"); 418 b.add("2"); 419 assertTrue(!CollectionUtils.isProperSubCollection(b,a)); 420 assertTrue(!CollectionUtils.isProperSubCollection(a,b)); 421 a.add("1"); 422 assertTrue(CollectionUtils.isProperSubCollection(b,a)); 423 assertTrue(CollectionUtils.isProperSubCollection( 424 CollectionUtils.intersection(collectionA, collectionB), collectionA)); 425 assertTrue(CollectionUtils.isProperSubCollection( 426 CollectionUtils.subtract(a, b), a)); 427 assertTrue(!CollectionUtils.isProperSubCollection( 428 a, CollectionUtils.subtract(a, b))); 429 } 430 431 public void testFind() { 432 Predicate testPredicate = PredicateUtils.equalPredicate("d"); 433 Object test = CollectionUtils.find(collectionA, testPredicate); 434 assertTrue(test.equals("d")); 435 testPredicate = PredicateUtils.equalPredicate("de"); 436 test = CollectionUtils.find(collectionA, testPredicate); 437 assertTrue(test == null); 438 assertEquals(CollectionUtils.find(null,testPredicate), null); 439 assertEquals(CollectionUtils.find(collectionA, null), null); 440 } 441 442 public void testForAllDo() { 443 Closure testClosure = ClosureUtils.invokerClosure("clear"); 444 Collection col = new ArrayList(); 445 col.add(collectionA); 446 col.add(collectionB); 447 CollectionUtils.forAllDo(col, testClosure); 448 assertTrue(collectionA.isEmpty() && collectionB.isEmpty()); 449 CollectionUtils.forAllDo(col, null); 450 assertTrue(collectionA.isEmpty() && collectionB.isEmpty()); 451 CollectionUtils.forAllDo(null, testClosure); 452 col.add(null); 453 CollectionUtils.forAllDo(col, testClosure); 455 col.add("x"); 456 try { 458 CollectionUtils.forAllDo(col, testClosure); 459 fail("Expecting FunctorException"); 460 } catch (FunctorException ex) { 461 } 463 } 464 465 public void testIndex() { 466 Map map = new HashMap(); 468 map.put(new Integer(0), "zero"); 469 map.put(new Integer(-1), "minusOne"); 470 Object test = CollectionUtils.index(map, 0); 471 assertTrue(test.equals("zero")); 472 test = CollectionUtils.index(map, new Integer(-1)); 473 assertTrue(test.equals("minusOne")); 474 475 test = CollectionUtils.index(map, "missing"); 477 assertTrue(test.equals(map)); 478 479 test = CollectionUtils.index(map, new Integer(1)); 481 assertTrue(map.keySet().contains(test)); 482 483 test = CollectionUtils.index(map, new Integer(4)); 485 assertTrue((test instanceof Iterator) && !((Iterator) test).hasNext()); 486 487 SortedMap map2 = new TreeMap(); 489 map2.put(new Integer(23), "u"); 490 map2.put(new Integer(21), "x"); 491 map2.put(new Integer(17), "v"); 492 map2.put(new Integer(42), "w"); 493 Integer val = (Integer) CollectionUtils.index(map2, 0); 494 assertTrue(val.intValue() == 17); 495 val = (Integer) CollectionUtils.index(map2, 1); 496 assertTrue(val.intValue() == 21); 497 val = (Integer) CollectionUtils.index(map2, 2); 498 assertTrue(val.intValue() == 23); 499 val = (Integer) CollectionUtils.index(map2, 3); 500 assertTrue(val.intValue() == 42); 501 502 List list = new ArrayList(); 504 list.add("zero"); 505 list.add("one"); 506 test = CollectionUtils.index(list, 0); 507 assertTrue(test.equals("zero")); 508 test = CollectionUtils.index(list, 1); 509 assertTrue(test.equals("one")); 510 511 try { 513 test = CollectionUtils.index(list, 2); 514 fail("Expecting IndexOutOfBoundsException"); 515 } catch (IndexOutOfBoundsException e) { 516 } 518 519 Iterator iterator = list.iterator(); 521 test = CollectionUtils.index(iterator,0); 522 assertTrue(test.equals("zero")); 523 iterator = list.iterator(); 524 test = CollectionUtils.index(iterator,1); 525 assertTrue(test.equals("one")); 526 527 test = CollectionUtils.index(iterator,3); 529 assertTrue(test.equals(iterator) && !iterator.hasNext()); 530 531 Vector vector = new Vector(list); 533 Enumeration enum = vector.elements(); 534 test = CollectionUtils.index(enum,0); 535 assertTrue(test.equals("zero")); 536 enum = vector.elements(); 537 test = CollectionUtils.index(enum,1); 538 assertTrue(test.equals("one")); 539 540 test = CollectionUtils.index(enum,3); 542 assertTrue(test.equals(enum) && !enum.hasMoreElements()); 543 544 Bag bag = new HashBag(); 546 bag.add("element", 1); 547 test = CollectionUtils.index(bag, 0); 548 assertTrue(test.equals("element")); 549 550 test = CollectionUtils.index(bag, 2); 552 assertTrue((test instanceof Iterator) && !((Iterator) test).hasNext()); 553 554 Object[] objArray = new Object[2]; 556 objArray[0] = "zero"; 557 objArray[1] = "one"; 558 test = CollectionUtils.index(objArray,0); 559 assertTrue(test.equals("zero")); 560 test = CollectionUtils.index(objArray,1); 561 assertTrue(test.equals("one")); 562 563 try { 565 test = CollectionUtils.index(objArray,2); 566 fail("Expecting ArrayIndexOutOfBoundsException."); 567 } catch (ArrayIndexOutOfBoundsException ex) { 568 } 570 571 Object obj = new Object(); 573 test = CollectionUtils.index(obj, obj); 574 assertTrue(test.equals(obj)); 575 } 576 577 public void testGet() { 578 { 579 Map expected = new HashMap(); 581 expected.put("zeroKey", "zero"); 582 expected.put("oneKey", "one"); 583 584 Map found = new HashMap(); 585 Map.Entry entry = (Map.Entry)(CollectionUtils.get(expected, 0)); 586 found.put(entry.getKey(),entry.getValue()); 587 entry = (Map.Entry)(CollectionUtils.get(expected, 1)); 588 found.put(entry.getKey(),entry.getValue()); 589 assertEquals(expected,found); 590 591 try { 593 CollectionUtils.get(expected, 2); 594 fail("Expecting IndexOutOfBoundsException."); 595 } catch (IndexOutOfBoundsException e) { 596 } 598 try { 599 CollectionUtils.get(expected, -2); 600 fail("Expecting IndexOutOfBoundsException."); 601 } catch (IndexOutOfBoundsException e) { 602 } 604 } 605 606 { 607 SortedMap map = new TreeMap(); 609 map.put("zeroKey", "zero"); 610 map.put("oneKey", "one"); 611 Object test = CollectionUtils.get(map, 1); 612 assertEquals("zeroKey",((Map.Entry) test).getKey()); 613 assertEquals("zero",((Map.Entry) test).getValue()); 614 test = CollectionUtils.get(map, 0); 615 assertEquals("oneKey",((Map.Entry) test).getKey()); 616 assertEquals("one",((Map.Entry) test).getValue()); 617 } 618 619 { 620 List list = new ArrayList(); 622 list.add("zero"); 623 list.add("one"); 624 assertEquals("zero",CollectionUtils.get(list, 0)); 625 assertEquals("one",CollectionUtils.get(list, 1)); 626 try { 628 CollectionUtils.get(list, 2); 629 fail("Expecting IndexOutOfBoundsException"); 630 } catch (IndexOutOfBoundsException e) { 631 } 633 634 Iterator iterator = list.iterator(); 636 assertEquals("zero",CollectionUtils.get(iterator,0)); 637 iterator = list.iterator(); 638 assertEquals("one",CollectionUtils.get(iterator,1)); 639 640 try { 642 CollectionUtils.get(iterator,3); 643 fail("Expecting IndexOutOfBoundsException."); 644 } catch (IndexOutOfBoundsException e) { 645 } 647 assertTrue(!iterator.hasNext()); 648 } 649 650 { 651 Vector vector = new Vector(); 653 vector.addElement("zero"); 654 vector.addElement("one"); 655 Enumeration enum = vector.elements(); 656 assertEquals("zero",CollectionUtils.get(enum,0)); 657 enum = vector.elements(); 658 assertEquals("one",CollectionUtils.get(enum,1)); 659 660 try { 662 CollectionUtils.get(enum,3); 663 fail("Expecting IndexOutOfBoundsException."); 664 } catch (IndexOutOfBoundsException e) { 665 } 667 assertTrue(!enum.hasMoreElements()); 668 } 669 670 { 671 Bag bag = new HashBag(); 673 bag.add("element", 1); 674 assertEquals("element",CollectionUtils.get(bag, 0)); 675 676 try { 678 CollectionUtils.get(bag, 1); 679 fail("Expceting IndexOutOfBoundsException."); 680 } catch (IndexOutOfBoundsException e) { 681 } 683 } 684 685 { 686 Object[] objArray = new Object[2]; 688 objArray[0] = "zero"; 689 objArray[1] = "one"; 690 assertEquals("zero",CollectionUtils.get(objArray,0)); 691 assertEquals("one",CollectionUtils.get(objArray,1)); 692 693 try { 695 CollectionUtils.get(objArray,2); 696 fail("Expecting IndexOutOfBoundsException."); 697 } catch (IndexOutOfBoundsException ex) { 698 } 700 } 701 702 { 703 int[] array = new int[2]; 705 array[0] = 10; 706 array[1] = 20; 707 assertEquals(new Integer(10), CollectionUtils.get(array,0)); 708 assertEquals(new Integer(20), CollectionUtils.get(array,1)); 709 710 try { 712 CollectionUtils.get(array,2); 713 fail("Expecting IndexOutOfBoundsException."); 714 } catch (IndexOutOfBoundsException ex) { 715 } 717 } 718 719 { 720 Object obj = new Object(); 722 try { 723 CollectionUtils.get(obj, 0); 724 fail("Expecting IllegalArgumentException."); 725 } catch (IllegalArgumentException e) { 726 } 728 try { 729 CollectionUtils.get(null, 0); 730 fail("Expecting IllegalArgumentException."); 731 } catch (IllegalArgumentException e) { 732 } 734 } 735 } 736 737 public void testSize_List() { 738 List list = new ArrayList(); 739 assertEquals(0, CollectionUtils.size(list)); 740 list.add("a"); 741 assertEquals(1, CollectionUtils.size(list)); 742 list.add("b"); 743 assertEquals(2, CollectionUtils.size(list)); 744 } 745 public void testSize_Map() { 746 Map map = new HashMap(); 747 assertEquals(0, CollectionUtils.size(map)); 748 map.put("1", "a"); 749 assertEquals(1, CollectionUtils.size(map)); 750 map.put("2", "b"); 751 assertEquals(2, CollectionUtils.size(map)); 752 } 753 public void testSize_Array() { 754 Object[] objectArray = new Object[0]; 755 assertEquals(0, CollectionUtils.size(objectArray)); 756 757 String[] stringArray = new String[3]; 758 assertEquals(3, CollectionUtils.size(stringArray)); 759 stringArray[0] = "a"; 760 stringArray[1] = "b"; 761 stringArray[2] = "c"; 762 assertEquals(3, CollectionUtils.size(stringArray)); 763 } 764 public void testSize_PrimitiveArray() { 765 int[] intArray = new int[0]; 766 assertEquals(0, CollectionUtils.size(intArray)); 767 768 double[] doubleArray = new double[3]; 769 assertEquals(3, CollectionUtils.size(doubleArray)); 770 doubleArray[0] = 0.0d; 771 doubleArray[1] = 1.0d; 772 doubleArray[2] = 2.5d; 773 assertEquals(3, CollectionUtils.size(doubleArray)); 774 } 775 public void testSize_Enumeration() { 776 Vector list = new Vector(); 777 assertEquals(0, CollectionUtils.size(list.elements())); 778 list.add("a"); 779 assertEquals(1, CollectionUtils.size(list.elements())); 780 list.add("b"); 781 assertEquals(2, CollectionUtils.size(list.elements())); 782 } 783 public void testSize_Iterator() { 784 List list = new ArrayList(); 785 assertEquals(0, CollectionUtils.size(list.iterator())); 786 list.add("a"); 787 assertEquals(1, CollectionUtils.size(list.iterator())); 788 list.add("b"); 789 assertEquals(2, CollectionUtils.size(list.iterator())); 790 } 791 public void testSize_Other() { 792 try { 793 CollectionUtils.size(null); 794 fail("Expecting IllegalArgumentException"); 795 } catch (IllegalArgumentException e) {} 796 try { 797 CollectionUtils.size("not a list"); 798 fail("Expecting IllegalArgumentException"); 799 } catch (IllegalArgumentException e) {} 800 } 801 802 private static Predicate EQUALS_TWO = new Predicate() { 804 public boolean evaluate(Object input) { 805 return (input.equals("Two")); 806 } 807 }; 808 809 public void testFilter() { 810 List list = new ArrayList(); 811 list.add("One"); 812 list.add("Two"); 813 list.add("Three"); 814 list.add("Four"); 815 CollectionUtils.filter(list, EQUALS_TWO); 816 assertEquals(1, list.size()); 817 assertEquals("Two", list.get(0)); 818 819 list = new ArrayList(); 820 list.add("One"); 821 list.add("Two"); 822 list.add("Three"); 823 list.add("Four"); 824 CollectionUtils.filter(list, null); 825 assertEquals(4, list.size()); 826 CollectionUtils.filter(null, EQUALS_TWO); 827 assertEquals(4, list.size()); 828 CollectionUtils.filter(null, null); 829 assertEquals(4, list.size()); 830 } 831 832 public void testCountMatches() { 833 List list = new ArrayList(); 834 list.add("One"); 835 list.add("Two"); 836 list.add("Three"); 837 list.add("Four"); 838 int count = CollectionUtils.countMatches(list, EQUALS_TWO); 839 assertEquals(4, list.size()); 840 assertEquals(1, count); 841 assertEquals(0, CollectionUtils.countMatches(list, null)); 842 assertEquals(0, CollectionUtils.countMatches(null, EQUALS_TWO)); 843 assertEquals(0, CollectionUtils.countMatches(null, null)); 844 } 845 846 public void testExists() { 847 List list = new ArrayList(); 848 assertEquals(false, CollectionUtils.exists(null, null)); 849 assertEquals(false, CollectionUtils.exists(list, null)); 850 assertEquals(false, CollectionUtils.exists(null, EQUALS_TWO)); 851 assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO)); 852 list.add("One"); 853 list.add("Three"); 854 list.add("Four"); 855 assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO)); 856 857 list.add("Two"); 858 assertEquals(true, CollectionUtils.exists(list, EQUALS_TWO)); 859 } 860 861 public void testSelect() { 862 List list = new ArrayList(); 863 list.add("One"); 864 list.add("Two"); 865 list.add("Three"); 866 list.add("Four"); 867 Collection output = CollectionUtils.select(list, EQUALS_TWO); 868 assertEquals(4, list.size()); 869 assertEquals(1, output.size()); 870 assertEquals("Two", output.iterator().next()); 871 } 872 873 public void testSelectRejected() { 874 List list = new ArrayList(); 875 list.add("One"); 876 list.add("Two"); 877 list.add("Three"); 878 list.add("Four"); 879 Collection output = CollectionUtils.selectRejected(list, EQUALS_TWO); 880 assertEquals(4, list.size()); 881 assertEquals(3, output.size()); 882 assertTrue(output.contains("One")); 883 assertTrue(output.contains("Three")); 884 assertTrue(output.contains("Four")); 885 } 886 887 public void testCollect() { 888 Transformer transformer = TransformerUtils.constantTransformer("z"); 889 Collection collection = CollectionUtils.collect(collectionA, transformer); 890 assertTrue(collection.size() == collectionA.size()); 891 assertTrue(collectionA.contains("a") && ! collectionA.contains("z")); 892 assertTrue(collection.contains("z") && !collection.contains("a")); 893 894 collection = new ArrayList(); 895 CollectionUtils.collect(collectionA, transformer, collection); 896 assertTrue(collection.size() == collectionA.size()); 897 assertTrue(collectionA.contains("a") && ! collectionA.contains("z")); 898 assertTrue(collection.contains("z") && !collection.contains("a")); 899 900 Iterator iterator = null; 901 collection = new ArrayList(); 902 CollectionUtils.collect(iterator, transformer, collection); 903 904 iterator = collectionA.iterator(); 905 CollectionUtils.collect(iterator, transformer, collection); 906 assertTrue(collection.size() == collectionA.size()); 907 assertTrue(collectionA.contains("a") && ! collectionA.contains("z")); 908 assertTrue(collection.contains("z") && !collection.contains("a")); 909 910 iterator = collectionA.iterator(); 911 collection = CollectionUtils.collect(iterator, transformer); 912 assertTrue(collection.size() == collectionA.size()); 913 assertTrue(collection.contains("z") && !collection.contains("a")); 914 collection = CollectionUtils.collect((Iterator) null, (Transformer) null); 915 assertTrue(collection.size() == 0); 916 917 int size = collectionA.size(); 918 CollectionUtils.collect((Collection) null, transformer, collectionA); 919 assertTrue(collectionA.size() == size && collectionA.contains("a")); 920 CollectionUtils.collect(collectionB, null, collectionA); 921 assertTrue(collectionA.size() == size && collectionA.contains("a")); 922 923 } 924 925 Transformer TRANSFORM_TO_INTEGER = new Transformer() { 926 public Object transform(Object input) { 927 return new Integer((String) input); 928 } 929 }; 930 931 public void testTransform1() { 932 List list = new ArrayList(); 933 list.add("1"); 934 list.add("2"); 935 list.add("3"); 936 CollectionUtils.transform(list, TRANSFORM_TO_INTEGER); 937 assertEquals(3, list.size()); 938 assertEquals(new Integer(1), list.get(0)); 939 assertEquals(new Integer(2), list.get(1)); 940 assertEquals(new Integer(3), list.get(2)); 941 942 list = new ArrayList(); 943 list.add("1"); 944 list.add("2"); 945 list.add("3"); 946 CollectionUtils.transform(null, TRANSFORM_TO_INTEGER); 947 assertEquals(3, list.size()); 948 CollectionUtils.transform(list, null); 949 assertEquals(3, list.size()); 950 CollectionUtils.transform(null, null); 951 assertEquals(3, list.size()); 952 } 953 954 public void testTransform2() { 955 Set set = new HashSet(); 956 set.add("1"); 957 set.add("2"); 958 set.add("3"); 959 CollectionUtils.transform(set, new Transformer() { 960 public Object transform(Object input) { 961 return new Integer(4); 962 } 963 }); 964 assertEquals(1, set.size()); 965 assertEquals(new Integer(4), set.iterator().next()); 966 } 967 968 public void testPredicatedCollection() { 969 Predicate predicate = new Predicate() { 970 public boolean evaluate(Object o) { 971 return o instanceof String; 972 } 973 }; 974 Collection collection = 975 CollectionUtils.predicatedCollection(new ArrayList(), predicate); 976 assertTrue("returned object should be a PredicatedCollection", 977 collection instanceof PredicatedCollection); 978 try { 979 collection = 980 CollectionUtils.predicatedCollection(new ArrayList(), null); 981 fail("Expecting IllegalArgumentException for null predicate."); 982 } catch (IllegalArgumentException ex) { 983 } 985 try { 986 collection = 987 CollectionUtils.predicatedCollection(null, predicate); 988 fail("Expecting IllegalArgumentException for null collection."); 989 } catch (IllegalArgumentException ex) { 990 } 992 } 993 994 995 996 public BulkTest bulkTestTypedCollection() { 997 return new TestTypedCollection("") { 998 public Collection typedCollection() { 999 return CollectionUtils.typedCollection( 1000 new ArrayList(), 1001 super.getType()); 1002 } 1003 1004 public BulkTest bulkTestAll() { 1005 return new AbstractTestCollection("") { 1006 public Collection makeCollection() { 1007 return typedCollection(); 1008 } 1009 1010 public Collection makeConfirmedCollection() { 1011 return new ArrayList(); 1012 } 1013 1014 public Collection makeConfirmedFullCollection() { 1015 ArrayList list = new ArrayList(); 1016 list.addAll(java.util.Arrays.asList(getFullElements())); 1017 return list; 1018 } 1019 1020 public Object[] getFullElements() { 1021 return getFullNonNullStringElements(); 1022 } 1023 1024 public Object[] getOtherElements() { 1025 return getOtherNonNullStringElements(); 1026 } 1027 1028 }; 1029 } 1030 }; 1031 } 1032 1033 public void testIsFull() { 1034 Set set = new HashSet(); 1035 set.add("1"); 1036 set.add("2"); 1037 set.add("3"); 1038 try { 1039 CollectionUtils.isFull(null); 1040 fail(); 1041 } catch (NullPointerException ex) {} 1042 assertEquals(false, CollectionUtils.isFull(set)); 1043 1044 BoundedFifoBuffer buf = new BoundedFifoBuffer(set); 1045 assertEquals(true, CollectionUtils.isFull(buf)); 1046 buf.remove("2"); 1047 assertEquals(false, CollectionUtils.isFull(buf)); 1048 buf.add("2"); 1049 assertEquals(true, CollectionUtils.isFull(buf)); 1050 1051 Buffer buf2 = BufferUtils.synchronizedBuffer(buf); 1052 assertEquals(true, CollectionUtils.isFull(buf2)); 1053 buf2.remove("2"); 1054 assertEquals(false, CollectionUtils.isFull(buf2)); 1055 buf2.add("2"); 1056 assertEquals(true, CollectionUtils.isFull(buf2)); 1057 } 1058 1059 public void testMaxSize() { 1060 Set set = new HashSet(); 1061 set.add("1"); 1062 set.add("2"); 1063 set.add("3"); 1064 try { 1065 CollectionUtils.maxSize(null); 1066 fail(); 1067 } catch (NullPointerException ex) {} 1068 assertEquals(-1, CollectionUtils.maxSize(set)); 1069 1070 BoundedFifoBuffer buf = new BoundedFifoBuffer(set); 1071 assertEquals(3, CollectionUtils.maxSize(buf)); 1072 buf.remove("2"); 1073 assertEquals(3, CollectionUtils.maxSize(buf)); 1074 buf.add("2"); 1075 assertEquals(3, CollectionUtils.maxSize(buf)); 1076 1077 Buffer buf2 = BufferUtils.synchronizedBuffer(buf); 1078 assertEquals(3, CollectionUtils.maxSize(buf2)); 1079 buf2.remove("2"); 1080 assertEquals(3, CollectionUtils.maxSize(buf2)); 1081 buf2.add("2"); 1082 assertEquals(3, CollectionUtils.maxSize(buf2)); 1083 } 1084 1085 public void testIntersectionUsesMethodEquals() { 1086 Object elta = new Integer(17); 1088 Object eltb = new Integer(17); 1089 1090 assertEquals(elta,eltb); 1092 assertEquals(eltb,elta); 1093 1094 assertTrue(elta != eltb); 1096 1097 Collection cola = new ArrayList(); 1099 Collection colb = new ArrayList(); 1100 1101 cola.add(elta); 1104 colb.add(eltb); 1105 1106 Collection intersection = CollectionUtils.intersection(cola,colb); 1109 assertEquals(1,intersection.size()); 1110 1111 Object eltc = intersection.iterator().next(); 1115 assertTrue((eltc == elta && eltc != eltb) || (eltc != elta && eltc == eltb)); 1116 1117 assertEquals(elta,eltc); 1120 assertEquals(eltc,elta); 1121 assertEquals(eltb,eltc); 1122 assertEquals(eltc,eltb); 1123 } 1124 1125 public void testTransformedCollection() { 1126 Transformer transformer = TransformerUtils.nopTransformer(); 1127 Collection collection = 1128 CollectionUtils.transformedCollection(new ArrayList(), transformer); 1129 assertTrue("returned object should be a TransformedCollection", 1130 collection instanceof TransformedCollection); 1131 try { 1132 collection = 1133 CollectionUtils.transformedCollection(new ArrayList(), null); 1134 fail("Expecting IllegalArgumentException for null transformer."); 1135 } catch (IllegalArgumentException ex) { 1136 } 1138 try { 1139 collection = 1140 CollectionUtils.transformedCollection(null, transformer); 1141 fail("Expecting IllegalArgumentException for null collection."); 1142 } catch (IllegalArgumentException ex) { 1143 } 1145 } 1146 1147 public void testSynchronizedCollection() { 1148 Collection col = CollectionUtils.synchronizedCollection(new ArrayList()); 1149 assertTrue("Returned object should be a SynchronizedCollection.", 1150 col instanceof SynchronizedCollection); 1151 try { 1152 col = CollectionUtils.synchronizedCollection(null); 1153 fail("Expecting IllegalArgumentException for null collection."); 1154 } catch (IllegalArgumentException ex) { 1155 } 1157 } 1158 1159 public void testUnmodifiableCollection() { 1160 Collection col = CollectionUtils.unmodifiableCollection(new ArrayList()); 1161 assertTrue("Returned object should be a UnmodifiableCollection.", 1162 col instanceof UnmodifiableCollection); 1163 try { 1164 col = CollectionUtils.unmodifiableCollection(null); 1165 fail("Expecting IllegalArgumentException for null collection."); 1166 } catch (IllegalArgumentException ex) { 1167 } 1169 } 1170 1171} 1172 | Popular Tags |