1 21 22 package org.apache.derbyTesting.functionTests.tests.upgradeTests; 23 24 import java.net.URLClassLoader ; 25 import java.net.URL ; 26 import java.net.MalformedURLException ; 27 import java.util.Properties ; 28 import java.io.File ; 29 30 import java.sql.Connection ; 31 import java.sql.ResultSet ; 32 import java.sql.Statement ; 33 import java.sql.PreparedStatement ; 34 import java.sql.SQLException ; 35 import javax.sql.DataSource ; 36 37 import org.apache.derbyTesting.functionTests.harness.jvm; 38 39 83 public class UpgradeTester { 84 85 88 private static final String [] PHASES = 89 {"CREATE", "SOFT UPGRADE", "POST SOFT UPGRADE", "UPGRADE", "POST UPGRADE"}; 90 91 92 95 static final int PH_CREATE = 0; 96 99 static final int PH_SOFT_UPGRADE = 1; 100 103 static final int PH_POST_SOFT_UPGRADE = 2; 104 107 static final int PH_HARD_UPGRADE = 3; 108 111 static final int PH_POST_HARD_UPGRADE = 4; 112 113 116 private static final int OLD_RELEASE = 0; 117 120 private static final int NEW_RELEASE = 1; 121 122 private String oldJarLoc; 124 private String newJarLoc; 125 126 private URLClassLoader oldClassLoader; 128 private URLClassLoader newClassLoader; 129 130 private int oldMajorVersion; 132 private int oldMinorVersion; 133 134 private int newMajorVersion; 136 private int newMinorVersion; 137 138 private boolean allowPreReleaseUpgrade; 140 141 private final String dbName = "wombat"; 142 143 private String [] jarFiles = new String [] { "derby.jar", 145 "derbynet.jar", 146 "derbyclient.jar", 147 "derbytools.jar"}; 148 149 private String testJar = "derbyTesting.jar"; 151 152 private boolean[] isJar = new boolean[1]; 155 156 167 public UpgradeTester(int oldMajorVersion, int oldMinorVersion, 168 int newMajorVersion, int newMinorVersion, 169 boolean allowPreReleaseUpgrade) { 170 this.oldMajorVersion = oldMajorVersion; 171 this.oldMinorVersion = oldMinorVersion; 172 this.newMajorVersion = newMajorVersion; 173 this.newMinorVersion = newMinorVersion; 174 this.allowPreReleaseUpgrade = allowPreReleaseUpgrade; 175 } 176 177 180 private void setJarLocations() { 181 this.oldJarLoc = getOldJarLocation(); 182 this.newJarLoc = getNewJarLocation(); 183 } 184 185 191 private String getOldJarLocation() { 192 String jarLocation = null; 193 194 String jarPath = System.getProperty("derbyTesting.jar.path"); 195 196 if((jarPath != null) && (jarPath.compareTo("JAR_PATH_NOT_SET") == 0)) { 197 System.out.println("FAIL: Path to previous release jars not set"); 198 System.out.println("Check if derbyTesting.jar.path property has been set in ant.properties file"); 199 System.exit(-1); 200 } 201 202 String version = oldMajorVersion + "." + oldMinorVersion; 203 jarLocation = jarPath + File.separator + version; 204 205 return jarLocation; 206 } 207 208 214 private String getNewJarLocation() { 215 return jvm.findCodeBase(isJar); 216 } 217 218 226 private void createClassLoaders() throws MalformedURLException { 227 if(isJar[0]){ 228 oldClassLoader = createClassLoader(oldJarLoc); 229 newClassLoader = createClassLoader(newJarLoc); 230 } else { 231 createLoadersUsingClasses(); 233 } 234 } 235 236 244 private URLClassLoader createClassLoader(String jarLoc) 245 throws MalformedURLException { 246 URL [] url = new URL [jarFiles.length + 1]; 247 248 for(int i=0; i < jarFiles.length; i++) { 249 url[i] = new File (jarLoc + File.separator + jarFiles[i]).toURL(); 250 } 251 252 url[jarFiles.length] = new File (newJarLoc + File.separator + testJar).toURL(); 254 255 return new URLClassLoader (url, null); 258 } 259 260 266 private void createLoadersUsingClasses() 267 throws MalformedURLException { 268 URL [] oldUrl = new URL [jarFiles.length + 1]; 269 270 for(int i=0; i < jarFiles.length; i++) { 271 oldUrl[i] = new File (oldJarLoc + File.separator + jarFiles[i]).toURL(); 272 } 273 274 oldUrl[jarFiles.length] = new File (newJarLoc).toURL(); 279 280 oldClassLoader = new URLClassLoader (oldUrl, null); 281 282 URL [] newUrl = new URL [] {new File (newJarLoc).toURL()}; 283 newClassLoader = new URLClassLoader (newUrl, null); 284 } 285 286 290 private static void setClassLoader(URLClassLoader classLoader) { 291 Thread.currentThread().setContextClassLoader(classLoader); 292 } 293 294 297 private static void setNullClassLoader() { 298 Thread.currentThread().setContextClassLoader(null); 299 } 300 301 305 public void runUpgradeTests() throws Exception { 306 if(allowPreReleaseUpgrade) 309 System.setProperty("derby.database.allowPreReleaseUpgrade", 310 "true"); 311 else 312 System.setProperty("derby.database.allowPreReleaseUpgrade", 313 "false"); 314 315 setJarLocations(); 316 createClassLoaders(); 317 runPhase(OLD_RELEASE, PH_CREATE); 318 runPhase(NEW_RELEASE, PH_SOFT_UPGRADE); 319 runPhase(OLD_RELEASE, PH_POST_SOFT_UPGRADE); 320 runPhase(NEW_RELEASE, PH_HARD_UPGRADE); 321 runPhase(OLD_RELEASE, PH_POST_HARD_UPGRADE); 322 } 323 324 335 private void runPhase(int version, int phase) 336 throws Exception { 337 System.out.println("\n\nSTART - phase " + PHASES[phase]); 338 339 URLClassLoader classLoader = null; 340 switch(version) { 341 case OLD_RELEASE: 342 classLoader = oldClassLoader; 343 break; 344 case NEW_RELEASE: 345 classLoader = newClassLoader; 346 break; 347 default: 348 System.out.println("ERROR: Specified an invalid release type"); 349 return; 350 } 351 352 boolean passed = true; 353 Connection conn = null; 354 355 setClassLoader(classLoader); 356 357 conn = getConnection(classLoader, phase); 358 359 if(conn != null) { 360 passed = caseVersionCheck(version, conn); 361 passed = caseReusableRecordIdSequenceNumber(conn, phase, 362 oldMajorVersion, oldMinorVersion) && passed; 363 passed = caseInitialize(conn, phase) && passed; 364 passed = caseProcedures(conn, phase, oldMajorVersion, 365 oldMinorVersion) && passed; 366 passed = caseTriggerVTI(conn, phase, oldMajorVersion, 367 oldMinorVersion) && passed; 368 passed = caseGrantRevoke(conn, phase, classLoader, false) && passed; 369 if(phase == PH_HARD_UPGRADE) { 371 setSQLAuthorization(conn, true); 372 conn = restartDatabase(classLoader); 373 passed = caseGrantRevoke(conn, phase, classLoader, true) && passed; 374 checkSysSchemas(conn); 375 checkRoutinePermissions(conn); 376 } 377 runMetadataTest(classLoader, conn); 378 conn.close(); 379 shutdownDatabase(classLoader); 380 } 381 382 383 if (isJar[0]) { 391 passed = caseEncryptUnEncryptedDb(classLoader, phase) && passed; 394 passed = caseEncryptDatabaseWithNewKey(classLoader, phase) && passed; 395 } 396 397 setNullClassLoader(); 398 399 System.out.println("END - " + (passed ? "PASS" : "FAIL") + 400 " - phase " + PHASES[phase]); 401 } 402 403 412 private Connection getConnection(URLClassLoader classLoader, 413 int phase) throws Exception { 414 Connection conn = null; 415 Properties prop = new Properties (); 416 prop.setProperty("databaseName", dbName); 417 418 switch(phase) { 419 case PH_CREATE: 420 prop.setProperty("connectionAttributes", "create=true"); 421 break; 422 case PH_SOFT_UPGRADE: 423 case PH_POST_SOFT_UPGRADE: 424 case PH_POST_HARD_UPGRADE: 425 break; 426 case PH_HARD_UPGRADE: 427 prop.setProperty("connectionAttributes", "upgrade=true"); 428 break; 429 default: 430 break; 431 } 432 433 try { 434 conn = getConnectionUsingDataSource(classLoader, prop); 435 } catch (SQLException sqle) { 436 if(phase != PH_POST_HARD_UPGRADE) 437 throw sqle; 438 439 if(sqle.getSQLState().equals("XJ040")) { 443 SQLException nextSqle = sqle.getNextException(); 444 if(nextSqle.getSQLState().equals("XSLAP") || 445 nextSqle.getSQLState().equals("XSLAN") ) 446 System.out.println("Expected exception: Failed to start" + 447 " database with old version after hard upgrade"); 448 } 449 } 450 451 return conn; 452 } 453 454 463 private Connection getConnectionUsingDataSource(URLClassLoader classLoader, Properties prop) throws Exception { 464 Connection conn = null; 465 466 try { 467 Class testUtilClass = Class.forName("org.apache.derbyTesting.functionTests.util.TestUtil", 468 true, classLoader); 469 Object testUtilObject = testUtilClass.newInstance(); 470 471 java.lang.reflect.Method method = testUtilClass.getMethod("getDataSource", new Class [] { prop.getClass() }); 476 DataSource ds = (DataSource ) method.invoke(testUtilClass, new Object [] { prop }); 477 conn = ds.getConnection(); 478 } catch(SQLException sqle) { 479 throw sqle; 480 } catch (Exception e) { 481 handleReflectionExceptions(e); 482 throw e; 483 } 484 485 return conn; 486 } 487 488 494 private boolean caseVersionCheck(int version, Connection conn) 495 throws SQLException { 496 boolean passed = false; 497 int actualMajorVersion; 498 int actualMinorVersion; 499 500 if (conn == null) 501 return false; 502 503 actualMajorVersion = conn.getMetaData().getDatabaseMajorVersion(); 504 actualMinorVersion = conn.getMetaData().getDatabaseMinorVersion(); 505 506 switch(version) { 507 case OLD_RELEASE: 508 passed = (actualMajorVersion == oldMajorVersion) && (actualMinorVersion == oldMinorVersion); 509 break; 510 case NEW_RELEASE: 511 passed = (actualMajorVersion == newMajorVersion) && (actualMinorVersion == newMinorVersion); 512 break; 513 default: 514 passed = false; 515 break; 516 } 517 518 System.out.println("complete caseVersionCheck - passed " + passed); 519 return passed; 520 } 521 522 529 static boolean caseReusableRecordIdSequenceNumber(Connection conn, 530 int phase, 531 int dbMajor, int dbMinor) 532 throws SQLException 533 { 534 boolean runCompress = dbMajor>10 || dbMajor==10 && dbMinor>=1; 535 final boolean passed; 536 switch(phase) { 537 case PH_CREATE: { 538 Statement s = conn.createStatement(); 539 s.execute("create table CT1(id int)"); 540 s.execute("insert into CT1 values 1,2,3,4,5,6,7,8,9,10"); 541 conn.commit(); 542 passed = true; 543 break; 544 } 545 case PH_SOFT_UPGRADE: 546 if (runCompress) { 547 System.out.println("caseReusableRecordIdSequenceNumber - Running compress"); 548 PreparedStatement ps = conn.prepareStatement 549 ("call SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE(?,?,?,?,?)"); 550 ps.setString(1, "APP"); ps.setString(2, "CT1"); ps.setInt(3, 1); ps.setInt(4, 1); ps.setInt(5, 1); ps.executeUpdate(); 556 conn.commit(); 557 } 558 passed = true; 559 break; 560 case PH_POST_SOFT_UPGRADE: { 561 Statement s = conn.createStatement(); 563 ResultSet rs = s.executeQuery("select * from CT1"); 564 while (rs.next()) { 565 rs.getInt(1); 566 } 567 s.execute("insert into CT1 values 11,12,13,14,15,16,17,18,19"); 568 conn.commit(); 569 passed = true; 570 break; 571 } 572 case PH_HARD_UPGRADE: 573 passed = true; 574 break; 575 default: 576 passed = false; 577 break; 578 } 579 System.out.println("complete caseReusableRecordIdSequenceNumber - passed " + passed); 580 return passed; 581 } 582 583 591 private boolean caseInitialize(Connection conn, int phase) 592 throws SQLException { 593 594 boolean passed = true; 595 596 switch (phase) { 597 case PH_CREATE: 598 conn.createStatement().executeUpdate("CREATE TABLE PHASE" + 599 "(id INT NOT NULL, ok INT)"); 600 conn.createStatement().executeUpdate("CREATE TABLE TABLE1" + 601 "(id INT NOT NULL PRIMARY KEY, name varchar(200))"); 602 break; 603 case PH_SOFT_UPGRADE: 604 break; 605 case PH_POST_SOFT_UPGRADE: 606 break; 607 case PH_HARD_UPGRADE: 608 break; 609 default: 610 passed = false; 611 break; 612 } 613 614 PreparedStatement ps = conn.prepareStatement("INSERT INTO PHASE(id) " + 615 "VALUES (?)"); 616 ps.setInt(1, phase); 617 ps.executeUpdate(); 618 ps.close(); 619 620 ps = conn.prepareStatement("INSERT INTO TABLE1 VALUES (?, ?)"); 622 for (int i = 1; i < 20; i++) 623 { 624 ps.setInt(1, i + (phase * 100)); 625 ps.setString(2, "p" + phase + "i" + i); 626 ps.executeUpdate(); 627 } 628 ps.close(); 629 ps = conn.prepareStatement("UPDATE TABLE1 set name = name || 'U' " + 630 " where id = ?"); 631 for (int i = 1; i < 20; i+=3) 632 { 633 ps.setInt(1, i + (phase * 100)); 634 ps.executeUpdate(); 635 } 636 ps.close(); 637 ps = conn.prepareStatement("DELETE FROM TABLE1 where id = ?"); 638 for (int i = 1; i < 20; i+=4) 639 { 640 ps.setInt(1, i + (phase * 100)); 641 ps.executeUpdate(); 642 } 643 ps.close(); 644 System.out.println("complete caseInitialize - passed " + passed); 645 return passed; 646 } 647 648 660 private boolean caseProcedures(Connection conn, int phase, 661 int dbMajor, int dbMinor) 662 throws SQLException { 663 664 boolean signaturesAllowedInOldRelease = 665 dbMajor > 10 || (dbMajor == 10 && dbMinor >= 1); 666 667 boolean passed = true; 668 669 switch (phase) { 670 case PH_CREATE: 671 break; 672 case PH_SOFT_UPGRADE: 673 674 try { 675 conn.createStatement().execute("CREATE PROCEDURE GC() " + 676 "LANGUAGE JAVA PARAMETER STYLE JAVA EXTERNAL NAME" + 677 " 'java.lang.System.gc()'"); 678 if (!signaturesAllowedInOldRelease) 679 { 680 System.out.println("FAIL : created procedure with " + 681 "signature"); 682 passed = false; 683 } 684 } catch (SQLException sqle) { 685 if (signaturesAllowedInOldRelease 686 || !"XCL47".equals(sqle.getSQLState())) { 687 System.out.println("FAIL " + sqle.getSQLState() 688 + " -- " + sqle.getMessage()); 689 passed = false; 690 } 691 } 692 break; 693 case PH_POST_SOFT_UPGRADE: 694 try { 695 conn.createStatement().execute("CALL GC()"); 696 if (!signaturesAllowedInOldRelease) 697 System.out.println("FAIL : procedure was created" + 698 " in soft upgrade!"); 699 700 } catch (SQLException sqle) 701 { 702 if (signaturesAllowedInOldRelease) 703 System.out.println("FAIL : procedure was created not in " + 704 "soft upgrade!" + sqle.getMessage()); 705 } 706 break; 707 case PH_HARD_UPGRADE: 708 if (!signaturesAllowedInOldRelease) 709 conn.createStatement().execute("CREATE PROCEDURE GC() " + 710 "LANGUAGE JAVA PARAMETER STYLE JAVA EXTERNAL NAME " + 711 "'java.lang.System.gc()'"); 712 conn.createStatement().execute("CALL GC()"); 713 break; 714 default: 715 passed = false; 716 break; 717 } 718 719 System.out.println("complete caseProcedures - passed " + passed); 720 return passed; 721 } 722 743 private boolean caseTriggerVTI(Connection conn, int phase, 744 int dbMajor, int dbMinor) 745 throws SQLException { 746 747 boolean passed = true; 748 749 Statement s = conn.createStatement(); 750 751 switch (phase) { 752 case PH_CREATE: 753 s.execute("CREATE TABLE D438.T438(a int, b varchar(20), c int)"); 754 s.execute("INSERT INTO D438.T438 VALUES(1, 'DERBY-438', 2)"); 755 s.execute("CREATE TABLE D438.T438_T1(a int, b varchar(20))"); 756 s.execute("CREATE TABLE D438.T438_T2(a int, c int)"); 757 s.execute( 758 "create trigger D438.T438_ROW_1 after UPDATE on D438.T438 " + 759 "referencing new as n old as o " + 760 "for each row mode db2sql "+ 761 "insert into D438.T438_T1(a, b) values (n.a, n.b || '_ROW')"); 762 s.executeUpdate( 763 "create trigger D438.T438_STMT_1 after UPDATE on D438.T438 " + 764 "referencing new_table as n " + 765 "for each statement mode db2sql "+ 766 "insert into D438.T438_T1(a, b) select n.a, n.b || '_STMT' from n"); 767 768 conn.commit(); 769 showTriggerVTITables(phase, s); 770 break; 771 case PH_SOFT_UPGRADE: 772 s.execute( 773 "create trigger D438.T438_ROW_2 after UPDATE on D438.T438 " + 774 "referencing new as n old as o " + 775 "for each row mode db2sql "+ 776 "insert into D438.T438_T2(a, c) values (n.a, n.c + 100)"); 777 s.executeUpdate( 778 "create trigger D438.T438_STMT_2 after UPDATE on D438.T438 " + 779 "referencing new_table as n " + 780 "for each statement mode db2sql "+ 781 "insert into D438.T438_T2(a, c) select n.a, n.c + 4000 from n"); 782 783 conn.commit(); 784 showTriggerVTITables(phase, s); 785 break; 786 case PH_POST_SOFT_UPGRADE: 787 showTriggerVTITables(phase, s); 788 break; 789 case PH_HARD_UPGRADE: 790 showTriggerVTITables(phase, s); 791 break; 792 default: 793 passed = false; 794 break; 795 } 796 s.close(); 797 798 System.out.println("complete caseTriggerVTI - passed " + passed); 799 return passed; 800 } 801 802 805 private void showTriggerVTITables(int phase, Statement s) throws SQLException 806 { 807 System.out.println("Trigger VTI Phase: " + PHASES[phase]); 808 s.executeUpdate("UPDATE D438.T438 set c = c + 1"); 809 s.getConnection().commit(); 810 System.out.println("D438.T438_T1"); 811 ResultSet rs = s.executeQuery("SELECT a,b from D438.T438_T1 ORDER BY 2"); 812 while (rs.next()) { 813 System.out.println(rs.getInt(1) + ", " + rs.getString(2)); 814 } 815 rs.close(); 816 System.out.println("D438.T438_T2"); 817 rs = s.executeQuery("SELECT a,c from D438.T438_T2 ORDER BY 2"); 818 while (rs.next()) { 819 System.out.println(rs.getInt(1) + ", " + rs.getString(2)); 820 } 821 rs.close(); 822 s.executeUpdate("DELETE FROM D438.T438_T1"); 823 s.executeUpdate("DELETE FROM D438.T438_T2"); 824 s.getConnection().commit(); 825 } 826 827 841 private boolean caseGrantRevoke(Connection conn, int phase, 842 URLClassLoader classLoader, 843 boolean sqlAuthorization) 844 throws Exception { 845 System.out.println("Test grant/revoke, Phase: " + PHASES[phase] + "; " 846 + "derby.database.sqlAuthorization=" + sqlAuthorization); 847 848 boolean passed = true; 849 boolean grantRevokeSupport = ((oldMajorVersion==10 && oldMinorVersion>=2) || 850 (newMajorVersion==10 && newMinorVersion>=2)) 851 && sqlAuthorization; 852 853 Statement s = conn.createStatement(); 854 855 switch (phase) { 856 case PH_CREATE: 857 s.execute("create table GR_TAB (id int)"); 858 break; 859 case PH_SOFT_UPGRADE: 860 case PH_POST_SOFT_UPGRADE: 861 passed = testGrantRevokeSupport(s, phase, grantRevokeSupport); 862 break; 863 case PH_HARD_UPGRADE: 864 passed = testGrantRevokeSupport(s, phase, grantRevokeSupport); 865 break; 866 default: 867 passed = false; 868 break; 869 } 870 s.close(); 871 872 System.out.println("complete caseGrantRevoke - passed " + passed); 873 return passed; 874 } 875 876 886 private boolean testGrantRevokeSupport(Statement s, int phase, 887 boolean grantRevokeSupport) { 888 boolean passed = true; 889 try { 890 s.execute("grant select on GR_TAB to some_user"); 891 } catch(SQLException sqle) { 892 passed = checkGrantRevokeException(sqle, phase, grantRevokeSupport); 893 } 894 895 896 try { 897 s.execute("revoke select on GR_TAB from some_user"); 898 } catch(SQLException sqle) { 899 passed = checkGrantRevokeException(sqle, phase, grantRevokeSupport); 900 } 901 902 return passed; 903 } 904 905 915 private boolean checkGrantRevokeException(SQLException sqle, int phase, 916 boolean grantRevokeSupported) { 917 boolean passed = true; 918 919 if(grantRevokeSupported) { 921 dumpSQLExceptions(sqle); 922 return false; 923 } 924 925 switch (phase) { 926 case PH_SOFT_UPGRADE: 927 passed = isExpectedException(sqle, "XCL47"); 929 break; 930 case PH_POST_SOFT_UPGRADE: 931 passed = isExpectedException(sqle, "42X01"); 933 break; 934 case PH_HARD_UPGRADE: 935 passed = isExpectedException(sqle, "42Z60"); 937 break; 938 default: 939 passed = false; 940 } 941 942 return passed; 943 } 944 945 951 private void setSQLAuthorization(Connection conn, boolean sqlAuth) { 952 String authorization = sqlAuth ? "true" : "false"; 953 954 try { 955 Statement s = conn.createStatement(); 956 s.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" + 957 "'derby.database.sqlAuthorization', '" + authorization + 958 "')"); 959 } catch (SQLException sqle) { 960 dumpSQLExceptions(sqle); 961 } 962 } 963 964 972 private void checkSysSchemas(Connection conn) throws SQLException { 973 System.out.println("Checking SYSSCHEMAS"); 974 975 Statement s = conn.createStatement(); 976 ResultSet rs = s.executeQuery("select * from SYS.SYSSCHEMAS"); 977 978 while(rs.next()) { 979 System.out.println("SCHEMANAME: " + rs.getString(2) + " , " 980 + "AUTHORIZATIONID: " + rs.getString(3)); 981 } 982 983 rs.close(); 984 s.close(); 985 } 986 987 994 private void checkRoutinePermissions(Connection conn) throws SQLException { 995 System.out.println("Checking routine permissions in SYSROUTINEPERMS"); 996 997 Statement s = conn.createStatement(); 998 ResultSet rs = s.executeQuery("select aliases.ALIAS, " + 999 "routinePerms.GRANTEE, routinePerms.GRANTOR from " + 1000 "SYS.SYSROUTINEPERMS routinePerms, " + 1001 "SYS.SYSALIASES aliases " + 1002 "where routinePerms.ALIASID=aliases.ALIASID " + 1003 "order by aliases.ALIAS"); 1004 1005 while(rs.next()) { 1006 System.out.println("ROUTINE NAME: " + rs.getString(1) + " , " + 1007 "GRANTEE: " + rs.getString(2) + " , " + 1008 "GRANTOR: " + rs.getString(3)); 1009 } 1010 1011 rs.close(); 1012 s.close(); 1013 } 1014 1015 1016 1023 private void runMetadataTest(URLClassLoader classLoader, Connection conn) 1024 throws Exception { 1025 try { 1026 Statement stmt = conn.createStatement(); 1027 1028 Class metadataClass = Class.forName("org.apache.derbyTesting.functionTests.tests.jdbcapi.metadata", 1029 true, classLoader); 1030 Object metadataObject = metadataClass.newInstance(); 1031 java.lang.reflect.Field f1 = metadataClass.getField("con"); 1032 f1.set(metadataObject, conn); 1033 java.lang.reflect.Field f2 = metadataClass.getField("s"); 1034 f2.set(metadataObject, stmt); 1035 java.lang.reflect.Method method = metadataClass.getMethod("runTest", 1036 null); 1037 method.invoke(metadataObject, null); 1038 } catch(SQLException sqle) { 1039 throw sqle; 1040 } catch (Exception e) { 1041 handleReflectionExceptions(e); 1042 throw e; 1043 } 1044 } 1045 1046 1057 private boolean caseEncryptUnEncryptedDb(URLClassLoader classLoader, 1058 int phase) throws Exception { 1059 Properties prop = new Properties (); 1060 1061 1066 String enDbName = "wombat_en"; 1067 prop.setProperty("databaseName", enDbName); 1068 1069 boolean reEncryptionAllowed = (oldMajorVersion > 10 || 1071 (oldMajorVersion ==10 && 1072 oldMinorVersion>=2)); 1073 boolean passed = true; 1074 switch(phase) { 1075 case PH_CREATE: 1076 prop.setProperty("connectionAttributes", 1077 "create=true"); 1078 break; 1079 case PH_SOFT_UPGRADE: 1080 prop.setProperty("connectionAttributes", 1082 "dataEncryption=true;" + 1083 "bootPassword=xyz1234abc"); 1084 break; 1085 case PH_POST_SOFT_UPGRADE: 1086 if (reEncryptionAllowed) 1088 prop.setProperty("connectionAttributes", 1089 "bootPassword=xyz1234abc"); 1090 break; 1091 case PH_HARD_UPGRADE: 1092 if (reEncryptionAllowed) { 1093 prop.setProperty("connectionAttributes", 1096 "upgrade=true;bootPassword=xyz1234abc"); 1097 } else { 1098 prop.setProperty("connectionAttributes", 1101 "upgrade=true;dataEncryption=true;" + 1102 "bootPassword=xyz1234abc"); 1103 1104 } 1105 break; 1108 default: 1109 return passed; 1110 } 1111 1112 Connection conn = null; 1113 try { 1114 conn = getConnectionUsingDataSource(classLoader, prop); 1115 } catch (SQLException sqle) { 1116 if(phase != PH_SOFT_UPGRADE) 1117 throw sqle ; 1118 else { 1119 if(!reEncryptionAllowed) { 1123 passed = isExpectedException(sqle, "XJ040"); 1124 SQLException nextSqle = sqle.getNextException(); 1125 passed = isExpectedException(nextSqle, "XCL47"); 1126 } else 1127 throw sqle; 1128 } 1129 } 1130 1131 if (conn != null) { 1132 conn.close(); 1133 shutdownDatabase(classLoader, enDbName, false); 1134 } 1135 return passed; 1136 } 1137 1138 1139 1150 private boolean caseEncryptDatabaseWithNewKey(URLClassLoader classLoader, 1151 int phase) throws Exception { 1152 Properties prop = new Properties (); 1153 1154 1159 String renDbName = "wombat_ren"; 1160 prop.setProperty("databaseName", renDbName); 1161 1162 boolean reEncryptionAllowed = (oldMajorVersion > 10 || 1164 (oldMajorVersion ==10 && 1165 oldMinorVersion>=2)); 1166 boolean passed = true; 1167 String bootPwd = (reEncryptionAllowed ? "new1234abc" : "xyz1234abc"); 1168 switch(phase) { 1169 case PH_CREATE: 1170 prop.setProperty("connectionAttributes", 1172 "create=true;" + 1173 "dataEncryption=true;bootPassword=xyz1234abc"); 1174 break; 1175 case PH_SOFT_UPGRADE: 1176 prop.setProperty("connectionAttributes", 1178 "bootPassword=xyz1234abc;" + 1179 "newBootPassword=new1234abc"); 1180 break; 1181 case PH_POST_SOFT_UPGRADE: 1182 prop.setProperty("connectionAttributes", 1183 "bootPassword=" + bootPwd); 1184 break; 1185 case PH_HARD_UPGRADE: 1186 prop.setProperty("connectionAttributes", 1187 "upgrade=true;bootPassword=" + bootPwd + 1188 ";newBootPassword=new1234xyz"); 1189 break; 1190 default: 1191 return passed; 1192 } 1193 1194 Connection conn = null; 1195 try { 1196 conn = getConnectionUsingDataSource(classLoader, prop); 1197 } catch (SQLException sqle) { 1198 if(phase != PH_SOFT_UPGRADE) 1199 throw sqle ; 1200 else { 1201 if(!reEncryptionAllowed) { 1206 passed = isExpectedException(sqle, "XJ040"); 1207 SQLException nextSqle = sqle.getNextException(); 1208 passed = isExpectedException(nextSqle, "XCL47"); 1209 } else 1210 throw sqle; 1211 } 1212 } 1213 1214 if (conn != null) { 1215 conn.close(); 1216 shutdownDatabase(classLoader, renDbName, false); 1217 } 1218 return passed; 1219 } 1220 1221 1222 1227 private void shutdownDatabase(URLClassLoader classLoader) 1228 throws Exception 1229 { 1230 shutdownDatabase(classLoader, dbName, true); 1231 } 1232 1233 1234 1240 private void shutdownDatabase(URLClassLoader classLoader, 1241 String databaseName, 1242 boolean printMessage) 1243 throws Exception { 1244 Properties prop = new Properties (); 1245 prop.setProperty("databaseName", databaseName); 1246 prop.setProperty("connectionAttributes", "shutdown=true"); 1247 1248 try { 1249 getConnectionUsingDataSource(classLoader, prop); 1250 } catch (SQLException sqle) { 1251 if(sqle.getSQLState().equals("08006")) { 1252 if (printMessage) 1253 System.out.println("Expected exception during shutdown: " 1254 + sqle.getMessage()); 1255 } else 1256 throw sqle; 1257 } 1258 } 1259 1260 1267 private Connection startDatabase(URLClassLoader classLoader) 1268 throws Exception { 1269 Connection conn = null; 1270 Properties prop = new Properties (); 1271 prop.setProperty("databaseName", dbName); 1272 1273 try { 1274 conn = getConnectionUsingDataSource(classLoader, prop); 1275 } catch (SQLException sqle) { 1276 dumpSQLExceptions(sqle); 1277 } 1278 1279 return conn; 1280 } 1281 1282 1288 private Connection restartDatabase(URLClassLoader classLoader) 1289 throws Exception { 1290 shutdownDatabase(classLoader); 1291 return startDatabase(classLoader); 1292 } 1293 1294 1298 public static void dumpSQLExceptions(SQLException sqle) { 1299 do 1300 { 1301 System.out.println("SQLSTATE("+sqle.getSQLState()+"): " 1302 + sqle.getMessage()); 1303 sqle = sqle.getNextException(); 1304 } while (sqle != null); 1305 } 1306 1307 1314 private boolean isExpectedException(SQLException sqle, String expectedSQLState) { 1315 boolean passed = true; 1316 1317 if(!expectedSQLState.equals(sqle.getSQLState())) { 1318 passed = false; 1319 System.out.println("Fail - Unexpected exception:"); 1320 dumpSQLExceptions(sqle); 1321 } 1322 1323 return passed; 1324 } 1325 1326 1332 private void handleReflectionExceptions(Exception e) { 1333 System.out.println("FAIL - Unexpected exception - " + e.getMessage()); 1334 System.out.println("Possible Reason - Test could not find the " + 1335 "location of jar files. Please check if you are running " + 1336 "with jar files in the classpath. The test does not run with " + 1337 "classes folder in the classpath. Also, check that old " + 1338 "jars are checked out from the repository or specified in " + 1339 "derbyTesting.jar.path property in ant.properties"); 1340 e.printStackTrace(); 1341 } 1342} 1343 | Popular Tags |