1 26 27 package net.sourceforge.groboutils.junit.v1; 28 29 import org.apache.log4j.Logger; 30 import junit.framework.TestCase; 31 import junit.framework.TestResult; 32 import junit.framework.AssertionFailedError; 33 import junit.framework.Assert; 34 35 36 56 public class AssertTestFactory 57 { 58 62 public static abstract class InnerTest extends TestCase 63 { 64 private String message; 65 66 71 public InnerTest( String name, String msg ) 72 { 73 super( name ); 74 this.message = msg; 75 } 76 77 public void setMessage( String msg ) 78 { 79 this.message = msg; 80 } 81 82 public String getMessage() 83 { 84 return this.message; 85 } 86 87 public String getFullMessage() 88 { 89 String msg = getMessage(); 90 String name = this.getName(); 91 if (name != null) 92 { 93 msg = name + ": " + msg; 94 } 95 return msg; 96 } 97 98 protected final void runTest() 99 { 100 callAssert( getFullMessage() ); 101 } 102 103 public abstract void callAssert( String message ); 104 } 105 106 107 private String name; 108 private int index = 0; 109 private boolean useIndex = false; 110 111 112 116 public AssertTestFactory() 117 { 118 } 120 121 122 128 public AssertTestFactory( String name ) 129 { 130 this( name, false ); 131 } 132 133 134 144 public AssertTestFactory( String name, boolean useIndexWithName ) 145 { 146 setName( name ); 147 setUseIndexWithName( useIndexWithName ); 148 } 149 150 151 156 public void setName( String name ) 157 { 158 this.name = name; 159 } 160 161 162 168 public String getName() 169 { 170 return this.name; 171 } 172 173 174 184 public void setUseIndexWithName( boolean useIndexWithName ) 185 { 186 this.useIndex = useIndexWithName; 187 } 188 189 190 198 public boolean getUseIndexWithName() 199 { 200 return this.useIndex; 201 } 202 203 204 205 207 private static class AssertTrue1 extends InnerTest 208 { 209 boolean condition; 210 public AssertTrue1( String n, String m, boolean c ) 211 { 212 super( n, m ); 213 this.condition = c; 214 } 215 public void callAssert( String msg ) 216 { 217 Assert.assertTrue( msg, this.condition ); 218 } 219 } 220 221 222 230 public InnerTest createAssertTrue( String message, boolean condition ) 231 { 232 return new AssertTrue1( getNextTestName(), message, condition ); 233 } 234 235 236 242 public InnerTest createAssertTrue( boolean condition ) 243 { 244 return createAssertTrue( null, condition ); 245 } 246 247 248 257 public InnerTest createAssertFalse( String message, boolean condition ) 258 { 259 return new AssertTrue1( getNextTestName(), message, !condition ); 260 } 261 262 263 270 public InnerTest createAssertFalse( boolean condition ) 271 { 272 return createAssertFalse( null, condition ); 275 } 276 277 279 private static class Fail1 extends InnerTest 280 { 281 public Fail1( String n, String m ) 282 { 283 super( n, m ); 284 } 285 public void callAssert( String msg ) 286 { 287 Assert.fail( msg ); 288 } 289 } 290 291 292 298 public InnerTest createFail( String message ) 299 { 300 return new Fail1( getNextTestName(), message ); 301 } 302 303 304 307 public InnerTest createFail() 308 { 309 return createFail( null ); 310 } 311 312 313 315 316 private static class AssertEquals1 extends InnerTest 317 { 318 Object expected; 319 Object actual; 320 public AssertEquals1( String n, String m, Object e, Object a ) 321 { 322 super( n, m ); 323 this.expected = e; 324 this.actual = a; 325 } 326 public void callAssert( String msg ) 327 { 328 Assert.assertEquals( msg, this.expected, this.actual ); 329 } 330 } 331 332 333 343 public InnerTest createAssertEquals( String message, Object expected, 344 Object actual ) 345 { 346 return new AssertEquals1( getNextTestName(), message, expected, actual ); 347 } 348 349 350 358 public InnerTest createAssertEquals( Object expected, Object actual ) 359 { 360 return createAssertEquals(null, expected, actual); 361 } 362 363 364 375 public InnerTest createAssertEquals( String message, String expected, 376 String actual ) 377 { 378 return new AssertEquals1( getNextTestName(), message, expected, actual ); 379 } 380 381 382 391 public InnerTest createAssertEquals( String expected, String actual ) 392 { 393 return createAssertEquals( null, expected, actual ); 394 } 395 396 398 private static class AssertEquals2 extends InnerTest 399 { 400 double expected; 401 double actual; 402 double delta; 403 public AssertEquals2( String n, String m, double e, double a, 404 double d ) 405 { 406 super( n, m ); 407 this.expected = e; 408 this.actual = a; 409 this.delta = d; 410 } 411 public void callAssert( String msg ) 412 { 413 Assert.assertEquals( msg, this.expected, this.actual, 414 this.delta ); 415 } 416 } 417 418 419 434 public InnerTest createAssertEquals( String message, double expected, 435 double actual, double delta ) 436 { 437 return new AssertEquals2( getNextTestName(), message, expected, actual, 438 delta ); 439 } 440 441 442 455 public InnerTest createAssertEquals( double expected, double actual, 456 double delta ) 457 { 458 return createAssertEquals( null, expected, actual, delta ); 459 } 460 461 462 464 465 private static class AssertEquals3 extends InnerTest 466 { 467 float expected; 468 float actual; 469 float delta; 470 public AssertEquals3( String n, String m, float e, float a, float d ) 471 { 472 super( n, m ); 473 this.expected = e; 474 this.actual = a; 475 this.delta = d; 476 } 477 public void callAssert( String msg ) 478 { 479 Assert.assertEquals( msg, this.expected, this.actual, 480 this.delta ); 481 } 482 } 483 484 485 500 public InnerTest createAssertEquals( String message, float expected, 501 float actual, float delta ) 502 { 503 return new AssertEquals3( getNextTestName(), message, expected, actual, 504 delta ); 505 } 506 507 508 521 public InnerTest createAssertEquals( float expected, float actual, 522 float delta ) 523 { 524 return createAssertEquals( null, expected, actual, delta ); 525 } 526 527 528 537 public InnerTest createAssertEquals( String message, long expected, 538 long actual ) 539 { 540 return createAssertEquals( message, new Long ( expected ), 541 new Long ( actual ) ); 542 } 543 544 545 552 public InnerTest createAssertEquals( long expected, long actual ) 553 { 554 return createAssertEquals( null, expected, actual ); 555 } 556 557 558 567 public InnerTest createAssertEquals( String message, boolean expected, 568 boolean actual ) 569 { 570 return createAssertEquals( message, new Boolean ( expected ), 571 new Boolean ( actual ) ); 572 } 573 574 575 582 public InnerTest createAssertEquals( boolean expected, boolean actual ) 583 { 584 return createAssertEquals( null, expected, actual ); 585 } 586 587 588 597 public InnerTest createAssertEquals( String message, byte expected, 598 byte actual ) 599 { 600 return createAssertEquals( message, new Byte ( expected ), 601 new Byte ( actual ) ); 602 } 603 604 605 612 public InnerTest createAssertEquals( byte expected, byte actual ) 613 { 614 return createAssertEquals( null, expected, actual ); 615 } 616 617 618 627 public InnerTest createAssertEquals( String message, char expected, 628 char actual ) 629 { 630 return createAssertEquals( message, new Character ( expected ), 631 new Character ( actual ) ); 632 } 633 634 635 642 public InnerTest createAssertEquals( char expected, char actual ) 643 { 644 return createAssertEquals( null, expected, actual ); 645 } 646 647 648 657 public InnerTest createAssertEquals( String message, short expected, 658 short actual ) 659 { 660 return createAssertEquals( message, new Short ( expected ), 661 new Short ( actual ) ); 662 } 663 664 665 672 public InnerTest createAssertEquals( short expected, short actual ) 673 { 674 return createAssertEquals( null, expected, actual ); 675 } 676 677 678 687 public InnerTest createAssertEquals( String message, int expected, 688 int actual ) 689 { 690 return createAssertEquals( message, new Integer ( expected ), 691 new Integer ( actual ) ); 692 } 693 694 695 702 public InnerTest createAssertEquals( int expected, int actual ) 703 { 704 return createAssertEquals( null, expected, actual ); 705 } 706 707 708 710 711 712 private static class AssertNotNull1 extends InnerTest 713 { 714 Object object; 715 public AssertNotNull1( String n, String m, Object o ) 716 { 717 super( n, m ); 718 this.object = o; 719 } 720 public void callAssert( String msg ) 721 { 722 Assert.assertNotNull( msg, this.object ); 723 } 724 } 725 726 727 734 public InnerTest createAssertNotNull( String message, Object object ) 735 { 736 return new AssertNotNull1( getNextTestName(), message, object ); 737 } 738 739 740 745 public InnerTest createAssertNotNull( Object object ) 746 { 747 return createAssertNotNull( null, object ); 748 } 749 750 751 753 754 private static class AssertNull1 extends InnerTest 755 { 756 Object object; 757 public AssertNull1( String n, String m, Object o ) 758 { 759 super( n, m ); 760 this.object = o; 761 } 762 public void callAssert( String msg ) 763 { 764 Assert.assertNull( msg, this.object ); 765 } 766 } 767 768 769 776 public InnerTest createAssertNull( String message, Object object ) 777 { 778 return new AssertNull1( getNextTestName(), message, object ); 779 } 780 781 782 787 public InnerTest createAssertNull( Object object ) 788 { 789 return createAssertNull( null, object ); 790 } 791 792 793 795 796 private static class AssertSame1 extends InnerTest 797 { 798 Object expected; 799 Object actual; 800 public AssertSame1( String n, String m, Object e, Object a ) 801 { 802 super( n, m ); 803 this.expected = e; 804 this.actual = a; 805 } 806 public void callAssert( String msg ) 807 { 808 Assert.assertSame( msg, this.expected, this.actual ); 809 } 810 } 811 812 813 823 public InnerTest createAssertSame( String message, Object expected, 824 Object actual ) 825 { 826 return new AssertSame1( getNextTestName(), message, expected, actual ); 827 } 828 829 830 838 public InnerTest createAssertSame( Object expected, Object actual ) 839 { 840 return createAssertSame(null, expected, actual); 841 } 842 843 844 855 public InnerTest createAssertNotSame( String message, Object expected, 856 Object actual ) 857 { 858 String str = "expected not same"; 859 if (message != null) 860 { 861 str = message + ' ' + str; 862 } 863 return new AssertTrue1( getNextTestName(), 865 str, expected != actual ); 866 } 867 868 869 878 public InnerTest createAssertNotSame( Object expected, Object actual ) 879 { 880 return createAssertNotSame( null, expected, actual ); 881 } 882 883 884 891 private String getNextTestName() 892 { 893 String n = getName(); 894 if (this.useIndex) 895 { 896 synchronized( this ) 897 { 898 ++this.index; 899 n += this.index; 900 } 901 } 902 return n; 903 } 904 } 905 906 | Popular Tags |