1 16 package org.apache.commons.collections; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.PrintStream ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.ListResourceBundle ; 24 import java.util.Map ; 25 import java.util.ResourceBundle ; 26 import java.util.Set ; 27 import java.util.TreeMap ; 28 29 import junit.framework.Test; 30 31 import org.apache.commons.collections.map.LazyMap; 32 import org.apache.commons.collections.map.PredicatedMap; 33 import org.apache.commons.collections.map.TestPredicatedMap; 34 35 45 public class TestMapUtils extends BulkTest { 46 47 public TestMapUtils(String name) { 48 super(name); 49 } 50 51 52 public static Test suite() { 53 return BulkTest.makeSuite(TestMapUtils.class); 54 } 55 56 public Predicate getPredicate() { 57 return new Predicate() { 58 public boolean evaluate(Object o) { 59 return o instanceof String ; 60 } 61 }; 62 } 63 64 public void testPredicatedMap() { 65 Predicate p = getPredicate(); 66 Map map = MapUtils.predicatedMap(new HashMap (), p, p); 67 assertTrue("returned object should be a PredicatedMap", 68 map instanceof PredicatedMap); 69 try { 70 map = MapUtils.predicatedMap(null, p, p); 71 fail("Expecting IllegalArgumentException for null map."); 72 } catch (IllegalArgumentException e) { 73 } 75 } 76 77 public void testTypedMapIllegalPut() { 79 final Map map = MapUtils.typedMap(new HashMap (), String .class, String .class); 80 81 try { 82 map.put("Hi", new Integer (3)); 83 fail("Illegal value should raise IllegalArgument"); 84 } catch (IllegalArgumentException e) { 85 } 87 88 try { 89 map.put(new Integer (3), "Hi"); 90 fail("Illegal key should raise IllegalArgument"); 91 } catch (IllegalArgumentException e) { 92 } 94 95 assertTrue(!map.containsKey(new Integer (3))); 96 assertTrue(!map.containsValue(new Integer (3))); 97 98 Map map2 = new HashMap (); 99 map2.put("A", "a"); 100 map2.put("B", "b"); 101 map2.put("C", "c"); 102 map2.put("c", new Integer (3)); 103 104 try { 105 map.putAll(map2); 106 fail("Illegal value should raise IllegalArgument"); 107 } catch (IllegalArgumentException e) { 108 } 110 111 map.put("E", "e"); 112 Iterator iterator = map.entrySet().iterator(); 113 try { 114 Map.Entry entry = (Map.Entry )iterator.next(); 115 entry.setValue(new Integer (3)); 116 fail("Illegal value should raise IllegalArgument"); 117 } catch (IllegalArgumentException e) { 118 } 120 121 } 122 123 public BulkTest bulkTestTypedMap() { 124 return new TestPredicatedMap("") { 125 public boolean isAllowNullKey() { 126 return false; 127 } 128 129 public boolean isAllowNullValue() { 130 return false; 131 } 132 133 public Map makeEmptyMap() { 134 return MapUtils.typedMap(new HashMap (), String .class, String .class); 135 } 136 }; 137 } 138 139 public void testLazyMapFactory() { 140 Factory factory = FactoryUtils.constantFactory(new Integer (5)); 141 Map map = MapUtils.lazyMap(new HashMap (), factory); 142 assertTrue(map instanceof LazyMap); 143 try { 144 map = MapUtils.lazyMap(new HashMap (), (Factory) null); 145 fail("Expecting IllegalArgumentException for null factory"); 146 } catch (IllegalArgumentException e) { 147 } 149 try { 150 map = MapUtils.lazyMap(null, factory); 151 fail("Expecting IllegalArgumentException for null map"); 152 } catch (IllegalArgumentException e) { 153 } 155 Transformer transformer = TransformerUtils.asTransformer(factory); 156 map = MapUtils.lazyMap(new HashMap (), transformer); 157 assertTrue(map instanceof LazyMap); 158 try { 159 map = MapUtils.lazyMap(new HashMap (), (Transformer) null); 160 fail("Expecting IllegalArgumentException for null transformer"); 161 } catch (IllegalArgumentException e) { 162 } 164 try { 165 map = MapUtils.lazyMap(null, transformer); 166 fail("Expecting IllegalArgumentException for null map"); 167 } catch (IllegalArgumentException e) { 168 } 170 } 171 172 public void testLazyMapTransformer() { 173 Map map = MapUtils.lazyMap(new HashMap (), new Transformer() { 174 public Object transform(Object mapKey) { 175 if (mapKey instanceof String ) { 176 return new Integer ((String ) mapKey); 177 } 178 return null; 179 } 180 }); 181 182 assertEquals(0, map.size()); 183 Integer i1 = (Integer ) map.get("5"); 184 assertEquals(new Integer (5), i1); 185 assertEquals(1, map.size()); 186 Integer i2 = (Integer ) map.get(new String (new char[] {'5'})); 187 assertEquals(new Integer (5), i2); 188 assertEquals(1, map.size()); 189 assertSame(i1, i2); 190 } 191 192 public void testInvertMap() { 193 final Map in = new HashMap ( 5 , 1 ); 194 in.put( "1" , "A" ); 195 in.put( "2" , "B" ); 196 in.put( "3" , "C" ); 197 in.put( "4" , "D" ); 198 in.put( "5" , "E" ); 199 200 final Set inKeySet = new HashSet ( in.keySet() ); 201 final Set inValSet = new HashSet ( in.values() ); 202 203 final Map out = MapUtils.invertMap(in); 204 205 final Set outKeySet = new HashSet ( out.keySet() ); 206 final Set outValSet = new HashSet ( out.values() ); 207 208 assertTrue( inKeySet.equals( outValSet )); 209 assertTrue( inValSet.equals( outKeySet )); 210 211 assertEquals( out.get("A"), "1" ); 212 assertEquals( out.get("B"), "2" ); 213 assertEquals( out.get("C"), "3" ); 214 assertEquals( out.get("D"), "4" ); 215 assertEquals( out.get("E"), "5" ); 216 } 217 218 public void testConvertResourceBundle() { 219 final Map in = new HashMap ( 5 , 1 ); 220 in.put( "1" , "A" ); 221 in.put( "2" , "B" ); 222 in.put( "3" , "C" ); 223 in.put( "4" , "D" ); 224 in.put( "5" , "E" ); 225 226 ResourceBundle b = new ListResourceBundle () { 227 public Object [][] getContents() { 228 final Object [][] contents = new Object [ in.size() ][2]; 229 final Iterator i = in.keySet().iterator(); 230 int n = 0; 231 while ( i.hasNext() ) { 232 final Object key = i.next(); 233 final Object val = in.get( key ); 234 contents[ n ][ 0 ] = key; 235 contents[ n ][ 1 ] = val; 236 ++n; 237 } 238 return contents; 239 } 240 }; 241 242 final Map out = MapUtils.toMap(b); 243 244 assertTrue( in.equals(out)); 245 } 246 247 public void testDebugAndVerbosePrintCasting() { 248 final Map inner = new HashMap (2, 1); 249 inner.put( new Integer (2) , "B" ); 250 inner.put( new Integer (3) , "C" ); 251 252 final Map outer = new HashMap (2, 1); 253 outer.put( new Integer (0) , inner ); 254 outer.put( new Integer (1) , "A"); 255 256 257 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 258 final PrintStream outPrint = new PrintStream (out); 259 260 try { 261 MapUtils.debugPrint(outPrint, "Print Map", outer); 262 } catch (final ClassCastException e) { 263 fail("No Casting should be occurring!"); 264 } 265 } 266 267 public void testDebugAndVerbosePrintNullMap() { 268 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 269 final PrintStream outPrint = new PrintStream (out); 270 271 final String LABEL = "Print Map"; 272 outPrint.println(LABEL + " = " + String.valueOf((Object ) null)); 273 final String EXPECTED_OUT = out.toString(); 274 275 out.reset(); 276 277 MapUtils.debugPrint(outPrint, LABEL, null); 278 assertEquals(EXPECTED_OUT, out.toString()); 279 280 out.reset(); 281 282 MapUtils.verbosePrint(outPrint, LABEL, null); 283 assertEquals(EXPECTED_OUT, out.toString()); 284 } 285 286 public void testVerbosePrintNullLabel() { 287 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 288 final PrintStream outPrint = new PrintStream (out); 289 290 final String INDENT = " "; 291 292 final Map map = new TreeMap (); map.put( new Integer (2) , "B" ); 294 map.put( new Integer (3) , "C" ); 295 map.put( new Integer (4) , null ); 296 297 outPrint.println("{"); 298 outPrint.println(INDENT + "2 = B"); 299 outPrint.println(INDENT + "3 = C"); 300 outPrint.println(INDENT + "4 = null"); 301 outPrint.println("}"); 302 final String EXPECTED_OUT = out.toString(); 303 out.reset(); 304 305 MapUtils.verbosePrint(outPrint, null, map); 306 assertEquals(EXPECTED_OUT, out.toString()); 307 } 308 309 public void testDebugPrintNullLabel() { 310 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 311 final PrintStream outPrint = new PrintStream (out); 312 313 final String INDENT = " "; 314 315 final Map map = new TreeMap (); map.put( new Integer (2) , "B" ); 317 map.put( new Integer (3) , "C" ); 318 map.put( new Integer (4) , null ); 319 320 outPrint.println("{"); 321 outPrint.println(INDENT + "2 = B " + String .class.getName()); 322 outPrint.println(INDENT + "3 = C " + String .class.getName()); 323 outPrint.println(INDENT + "4 = null"); 324 outPrint.println("} " + TreeMap .class.getName()); 325 final String EXPECTED_OUT = out.toString(); 326 out.reset(); 327 328 MapUtils.debugPrint(outPrint, null, map); 329 assertEquals(EXPECTED_OUT, out.toString()); 330 } 331 332 public void testVerbosePrintNullLabelAndMap() { 333 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 334 final PrintStream outPrint = new PrintStream (out); 335 336 outPrint.println("null"); 337 final String EXPECTED_OUT = out.toString(); 338 out.reset(); 339 340 MapUtils.verbosePrint(outPrint, null, null); 341 assertEquals(EXPECTED_OUT, out.toString()); 342 } 343 344 public void testDebugPrintNullLabelAndMap() { 345 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 346 final PrintStream outPrint = new PrintStream (out); 347 348 outPrint.println("null"); 349 final String EXPECTED_OUT = out.toString(); 350 out.reset(); 351 352 MapUtils.debugPrint(outPrint, null, null); 353 assertEquals(EXPECTED_OUT, out.toString()); 354 } 355 356 public void testVerbosePrintNullStream() { 357 try { 358 MapUtils.verbosePrint(null, "Map", new HashMap ()); 359 fail("Should generate NullPointerException"); 360 } catch (NullPointerException expected) { 361 } 362 } 363 364 public void testDebugPrintNullStream() { 365 try { 366 MapUtils.debugPrint(null, "Map", new HashMap ()); 367 fail("Should generate NullPointerException"); 368 } catch (NullPointerException expected) { 369 } 370 } 371 372 public void testDebugPrintNullKey() { 373 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 374 final PrintStream outPrint = new PrintStream (out); 375 376 final String INDENT = " "; 377 378 final Map map = new HashMap (); 379 map.put( null , "A" ); 380 381 outPrint.println("{"); 382 outPrint.println(INDENT + "null = A " + String .class.getName()); 383 outPrint.println("} " + HashMap .class.getName()); 384 final String EXPECTED_OUT = out.toString(); 385 out.reset(); 386 387 MapUtils.debugPrint(outPrint, null, map); 388 assertEquals(EXPECTED_OUT, out.toString()); 389 } 390 391 public void testVerbosePrintNullKey() { 392 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 393 final PrintStream outPrint = new PrintStream (out); 394 395 final String INDENT = " "; 396 397 final Map map = new HashMap (); 398 map.put( null , "A" ); 399 400 outPrint.println("{"); 401 outPrint.println(INDENT + "null = A"); 402 outPrint.println("}"); 403 final String EXPECTED_OUT = out.toString(); 404 out.reset(); 405 406 MapUtils.verbosePrint(outPrint, null, map); 407 assertEquals(EXPECTED_OUT, out.toString()); 408 } 409 410 public void testDebugPrintNullKeyToMap1() { 411 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 412 final PrintStream outPrint = new PrintStream (out); 413 414 final String INDENT = " "; 415 416 final Map map = new HashMap (); 417 map.put( null , map ); 418 419 outPrint.println("{"); 420 outPrint.println(INDENT + "null = (this Map) " + HashMap .class.getName()); 421 outPrint.println("} " + HashMap .class.getName()); 422 final String EXPECTED_OUT = out.toString(); 423 out.reset(); 424 425 MapUtils.debugPrint(outPrint, null, map); 426 assertEquals(EXPECTED_OUT, out.toString()); 427 } 428 429 public void testVerbosePrintNullKeyToMap1() { 430 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 431 final PrintStream outPrint = new PrintStream (out); 432 433 final String INDENT = " "; 434 435 final Map map = new HashMap (); 436 map.put( null , map ); 437 438 outPrint.println("{"); 439 outPrint.println(INDENT + "null = (this Map)"); 440 outPrint.println("}"); 441 final String EXPECTED_OUT = out.toString(); 442 out.reset(); 443 444 MapUtils.verbosePrint(outPrint, null, map); 445 assertEquals(EXPECTED_OUT, out.toString()); 446 } 447 448 public void testDebugPrintNullKeyToMap2() { 449 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 450 final PrintStream outPrint = new PrintStream (out); 451 452 final String INDENT = " "; 453 454 final Map map = new HashMap (); 455 final Map map2= new HashMap (); 456 map.put( null , map2 ); 457 map2.put( "2", "B" ); 458 459 outPrint.println("{"); 460 outPrint.println(INDENT + "null = "); 461 outPrint.println(INDENT + "{"); 462 outPrint.println(INDENT + INDENT + "2 = B " + String .class.getName()); 463 outPrint.println(INDENT + "} " + HashMap .class.getName()); 464 outPrint.println("} " + HashMap .class.getName()); 465 final String EXPECTED_OUT = out.toString(); 466 out.reset(); 467 468 MapUtils.debugPrint(outPrint, null, map); 469 assertEquals(EXPECTED_OUT, out.toString()); 470 } 471 472 public void testVerbosePrintNullKeyToMap2() { 473 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 474 final PrintStream outPrint = new PrintStream (out); 475 476 final String INDENT = " "; 477 478 final Map map = new HashMap (); 479 final Map map2= new HashMap (); 480 map.put( null , map2 ); 481 map2.put( "2", "B" ); 482 483 outPrint.println("{"); 484 outPrint.println(INDENT + "null = "); 485 outPrint.println(INDENT + "{"); 486 outPrint.println(INDENT + INDENT + "2 = B"); 487 outPrint.println(INDENT + "}"); 488 outPrint.println("}"); 489 final String EXPECTED_OUT = out.toString(); 490 out.reset(); 491 492 MapUtils.verbosePrint(outPrint, null, map); 493 assertEquals(EXPECTED_OUT, out.toString()); 494 } 495 496 public void testVerbosePrint() { 497 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 498 final PrintStream outPrint = new PrintStream (out); 499 500 final String LABEL = "Print Map"; 501 final String INDENT = " "; 502 503 outPrint.println(LABEL + " = "); 504 outPrint.println("{"); 505 outPrint.println(INDENT + "0 = A"); 506 outPrint.println(INDENT + "1 = "); 507 outPrint.println(INDENT + "{"); 508 outPrint.println(INDENT + INDENT + "2 = B"); 509 outPrint.println(INDENT + INDENT + "3 = C"); 510 outPrint.println(INDENT + "}"); 511 outPrint.println(INDENT + "7 = (this Map)"); 512 outPrint.println("}"); 513 514 final String EXPECTED_OUT = out.toString(); 515 516 out.reset(); 517 518 final Map inner = new TreeMap (); inner.put( new Integer (2) , "B" ); 520 inner.put( new Integer (3) , "C" ); 521 522 final Map outer = new TreeMap (); 523 outer.put( new Integer (1) , inner ); 524 outer.put( new Integer (0) , "A"); 525 outer.put( new Integer (7) , outer); 526 527 MapUtils.verbosePrint(outPrint, "Print Map", outer); 528 assertEquals(EXPECTED_OUT, out.toString()); 529 } 530 531 public void testDebugPrint() { 532 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 533 final PrintStream outPrint = new PrintStream (out); 534 535 final String LABEL = "Print Map"; 536 final String INDENT = " "; 537 538 outPrint.println(LABEL + " = "); 539 outPrint.println("{"); 540 outPrint.println(INDENT + "0 = A " + String .class.getName()); 541 outPrint.println(INDENT + "1 = "); 542 outPrint.println(INDENT + "{"); 543 outPrint.println(INDENT + INDENT + "2 = B " + String .class.getName()); 544 outPrint.println(INDENT + INDENT + "3 = C " + String .class.getName()); 545 outPrint.println(INDENT + "} " + TreeMap .class.getName()); 546 outPrint.println(INDENT + "7 = (this Map) " + TreeMap .class.getName()); 547 outPrint.println("} " + TreeMap .class.getName()); 548 549 final String EXPECTED_OUT = out.toString(); 550 551 out.reset(); 552 553 final Map inner = new TreeMap (); inner.put( new Integer (2) , "B" ); 555 inner.put( new Integer (3) , "C" ); 556 557 final Map outer = new TreeMap (); 558 outer.put( new Integer (1) , inner ); 559 outer.put( new Integer (0) , "A"); 560 outer.put( new Integer (7) , outer); 561 562 MapUtils.debugPrint(outPrint, "Print Map", outer); 563 assertEquals(EXPECTED_OUT, out.toString()); 564 } 565 566 public void testVerbosePrintSelfReference() { 567 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 568 final PrintStream outPrint = new PrintStream (out); 569 570 final String LABEL = "Print Map"; 571 final String INDENT = " "; 572 573 574 final Map grandfather = new TreeMap (); final Map father = new TreeMap (); 576 final Map son = new TreeMap (); 577 578 grandfather.put( new Integer (0), "A" ); 579 grandfather.put( new Integer (1), father ); 580 581 father.put( new Integer (2), "B" ); 582 father.put( new Integer (3), grandfather); 583 father.put( new Integer (4), son); 584 585 son.put( new Integer (5), "C"); 586 son.put( new Integer (6), grandfather); 587 son.put( new Integer (7), father); 588 589 outPrint.println(LABEL + " = "); 590 outPrint.println("{"); 591 outPrint.println(INDENT + "0 = A"); 592 outPrint.println(INDENT + "1 = "); 593 outPrint.println(INDENT + "{"); 594 outPrint.println(INDENT + INDENT + "2 = B"); 595 outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map)"); 596 outPrint.println(INDENT + INDENT + "4 = "); 597 outPrint.println(INDENT + INDENT + "{"); 598 outPrint.println(INDENT + INDENT + INDENT + "5 = C"); 599 outPrint.println(INDENT + INDENT + INDENT + "6 = (ancestor[1] Map)"); 600 outPrint.println(INDENT + INDENT + INDENT + "7 = (ancestor[0] Map)"); 601 outPrint.println(INDENT + INDENT + "}"); 602 outPrint.println(INDENT + "}"); 603 outPrint.println("}"); 604 605 final String EXPECTED_OUT = out.toString(); 606 607 out.reset(); 608 MapUtils.verbosePrint(outPrint, "Print Map", grandfather); 609 610 assertEquals(EXPECTED_OUT, out.toString()); 611 } 612 613 public void testDebugPrintSelfReference() { 614 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 615 final PrintStream outPrint = new PrintStream (out); 616 617 final String LABEL = "Print Map"; 618 final String INDENT = " "; 619 620 621 final Map grandfather = new TreeMap (); final Map father = new TreeMap (); 623 final Map son = new TreeMap (); 624 625 grandfather.put( new Integer (0), "A" ); 626 grandfather.put( new Integer (1), father ); 627 628 father.put( new Integer (2), "B" ); 629 father.put( new Integer (3), grandfather); 630 father.put( new Integer (4), son); 631 632 son.put( new Integer (5), "C"); 633 son.put( new Integer (6), grandfather); 634 son.put( new Integer (7), father); 635 636 outPrint.println(LABEL + " = "); 637 outPrint.println("{"); 638 outPrint.println(INDENT + "0 = A " + String .class.getName()); 639 outPrint.println(INDENT + "1 = "); 640 outPrint.println(INDENT + "{"); 641 outPrint.println(INDENT + INDENT + "2 = B " + String .class.getName()); 642 outPrint.println(INDENT + INDENT + "3 = (ancestor[0] Map) " + TreeMap .class.getName()); 643 outPrint.println(INDENT + INDENT + "4 = "); 644 outPrint.println(INDENT + INDENT + "{"); 645 outPrint.println(INDENT + INDENT + INDENT + "5 = C " + String .class.getName()); 646 outPrint.println(INDENT + INDENT + INDENT + "6 = (ancestor[1] Map) " + TreeMap .class.getName()); 647 outPrint.println(INDENT + INDENT + INDENT + "7 = (ancestor[0] Map) " + TreeMap .class.getName()); 648 outPrint.println(INDENT + INDENT + "} " + TreeMap .class.getName()); 649 outPrint.println(INDENT + "} " + TreeMap .class.getName()); 650 outPrint.println("} " + TreeMap .class.getName()); 651 652 final String EXPECTED_OUT = out.toString(); 653 654 out.reset(); 655 MapUtils.debugPrint(outPrint, "Print Map", grandfather); 656 657 assertEquals(EXPECTED_OUT, out.toString()); 658 } 659 660 } 661 | Popular Tags |