1 23 package org.objectweb.jorm.mapper.rdb.util; 24 25 import java.io.PrintStream ; 26 import java.math.BigDecimal ; 27 import java.sql.Connection ; 28 import java.sql.Date ; 29 import java.sql.DriverManager ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 import java.sql.Types ; 35 36 43 public class DriverConformTest { 44 45 48 public static void main(String [] args) { 49 boolean interac = false; 50 boolean debug = false; 51 out = System.out; 52 String sql = null; 53 String javaType = null; 54 String value = null; 55 for (int i = 0; i < args.length; i++) { 56 if (args[i].equals("-driver") && (i + 1) < args.length) 57 drvCN = args[++i]; 58 else if (args[i].equals("-url") && (i + 1) < args.length) 59 url = args[++i]; 60 else if (args[i].equals("-user") && (i + 1) < args.length) 61 user = args[++i]; 62 else if (args[i].equals("-passwd") && (i + 1) < args.length) 63 passwd = args[++i]; 64 else if (args[i].equals("-sql") && (i + 1) < args.length) 65 sql = args[++i]; 66 else if (args[i].equals("-java") && (i + 1) < args.length) 67 javaType = args[++i]; 68 else if (args[i].equals("-value") && (i + 1) < args.length) 69 value = args[++i]; 70 else if (args[i].equals("-debug")) 71 debug = true; 72 else { 73 System.out.println("Unknown argument: " + args[i]); 74 usage(); 75 System.exit(1); 76 } 77 78 } 79 DriverConformTest d = new DriverConformTest(debug); 80 if (interac) { 81 String getter = null; 82 String setter = null; 83 try { 84 sql = readString("Enter a SQL TYPE: "); 85 javaType = readString("Enter the java type (ex: java.lang.Integer): "); 86 value = readString("Enter a value(ex: 0): "); 87 String answer = readString("Do you wan to use standard sql accessors [Y|n]:"); 88 getter = null; 89 setter = null; 90 if ("n".equalsIgnoreCase(answer.trim())) { 91 getter = readString("Enter the sql getter (ex: getBoolean): "); 92 setter = readString("Enter the sql setter (ex: setBoolean): "); 93 } 94 } catch (Exception e) { 95 e.printStackTrace(); 96 } 97 d.specif(sql, javaType, value, getter, setter); 98 } else if (sql != null && javaType != null && value != null) { 99 d.specif(sql, javaType, value, null, null); 100 } else { 101 d.standard(); 102 } 103 d.close(); 104 } 105 106 private static void usage() { 107 System.out.println("\nUsage: DriverConformTest" 108 + " -driver <driver class name> " 109 + " -url <database url> " 110 + "\n [-user <user name> -passwd <password of the user>]" 111 + " -debug" 112 + "\n -sql <SQL type> " 113 + " -java <java type> " 114 + " -value <a value> " 115 ); 116 System.out.println("* debug: The debug option prints all test results."); 117 } 119 120 private static String drvCN; 121 private static String user; 122 private static String url; 123 private static String passwd; 124 private static PrintStream out; 125 126 private Connection conn = null; 127 private boolean debug; 128 129 public DriverConformTest(boolean debug) { 130 this.debug = debug; 131 if (drvCN == null || drvCN.length() == 0) { 132 System.out.println("No driver specified !"); 133 usage(); 134 System.exit(1); 135 } 136 try { 137 Class.forName(drvCN); 139 } catch (Exception e) { 140 out.println("Impossible to load the driver: " + drvCN); 141 out.println(e.getMessage()); 142 System.exit(1); 143 } 144 if (url == null || url.length() == 0) { 145 out.println("No url specified !"); 146 usage(); 147 System.exit(1); 148 } 149 try { 151 if (user == null || user.length() == 0) 152 conn = DriverManager.getConnection(url); 153 else 154 conn = DriverManager.getConnection(url, user, passwd); 155 } catch (SQLException e) { 156 out.println("Impossible to fetch a connection:"); 157 out.println(e.getMessage()); 158 System.exit(1); 159 } 160 } 161 162 public void specif(String sqlType, String stype, String sval, String getter, String setter) { 163 try { 164 Object val = null; 165 Class type = null; 166 try { 167 type = Class.forName(stype); 168 val = type 169 .getDeclaredMethod("valueOf", new Class []{String .class}) 170 .invoke(null, new Object []{sval}); 171 } catch (Exception e) { 172 try { 173 val = type 174 .getDeclaredMethod("valueOf", new Class []{Object .class}) 175 .invoke(null, new Object []{sval}); 176 } catch (Exception e2) { 177 System.err.println("Impossible to build a " + sval); 178 System.exit(1); 179 } 180 } 181 String tableName = "table_" + sqlType; 182 _testCreateTable(tableName, sqlType); 183 _testInsertSelect(sqlType, tableName, val, type, getter, setter); 184 } catch (Exception e) { 185 e.printStackTrace(); 186 } 187 } 188 189 public void standard() { 190 testBoolean(); 191 testByte(); 192 testShort(); 193 testInt(); 194 testLong(); 195 testFloat(); 196 testDouble(); 197 testDate(); 198 testString(); 199 testBigDecimal(); 200 } 201 202 public void close() { 203 if (conn != null) 204 try { 205 conn.close(); 206 } catch (SQLException e) { 207 } 208 } 209 210 private boolean _testCreateTable(String tableName, String colType) { 211 Statement s = null; 212 try { 213 s = conn.createStatement(); 214 s.execute("DROP TABLE " + tableName); 215 216 } catch (SQLException e) { 217 } finally { 218 try { 219 s.close(); 220 } catch (SQLException e) { 221 } 222 } 223 try { 224 s = conn.createStatement(); 225 s.execute( 226 "CREATE TABLE " + tableName + "(col1 " + colType + ")"); 227 ok("create table", colType); 228 return true; 229 } catch (SQLException e) { 230 fail("create table: ", e, colType); 231 return false; 232 } finally { 233 try { 234 s.close(); 235 } catch (SQLException e) { 236 } 237 } 238 } 239 240 private void _testInsertSelect(String colType, 241 String tableName, 242 Object val, 243 Class type, 244 String getter, 245 String setter) { 246 String sval = "" + val; 247 PreparedStatement ps = null; 248 ResultSet rs = null; 249 try { 250 ps = conn.prepareStatement("INSERT INTO " + tableName + " VALUES (?)"); 251 assignValue(ps, 1, val, type, setter); 252 ps.execute(); 253 ok("insert value " + sval, colType); 254 255 ps = conn.prepareStatement("SELECT * from " + tableName + " WHERE col1 = ?"); 256 assignValue(ps, 1, val, type, setter); 257 rs = ps.executeQuery(); 258 if (rs.next()) { 259 if (val.equals(retrieveValue(rs, 1, type, getter))) 260 ok("select value " + sval, colType); 261 else 262 fail("select value " + sval + ": bad value", colType); 263 264 Object o = rs.getObject(1); 265 if (type.equals(o.getClass()) && 266 ((val == null && o == null) || (val != null && val.equals(o)))) 267 ok("select value " + sval + " with getObject", colType); 268 else 269 fail("select value " + sval + " with getObject", colType); 270 271 if (rs.next()) 272 fail("select value " + sval + ": more than one result", colType); 273 } else 274 fail("select value " + sval + ": no result", colType); 275 } catch (Exception e) { 276 fail("insert & select: ", e, colType); 277 } finally { 278 try { 279 if (rs != null) 280 rs.close(); 281 } catch (SQLException e) { 282 } 283 try { 284 if (ps != null) 285 ps.close(); 286 } catch (SQLException e) { 287 } 288 } 289 } 290 291 private void assignValue(PreparedStatement ps, 292 int index, 293 Object value, 294 Class type, 295 String setter) throws Exception { 296 if (value == null) { 297 ps.setNull(index, class2sqlType(type)); 298 } else if (setter != null && setter.length() > 0) { 299 try { 300 PreparedStatement .class.getMethod(setter, new Class []{Integer.TYPE, type}) 301 .invoke(ps, new Object []{new Integer (1), value}); 302 } catch (NoSuchMethodException e) { 303 fail("Method " + setter + " not found"); 304 throw e; 305 } 306 } else if (Boolean .class.equals(type)) 307 ps.setBoolean(index, ((Boolean ) value).booleanValue()); 308 309 else if (Byte .class.equals(type)) 310 ps.setByte(index, ((Byte ) value).byteValue()); 311 312 else if (Short .class.equals(type)) 313 ps.setShort(index, ((Short ) value).shortValue()); 314 315 else if (Integer .class.equals(type)) 316 ps.setInt(index, ((Integer ) value).intValue()); 317 318 else if (Long .class.equals(type)) 319 ps.setLong(index, ((Long ) value).longValue()); 320 321 else if (Float .class.equals(type)) 322 ps.setFloat(index, ((Float ) value).floatValue()); 323 324 else if (Double .class.equals(type)) 325 ps.setDouble(index, ((Double ) value).doubleValue()); 326 327 else if (String .class.equals(type)) 328 ps.setString(index, (String ) value); 329 330 else if (Date .class.equals(type)) 331 ps.setDate(index, (Date ) value); 332 333 else if (BigDecimal .class.equals(type)) 334 ps.setBigDecimal(index, (BigDecimal ) value); 335 336 else 337 throw new Exception ("Unmanaged type: " + type); 338 } 339 340 private int class2sqlType(Class type) throws Exception { 341 if (Boolean .class.equals(type)) 342 return Types.BIT; 343 else if (Byte .class.equals(type)) 344 return Types.TINYINT; 345 else if (Short .class.equals(type)) 346 return Types.SMALLINT; 347 else if (Integer .class.equals(type)) 348 return Types.INTEGER; 349 else if (Long .class.equals(type)) 350 return Types.BIGINT; 351 else if (Float .class.equals(type)) 352 return Types.REAL; 353 else if (Double .class.equals(type)) 354 return Types.DOUBLE; 355 else if (String .class.equals(type)) 356 return Types.VARCHAR; 357 else if (Date .class.equals(type)) 358 return Types.DATE; 359 else if (BigDecimal .class.equals(type)) 360 return Types.DECIMAL; 361 else 362 throw new Exception ("Unmanaged type: " + type); 363 } 364 365 private Object retrieveValue(ResultSet rs, 366 int index, 367 Class type, 368 String getter) throws Exception { 369 Object o = null; 370 if (getter != null && getter.length() > 0) { 371 try { 372 o = ResultSet .class.getMethod(getter, new Class []{Integer.TYPE}) 373 .invoke(rs, new Object []{new Integer (1)}); 374 } catch (NoSuchMethodException e) { 375 fail("Method " + getter + " not found"); 376 throw e; 377 } 378 } else if (Boolean .class.equals(type)) 379 o = new Boolean (rs.getBoolean(index)); 380 381 else if (Byte .class.equals(type)) 382 o = new Byte (rs.getByte(index)); 383 384 else if (Short .class.equals(type)) 385 o = new Short (rs.getShort(index)); 386 387 else if (Integer .class.equals(type)) 388 o = new Integer (rs.getInt(index)); 389 390 else if (Long .class.equals(type)) 391 o = new Long (rs.getLong(index)); 392 393 else if (Float .class.equals(type)) 394 o = new Float (rs.getFloat(index)); 395 396 else if (Double .class.equals(type)) 397 o = new Double (rs.getDouble(index)); 398 399 else if (String .class.equals(type)) 400 o = rs.getString(index); 401 402 else if (Date .class.equals(type)) 403 o = rs.getDate(index); 404 405 else if (BigDecimal .class.equals(type)) 406 o = rs.getBigDecimal(index); 407 408 else 409 throw new Exception ("Unknown type: " + type); 410 return (rs.wasNull() ? null : o); 411 412 } 413 414 415 public void testBoolean() { 416 String tableName = "JormBoolean"; 417 String colType = "BIT"; 418 if (_testCreateTable(tableName, colType)) { 419 _testInsertSelect(colType, tableName, Boolean.TRUE, Boolean .class, null, null); 420 _testInsertSelect(colType, tableName, Boolean.FALSE, Boolean .class, null, null); 421 } 422 } 423 424 public void testByte() { 425 String tableName = "JormByte"; 426 String colType = "TINYINT"; 427 if (_testCreateTable(tableName, colType)) { 428 _testInsertSelect(colType, tableName, new Byte ((byte) 0), Byte .class, null, null); 429 _testInsertSelect(colType, tableName, new Byte ((byte) 1), Byte .class, null, null); 430 _testInsertSelect(colType, tableName, new Byte ((byte) 254), Byte .class, null, null); 431 } 432 } 433 434 public void testShort() { 435 String tableName = "JormShort"; 436 String colType = "SMALLINT"; 437 if (_testCreateTable(tableName, colType)) { 438 _testInsertSelect(colType, tableName, new Short ((short) 0), Short .class, null, null); 439 _testInsertSelect(colType, tableName, new Short ((short) 1), Short .class, null, null); 440 _testInsertSelect(colType, tableName, new Short ((short) 254), Short .class, null, null); 441 } 442 } 443 444 445 public void testInt() { 446 String tableName = "JormInt"; 447 String colType = "INTEGER"; 448 if (_testCreateTable(tableName, colType)) { 449 _testInsertSelect(colType, tableName, new Integer (0), Integer .class, null, null); 450 _testInsertSelect(colType, tableName, new Integer (1), Integer .class, null, null); 451 _testInsertSelect(colType, tableName, new Integer (254), Integer .class, null, null); 452 } 453 } 454 455 public void testLong() { 456 String tableName = "JormLong"; 457 String colType = "BIGINT"; 458 if (_testCreateTable(tableName, colType)) { 459 _testInsertSelect(colType, tableName, new Long (0), Long .class, null, null); 460 _testInsertSelect(colType, tableName, new Long (1), Long .class, null, null); 461 _testInsertSelect(colType, tableName, new Long (254), Long .class, null, null); 462 } 463 } 464 465 public void testFloat() { 466 String tableName = "JormFloat"; 467 String colType = "REAL"; 468 if (_testCreateTable(tableName, colType)) { 469 _testInsertSelect(colType, tableName, new Float (0.0), Float .class, null, null); 470 _testInsertSelect(colType, tableName, new Float (1), Float .class, null, null); 471 _testInsertSelect(colType, tableName, new Float (254), Float .class, null, null); 472 } 473 } 474 475 public void testDouble() { 476 String tableName = "JormDouble"; 477 String colType = "DOUBLE"; 478 if (_testCreateTable(tableName, colType)) { 479 _testInsertSelect(colType, tableName, new Double (0.0), Double .class, null, null); 480 _testInsertSelect(colType, tableName, new Double (1), Double .class, null, null); 481 _testInsertSelect(colType, tableName, new Double (254), Double .class, null, null); 482 } 483 } 484 485 public void testDate() { 486 String tableName = "JormDate"; 487 String colType = "DATE"; 488 if (_testCreateTable(tableName, colType)) { 489 } 491 } 492 493 public void testString() { 494 _testCreateTable("JormString", "VARCHAR"); 495 String tableName = "JormString"; 496 String colType = "VARCHAR"; 497 if (_testCreateTable(tableName, colType)) { 498 _testInsertSelect(colType, tableName, "", String .class, null, null); 499 _testInsertSelect(colType, tableName, "abcd", String .class, null, null); 500 } 501 } 502 503 public void testBigDecimal() { 504 _testCreateTable("JormBigDecimal", "DECIMAL"); 505 String tableName = "JormBigDecimal"; 506 String colType = "DECIMAL"; 507 if (_testCreateTable(tableName, colType)) { 508 _testInsertSelect(colType, tableName, new BigDecimal (0.0), BigDecimal .class, null, null); 509 _testInsertSelect(colType, tableName, new BigDecimal ("8764236557855635636.78966564"), BigDecimal .class, null, null); 510 } 511 } 512 513 public void testTableExistence() { 514 String tableName = "TableExist"; 515 _testCreateTable(tableName, "INTEGER"); 516 ResultSet rs = null; 517 boolean existtable = false; 518 try { 519 rs = conn.getMetaData().getTables(null, null, tableName, new String []{"TABLE"}); 520 while (rs.next() && !existtable) { 521 existtable = tableName.equalsIgnoreCase(rs.getString(3)); 522 } 523 if (existtable) { 524 ok("Able to find an existing table"); 525 } else { 526 fail("Unable to found a table"); 527 } 528 } catch(Exception e) { 529 } finally { 530 if (rs != null) { 531 try { 532 rs.close(); 533 } catch (SQLException e) { 534 fail(e.getMessage()); 535 } 536 } 537 } 538 } 539 540 public void testSequence() { 541 String seqName = "TotoSeq"; 542 Statement s = null; 543 try { 544 s = conn.createStatement(); 545 s.execute("DROP SEQUENCE " + seqName); 546 547 } catch (SQLException e) { 548 } finally { 549 try { 550 s.close(); 551 } catch (SQLException e) { 552 } 553 } 554 try { 555 s = conn.createStatement(); 556 s.execute( 557 "CREATE SEQUENCE " + seqName); 558 ok("create sequence"); 559 } catch (SQLException e) { 560 fail("create sequence: " + e.getMessage(), e); 561 return; 562 } finally { 563 try { 564 s.close(); 565 } catch (SQLException e) { 566 } 567 } 568 ResultSet rs = null; 569 boolean existtable = false; 570 try { 571 rs = conn.getMetaData().getTables(null, null, seqName, null); 572 while (rs.next() && !existtable) { 573 existtable = seqName.equalsIgnoreCase(rs.getString(3)); 574 } 575 if (existtable) { 576 ok("Able to find an existing sequence"); 577 } else { 578 fail("Unable to found an existing sequence"); 579 } 580 } catch(Exception e) { 581 } finally { 582 if (rs != null) { 583 try { 584 rs.close(); 585 } catch (SQLException e) { 586 fail(e.getMessage()); 587 } 588 } 589 } 590 } 591 592 public static String readString(String invitation) throws Exception { 593 System.out.print(invitation); 594 System.out.flush(); 595 StringBuffer reply_buffer = null; 596 char b = (char) System.in.read(); 597 while (b > 0 && b != '\n' && Character.isWhitespace(b)) { 598 b = (char) System.in.read(); 599 } 600 while (b > 0 && b != '\n' && b != 13) { 601 if (reply_buffer == null) { 602 reply_buffer = new StringBuffer (); 603 } 604 reply_buffer.append(b); 605 b = (char) System.in.read(); 606 } 607 if (System.in.available() == 1) { 608 System.in.read(); 609 } 610 if (reply_buffer != null) { 611 return reply_buffer.toString(); 612 } else { 613 return ""; 614 } 615 } 616 617 public void fail(String msg, Throwable t, String colType) { 618 fail(colType + ": " + msg, t); 619 } 620 621 public void fail(String msg, Throwable t) { 622 if (debug) { 623 fail(msg); 624 t.printStackTrace(out); 625 } else 626 fail(msg + t.getMessage()); 627 } 628 629 public void fail(String msg, String colType) { 630 fail(colType + ": " + msg); 631 } 632 633 public void fail(String msg) { 634 out.println("**FAIL: " + msg); 635 } 636 637 public void ok(String msg, String colType) { 638 ok(colType + ": " + msg); 639 } 640 641 public void ok(String msg) { 642 if (debug) 643 out.println("OK: " + msg); 644 } 645 646 public void debug(String msg) { 647 if (debug) 648 out.println("DEBUG: " + msg); 649 } 650 } 651 | Popular Tags |