1 16 package org.apache.commons.lang.builder; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 import junit.textui.TestRunner; 26 27 35 public class ToStringBuilderTest extends TestCase { 36 37 private final Integer base = new Integer (5); 38 private final String baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base)); 39 40 public ToStringBuilderTest(String name) { 41 super(name); 42 } 43 44 public static void main(String [] args) { 45 TestRunner.run(suite()); 46 } 47 48 public static Test suite() { 49 TestSuite suite = new TestSuite(ToStringBuilderTest.class); 50 suite.setName("ToStringBuilder Tests"); 51 return suite; 52 } 53 54 protected void setUp() throws Exception { 55 super.setUp(); 56 } 57 58 protected void tearDown() throws Exception { 59 super.tearDown(); 60 } 61 62 64 public void testConstructorEx1() { 65 assertEquals("<null>", new ToStringBuilder(null).toString()); 66 } 67 68 public void testConstructorEx2() { 69 assertEquals("<null>", new ToStringBuilder(null, null).toString()); 70 new ToStringBuilder(this.base, null).toString(); 71 } 72 73 public void testConstructorEx3() { 74 assertEquals("<null>", new ToStringBuilder(null, null, null).toString()); 75 new ToStringBuilder(this.base, null, null); 76 new ToStringBuilder(this.base, ToStringStyle.DEFAULT_STYLE, null); 77 } 78 79 public void testGetSetDefault() { 80 try { 81 ToStringBuilder.setDefaultStyle(ToStringStyle.NO_FIELD_NAMES_STYLE); 82 assertSame(ToStringStyle.NO_FIELD_NAMES_STYLE, ToStringBuilder.getDefaultStyle()); 83 } finally { 84 ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE); 86 } 87 } 88 89 public void testSetDefaultEx() { 90 try { 91 ToStringBuilder.setDefaultStyle(null); 92 93 } catch (IllegalArgumentException ex) { 94 return; 95 } 96 fail(); 97 } 98 99 public void testBlank() { 100 assertEquals(baseStr + "[]", new ToStringBuilder(base).toString()); 101 } 102 103 106 public void testReflectionInteger() { 107 assertEquals(baseStr + "[value=5]", ToStringBuilder.reflectionToString(base)); 108 } 109 110 113 public void testReflectionCharacter() { 114 Character c = new Character ('A'); 115 assertEquals(this.toBaseString(c) + "[value=A]", ToStringBuilder.reflectionToString(c)); 116 } 117 118 121 public void testReflectionBoolean() { 122 Boolean b; 123 b = Boolean.TRUE; 124 assertEquals(this.toBaseString(b) + "[value=true]", ToStringBuilder.reflectionToString(b)); 125 b = Boolean.FALSE; 126 assertEquals(this.toBaseString(b) + "[value=false]", ToStringBuilder.reflectionToString(b)); 127 } 128 129 134 private String toBaseString(Object o) { 135 return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o)); 136 } 137 138 140 153 public void assertReflectionArray(String expected, Object actual) { 154 if (actual == null) { 155 return; 157 } 158 assertEquals(expected, ToStringBuilder.reflectionToString(actual)); 159 assertEquals(expected, ToStringBuilder.reflectionToString(actual, null)); 160 assertEquals(expected, ToStringBuilder.reflectionToString(actual, null, true)); 161 assertEquals(expected, ToStringBuilder.reflectionToString(actual, null, false)); 162 } 163 164 public void testReflectionObjectArray() { 165 Object [] array = new Object [] { null, base, new int[] { 3, 6 } }; 166 String baseStr = this.toBaseString(array); 167 assertEquals(baseStr + "[{<null>,5,{3,6}}]", ToStringBuilder.reflectionToString(array)); 168 array = null; 169 assertReflectionArray("<null>", array); 170 this.validateEmptyReflectionRegistry(); 171 } 172 173 public void testReflectionLongArray() { 174 long[] array = new long[] { 1, 2, -3, 4 }; 175 String baseStr = this.toBaseString(array); 176 assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); 177 array = null; 178 assertReflectionArray("<null>", array); 179 this.validateEmptyReflectionRegistry(); 180 } 181 182 public void testReflectionIntArray() { 183 int[] array = new int[] { 1, 2, -3, 4 }; 184 String baseStr = this.toBaseString(array); 185 assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); 186 array = null; 187 assertReflectionArray("<null>", array); 188 this.validateEmptyReflectionRegistry(); 189 } 190 191 public void testReflectionShortArray() { 192 short[] array = new short[] { 1, 2, -3, 4 }; 193 String baseStr = this.toBaseString(array); 194 assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); 195 array = null; 196 assertReflectionArray("<null>", array); 197 this.validateEmptyReflectionRegistry(); 198 } 199 200 public void testReflectionyteArray() { 201 byte[] array = new byte[] { 1, 2, -3, 4 }; 202 String baseStr = this.toBaseString(array); 203 assertEquals(baseStr + "[{1,2,-3,4}]", ToStringBuilder.reflectionToString(array)); 204 array = null; 205 assertReflectionArray("<null>", array); 206 this.validateEmptyReflectionRegistry(); 207 } 208 209 public void testReflectionCharArray() { 210 char[] array = new char[] { 'A', '2', '_', 'D' }; 211 String baseStr = this.toBaseString(array); 212 assertEquals(baseStr + "[{A,2,_,D}]", ToStringBuilder.reflectionToString(array)); 213 array = null; 214 assertReflectionArray("<null>", array); 215 this.validateEmptyReflectionRegistry(); 216 } 217 218 public void testReflectionDoubleArray() { 219 double[] array = new double[] { 1.0, 2.9876, -3.00001, 4.3 }; 220 String baseStr = this.toBaseString(array); 221 assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", ToStringBuilder.reflectionToString(array)); 222 array = null; 223 assertReflectionArray("<null>", array); 224 this.validateEmptyReflectionRegistry(); 225 } 226 227 public void testReflectionFloatArray() { 228 float[] array = new float[] { 1.0f, 2.9876f, -3.00001f, 4.3f }; 229 String baseStr = this.toBaseString(array); 230 assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", ToStringBuilder.reflectionToString(array)); 231 array = null; 232 assertReflectionArray("<null>", array); 233 this.validateEmptyReflectionRegistry(); 234 } 235 236 public void testReflectionBooleanArray() { 237 boolean[] array = new boolean[] { true, false, false }; 238 String baseStr = this.toBaseString(array); 239 assertEquals(baseStr + "[{true,false,false}]", ToStringBuilder.reflectionToString(array)); 240 array = null; 241 assertReflectionArray("<null>", array); 242 this.validateEmptyReflectionRegistry(); 243 } 244 245 247 public void testReflectionFloatArrayArray() { 248 float[][] array = new float[][] { { 1.0f, 2.29686f }, null, { Float.NaN } }; 249 String baseStr = this.toBaseString(array); 250 assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", ToStringBuilder.reflectionToString(array)); 251 array = null; 252 assertReflectionArray("<null>", array); 253 this.validateEmptyReflectionRegistry(); 254 } 255 256 257 public void testReflectionLongArrayArray() { 258 long[][] array = new long[][] { { 1, 2 }, null, { 5 } }; 259 String baseStr = this.toBaseString(array); 260 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array)); 261 array = null; 262 assertReflectionArray("<null>", array); 263 this.validateEmptyReflectionRegistry(); 264 } 265 266 public void testReflectionIntArrayArray() { 267 int[][] array = new int[][] { { 1, 2 }, null, { 5 } }; 268 String baseStr = this.toBaseString(array); 269 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array)); 270 array = null; 271 assertReflectionArray("<null>", array); 272 this.validateEmptyReflectionRegistry(); 273 } 274 275 public void testReflectionhortArrayArray() { 276 short[][] array = new short[][] { { 1, 2 }, null, { 5 } }; 277 String baseStr = this.toBaseString(array); 278 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array)); 279 array = null; 280 assertReflectionArray("<null>", array); 281 this.validateEmptyReflectionRegistry(); 282 } 283 284 public void testReflectionByteArrayArray() { 285 byte[][] array = new byte[][] { { 1, 2 }, null, { 5 } }; 286 String baseStr = this.toBaseString(array); 287 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", ToStringBuilder.reflectionToString(array)); 288 array = null; 289 assertReflectionArray("<null>", array); 290 this.validateEmptyReflectionRegistry(); 291 } 292 293 public void testReflectionCharArrayArray() { 294 char[][] array = new char[][] { { 'A', 'B' }, null, { 'p' } }; 295 String baseStr = this.toBaseString(array); 296 assertEquals(baseStr + "[{{A,B},<null>,{p}}]", ToStringBuilder.reflectionToString(array)); 297 array = null; 298 assertReflectionArray("<null>", array); 299 this.validateEmptyReflectionRegistry(); 300 } 301 302 public void testReflectionDoubleArrayArray() { 303 double[][] array = new double[][] { { 1.0, 2.29686 }, null, { Double.NaN } }; 304 String baseStr = this.toBaseString(array); 305 assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", ToStringBuilder.reflectionToString(array)); 306 array = null; 307 assertReflectionArray("<null>", array); 308 this.validateEmptyReflectionRegistry(); 309 } 310 311 public void testReflectionBooleanArrayArray() { 312 boolean[][] array = new boolean[][] { { true, false }, null, { false } }; 313 String baseStr = this.toBaseString(array); 314 assertEquals(baseStr + "[{{true,false},<null>,{false}}]", ToStringBuilder.reflectionToString(array)); 315 assertEquals(baseStr + "[{{true,false},<null>,{false}}]", ToStringBuilder.reflectionToString(array)); 316 array = null; 317 assertReflectionArray("<null>", array); 318 this.validateEmptyReflectionRegistry(); 319 } 320 321 323 public void testReflectionHierarchyArrayList() { 324 List base = new ArrayList (); 325 String baseStr = this.toBaseString(base); 326 assertEquals(baseStr + "[elementData={<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>,<null>},size=0,modCount=0]", ToStringBuilder.reflectionToString(base, null, true)); 327 assertEquals(baseStr + "[size=0]", ToStringBuilder.reflectionToString(base, null, false)); 328 this.validateEmptyReflectionRegistry(); 329 } 330 331 public void testReflectionHierarchy() { 332 ReflectionTestFixtureA baseA = new ReflectionTestFixtureA(); 333 String baseStr = this.toBaseString(baseA); 334 assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA)); 335 assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null)); 336 assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false)); 337 assertEquals(baseStr + "[a=a,transientA=t]", ToStringBuilder.reflectionToString(baseA, null, true)); 338 assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, null)); 339 assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, Object .class)); 340 assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, List .class)); 341 assertEquals(baseStr + "[a=a]", ToStringBuilder.reflectionToString(baseA, null, false, ReflectionTestFixtureA.class)); 342 343 ReflectionTestFixtureB baseB = new ReflectionTestFixtureB(); 344 baseStr = this.toBaseString(baseB); 345 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB)); 346 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB)); 347 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null)); 348 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false)); 349 assertEquals(baseStr + "[b=b,transientB=t,a=a,transientA=t]", ToStringBuilder.reflectionToString(baseB, null, true)); 350 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, null)); 351 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, Object .class)); 352 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, List .class)); 353 assertEquals(baseStr + "[b=b,a=a]", ToStringBuilder.reflectionToString(baseB, null, false, ReflectionTestFixtureA.class)); 354 assertEquals(baseStr + "[b=b]", ToStringBuilder.reflectionToString(baseB, null, false, ReflectionTestFixtureB.class)); 355 this.validateEmptyReflectionRegistry(); 356 } 357 358 static class ReflectionTestFixtureA { 359 private char a='a'; 360 private transient char transientA='t'; 361 } 362 363 static class ReflectionTestFixtureB extends ReflectionTestFixtureA { 364 private char b='b'; 365 private transient char transientB='t'; 366 } 367 368 public void testInnerClassReflection() { 369 Outer outer = new Outer(); 370 assertEquals(toBaseString(outer) + "[inner=" + toBaseString(outer.inner) + "[]]", outer.toString()); 371 } 372 373 static class Outer { 374 Inner inner = new Inner(); 375 class Inner { 376 public String toString() { 377 return ToStringBuilder.reflectionToString(this); 378 } 379 } 380 public String toString() { 381 return ToStringBuilder.reflectionToString(this); 382 } 383 } 384 385 387 390 public void testReflectionArrayCycle() throws Exception { 391 Object [] objects = new Object [1]; 392 objects[0] = objects; 393 assertEquals( 394 this.toBaseString(objects) + "[{" + this.toBaseString(objects) + "}]", 395 ToStringBuilder.reflectionToString(objects)); 396 this.validateEmptyReflectionRegistry(); 397 } 398 399 402 public void testReflectionArrayCycleLevel2() throws Exception { 403 Object [] objects = new Object [1]; 404 Object [] objectsLevel2 = new Object [1]; 405 objects[0] = objectsLevel2; 406 objectsLevel2[0] = (Object ) objects; 407 assertEquals( 408 this.toBaseString(objects) + "[{{" + this.toBaseString(objects) + "}}]", 409 ToStringBuilder.reflectionToString(objects)); 410 assertEquals( 411 this.toBaseString(objectsLevel2) + "[{{" + this.toBaseString(objectsLevel2) + "}}]", 412 ToStringBuilder.reflectionToString(objectsLevel2)); 413 this.validateEmptyReflectionRegistry(); 414 } 415 416 public void testReflectionArrayArrayCycle() throws Exception { 417 Object [][] objects = new Object [2][2]; 418 objects[0][0] = objects; 419 objects[0][1] = objects; 420 objects[1][0] = objects; 421 objects[1][1] = objects; 422 String basicToString = this.toBaseString(objects); 423 assertEquals( 424 basicToString 425 + "[{{" 426 + basicToString 427 + "," 428 + basicToString 429 + "},{" 430 + basicToString 431 + "," 432 + basicToString 433 + "}}]", 434 ToStringBuilder.reflectionToString(objects)); 435 this.validateEmptyReflectionRegistry(); 436 } 437 438 441 static class ReflectionTestCycleA { 442 ReflectionTestCycleB b; 443 444 public String toString() { 445 return ToStringBuilder.reflectionToString(this); 446 } 447 } 448 449 452 static class ReflectionTestCycleB { 453 ReflectionTestCycleA a; 454 455 public String toString() { 456 return ToStringBuilder.reflectionToString(this); 457 } 458 } 459 460 463 static class SimpleReflectionTestFixture { 464 Object o; 465 466 public SimpleReflectionTestFixture() { 467 } 468 469 public SimpleReflectionTestFixture(Object o) { 470 this.o = o; 471 } 472 473 public String toString() { 474 return ToStringBuilder.reflectionToString(this); 475 } 476 } 477 478 private static class SelfInstanceVarReflectionTestFixture { 479 private SelfInstanceVarReflectionTestFixture typeIsSelf; 480 481 public SelfInstanceVarReflectionTestFixture() { 482 this.typeIsSelf = this; 483 } 484 485 public String toString() { 486 return ToStringBuilder.reflectionToString(this); 487 } 488 } 489 490 private static class SelfInstanceTwoVarsReflectionTestFixture { 491 private SelfInstanceTwoVarsReflectionTestFixture typeIsSelf; 492 private String otherType = "The Other Type"; 493 494 public SelfInstanceTwoVarsReflectionTestFixture() { 495 this.typeIsSelf = this; 496 } 497 498 public String getOtherType(){ 499 return this.otherType; 500 } 501 502 public String toString() { 503 return ToStringBuilder.reflectionToString(this); 504 } 505 } 506 507 508 513 public void testSimpleReflectionObjectCycle() throws Exception { 514 SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture(); 515 simple.o = simple; 516 assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty()); 517 assertEquals(this.toBaseString(simple) + "[o=" + this.toBaseString(simple) + "]", simple.toString()); 518 this.validateEmptyReflectionRegistry(); 519 } 520 521 526 public void testSelfInstanceVarReflectionObjectCycle() throws Exception { 527 SelfInstanceVarReflectionTestFixture test = new SelfInstanceVarReflectionTestFixture(); 528 assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty()); 529 assertEquals(this.toBaseString(test) + "[typeIsSelf=" + this.toBaseString(test) + "]", test.toString()); 530 this.validateEmptyReflectionRegistry(); 531 } 532 533 539 public void testSelfInstanceTwoVarsReflectionObjectCycle() throws Exception { 540 SelfInstanceTwoVarsReflectionTestFixture test = new SelfInstanceTwoVarsReflectionTestFixture(); 541 assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty()); 542 assertEquals(this.toBaseString(test) + "[typeIsSelf=" + this.toBaseString(test) + ",otherType=" + test.getOtherType().toString() + "]", test.toString()); 543 this.validateEmptyReflectionRegistry(); 544 } 545 546 547 552 public void testReflectionObjectCycle() throws Exception { 553 ReflectionTestCycleA a = new ReflectionTestCycleA(); 554 ReflectionTestCycleB b = new ReflectionTestCycleB(); 555 a.b = b; 556 b.a = a; 557 assertEquals( 558 this.toBaseString(a) + "[b=" + this.toBaseString(b) + "[a=" + this.toBaseString(a) + "]]", 559 a.toString()); 560 this.validateEmptyReflectionRegistry(); 561 } 562 563 569 public void testReflectionArrayAndObjectCycle() throws Exception { 570 Object [] objects = new Object [1]; 571 SimpleReflectionTestFixture simple = new SimpleReflectionTestFixture(objects); 572 objects[0] = (Object ) simple; 573 assertEquals( 574 this.toBaseString(objects) 575 + "[{" 576 + this.toBaseString(simple) 577 + "[o=" 578 + this.toBaseString(objects) 579 + "]" 580 + "}]", 581 ToStringBuilder.reflectionToString(objects)); 582 assertEquals( 583 this.toBaseString(simple) 584 + "[o={" 585 + this.toBaseString(simple) 586 + "}]", 587 ToStringBuilder.reflectionToString(simple)); 588 this.validateEmptyReflectionRegistry(); 589 } 590 591 void validateEmptyReflectionRegistry() { 592 assertTrue(ReflectionToStringBuilder.getRegistry().isEmpty()); 593 } 594 596 public void testAppendSuper() { 597 assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString()); 598 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString()); 599 600 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString()); 601 assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").append("a", "hello").toString()); 602 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString()); 603 } 604 605 public void testAppendToString() { 606 assertEquals(baseStr + "[]", new ToStringBuilder(base).appendToString("Integer@8888[]").toString()); 607 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendToString("Integer@8888[<null>]").toString()); 608 609 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendToString("Integer@8888[]").append("a", "hello").toString()); 610 assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(base).appendToString("Integer@8888[<null>]").append("a", "hello").toString()); 611 assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendToString(null).append("a", "hello").toString()); 612 } 613 614 public void testObject() { 615 Integer i3 = new Integer (3); 616 Integer i4 = new Integer (4); 617 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) null).toString()); 618 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString()); 619 assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base).append("a", (Object ) null).toString()); 620 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", i3).toString()); 621 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString()); 622 assertEquals(baseStr + "[a=<Integer>]", new ToStringBuilder(base).append("a", i3, false).toString()); 623 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new ArrayList (), false).toString()); 624 assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new ArrayList (), true).toString()); 625 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", new HashMap (), false).toString()); 626 assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new HashMap (), true).toString()); 627 assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a", (Object ) new String [0], false).toString()); 628 assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", (Object ) new String [0], true).toString()); 629 } 630 631 public void testLong() { 632 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString()); 633 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", 3L).toString()); 634 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString()); 635 } 636 637 public void testInt() { 638 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((int) 3).toString()); 639 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (int) 3).toString()); 640 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (int) 3).append("b", (int) 4).toString()); 641 } 642 643 public void testShort() { 644 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((short) 3).toString()); 645 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (short) 3).toString()); 646 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (short) 3).append("b", (short) 4).toString()); 647 } 648 649 public void testChar() { 650 assertEquals(baseStr + "[A]", new ToStringBuilder(base).append((char) 65).toString()); 651 assertEquals(baseStr + "[a=A]", new ToStringBuilder(base).append("a", (char) 65).toString()); 652 assertEquals(baseStr + "[a=A,b=B]", new ToStringBuilder(base).append("a", (char) 65).append("b", (char) 66).toString()); 653 } 654 655 public void testByte() { 656 assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((byte) 3).toString()); 657 assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (byte) 3).toString()); 658 assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (byte) 3).append("b", (byte) 4).toString()); 659 } 660 661 public void testDouble() { 662 assertEquals(baseStr + "[3.2]", new ToStringBuilder(base).append((double) 3.2).toString()); 663 assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append("a", (double) 3.2).toString()); 664 assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append("a", (double) 3.2).append("b", (double) 4.3).toString()); 665 } 666 667 public void testFloat() { 668 assertEquals(baseStr + "[3.2]", new ToStringBuilder(base).append((float) 3.2).toString()); 669 assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append("a", (float) 3.2).toString()); 670 assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append("a", (float) 3.2).append("b", (float) 4.3).toString()); 671 } 672 673 public void testBoolean() { 674 assertEquals(baseStr + "[true]", new ToStringBuilder(base).append(true).toString()); 675 assertEquals(baseStr + "[a=true]", new ToStringBuilder(base).append("a", true).toString()); 676 assertEquals(baseStr + "[a=true,b=false]", new ToStringBuilder(base).append("a", true).append("b", false).toString()); 677 } 678 679 680 public void testObjectArray() { 681 Object [] array = new Object [] {null, base, new int[] {3, 6}}; 682 assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(array).toString()); 683 assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append((Object ) array).toString()); 684 array = null; 685 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 686 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 687 } 688 689 public void testLongArray() { 690 long[] array = new long[] {1, 2, -3, 4}; 691 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString()); 692 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object ) array).toString()); 693 array = null; 694 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 695 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 696 } 697 698 public void testIntArray() { 699 int[] array = new int[] {1, 2, -3, 4}; 700 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString()); 701 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object ) array).toString()); 702 array = null; 703 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 704 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 705 } 706 707 public void testShortArray() { 708 short[] array = new short[] {1, 2, -3, 4}; 709 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString()); 710 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object ) array).toString()); 711 array = null; 712 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 713 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 714 } 715 716 public void testByteArray() { 717 byte[] array = new byte[] {1, 2, -3, 4}; 718 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString()); 719 assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object ) array).toString()); 720 array = null; 721 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 722 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 723 } 724 725 public void testCharArray() { 726 char[] array = new char[] {'A', '2', '_', 'D'}; 727 assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append(array).toString()); 728 assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append((Object ) array).toString()); 729 array = null; 730 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 731 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 732 } 733 734 public void testDoubleArray() { 735 double[] array = new double[] {1.0, 2.9876, -3.00001, 4.3}; 736 assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(array).toString()); 737 assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append((Object ) array).toString()); 738 array = null; 739 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 740 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 741 } 742 743 public void testFloatArray() { 744 float[] array = new float[] {1.0f, 2.9876f, -3.00001f, 4.3f}; 745 assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(array).toString()); 746 assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append((Object ) array).toString()); 747 array = null; 748 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 749 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 750 } 751 752 public void testBooleanArray() { 753 boolean[] array = new boolean[] {true, false, false}; 754 assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append(array).toString()); 755 assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append((Object ) array).toString()); 756 array = null; 757 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 758 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 759 } 760 761 public void testLongArrayArray() { 762 long[][] array = new long[][] {{1, 2}, null, {5}}; 763 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString()); 764 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object ) array).toString()); 765 array = null; 766 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 767 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 768 } 769 770 public void testIntArrayArray() { 771 int[][] array = new int[][] {{1, 2}, null, {5}}; 772 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString()); 773 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object ) array).toString()); 774 array = null; 775 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 776 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 777 } 778 779 public void testShortArrayArray() { 780 short[][] array = new short[][] {{1, 2}, null, {5}}; 781 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString()); 782 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object ) array).toString()); 783 array = null; 784 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 785 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 786 } 787 788 public void testByteArrayArray() { 789 byte[][] array = new byte[][] {{1, 2}, null, {5}}; 790 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString()); 791 assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object ) array).toString()); 792 array = null; 793 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 794 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 795 } 796 797 public void testCharArrayArray() { 798 char[][] array = new char[][] {{'A', 'B'}, null, {'p'}}; 799 assertEquals(baseStr + "[{{A,B},<null>,{p}}]", new ToStringBuilder(base).append(array).toString()); 800 assertEquals(baseStr + "[{{A,B},<null>,{p}}]", new ToStringBuilder(base).append((Object ) array).toString()); 801 array = null; 802 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 803 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 804 } 805 806 public void testDoubleArrayArray() { 807 double[][] array = new double[][] {{1.0, 2.29686}, null, {Double.NaN}}; 808 assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append(array).toString()); 809 assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append((Object ) array).toString()); 810 array = null; 811 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 812 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 813 } 814 815 public void testFloatArrayArray() { 816 float[][] array = new float[][] {{1.0f, 2.29686f}, null, {Float.NaN}}; 817 assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append(array).toString()); 818 assertEquals(baseStr + "[{{1.0,2.29686},<null>,{NaN}}]", new ToStringBuilder(base).append((Object ) array).toString()); 819 array = null; 820 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 821 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 822 } 823 824 public void testBooleanArrayArray() { 825 boolean[][] array = new boolean[][] {{true, false}, null, {false}}; 826 assertEquals(baseStr + "[{{true,false},<null>,{false}}]", new ToStringBuilder(base).append(array).toString()); 827 assertEquals(baseStr + "[{{true,false},<null>,{false}}]", new ToStringBuilder(base).append((Object ) array).toString()); 828 array = null; 829 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString()); 830 assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object ) array).toString()); 831 } 832 833 public void testSimpleReflectionStatics() { 834 SimpleReflectionStaticFieldsFixture instance1 = new SimpleReflectionStaticFieldsFixture(); 835 assertEquals( 836 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", 837 ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class)); 838 assertEquals( 839 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", 840 ReflectionToStringBuilder.toString(instance1, null, true, true, SimpleReflectionStaticFieldsFixture.class)); 841 assertEquals( 842 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", 843 this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class)); 844 assertEquals( 845 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345]", 846 this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class)); 847 } 848 849 852 public void testReflectionStatics() { 853 ReflectionStaticFieldsFixture instance1 = new ReflectionStaticFieldsFixture(); 854 assertEquals( 855 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]", 856 ReflectionToStringBuilder.toString(instance1, null, false, true, ReflectionStaticFieldsFixture.class)); 857 assertEquals( 858 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,staticTransientString=staticTransientString,staticTransientInt=54321,instanceString=instanceString,instanceInt=67890,transientString=transientString,transientInt=98765]", 859 ReflectionToStringBuilder.toString(instance1, null, true, true, ReflectionStaticFieldsFixture.class)); 860 assertEquals( 861 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]", 862 this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class)); 863 assertEquals( 864 this.toBaseString(instance1) + "[staticString=staticString,staticInt=12345,instanceString=instanceString,instanceInt=67890]", 865 this.toStringWithStatics(instance1, null, ReflectionStaticFieldsFixture.class)); 866 } 867 868 871 public void testInheritedReflectionStatics() { 872 InheritedReflectionStaticFieldsFixture instance1 = new InheritedReflectionStaticFieldsFixture(); 873 assertEquals( 874 this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890]", 875 ReflectionToStringBuilder.toString(instance1, null, false, true, InheritedReflectionStaticFieldsFixture.class)); 876 assertEquals( 877 this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]", 878 ReflectionToStringBuilder.toString(instance1, null, false, true, SimpleReflectionStaticFieldsFixture.class)); 879 assertEquals( 880 this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]", 881 this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class)); 882 assertEquals( 883 this.toBaseString(instance1) + "[staticString2=staticString2,staticInt2=67890,staticString=staticString,staticInt=12345]", 884 this.toStringWithStatics(instance1, null, SimpleReflectionStaticFieldsFixture.class)); 885 } 886 887 912 public Object toStringWithStatics(Object object, ToStringStyle style, Class reflectUpToClass) { 913 return ReflectionToStringBuilder.toString(object, style, false, true, reflectUpToClass); 914 } 915 916 919 class ReflectionStaticFieldsFixture { 920 static final String staticString = "staticString"; 921 static final int staticInt = 12345; 922 static final transient String staticTransientString = "staticTransientString"; 923 static final transient int staticTransientInt = 54321; 924 String instanceString = "instanceString"; 925 int instanceInt = 67890; 926 transient String transientString = "transientString"; 927 transient int transientInt = 98765; 928 } 929 930 933 class SimpleReflectionStaticFieldsFixture { 934 static final String staticString = "staticString"; 935 static final int staticInt = 12345; 936 } 937 938 941 class InheritedReflectionStaticFieldsFixture extends SimpleReflectionStaticFieldsFixture { 942 static final String staticString2 = "staticString2"; 943 static final int staticInt2 = 67890; 944 } 945 946 public void testReflectionNull() { 947 assertEquals("<null>", ReflectionToStringBuilder.toString(null)); 948 } 949 } 950 | Popular Tags |