1 21 31 32 package org.apache.derbyTesting.functionTests.tests.junitTests.compatibility; 33 34 import java.io.*; 35 import java.sql.*; 36 import java.util.*; 37 38 import junit.framework.*; 39 40 import org.apache.derbyTesting.functionTests.util.DerbyJUnitTest; 41 42 public class CompatibilitySuite extends DerbyJUnitTest 43 { 44 50 public static final Version IBM_2_4 = new Version( 2, 4 ); 52 53 public static final Version DRB_10_0 = new Version( 10, 0 ); 55 public static final Version DRB_10_1 = new Version( 10, 1 ); 56 public static final Version DRB_10_2 = new Version( 10, 2 ); 57 58 public static final Version VM_1_3 = new Version( 1, 3 ); 60 public static final Version VM_1_4 = new Version( 1, 4 ); 61 public static final Version VM_1_5 = new Version( 1, 5 ); 62 63 public static final String SERVER_VERSION_FUNCTION = "getVMVersion"; 64 65 private static final String VERSION_PROPERTY = "java.version"; 66 67 private static final int EXPECTED_CLIENT_COUNT = 1; 68 69 75 private static Driver _driver; private static Version _clientVMLevel; private static Version _serverVMLevel; private static Version _driverLevel; private static Version _serverLevel; 81 87 95 public static Test suite() 96 { 97 TestSuite testSuite = new TestSuite(); 98 99 testSuite.addTestSuite( JDBCDriverTest.class ); 100 101 return testSuite; 102 } 103 104 105 111 125 public static void main( String args[] ) 126 throws Exception 127 { 128 int exitStatus = FAILURE_EXIT; 129 130 if ( 131 parseDebug() && 132 parseArgs( args ) && 133 parseVMLevel() && 134 findClient() && 135 findServer() 136 ) 137 { 138 TestResult result = junit.textui.TestRunner.run( suite() ); 139 140 exitStatus = result.errorCount() + result.failureCount(); 141 } 142 143 Runtime.getRuntime().exit( exitStatus ); 144 } 145 146 152 157 public Version getServerVersion() { return _serverLevel; } 158 159 164 public Version getDriverVersion() { return _driverLevel; } 165 166 171 public static Version getServerVMVersion() { return _serverVMLevel; } 172 173 178 public Version getClientVMVersion() { return _clientVMLevel; } 179 180 186 191 public static String getVMVersion() 192 { 193 return System.getProperty( VERSION_PROPERTY ); 194 } 195 196 202 208 214 private static boolean findClient() 221 throws Exception 222 { 223 if ( getClientSettings() != null ) { faultInDriver( getClientSettings() ); } 229 else 230 { 231 String currentClientName = null; 232 int legalCount = LEGAL_CLIENTS.length; 233 int foundCount = 0; 234 235 for ( int i = 0; i < legalCount; i++ ) 236 { 237 String [] candidate = LEGAL_CLIENTS[ i ]; 238 239 if ( faultInDriver( candidate ) ) 240 { 241 setClient( candidate ); 242 foundCount++; 243 } 244 } 245 246 if ( foundCount != EXPECTED_CLIENT_COUNT ) 247 { 248 throw new Exception ( "Wrong number of drivers: " + foundCount ); 249 } 250 } 251 252 254 try { 255 _driver = DriverManager.getDriver( getClientSettings()[ DATABASE_URL ] ); 256 _driverLevel = new Version( _driver.getMajorVersion(), _driver.getMinorVersion() ); 257 } 258 catch (SQLException e) 259 { 260 printStackTrace( e ); 261 262 throw new Exception 263 ( "Driver doesn't understand expected URL: " + getClientSettings()[ DATABASE_URL ] ); 264 } 265 266 println 267 ( 268 "Driver " + _driver.getClass().getName() + 269 " Version = " + _driverLevel 270 ); 271 272 return true; 273 } 274 275 private static boolean findServer() 280 throws Exception 281 { 282 try { 283 Connection conn = getConnection(); 284 DatabaseMetaData dmd = conn.getMetaData(); 285 String dbProductVersion = dmd.getDatabaseProductVersion(); 286 287 _serverLevel = new Version( dbProductVersion ); 288 289 parseServerVMVersion( conn ); 290 } 291 catch (Exception e) 292 { 293 printStackTrace( e ); 294 295 throw new Exception ( "Error lookup up server info: " + e.getMessage() ); 296 } 297 298 println( "Server Version = " + _serverLevel ); 299 300 return true; 301 } 302 303 private static boolean parseVMLevel() 304 throws Exception 305 { 306 String vmVersion = getVMVersion(); 307 308 try { 309 _clientVMLevel = new Version( vmVersion ); 310 } 311 catch (NumberFormatException e) 312 { 313 throw new Exception ( "Badly formatted vm version: " + vmVersion ); 314 } 315 316 println( "VM Version = " + _clientVMLevel ); 317 318 return true; 319 } 320 321 private static boolean parseArgs( String args[] ) 322 throws Exception 323 { 324 if ( ( args == null ) || (args.length == 0 ) ) 325 { throw new Exception ( "Missing database name." ); } 326 327 setDatabaseName( args[ 0 ] ); 328 329 if ( (args.length > 1) && !"".equals( args[ 1 ] ) ) 330 { 331 String desiredClientName = args[ 1 ]; 332 int count = LEGAL_CLIENTS.length; 333 334 for ( int i = 0; i < count; i++ ) 335 { 336 String [] candidate = LEGAL_CLIENTS[ i ]; 337 338 if ( desiredClientName.equals( candidate[ DRIVER_NAME ] ) ) 339 { 340 setClient( candidate ); 341 break; 342 } 343 } 344 345 if ( getClientSettings() == null ) 346 { 347 throw new Exception 348 ( "Could not find client " + desiredClientName + " on the classpath." ); 349 } 350 } 351 352 return true; 353 } 354 355 360 private static void parseServerVMVersion( Connection conn ) 361 throws SQLException 362 { 363 dropFunction( conn, SERVER_VERSION_FUNCTION ); 364 365 PreparedStatement ps = prepare 366 ( 367 conn, 368 "create function " + SERVER_VERSION_FUNCTION + "() returns varchar(50)\n" + 369 "parameter style java no sql language java\n" + 370 "external name 'org.apache.derbyTesting.functionTests.tests.junitTests.compatibility.CompatibilitySuite.getVMVersion'" 371 ); 372 ps.execute(); 373 close( ps ); 374 375 ps = prepare 376 ( 377 conn, 378 "values " + SERVER_VERSION_FUNCTION + "()" 379 ); 380 381 ResultSet rs = ps.executeQuery(); 382 rs.next(); 383 String rawVersion = rs.getString( 1 ); 384 close( rs ); 385 close( ps ); 386 387 _serverVMLevel = new Version( rawVersion ); 388 389 println( "Server VM Version = " + _serverVMLevel ); 390 } 391 392 398 404 409 public static final class Creator 410 { 411 private static CompatibilitySuite _driver = new CompatibilitySuite(); 412 413 422 public static void main( String [] args ) 423 throws Exception 424 { 425 String databaseName = args[ 0 ]; 426 427 CompatibilitySuite.findClient(); 428 429 _driver.createDB( databaseName ); 430 } 431 432 } 433 434 440 public static final class Version implements Comparable 441 { 442 private int _major; 443 private int _minor; 444 445 public Version( int major, int minor ) 446 { 447 constructorMinion( major, minor ); 448 } 449 450 public Version( String desc ) 451 throws NumberFormatException 452 { 453 StringTokenizer tokens = new StringTokenizer( desc, "." ); 454 455 constructorMinion 456 ( 457 java.lang.Integer.parseInt( tokens.nextToken() ), 458 java.lang.Integer.parseInt( tokens.nextToken() ) 459 ); 460 } 461 462 private void constructorMinion( int major, int minor ) 463 { 464 _major = major; 465 _minor = minor; 466 } 467 468 474 public boolean atLeast( Version that ) 475 { 476 return this.compareTo( that ) > -1; 477 } 478 479 480 486 public int compareTo( Object other ) 487 { 488 if ( other == null ) { return -1; } 489 if ( !( other instanceof Version ) ) { return -1; } 490 491 Version that = (Version) other; 492 493 if ( this._major < that._major ) { return -1; } 494 if ( this._major > that._major ) { return 1; } 495 496 return this._minor - that._minor; 497 } 498 499 505 public String toString() 506 { 507 return Integer.toString( _major ) + '.' + Integer.toString( _minor ); 508 } 509 510 public boolean equals( Object other ) 511 { 512 return (compareTo( other ) == 0); 513 } 514 515 public int hashCode() 516 { 517 return _major ^ _minor; 518 } 519 520 } 521 522 523 524 } 525 | Popular Tags |