1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
|
/*
* @(#)AssertTestFactory.java
*
* Copyright (C) 2002-2003 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package net.sourceforge.groboutils.junit.v1;
import org.apache.log4j.Logger;
import junit.framework.TestCase;
import junit.framework.TestResult;
import junit.framework.AssertionFailedError;
import junit.framework.Assert;
/**
* A factory that creates test instances for the standard set of assert
* methods. The created test instances should have their <tt>setName()</tt>
* method invoked to properly set the name of the test. Alternatively, the
* factory instance can have the name set so that all tests will have the
* same name.
* <P>
* To support JUnit 3.8 functionality, but remain backwards compatible with
* earlier JUnit libraries, the names for the JUnit 3.8 methods will be
* allowed, but they will call JUnit 3.7 compatible methods.
* <P>
* As of Dec 8, 2002, the factory can uniquely (per instance) name each
* generated test via an index. This can help traceability in identifying
* each created test. Alternatively, the user can set the factory's name
* before invoking a create method.
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @version $Date: 2004/01/09 20:32:26 $
* @since July 26, 2002
*/
public class AssertTestFactory
{
/**
* Inner test instance which specializes in generating a message with
* the test's instance's specific name.
*/
public static abstract class InnerTest extends TestCase
{
private String message;
/**
* JUnit 3.8 allows for test constructors to not have to specify
* a name in the super() call, but for JUnit 3.7 compatibility,
* we will only support the original usage.
*/
public InnerTest( String name, String msg )
{
super( name );
this.message = msg;
}
public void setMessage( String msg )
{
this.message = msg;
}
public String getMessage()
{
return this.message;
}
public String getFullMessage()
{
String msg = getMessage();
String name = this.getName();
if (name != null)
{
msg = name + ": " + msg;
}
return msg;
}
protected final void runTest()
{
callAssert( getFullMessage() );
}
public abstract void callAssert( String message );
}
private String name;
private int index = 0;
private boolean useIndex = false;
/**
* Creates a new factory that can generate assertions as independent
* test objects.
*/
public AssertTestFactory()
{
// do nothing
}
/**
* Creates a new factory with a specific name for each generated test,
* but will not add an index to each generated test's name.
*
* @param name default name shared by all generated assertion tests.
*/
public AssertTestFactory( String name )
{
this( name, false );
}
/**
* Creates a new factory with a specific name for each generated test,
* and can optionally add an index to each generated test's name.
*
* @param name default name shared by all generated assertion tests.
* @param useIndexWithName <tt>true</tt> if indecies will be appended
* to each generated test's name, or <tt>false</tt> if they
* will use the passed-in name exactly.
* @since 08-Dec-2002
*/
public AssertTestFactory( String name, boolean useIndexWithName )
{
setName( name );
setUseIndexWithName( useIndexWithName );
}
/**
* Sets the default test name. This will not reset the generated index.
*
* @param name default name shared by all generated assertion tests.
*/
public void setName( String name )
{
this.name = name;
}
/**
* Returns the default test name. If the name has never been set, then
* this will return <tt>null</tt>.
*
* @return default name shared by all generated assertion tests.
*/
public String getName()
{
return this.name;
}
/**
* Sets whether each generated test will add a unique (for this instance)
* index to the test's name. Reseting this value will not affect the
* index's value.
*
* @param useIndexWithName <tt>true</tt> if indecies will be appended
* to each generated test's name, or <tt>false</tt> if they
* will use the passed-in name exactly.
* @since 08-Dec-2002
*/
public void setUseIndexWithName( boolean useIndexWithName )
{
this.useIndex = useIndexWithName;
}
/**
* Returns whether each generated test will add a unique (for this
* instance) index to the test's name.
*
* @return <tt>true</tt> if an index is appended to the name, or
* <tt>false</tt> if the test's name is exactly the factory's name.
* @since 08-Dec-2002
*/
public boolean getUseIndexWithName()
{
return this.useIndex;
}
//-----------------------------------------------------------------------
private static class AssertTrue1 extends InnerTest
{
boolean condition;
public AssertTrue1( String n, String m, boolean c )
{
super( n, m );
this.condition = c;
}
public void callAssert( String msg )
{
Assert.assertTrue( msg, this.condition );
}
}
/**
* Asserts that a condition is true. If it isn't it throws an
* AssertionFailedError with the given message.
*
* @param message message that describes what failed if the assertion
* fails.
* @param condition boolean to check for failure
*/
public InnerTest createAssertTrue( String message, boolean condition )
{
return new AssertTrue1( getNextTestName(), message, condition );
}
/**
* Asserts that a condition is true. If it isn't it throws an
* AssertionFailedError.
*
* @param condition boolean to check for failure
*/
public InnerTest createAssertTrue( boolean condition )
{
return createAssertTrue( null, condition );
}
/**
* Asserts that a condition is false. If it isn't it throws an
* AssertionFailedError with the given message.
*
* @since 30-Oct-2002
* @param message message that describes what failed if the assertion
* fails.
* @param condition boolean to check for failure
*/
public InnerTest createAssertFalse( String message, boolean condition )
{
return new AssertTrue1( getNextTestName(), message, !condition );
}
/**
* Asserts that a condition is true. If it isn't it throws an
* AssertionFailedError.
*
* @since 30-Oct-2002
* @param condition boolean to check for failure
*/
public InnerTest createAssertFalse( boolean condition )
{
// don't 'not' the condition here - it will be done in the
// invoked method.
return createAssertFalse( null, condition );
}
//-----------------------------------------------------------------------
private static class Fail1 extends InnerTest
{
public Fail1( String n, String m )
{
super( n, m );
}
public void callAssert( String msg )
{
Assert.fail( msg );
}
}
/**
* Fails a test with the given message.
*
* @param message message that describes what failed if the assertion
* fails.
*/
public InnerTest createFail( String message )
{
return new Fail1( getNextTestName(), message );
}
/**
* Fails a test with no message.
*/
public InnerTest createFail()
{
return createFail( null );
}
//-----------------------------------------------------------------------
private static class AssertEquals1 extends InnerTest
{
Object expected;
Object actual;
public AssertEquals1( String n, String m, Object e, Object a )
{
super( n, m );
this.expected = e;
this.actual = a;
}
public void callAssert( String msg )
{
Assert.assertEquals( msg, this.expected, this.actual );
}
}
/**
* Asserts that two objects are equal. If they are not an
* AssertionFailedError is thrown.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, Object expected,
Object actual )
{
return new AssertEquals1( getNextTestName(), message, expected, actual );
}
/**
* Asserts that two objects are equal. If they are not an
* AssertionFailedError is thrown.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( Object expected, Object actual )
{
return createAssertEquals(null, expected, actual);
}
/**
* Asserts that two objects are equal. If they are not an
* AssertionFailedError is thrown.
*
* @since 30-Oct-2002
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, String expected,
String actual )
{
return new AssertEquals1( getNextTestName(), message, expected, actual );
}
/**
* Asserts that two objects are equal. If they are not an
* AssertionFailedError is thrown.
*
* @since 30-Oct-2002
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String expected, String actual )
{
return createAssertEquals( null, expected, actual );
}
//-----------------------------------------------------------------------
private static class AssertEquals2 extends InnerTest
{
double expected;
double actual;
double delta;
public AssertEquals2( String n, String m, double e, double a,
double d )
{
super( n, m );
this.expected = e;
this.actual = a;
this.delta = d;
}
public void callAssert( String msg )
{
Assert.assertEquals( msg, this.expected, this.actual,
this.delta );
}
}
/**
* Asserts that two doubles are equal concerning a delta. If the expected
* value is infinity then the delta value is ignored.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
* @param delta maximum distance between expected and actual such that
* the two values are considered equivalent. Necessary since
* floating-point numbers on computers are approximations of their
* equivalent values; that is, storing <tt>1.1</tt> may actually be
* stored as <tt>1.099999999999</tt>.
*/
public InnerTest createAssertEquals( String message, double expected,
double actual, double delta )
{
return new AssertEquals2( getNextTestName(), message, expected, actual,
delta );
}
/**
* Asserts that two doubles are equal concerning a delta. If the expected
* value is infinity then the delta value is ignored.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
* @param delta maximum distance between expected and actual such that
* the two values are considered equivalent. Necessary since
* floating-point numbers on computers are approximations of their
* equivalent values; that is, storing <tt>1.1</tt> may actually be
* stored as <tt>1.099999999999</tt>.
*/
public InnerTest createAssertEquals( double expected, double actual,
double delta )
{
return createAssertEquals( null, expected, actual, delta );
}
//-----------------------------------------------------------------------
private static class AssertEquals3 extends InnerTest
{
float expected;
float actual;
float delta;
public AssertEquals3( String n, String m, float e, float a, float d )
{
super( n, m );
this.expected = e;
this.actual = a;
this.delta = d;
}
public void callAssert( String msg )
{
Assert.assertEquals( msg, this.expected, this.actual,
this.delta );
}
}
/**
* Asserts that two floats are equal concerning a delta. If the expected
* value is infinity then the delta value is ignored.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
* @param delta maximum distance between expected and actual such that
* the two values are considered equivalent. Necessary since
* floating-point numbers on computers are approximations of their
* equivalent values; that is, storing <tt>1.1</tt> may actually be
* stored as <tt>1.099999999999</tt>.
*/
public InnerTest createAssertEquals( String message, float expected,
float actual, float delta )
{
return new AssertEquals3( getNextTestName(), message, expected, actual,
delta );
}
/**
* Asserts that two floats are equal concerning a delta. If the expected
* value is infinity then the delta value is ignored.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
* @param delta maximum distance between expected and actual such that
* the two values are considered equivalent. Necessary since
* floating-point numbers on computers are approximations of their
* equivalent values; that is, storing <tt>1.1</tt> may actually be
* stored as <tt>1.099999999999</tt>.
*/
public InnerTest createAssertEquals( float expected, float actual,
float delta )
{
return createAssertEquals( null, expected, actual, delta );
}
/**
* Asserts that two longs are equal.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, long expected,
long actual )
{
return createAssertEquals( message, new Long( expected ),
new Long( actual ) );
}
/**
* Asserts that two longs are equal.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( long expected, long actual )
{
return createAssertEquals( null, expected, actual );
}
/**
* Asserts that two booleans are equal.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, boolean expected,
boolean actual )
{
return createAssertEquals( message, new Boolean( expected ),
new Boolean( actual ) );
}
/**
* Asserts that two booleans are equal.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( boolean expected, boolean actual )
{
return createAssertEquals( null, expected, actual );
}
/**
* Asserts that two bytes are equal.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, byte expected,
byte actual )
{
return createAssertEquals( message, new Byte( expected ),
new Byte( actual ) );
}
/**
* Asserts that two bytes are equal.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( byte expected, byte actual )
{
return createAssertEquals( null, expected, actual );
}
/**
* Asserts that two chars are equal.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, char expected,
char actual )
{
return createAssertEquals( message, new Character( expected ),
new Character( actual ) );
}
/**
* Asserts that two chars are equal.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( char expected, char actual )
{
return createAssertEquals( null, expected, actual );
}
/**
* Asserts that two shorts are equal.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, short expected,
short actual )
{
return createAssertEquals( message, new Short( expected ),
new Short( actual ) );
}
/**
* Asserts that two shorts are equal.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( short expected, short actual )
{
return createAssertEquals( null, expected, actual );
}
/**
* Asserts that two ints are equal.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( String message, int expected,
int actual )
{
return createAssertEquals( message, new Integer( expected ),
new Integer( actual ) );
}
/**
* Asserts that two ints are equal.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertEquals( int expected, int actual )
{
return createAssertEquals( null, expected, actual );
}
//-----------------------------------------------------------------------
private static class AssertNotNull1 extends InnerTest
{
Object object;
public AssertNotNull1( String n, String m, Object o )
{
super( n, m );
this.object = o;
}
public void callAssert( String msg )
{
Assert.assertNotNull( msg, this.object );
}
}
/**
* Asserts that an object isn't null.
*
* @param message message that describes what failed if the assertion
* fails.
* @param object test object that must not be null.
*/
public InnerTest createAssertNotNull( String message, Object object )
{
return new AssertNotNull1( getNextTestName(), message, object );
}
/**
* Asserts that an object isn't null.
*
* @param object test object that must not be null.
*/
public InnerTest createAssertNotNull( Object object )
{
return createAssertNotNull( null, object );
}
//-----------------------------------------------------------------------
private static class AssertNull1 extends InnerTest
{
Object object;
public AssertNull1( String n, String m, Object o )
{
super( n, m );
this.object = o;
}
public void callAssert( String msg )
{
Assert.assertNull( msg, this.object );
}
}
/**
* Asserts that an object is null.
*
* @param message message that describes what failed if the assertion
* fails.
* @param object test object that must be null.
*/
public InnerTest createAssertNull( String message, Object object )
{
return new AssertNull1( getNextTestName(), message, object );
}
/**
* Asserts that an object is null.
*
* @param object test object that must be null.
*/
public InnerTest createAssertNull( Object object )
{
return createAssertNull( null, object );
}
//-----------------------------------------------------------------------
private static class AssertSame1 extends InnerTest
{
Object expected;
Object actual;
public AssertSame1( String n, String m, Object e, Object a )
{
super( n, m );
this.expected = e;
this.actual = a;
}
public void callAssert( String msg )
{
Assert.assertSame( msg, this.expected, this.actual );
}
}
/**
* Asserts that two objects refer to the same object. If they are not an
* AssertionFailedError is thrown.
*
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertSame( String message, Object expected,
Object actual )
{
return new AssertSame1( getNextTestName(), message, expected, actual );
}
/**
* Asserts that two objects refer to the same object. If they are not the
* same an AssertionFailedError is thrown.
*
* @param expected value that the test expects to find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertSame( Object expected, Object actual )
{
return createAssertSame(null, expected, actual);
}
/**
* Asserts that two objects refer to the same object. If they are not an
* AssertionFailedError is thrown.
*
* @since 30-Oct-2002
* @param message message that describes what failed if the assertion
* fails.
* @param expected value that the test expects to not find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertNotSame( String message, Object expected,
Object actual )
{
String str = "expected not same";
if (message != null)
{
str = message + ' ' + str;
}
// manual emulation of JUnit 3.8 functionality
return new AssertTrue1( getNextTestName(),
str, expected != actual );
}
/**
* Asserts that two objects refer to the same object. If they are not the
* same an AssertionFailedError is thrown.
*
* @since 30-Oct-2002
* @param expected value that the test expects to not find from the tested
* code.
* @param actual actual value generated by tested code.
*/
public InnerTest createAssertNotSame( Object expected, Object actual )
{
return createAssertNotSame( null, expected, actual );
}
/**
* Generates the next test's name.
*
* @return the next name for a generated test, possibly appending an
* index value.
* @since 08-Dec-2002
*/
private String getNextTestName()
{
String n = getName();
if (this.useIndex)
{
synchronized( this )
{
++this.index;
n += this.index;
}
}
return n;
}
}
|