1 26 27 package net.sourceforge.groboutils.junit.v1; 28 29 import junit.framework.AssertionFailedError; 30 import junit.framework.Assert; 31 32 import java.lang.reflect.Constructor ; 33 import java.lang.reflect.Modifier ; 34 35 36 44 public class AssertConstructor 45 { 46 49 public static final int PUBLIC = 0x01; 50 51 54 public static final int PROTECTED = 0x02; 55 56 59 public static final int PACKAGE = 0x04; 60 61 64 public static final int PRIVATE = 0x08; 65 66 69 public static final int ANY_PROTECTION = 70 PUBLIC | PROTECTED | PACKAGE | PRIVATE; 71 72 73 81 public static void assertHasDefaultConstructor( String message, Class c ) 82 { 83 assertHasSameConstructor( message, c, new Class [0] ); 84 } 85 86 87 93 public static void assertHasDefaultConstructor( Class c ) 94 { 95 assertHasSameConstructor( c, new Class [0] ); 96 } 97 98 99 107 public static void assertHasDefaultConstructor( String message, Object o ) 108 { 109 assertHasSameConstructor( message, o, new Class [0] ); 110 } 111 112 113 119 public static void assertHasDefaultConstructor( Object o ) 120 { 121 assertHasSameConstructor( o, new Class [0] ); 122 } 123 124 125 126 149 public static void assertHasConstructor( String message, 150 Class c, Class [] arguments, int protection ) 151 { 152 Assert.assertNotNull( "Null class argument.", c ); 153 154 Assert.assertNotNull( message, 155 getConstructor( c, arguments, protection ) ); 156 } 157 158 159 179 public static void assertHasConstructor( 180 Class c, Class [] arguments, int protection ) 181 { 182 Assert.assertNotNull( "Null class argument.", c ); 183 184 Assert.assertNotNull( getConstructor( c, arguments, protection ) ); 185 } 186 187 188 205 public static void assertHasConstructor( String message, 206 Class c, Class [] arguments ) 207 { 208 assertHasConstructor( message, c, arguments, PUBLIC ); 209 } 210 211 212 229 public static void assertHasConstructor( 230 Class c, Class [] arguments ) 231 { 232 assertHasConstructor( c, arguments, PUBLIC ); 233 } 234 235 236 256 public static void assertHasConstructor( String message, 257 Object o, Class [] arguments, int protection ) 258 { 259 Assert.assertNotNull( "Null object arguments.", o ); 260 assertHasConstructor( message, o.getClass(), arguments, protection ); 261 } 262 263 264 284 public static void assertHasConstructor( 285 Object o, Class [] arguments, int protection ) 286 { 287 Assert.assertNotNull( "Null object arguments.", o ); 288 assertHasConstructor( o.getClass(), arguments, protection ); 289 } 290 291 292 309 public static void assertHasConstructor( String message, 310 Object o, Class [] arguments ) 311 { 312 Assert.assertNotNull( "Null object arguments.", o ); 313 assertHasConstructor( message, o.getClass(), arguments ); 314 } 315 316 317 334 public static void assertHasConstructor( 335 Object o, Class [] arguments ) 336 { 337 Assert.assertNotNull( "Null object arguments.", o ); 338 assertHasConstructor( o.getClass(), arguments ); 339 } 340 341 342 344 345 365 public static void assertHasSameConstructor( String message, 366 Class c, Class [] arguments, int protection ) 367 { 368 Assert.assertNotNull( "Null class argument.", c ); 369 370 Assert.assertNotNull( message, 371 getSameConstructor( c, arguments, protection ) ); 372 } 373 374 375 395 public static void assertHasSameConstructor( 396 Class c, Class [] arguments, int protection ) 397 { 398 Assert.assertNotNull( "Null class argument.", c ); 399 400 Assert.assertNotNull( getSameConstructor( c, arguments, protection ) ); 401 } 402 403 404 421 public static void assertHasSameConstructor( String message, 422 Class c, Class [] arguments ) 423 { 424 assertHasSameConstructor( message, c, arguments, PUBLIC ); 425 } 426 427 428 445 public static void assertHasSameConstructor( 446 Class c, Class [] arguments ) 447 { 448 assertHasSameConstructor( c, arguments, PUBLIC ); 449 } 450 451 452 472 public static void assertHasSameConstructor( String message, 473 Object o, Class [] arguments, int protection ) 474 { 475 Assert.assertNotNull( "Null object arguments.", o ); 476 assertHasSameConstructor( message, o.getClass(), arguments, 477 protection ); 478 } 479 480 481 501 public static void assertHasSameConstructor( 502 Object o, Class [] arguments, int protection ) 503 { 504 Assert.assertNotNull( "Null object arguments.", o ); 505 assertHasSameConstructor( o.getClass(), arguments, protection ); 506 } 507 508 509 526 public static void assertHasSameConstructor( String message, 527 Object o, Class [] arguments ) 528 { 529 Assert.assertNotNull( "Null object arguments.", o ); 530 assertHasSameConstructor( message, o.getClass(), arguments ); 531 } 532 533 534 551 public static void assertHasSameConstructor( 552 Object o, Class [] arguments ) 553 { 554 Assert.assertNotNull( "Null object arguments.", o ); 555 assertHasSameConstructor( o.getClass(), arguments ); 556 } 557 558 559 578 public static Constructor getConstructor( Class c, Class [] arguments, 579 int protection ) 580 { 581 Assert.assertNotNull( "Null class argument.", c ); 582 Constructor [] cntrs = c.getConstructors(); 583 584 for (int i = 0; i < cntrs.length; ++i) 585 { 586 if ( hasCorrectProtection( cntrs[i], protection ) 587 && isInheritedParameters( cntrs[i], arguments ) ) 588 { 589 return cntrs[i]; 590 } 591 } 592 593 return null; 594 } 595 596 597 607 public static Constructor getSameConstructor( Class c, Class [] arguments, 608 int protection ) 609 { 610 Assert.assertNotNull( "Null class argument.", c ); 611 try 612 { 613 Constructor cntr = c.getConstructor( arguments ); 614 if (cntr != null && hasCorrectProtection( cntr, protection )) 615 { 616 return cntr; 617 } 618 } 619 catch (NoSuchMethodException nsme) 620 { 621 } 623 return null; 624 } 625 626 627 628 629 protected static boolean isInheritedParameters( Constructor cntr, 630 Class [] arguments ) 631 { 632 Class [] params = cntr.getParameterTypes(); 633 if ( arguments == null ) 634 { 635 return ( params.length == 0 ); 637 } 638 if ( params.length != arguments.length ) 639 { 640 return false; 641 } 642 643 for (int i = 0; i < params.length; ++i) 644 { 645 if ( arguments[ i ] != null ) 647 { 648 if ( !arguments[ i ].isAssignableFrom( params[ i ] ) ) 649 { 650 return false; 651 } 652 } 653 } 654 655 return true; 657 } 658 659 660 protected static boolean hasCorrectProtection( Constructor cntr, 661 int protection ) 662 { 663 int modifiers = cntr.getModifiers(); 664 boolean isPublic = Modifier.isPublic( modifiers ); 665 if ( (protection & PUBLIC) != 0 && isPublic ) 666 { 667 return true; 668 } 669 670 boolean isProtected = Modifier.isProtected( modifiers ); 671 if ( (protection & PROTECTED) != 0 && isProtected ) 672 { 673 return true; 674 } 675 676 boolean isPrivate = Modifier.isPrivate( modifiers ); 677 if ( (protection & PRIVATE) != 0 && isPrivate ) 678 { 679 return true; 680 } 681 682 if ( (protection & PACKAGE) != 0 683 && !isPublic 684 && !isProtected 685 && !isPrivate ) 686 { 687 return true; 688 } 689 690 return false; 691 } 692 } 693 694 | Popular Tags |