1 21 22 30 31 package org.apache.derbyTesting.functionTests.util; 32 33 import java.io.*; 34 import java.math.*; 35 import java.sql.*; 36 import java.util.*; 37 38 import junit.framework.*; 39 40 import org.apache.derby.tools.ij; 41 42 public class DerbyJUnitTest extends TestCase 43 { 44 50 51 public static final String DEBUG_FLAG = "drb.tests.debug"; 52 53 public static final int SUCCESS_EXIT = 0; 54 public static final int FAILURE_EXIT = 1; 55 56 public static final String DEFAULT_USER_NAME = "APP"; 57 public static final String DEFAULT_PASSWORD = "APP"; 58 public static final String DEFAULT_DATABASE_NAME = "wombat"; 59 60 protected static final int JDBC_BOOLEAN = 16; 62 63 private static final String TABLE = "table"; 67 private static final String FUNCTION = "function"; 68 private static final String PROCEDURE = "procedure"; 69 70 private static final String SERVER_URL = "jdbc:derby://localhost:1527/"; 74 private static final String CREATE_PROPERTY = "create=true"; 75 76 public static final int DATABASE_URL = 0; 81 public static final int DRIVER_NAME = DATABASE_URL + 1; 82 public static final int FRAMEWORK_NAME = DRIVER_NAME + 1; 83 84 private static final String [] DB2JCC_CLIENT = 86 { 87 "jdbc:derby:net://localhost:1527/", 88 "com.ibm.db2.jcc.DB2Driver", 89 "DerbyNet" 90 }; 91 private static final String [] DERBY_CLIENT = 92 { 93 "jdbc:derby://localhost:1527/", 94 "org.apache.derby.jdbc.ClientDriver", 95 "DerbyNetClient" 96 }; 97 private static final String [] EMBEDDED_CLIENT = 98 { 99 "jdbc:derby:", 100 "org.apache.derby.jdbc.EmbeddedDriver", 101 "embedded" 102 }; 103 104 public static final String [][] LEGAL_CLIENTS = 105 { 106 DB2JCC_CLIENT, 107 DERBY_CLIENT, 108 EMBEDDED_CLIENT 109 }; 110 111 117 private static boolean _debug; 119 private static PrintStream _outputStream = System.out; 121 private static String _databaseName; private static String [] _defaultClientSettings; private static boolean _initializedForTestHarness; 125 126 132 137 public DerbyJUnitTest() {} 138 139 145 150 public static void runUnderOldHarness( String [] args, Test suite ) 151 throws Exception 152 { 153 int exitStatus = FAILURE_EXIT; 154 155 initializeForOldHarness( args ); 156 157 TestResult result = junit.textui.TestRunner.run( suite ); 158 159 exitStatus = result.errorCount() + result.failureCount(); 160 161 Runtime.getRuntime().exit( exitStatus ); 162 } 163 164 169 public static void initializeForOldHarness( String [] args ) 170 throws Exception 171 { 172 if ( _initializedForTestHarness ) { return; } 173 174 parseDebug(); 175 setDatabaseName( DEFAULT_DATABASE_NAME ); 176 findClientFromProperties(); 177 178 ij.getPropertyArg( args ); 180 Connection conn = ij.startJBMS(); 181 182 _initializedForTestHarness = true; 183 } 184 185 190 public boolean usingEmbeddedClient() { return ( _defaultClientSettings == EMBEDDED_CLIENT ); } 191 192 197 public boolean usingDerbyClient() { return ( _defaultClientSettings == DERBY_CLIENT ); } 198 199 204 public boolean usingDB2Client() { return ( _defaultClientSettings == DB2JCC_CLIENT ); } 205 206 211 public static String [] getClientSettings() { return _defaultClientSettings; } 212 213 218 public static void setClient( String [] client ) { _defaultClientSettings = client; } 219 220 225 public static void setDatabaseName( String databaseName ) { _databaseName = databaseName; } 226 227 232 public static void setDebug( boolean value ) { _debug = value; } 233 234 240 public static boolean parseDebug() 241 { 242 _debug = Boolean.getBoolean( DEBUG_FLAG ); 243 244 return true; 245 } 246 247 252 public static void println( String text ) 253 { 254 if ( _debug ) { alarm( text ); } 255 } 256 257 262 public static void alarm( String text ) 263 { 264 _outputStream.println( text ); 265 _outputStream.flush(); 266 } 267 268 273 public static void printStackTrace( Throwable t ) 274 { 275 while ( t != null ) 276 { 277 t.printStackTrace( _outputStream ); 278 279 if ( t instanceof SQLException ) { t = ((SQLException) t).getNextException(); } 280 else { break; } 281 } 282 } 283 284 289 public static void findClientFromProperties() 290 throws Exception 291 { 292 Properties systemProps = System.getProperties(); 293 String frameworkName = systemProps.getProperty 294 ( "framework", EMBEDDED_CLIENT[ FRAMEWORK_NAME ] ); 295 int count = LEGAL_CLIENTS.length; 296 297 for ( int i = 0; i < count; i++ ) 298 { 299 String [] candidate = LEGAL_CLIENTS[ i ]; 300 301 if ( candidate[ FRAMEWORK_NAME ].equals( frameworkName ) ) 302 { 303 _defaultClientSettings = candidate; 304 return; 305 } 306 } 307 308 throw new Exception ( "Unrecognized framework: " + frameworkName ); 309 } 310 311 317 public void exit( int exitStatus ) 318 { 319 Runtime.getRuntime().exit( exitStatus ); 320 } 321 322 328 333 protected static boolean faultInDriver( String [] clientSettings ) 334 { 335 String currentClientName = clientSettings[ DRIVER_NAME ]; 336 337 try { 338 Class.forName( currentClientName ); 339 340 return true; 341 } 342 catch (Exception e) 343 { 344 println( "Could not find " + currentClientName ); 345 return false; 346 } 347 } 348 349 354 protected static Connection getConnection() 355 throws Exception 356 { 357 return getConnection( _defaultClientSettings, _databaseName, new Properties() ); 358 } 359 364 protected static Connection getConnection 365 ( 366 String [] clientSettings, 367 String databaseName, 368 Properties properties 369 ) 370 throws Exception 371 { 372 faultInDriver( clientSettings ); 373 374 properties.put( "user", DEFAULT_USER_NAME ); 375 properties.put( "password", DEFAULT_PASSWORD ); 376 properties.put( "retreiveMessagesFromServerOnGetMessage", "true" ); 377 378 Connection conn = DriverManager.getConnection 379 ( makeDatabaseURL( clientSettings, databaseName ), properties ); 380 381 println( "Connection is a " + conn.getClass().getName() ); 382 383 return conn; 384 } 385 386 391 private static String makeDatabaseURL( String [] clientSettings, String databaseName ) 392 { 393 return clientSettings[ DATABASE_URL ] + databaseName; 394 } 395 396 401 protected void createDB( String databaseName ) 402 throws Exception 403 { 404 String [] clientSettings = getClientSettings(); 405 String dbURL = makeDatabaseURL( clientSettings, databaseName ); 406 407 dbURL = dbURL + ';' + CREATE_PROPERTY; 408 409 Properties properties = new Properties(); 410 411 properties.put( "user", DEFAULT_USER_NAME ); 412 properties.put( "password", DEFAULT_PASSWORD ); 413 414 faultInDriver( clientSettings ); 415 416 Connection conn = DriverManager.getConnection( dbURL, properties ); 417 418 conn.close(); 419 } 420 421 427 432 protected static void executeDDL( Connection conn, String text ) 433 throws SQLException 434 { 435 PreparedStatement ps = null; 436 437 try { 438 ps = prepare( conn, text ); 439 440 ps.execute(); 441 } 442 finally { close( ps ); } 443 } 444 445 452 protected static void execute( Connection conn, String text ) 453 throws SQLException 454 { 455 PreparedStatement ps = prepare( conn, text ); 456 457 ps.execute(); 458 close( ps ); 459 } 460 461 468 protected static PreparedStatement prepare( Connection conn, String text ) 469 throws SQLException 470 { 471 println( "Preparing: " + text ); 472 473 return conn.prepareStatement( text ); 474 } 475 476 483 protected static CallableStatement prepareCall( Connection conn, String text ) 484 throws SQLException 485 { 486 println( "Preparing procedure call: '" + text + "'" ); 487 488 CallableStatement cs = conn.prepareCall( text ); 489 490 return cs; 491 } 492 493 498 protected static void truncateTable( Connection conn, String name ) 499 throws SQLException 500 { 501 PreparedStatement ps = prepare( conn, "delete from " + name ); 502 503 ps.execute(); 504 } 505 506 515 protected static void dropTable( Connection conn, String name ) 516 { 517 dropSchemaObject( conn, TABLE, name ); 518 } 519 520 529 protected static void dropFunction( Connection conn, String name ) 530 { 531 dropSchemaObject( conn, FUNCTION, name ); 532 } 533 534 543 protected static void dropProcedure( Connection conn, String name ) 544 { 545 dropSchemaObject( conn, PROCEDURE, name ); 546 } 547 548 557 protected static void dropSchemaObject( Connection conn, String genus, String objectName ) 558 { 559 PreparedStatement ps = null; 560 561 try { 562 ps = prepare( conn, "drop " + genus + " " + objectName ); 563 564 ps.execute(); 565 } 566 catch (SQLException e) 567 { 568 if ( _debug ) { printStackTrace( e ); } 569 } 570 571 close( ps ); 572 } 573 574 583 protected static void close( ResultSet rs ) 584 { 585 try { 586 if ( rs != null ) { rs.close(); } 587 } 588 catch (SQLException e) { printStackTrace( e ); } 589 } 590 591 600 protected static void close( Statement statement ) 601 { 602 try { 603 if ( statement != null ) { statement.close(); } 604 } 605 catch (SQLException e) { printStackTrace( e ); } 606 } 607 608 617 protected static void close( Connection conn ) 618 { 619 try { 620 if ( conn != null ) { conn.close(); } 621 } 622 catch (SQLException e) { printStackTrace( e ); } 623 } 624 625 632 protected Object getColumn( ResultSet rs, String columnName, int jdbcType ) 633 throws Exception 634 { 635 Object retval = null; 636 637 switch( jdbcType ) 638 { 639 case JDBC_BOOLEAN: 640 retval = new Boolean ( rs.getBoolean( columnName ) ); 641 break; 642 643 case Types.BIGINT: 644 retval = new Long ( rs.getLong( columnName ) ); 645 break; 646 647 case Types.BLOB: 648 retval = rs.getBlob( columnName ); 649 break; 650 651 case Types.CHAR: 652 case Types.LONGVARCHAR: 653 case Types.VARCHAR: 654 retval = rs.getString( columnName ); 655 break; 656 657 case Types.BINARY: 658 case Types.LONGVARBINARY: 659 case Types.VARBINARY: 660 retval = rs.getBytes( columnName ); 661 break; 662 663 case Types.CLOB: 664 retval = rs.getClob( columnName ); 665 break; 666 667 case Types.DATE: 668 retval = rs.getDate( columnName ); 669 break; 670 671 case Types.DECIMAL: 672 case Types.NUMERIC: 673 retval = rs.getBigDecimal( columnName ); 674 break; 675 676 case Types.DOUBLE: 677 retval = new Double ( rs.getDouble( columnName ) ); 678 break; 679 680 case Types.REAL: 681 retval = new Float ( rs.getFloat( columnName ) ); 682 break; 683 684 case Types.INTEGER: 685 retval = new Integer ( rs.getInt( columnName ) ); 686 break; 687 688 case Types.SMALLINT: 689 retval = new Short ( rs.getShort( columnName ) ); 690 break; 691 692 case Types.TIME: 693 retval = rs.getTime( columnName ); 694 break; 695 696 case Types.TIMESTAMP: 697 retval = rs.getTimestamp( columnName ); 698 break; 699 700 default: 701 fail( "Unknown jdbc type " + jdbcType + " used to retrieve column: " + columnName ); 702 break; 703 } 704 705 if ( rs.wasNull() ) { retval = null; } 706 707 return retval; 708 } 709 710 721 protected Object getColumn( ResultSet rs, int param, Object value ) 722 throws Exception 723 { 724 Object retval; 725 726 if ( value == null ) 727 { 728 retval = rs.getObject( param ); 729 } 730 else if ( value instanceof Boolean ) { retval = new Boolean ( rs.getBoolean( param ) ); } 731 else if ( value instanceof Byte ) { retval = new Byte ( rs.getByte( param ) ); } 732 else if ( value instanceof Short ) { retval = new Short ( rs.getShort( param ) ); } 733 else if ( value instanceof Integer ) { retval = new Integer ( rs.getInt( param ) ); } 734 else if ( value instanceof Long ) { retval = new Long ( rs.getLong( param ) ); } 735 else if ( value instanceof Float ) { retval = new Float ( rs.getFloat( param ) ); } 736 else if ( value instanceof Double ) { retval = new Double ( rs.getDouble( param ) ); } 737 else if ( value instanceof String ) { retval = rs.getString( param ); } 738 else if ( value instanceof BigDecimal ) { retval = rs.getBigDecimal( param ); } 739 else { retval = rs.getObject( param ); } 740 741 if ( rs.wasNull() ) { retval = null; } 742 743 return retval; 744 } 745 746 753 protected Object getOutArg( CallableStatement cs, int arg, int jdbcType ) 754 throws Exception 755 { 756 Object retval = null; 757 758 switch( jdbcType ) 759 { 760 case JDBC_BOOLEAN: 761 retval = new Boolean ( cs.getBoolean( arg ) ); 762 break; 763 764 case Types.BIGINT: 765 retval = new Long ( cs.getLong( arg ) ); 766 break; 767 768 case Types.BLOB: 769 retval = cs.getBlob( arg ); 770 break; 771 772 case Types.CHAR: 773 case Types.LONGVARCHAR: 774 case Types.VARCHAR: 775 retval = cs.getString( arg ); 776 break; 777 778 case Types.BINARY: 779 case Types.LONGVARBINARY: 780 case Types.VARBINARY: 781 retval = cs.getBytes( arg ); 782 break; 783 784 case Types.CLOB: 785 retval = cs.getClob( arg ); 786 break; 787 788 case Types.DATE: 789 retval = cs.getDate( arg ); 790 break; 791 792 case Types.DECIMAL: 793 case Types.NUMERIC: 794 retval = cs.getBigDecimal( arg ); 795 break; 796 797 case Types.DOUBLE: 798 retval = new Double ( cs.getDouble( arg ) ); 799 break; 800 801 case Types.REAL: 802 retval = new Float ( cs.getFloat( arg ) ); 803 break; 804 805 case Types.INTEGER: 806 retval = new Integer ( cs.getInt( arg ) ); 807 break; 808 809 case Types.SMALLINT: 810 retval = new Short ( cs.getShort( arg ) ); 811 break; 812 813 case Types.TIME: 814 retval = cs.getTime( arg ); 815 break; 816 817 case Types.TIMESTAMP: 818 retval = cs.getTimestamp( arg ); 819 break; 820 821 default: 822 fail( "Unknown jdbc type " + jdbcType + " used to retrieve column: " + arg ); 823 break; 824 } 825 826 if ( cs.wasNull() ) { retval = null; } 827 828 return retval; 829 } 830 831 838 protected void setParameter( PreparedStatement ps, int param, int jdbcType, Object value ) 839 throws Exception 840 { 841 if ( value == null ) 842 { 843 ps.setNull( param, jdbcType ); 844 845 return; 846 } 847 848 switch( jdbcType ) 849 { 850 case JDBC_BOOLEAN: 851 ps.setBoolean( param, ((Boolean ) value ).booleanValue() ); 852 break; 853 854 case Types.BIGINT: 855 ps.setLong( param, ((Long ) value ).longValue() ); 856 break; 857 858 case Types.BLOB: 859 ps.setBlob( param, ((Blob) value ) ); 860 break; 861 862 case Types.CHAR: 863 case Types.LONGVARCHAR: 864 case Types.VARCHAR: 865 ps.setString( param, ((String ) value ) ); 866 break; 867 868 case Types.BINARY: 869 case Types.LONGVARBINARY: 870 case Types.VARBINARY: 871 ps.setBytes( param, (byte[]) value ); 872 break; 873 874 case Types.CLOB: 875 ps.setClob( param, ((Clob) value ) ); 876 break; 877 878 case Types.DATE: 879 ps.setDate( param, ((java.sql.Date ) value ) ); 880 break; 881 882 case Types.DECIMAL: 883 case Types.NUMERIC: 884 ps.setBigDecimal( param, ((BigDecimal) value ) ); 885 break; 886 887 case Types.DOUBLE: 888 ps.setDouble( param, ((Double ) value ).doubleValue() ); 889 break; 890 891 case Types.REAL: 892 ps.setFloat( param, ((Float ) value ).floatValue() ); 893 break; 894 895 case Types.INTEGER: 896 ps.setInt( param, ((Integer ) value ).intValue() ); 897 break; 898 899 case Types.SMALLINT: 900 ps.setShort( param, ((Short ) value ).shortValue() ); 901 break; 902 903 case Types.TIME: 904 ps.setTime( param, (Time) value ); 905 break; 906 907 case Types.TIMESTAMP: 908 ps.setTimestamp( param, (Timestamp) value ); 909 break; 910 911 default: 912 fail( "Unknown jdbc type: " + jdbcType ); 913 break; 914 } 915 916 } 917 918 926 protected void setParameter( PreparedStatement ps, int param, Object value ) 927 throws Exception 928 { 929 if ( value == null ) 930 { 931 ps.setObject( param, null ); 932 933 return; 934 } 935 936 if ( value instanceof Boolean ) { ps.setBoolean( param, ((Boolean ) value).booleanValue() ); } 937 else if ( value instanceof Byte ) { ps.setByte( param, ((Byte ) value).byteValue() ); } 938 else if ( value instanceof Short ) { ps.setShort( param, ((Short ) value).shortValue() ); } 939 else if ( value instanceof Integer ) { ps.setInt( param, ((Integer ) value).intValue() ); } 940 else if ( value instanceof Long ) { ps.setLong( param, ((Long ) value).longValue() ); } 941 else if ( value instanceof Float ) { ps.setFloat( param, ((Float ) value).floatValue() ); } 942 else if ( value instanceof Double ) { ps.setDouble( param, ((Double ) value).doubleValue() ); } 943 else if ( value instanceof String ) { ps.setString( param, ((String ) value) ); } 944 else { ps.setObject( param, value ); } 945 } 946 947 948 954 960 protected String singleQuote( String text ) 961 { 962 return "'" + text + "'"; 963 } 964 965 971 976 public void assertRow 977 ( ResultSet rs, Object [] expectedRow ) 978 throws Exception 979 { 980 int count = expectedRow.length; 981 982 for ( int i = 0; i < count; i++ ) 983 { 984 int columnNumber = i + 1; 985 Object expected = expectedRow[ i ]; 986 Object actual = getColumn( rs, columnNumber, expected ); 987 988 compareObjects( "Column number " + columnNumber, expected, actual ); 989 } 990 } 991 992 993 998 public void assertScalar 999 ( Connection conn, String queryText, Object expectedResult ) 1000 throws Exception 1001 { 1002 PreparedStatement ps = prepare( conn, queryText ); 1003 ResultSet rs = ps.executeQuery(); 1004 1005 rs.next(); 1006 1007 assertColumnEquals( queryText, rs, 1, expectedResult ); 1008 1009 close( rs ); 1010 close( ps ); 1011 } 1012 1013 1018 public void assertColumnEquals 1019 ( ResultSet rs, int columnNumber, Object [] expectedValues ) 1020 throws Exception 1021 { 1022 int count = expectedValues.length; 1023 1024 for ( int i = 0; i < count; i++ ) 1025 { 1026 rs.next(); 1027 assertColumnEquals( Integer.toString( i ), rs, columnNumber, expectedValues[ i ] ); 1028 } 1029 } 1030 1031 1036 public void assertColumnEquals 1037 ( String message, ResultSet rs, int columnNumber, Object expectedValue ) 1038 throws Exception 1039 { 1040 Object actualValue = getColumn( rs, columnNumber, expectedValue ); 1041 1042 compareObjects( message, expectedValue, actualValue ); 1043 } 1044 1045 1050 public void compareObjects( String message, Object left, Object right ) 1051 throws Exception 1052 { 1053 message = message + "\n\t expected = " + left + "\n\t actual = " + right; 1054 1055 if ( left == null ) 1056 { 1057 assertNull( message, right ); 1058 } 1059 else 1060 { 1061 assertNotNull( message, right ); 1062 1063 if ( left instanceof byte[] ) { compareBytes( message, left, right ); } 1064 else if ( left instanceof java.util.Date ) { compareDates( message, left, right ); } 1065 else { assertTrue( message, left.equals( right ) ); } 1066 } 1067 } 1068 1069 1074 public void compareBytes( String message, Object left, Object right ) 1075 throws Exception 1076 { 1077 if ( left == null ) { assertNull( message, right ); } 1078 else { assertNotNull( right ); } 1079 1080 if ( !(left instanceof byte[] ) ) { fail( message ); } 1081 if ( !(right instanceof byte[] ) ) { fail( message ); } 1082 1083 byte[] leftBytes = (byte[]) left; 1084 byte[] rightBytes = (byte[]) right; 1085 int count = leftBytes.length; 1086 1087 assertEquals( message, count, rightBytes.length ); 1088 1089 for ( int i = 0; i < count; i++ ) 1090 { 1091 assertEquals( message + "[ " + i + " ]", leftBytes[ i ], rightBytes[ i ] ); 1092 } 1093 } 1094 1095 1100 public void compareDates( String message, Object left, Object right ) 1101 throws Exception 1102 { 1103 if ( left == null ) { assertNull( message, right ); } 1104 else { assertNotNull( right ); } 1105 1106 if ( !(left instanceof java.util.Date ) ) { fail( message ); } 1107 if ( !(right instanceof java.util.Date ) ) { fail( message ); } 1108 1109 assertEquals( message, left.toString(), right.toString() ); 1110 } 1111 1112} 1113 1114 | Popular Tags |