1 65 66 67 package org.hsqldb.test; 68 69 import java.io.File ; 70 import java.sql.Connection ; 71 import java.sql.DatabaseMetaData ; 72 import java.sql.Date ; 73 import java.sql.DriverManager ; 74 import java.sql.PreparedStatement ; 75 import java.sql.ResultSet ; 76 import java.sql.Statement ; 77 import java.sql.Types ; 78 79 import org.hsqldb.lib.Sort; 80 81 117 class TestSelf extends TestUtil { 118 119 130 public static void main(String [] argv) { 131 132 print("Usage: TestSelf [records [-m]] (-m means in-memory only)"); 133 134 int max = 500; 135 136 if (argv.length >= 1) { 137 max = Integer.parseInt(argv[0]); 138 } 139 140 boolean persistent = true; 141 boolean update = false; 142 143 if (argv.length >= 2) { 144 String a1 = argv[1]; 145 146 if (a1.equals("-m")) { 147 persistent = false; 148 } 149 } 150 151 test(max, persistent); 152 } 153 154 160 static void test(int max, boolean persistent) { 161 162 try { 164 DriverManager.registerDriver(new org.hsqldb.jdbcDriver()); 165 166 if (persistent) { 167 testPersistence(); 168 deleteDatabase("test2"); 169 test("jdbc:hsqldb:test2", "sa", "", true); 170 testPerformance("jdbc:hsqldb:test2", "sa", "", max, true); 171 } 172 173 test("jdbc:hsqldb:.", "sa", "", false); 174 testPerformance("jdbc:hsqldb:.", "sa", "", max, false); 175 } catch (Exception e) { 176 print("TestSelf error: " + e.getMessage()); 177 e.printStackTrace(); 178 } 179 } 180 181 static void delete(String file) { 182 183 try { 184 new File (file).delete(); 185 } catch (Exception e) {} 186 } 187 188 static void deleteDatabase(String path) { 189 190 delete(path + ".backup"); 191 delete(path + ".properties"); 192 delete(path + ".script"); 193 delete(path + ".data"); 194 delete(path + ".log"); 195 } 196 197 static void test(String url, String user, String password, 198 boolean persistent) throws Exception { 199 200 String name = persistent ? "Persistent" 201 : "Memory"; 202 203 print(name); 204 205 Connection cConnection = null; 206 207 try { 208 cConnection = DriverManager.getConnection(url, user, password); 209 } catch (Exception e) { 210 e.printStackTrace(); 211 print("TestSelf init error: " + e.getMessage()); 212 } 213 214 testMainScript(cConnection, persistent); 215 testTabProfile(cConnection, persistent); 216 testMarotest(cConnection, persistent); 217 cConnection.createStatement().execute("SHUTDOWN"); 218 cConnection.close(); 219 } 220 221 static void testPersistence() { 222 223 deleteDatabase("test1"); 224 225 try { 226 String url = "jdbc:hsqldb:test1;sql.enforce_strict_size=true"; 227 String user = "sa"; 228 String password = ""; 229 Connection cConnection = null; 230 String [] filelist; 231 String absolute = new File ("TestSelf.txt").getAbsolutePath(); 232 233 filelist = new File (new File (absolute).getParent()).list(); 234 235 Sort.sort((Object []) filelist, new Sort.StringComparator(), 0, 236 filelist.length - 1); 237 238 for (int i = 0; i < filelist.length; i++) { 239 String fname = filelist[i]; 240 241 if (fname.startsWith("TestSelf") && fname.endsWith(".txt") 242 &&!fname.equals("TestSelf.txt")) { 243 print("Openning DB"); 244 245 cConnection = DriverManager.getConnection(url, user, 246 password); 247 248 testScript(cConnection, fname); 249 cConnection.close(); 250 } 251 } 252 } catch (Exception e) { 253 e.printStackTrace(); 254 print("TestSelf init error: " + e.getMessage()); 255 } 256 } 257 258 static void testMainScript(Connection cConnection, boolean persistent) { 259 260 String name = persistent ? "Persistent" 261 : "Memory"; 262 263 print(name + " TestScript"); 264 265 String path = "TestSelf.txt"; 267 268 testScript(cConnection, path); 269 } 270 271 static byte[] b1 = { 272 0, 1, -128, 44, 12 273 }; 274 static byte[] b2 = { 275 10, 127 276 }; 277 278 static void testTabProfile(Connection cConnection, boolean persistent) { 279 280 Statement sStatement = null; 281 ResultSet r; 282 String s = ""; 283 long start; 284 boolean bDropError = false; 285 String name = persistent ? "Persistent" 286 : "Memory"; 287 288 print(name + " TabProfile"); 289 290 try { 291 sStatement = cConnection.createStatement(); 292 } catch (Exception e) { 293 e.printStackTrace(); 294 print("TabProfile init error: " + e.getMessage()); 295 296 return; 297 } 298 299 try { 300 301 s = "create table TabProfile(id int primary key," 303 + "car char,won bit,licence varbinary," 304 + "name char,sex char,chance double,birthday date,temp char)"; 305 306 sStatement.execute(s); 307 308 s = "insert into TabProfile values ( ?, ?, ?, ?," 309 + "'\"John\" the bird''s best friend', 'M',?,?,'')"; 310 311 PreparedStatement p = cConnection.prepareStatement(s); 312 313 p.clearParameters(); 314 p.setInt(1, 10); 315 p.setString(2, "Matchcartoon"); 316 p.setBoolean(3, true); 317 p.setBytes(4, b1); 318 p.setDouble(5, 50.5); 319 p.setNull(6, Types.DATE); 320 p.executeUpdate(); 321 p.clearParameters(); 322 p.setInt(1, -2); 323 p.setString(2, "\"Birdie\"'s car ?"); 324 p.setBoolean(3, false); 325 326 byte[] b2 = { 327 10, 127 328 }; 329 330 p.setBytes(4, b2); 331 p.setDouble(5, -3.1415e-20); 332 333 java.util.Calendar cal = java.util.Calendar.getInstance(); 334 335 cal.set(2000, 2, 29); 336 337 p.setDate(6, new Date (cal.getTime().getTime())); 339 p.executeUpdate(); 340 readTabProfileTest(sStatement); 341 342 byte[] b2n; 343 byte[] b1n; 344 boolean mismatch; 345 346 s = "select \"org.hsqldb.lib.ArrayUtil.containsAt\"(licence,0, ?) " 347 + "from TabProfile"; 348 p = cConnection.prepareStatement(s); 349 350 p.setBytes(1, b2); 351 352 r = p.executeQuery(); 353 354 r.next(); 355 356 boolean boo1 = r.getBoolean(1); 357 358 r.next(); 359 360 boolean boo2 = r.getBoolean(1); 361 362 364 365 379 380 381 395 s = "update tabprofile set temp = \"org.hsqldb.lib.StringConverter.byteToHex\"(licence)"; 396 397 sStatement.executeUpdate(s); 398 399 s = "select \"org.hsqldb.lib.StringConverter.hexToByte\"(temp) " 400 + "from TabProfile order by id desc"; 401 r = sStatement.executeQuery(s); 402 403 r.next(); 404 405 b1n = r.getBytes(1); 406 407 for (int i = 0; i < b1n.length; i++) { 408 if (b1[i] != b1n[i]) { 409 mismatch = true; 410 } 411 } 412 413 r.next(); 414 415 b2n = r.getBytes(1); 416 417 for (int i = 0; i < b2n.length; i++) { 418 if (b2[i] != b2n[i]) { 419 mismatch = true; 420 } 421 } 422 423 s = "create table obj(id int,o object)"; 426 427 sStatement.execute(s); 428 429 s = "insert into obj values(?,?)"; 430 p = cConnection.prepareStatement(s); 431 432 p.setInt(1, 1); 433 434 int[] ia1 = { 435 1, 2, 3 436 }; 437 438 p.setObject(2, ia1); 439 p.executeUpdate(); 440 p.clearParameters(); 441 p.setInt(1, 2); 442 443 java.awt.Rectangle r1 = new java.awt.Rectangle (10, 11, 12, 13); 444 445 p.setObject(2, r1); 446 p.executeUpdate(); 447 448 r = sStatement.executeQuery("SELECT o FROM obj ORDER BY id DESC"); 449 450 r.next(); 451 452 java.awt.Rectangle r2 = (java.awt.Rectangle ) r.getObject(1); 453 454 if (r2.x != 10 || r2.y != 11 || r2.width != 12 455 || r2.height != 13) { 456 throw new Exception ("Object data error: Rectangle"); 457 } 458 459 r.next(); 460 461 int[] ia2 = (int[]) (r.getObject(1)); 462 463 if (ia2[0] != 1 || ia2[1] != 2 || ia2[2] != 3 464 || ia2.length != 3) { 465 throw new Exception ("Object data error: int[]"); 466 } 467 468 sStatement.close(); 471 } catch (Exception e) { 472 print(""); 473 print("TabProfile error: " + e); 474 print("with SQL command: " + s); 475 e.printStackTrace(); 476 } 477 } 478 479 static void readTabProfileTest(Statement sStatement) throws Exception { 480 481 String s = "select * from TabProfile where id=-2"; 482 ResultSet r = sStatement.executeQuery(s); 483 484 r.next(); 485 486 if (!r.getString(2).equals("\"Birdie\"'s car ?")) { 487 throw new Exception ("Unicode error."); 488 } 489 490 boolean mismatch = false; 491 byte[] b2n = r.getBytes(4); 492 493 for (int i = 0; i < b2n.length; i++) { 494 if (b2[i] != b2n[i]) { 495 mismatch = true; 496 } 497 } 498 499 r.close(); 500 501 s = "select * from TabProfile where id=10"; 502 r = sStatement.executeQuery(s); 503 504 r.next(); 505 506 byte[] b1n = r.getBytes(4); 507 508 for (int i = 0; i < b1n.length; i++) { 509 if (b1[i] != b1n[i]) { 510 mismatch = true; 511 } 512 } 513 514 r.close(); 515 } 516 517 static void testMarotest(Connection cConnection, boolean persistent) { 518 519 Statement sStatement = null; 520 ResultSet r; 521 String s = ""; 522 long start; 523 boolean bDropError = false; 524 String name = persistent ? "Persistent" 525 : "Memory"; 526 527 print(name + " Marotest"); 528 529 try { 530 sStatement = cConnection.createStatement(); 531 } catch (Exception e) { 532 e.printStackTrace(); 533 print("Marotest init error: " + e.getMessage()); 534 } 535 536 try { 537 538 s = "CREATE TABLE marotest (id int PRIMARY KEY, dat int);" 540 + "INSERT INTO marotest VALUES (1,0);" 541 + "INSERT INTO marotest VALUES (2,0);" 542 + "INSERT INTO marotest VALUES (2,0);"; 543 544 try { 545 sStatement.execute(s); 546 547 s = ""; 548 } catch (Exception e) {} 549 550 if (s.equals("")) { 551 throw new Exception ("Duplicate key gave no error on insert"); 552 } 553 554 try { 555 s = "UPDATE marotest SET id=1, dat=-1 WHERE dat=0"; 556 557 sStatement.execute(s); 558 559 s = ""; 560 } catch (Exception e) {} 561 562 if (s.equals("")) { 563 throw new Exception ("Duplicate key gave no error on update"); 564 } 565 566 int count = 0; 567 568 s = "SELECT *, id as marotest_id FROM marotest"; 569 r = sStatement.executeQuery(s); 570 571 while (r.next()) { 572 r.getFloat(1); 573 r.getString("ID"); 574 r.getInt("DAT"); 575 r.getInt("MAROTEST_ID"); 576 577 if (r.getShort("DAT") != 0) { 578 throw new Exception ("Bad update worked"); 579 } 580 581 r.getLong("DAT"); 582 r.getString(2); 583 r.getObject("ID"); 584 r.clearWarnings(); 585 586 try { 587 588 r.getTimestamp("Timestamp?"); 590 591 count = 99; 592 } catch (Exception e) {} 593 594 count++; 595 } 596 597 r.close(); 598 599 if (count != 2) { 600 throw new Exception ("Should have 2 but has " + count 601 + " rows"); 602 } 603 604 DatabaseMetaData dbMeta = cConnection.getMetaData(); 606 607 r = dbMeta.getColumns(null, "DBO", "MAROTEST", "%"); 608 609 while (r.next()) { 610 s = r.getString(4).trim(); 612 int i = r.getInt(5); 614 s += i + r.getString("TYPE_NAME"); 615 i = r.getInt(7); i = r.getInt(9); i = r.getInt(11); s = s.toUpperCase(); 619 620 if (!s.equals("ID4INTEGER") &&!s.equals("DAT4INTEGER")) { 621 throw new Exception ("Wrong database meta data"); 622 } 623 } 624 625 s = "DROP TABLE marotest"; 626 627 sStatement.execute(s); 628 sStatement.close(); 629 } catch (Exception e) { 630 print(""); 631 print("Marotest error: " + e); 632 print("with SQL command: " + s); 633 e.printStackTrace(); 634 } 635 } 636 637 static void testPerformance(String url, String user, String password, 638 int max, 639 boolean persistent) throws Exception { 640 641 if (persistent) { 642 deleteDatabase("test2"); 643 } 644 645 Statement sStatement = null; 646 Connection cConnection = null; 647 ResultSet r; 648 String s = ""; 649 long start; 650 boolean bDropError = false; 651 String name = persistent ? "Persistent" 652 : "Memory"; 653 654 print(name + " Performance"); 655 656 try { 657 cConnection = DriverManager.getConnection(url, user, password); 658 sStatement = cConnection.createStatement(); 659 } catch (Exception e) { 660 e.printStackTrace(); 661 print("TestSelf init error: " + e.getMessage()); 662 } 663 664 try { 665 666 s = "CREATE CACHED TABLE Addr(ID INT PRIMARY KEY,First CHAR," 668 + "Name CHAR,ZIP INT)"; 669 670 sStatement.execute(s); 671 672 s = "CREATE INDEX iName ON Addr(Name)"; 673 674 sStatement.execute(s); 675 676 s = "SET WRITE_DELAY TRUE"; 677 678 sStatement.execute(s); 679 680 start = System.currentTimeMillis(); 681 682 for (int i = 0; i < max; i++) { 683 s = "INSERT INTO Addr VALUES(" + i + ",'Marcel" + i + "'," 684 + "'Renggli" + (max - i - (i % 31)) + "'," 685 + (3000 + i % 100) + ")"; 686 687 if (sStatement.executeUpdate(s) != 1) { 688 throw new Exception ("Insert failed"); 689 } 690 691 if (i % 100 == 0) { 692 printStatus("insert ", i, max, start); 693 } 694 } 695 696 printStatus("insert ", max, max, start); 697 print(""); 698 699 s = "SELECT COUNT(*) FROM Addr"; 700 r = sStatement.executeQuery(s); 701 702 r.next(); 703 704 int c = r.getInt(1); 705 706 if (c != max) { 707 throw new Exception ("Count should be " + (max) + " but is " 708 + c); 709 } 710 711 if (persistent) { 712 713 cConnection.close(); 715 716 cConnection = DriverManager.getConnection(url, user, 717 password); 718 sStatement = cConnection.createStatement(); 719 } 720 721 start = System.currentTimeMillis(); 722 723 for (int i = 0; i < max; i++) { 724 s = "UPDATE Addr SET Name='Robert" + (i + (i % 31)) 725 + "' WHERE ID=" + i; 726 727 if (sStatement.executeUpdate(s) != 1) { 728 throw new Exception ("Update failed"); 729 } 730 731 if (i % 100 == 0) { 732 printStatus("updated ", i, max, start); 733 734 } 742 } 743 744 printStatus("update ", max, max, start); 745 print(""); 746 747 if (persistent) { 748 s = "SHUTDOWN IMMEDIATELY"; 749 750 sStatement.execute(s); 751 752 cConnection.close(); 754 755 cConnection = DriverManager.getConnection(url, user, 756 password); 757 sStatement = cConnection.createStatement(); 758 } 759 760 start = System.currentTimeMillis(); 761 762 for (int i = 0; i < max; i++) { 763 s = "DELETE FROM Addr WHERE ID=" + (max - 1 - i); 764 765 if (sStatement.executeUpdate(s) != 1) { 766 throw new Exception ("Delete failed"); 767 } 768 769 if (i % 100 == 0) { 770 printStatus("deleting ", i, max, start); 771 772 } 780 } 781 782 printStatus("delete ", max, max, start); 783 print(""); 784 sStatement.execute("DROP TABLE Addr"); 785 } catch (Exception e) { 786 print(""); 787 print("TestSelf error: " + e); 788 print("with SQL command: " + s); 789 e.printStackTrace(); 790 } 791 792 cConnection.close(); 793 print("Test finished"); 794 } 795 796 804 static void printStatus(String s, int i, int max, long start) { 805 806 System.out.print(s + ": " + i + "/" + max + " " + (100 * i / max) 807 + "% "); 808 809 long now = System.currentTimeMillis(); 810 811 if (now > start) { 812 System.out.print((i * 1000 / (now - start))); 813 } 814 815 System.out.print(" rows/s \r"); 816 } 817 } 818 | Popular Tags |