1 30 31 32 package org.hsqldb.test; 33 34 import java.io.File ; 35 import java.io.FileReader ; 36 import java.io.LineNumberReader ; 37 import java.sql.Connection ; 38 import java.sql.ResultSet ; 39 import java.sql.SQLException ; 40 import java.sql.Statement ; 41 42 import org.hsqldb.lib.HsqlArrayList; 43 import org.hsqldb.lib.StringUtil; 44 45 54 public class TestUtil { 55 56 67 static void testScript(Connection aConnection, String aPath) { 68 69 try { 70 Statement statement = aConnection.createStatement(); 71 File testfile = new File (aPath); 72 LineNumberReader reader = 73 new LineNumberReader (new FileReader (testfile)); 74 HsqlArrayList section = null; 75 76 print("Opened test script file: " + testfile.getAbsolutePath()); 77 78 85 int startLine = 1; 86 87 while (true) { 88 boolean startSection = false; 89 String line = reader.readLine(); 90 91 if (line == null) { 92 break; 93 } 94 95 line = line.substring( 96 0, org.hsqldb.lib.StringUtil.rTrimSize(line)); 97 98 if ((line.length() == 0) || line.startsWith("--")) { 100 continue; 101 } 102 103 if (line.startsWith("/*")) { 105 startSection = true; 106 } 107 108 if (line.charAt(0) != ' ' && line.charAt(0) != '*') { 109 startSection = true; 110 } 111 112 if (startSection) { 113 114 if (section != null) { 116 testSection(statement, section, startLine); 117 } 118 119 section = new HsqlArrayList(); 121 startLine = reader.getLineNumber(); 122 } 123 124 section.add(line); 125 } 126 127 if (section != null) { 129 testSection(statement, section, startLine); 130 } 131 132 statement.close(); 133 print("Processed lines: " + reader.getLineNumber()); 134 } catch (Exception e) { 135 e.printStackTrace(); 136 print("test script file error: " + e.getMessage()); 137 } 138 } 139 140 147 static void test(Statement stat, String s, int line) { 148 149 HsqlArrayList section = new HsqlArrayList(); 151 152 section.add(s); 153 testSection(stat, section, line); 154 } 155 156 160 static void print(String s) { 161 System.out.println(s); 162 } 163 164 177 private static void testSection(Statement stat, HsqlArrayList section, 178 int line) { 179 180 ParsedSection pSection = parsedSectionFactory(section); 182 183 if (pSection == null) { print("The section starting at line " + line 185 + " could not be parsed, " + "and so was not processed.\n"); 186 } else if (pSection instanceof IgnoreParsedSection) { 187 print("Line " + line + ": " + pSection.getResultString()); 188 } else if (pSection instanceof DisplaySection) { 189 190 print(pSection.getResultString()); 192 } else if (!pSection.test(stat)) { 193 print("section starting at line " + line); 194 print("returned an unexpected result:"); 195 print(pSection.toString()); 196 } 197 } 198 199 204 private static ParsedSection parsedSectionFactory( 205 HsqlArrayList aSection) { 206 207 char type = ' '; 209 210 String [] rows = null; 213 214 String topLine = (String ) aSection.get(0); 216 217 if (topLine.startsWith("/*")) { 219 type = topLine.charAt(2); 220 221 if (!ParsedSection.isValidCode(type)) { 223 return null; 224 } 225 226 if ((Character.isUpperCase(type)) 229 && (Boolean.getBoolean("IgnoreCodeCase"))) { 230 type = Character.toLowerCase(type); 231 } 232 233 topLine = topLine.substring(3); 235 } 236 237 int offset = 0; 242 243 if (topLine.trim().length() > 0) { 244 rows = new String [aSection.size()]; 245 rows[0] = topLine; 246 } else { 247 rows = new String [aSection.size() - 1]; 248 offset = 1; 249 } 250 251 for (int i = (1 - offset); i < rows.length; i++) { 253 rows[i] = (String ) aSection.get(i + offset); 254 } 255 256 switch (type) { 259 260 case 'u' : 261 return new UpdateParsedSection(rows); 262 263 case 's' : 264 return new SilentParsedSection(rows); 265 266 case 'r' : 267 return new ResultSetParsedSection(rows); 268 269 case 'c' : 270 return new CountParsedSection(rows); 271 272 case 'd' : 273 return new DisplaySection(rows); 274 275 case 'e' : 276 return new ExceptionParsedSection(rows); 277 278 case ' ' : 279 return new BlankParsedSection(rows); 280 281 default : 282 283 return new IgnoreParsedSection(rows, type); 287 } 288 } 289 } 290 291 295 abstract class ParsedSection { 296 297 301 protected char type = ' '; 302 303 304 String message = null; 305 306 307 protected String [] lines = null; 308 309 310 protected int resEndRow = 0; 311 312 313 protected String sqlString = null; 314 315 319 protected ParsedSection() {} 320 321 326 protected ParsedSection(String [] aLines) { 327 328 lines = aLines; 329 330 StringBuffer sqlBuff = new StringBuffer (); 333 int endIndex = 0; 334 int k = lines.length - 1; 335 336 do { 337 338 if ((endIndex = lines[k].indexOf("*/")) != -1) { 340 341 sqlBuff.insert(0, lines[k].substring(endIndex + 2)); 343 344 lines[k] = lines[k].substring(0, endIndex); 345 346 if (lines[k].length() == 0) { 347 resEndRow = k - 1; 348 } else { 349 resEndRow = k; 350 } 351 352 break; 353 } else { 354 sqlBuff.insert(0, lines[k]); 355 } 356 357 k--; 358 } while (k >= 0); 359 360 sqlString = sqlBuff.toString(); 362 } 363 364 368 public String toString() { 369 370 StringBuffer b = new StringBuffer (); 371 372 b.append("\n******\n"); 373 b.append("contents of lines array:\n"); 374 375 for (int i = 0; i < lines.length; i++) { 376 if (lines[i].trim().length() > 0) { 377 b.append("line ").append(i).append(": ").append( 378 lines[i]).append("\n"); 379 } 380 } 381 382 b.append("Type: "); 383 b.append(getType()).append("\n"); 384 b.append("SQL: ").append(getSql()).append("\n"); 385 b.append("results:\n"); 386 b.append(getResultString()); 387 388 if (getMessage() != null) { 390 b.append("\nmessage:\n"); 391 b.append(getMessage()); 392 } 393 394 b.append("\n******\n"); 395 396 return b.toString(); 397 } 398 399 403 protected abstract String getResultString(); 404 405 410 protected String getMessage() { 411 return message; 412 } 413 414 418 protected char getType() { 419 return type; 420 } 421 422 426 protected String getSql() { 427 return sqlString; 428 } 429 430 435 protected boolean test(Statement aStatement) { 436 437 try { 438 aStatement.execute(getSql()); 439 } catch (Exception x) { 440 message = x.getMessage(); 441 442 return false; 443 } 444 445 return true; 446 } 447 448 453 protected static boolean isValidCode(char aCode) { 454 455 467 char testChar = Character.toLowerCase(aCode); 468 469 switch (testChar) { 470 471 case ' ' : 472 case 'r' : 473 case 'e' : 474 case 'c' : 475 case 'u' : 476 case 's' : 477 case 'd' : 478 return true; 479 } 480 481 return false; 482 } 483 } 484 485 486 class ResultSetParsedSection extends ParsedSection { 487 488 private String delim = System.getProperty("TestUtilFieldDelimiter", ","); 489 private String [] expectedRows = null; 490 491 496 protected ResultSetParsedSection(String [] lines) { 497 498 super(lines); 499 500 type = 'r'; 501 502 expectedRows = new String [(resEndRow + 1)]; 504 505 for (int i = 0; i <= resEndRow; i++) { 506 int skip = StringUtil.skipSpaces(lines[i], 0); 507 508 expectedRows[i] = lines[i].substring(skip); 509 } 510 } 511 512 protected String getResultString() { 513 514 StringBuffer printVal = new StringBuffer (); 515 516 for (int i = 0; i < getExpectedRows().length; i++) { 517 printVal.append(getExpectedRows()[i]).append("\n"); 518 } 519 520 return printVal.toString(); 521 } 522 523 protected boolean test(Statement aStatement) { 524 525 try { 526 try { 527 528 aStatement.execute(getSql()); 530 } catch (SQLException s) { 531 throw new Exception ( 532 "Expected a ResultSet, but got the error: " 533 + s.getMessage()); 534 } 535 536 if (aStatement.getUpdateCount() != -1) { 538 throw new Exception ( 539 "Expected a ResultSet, but got an update count of " 540 + aStatement.getUpdateCount()); 541 } 542 543 ResultSet results = aStatement.getResultSet(); 545 int count = 0; 546 547 while (results.next()) { 548 if (count < getExpectedRows().length) { 549 550 String [] expectedFields = 552 StringUtil.split(getExpectedRows()[count], delim); 553 554 if (results.getMetaData().getColumnCount() 556 == expectedFields.length) { 557 558 int j = 0; 560 561 for (int i = 0; i < expectedFields.length; i++) { 562 j = i + 1; 563 564 String actual = results.getString(j); 565 566 if (actual == null) { 569 if (!expectedFields[i].equalsIgnoreCase( 571 "NULL")) { 572 throw new Exception ( 573 "Expected row " + count 574 + " of the ResultSet to contain:\n" 575 + getExpectedRows()[count] 576 + "\nbut field " + j 577 + " contained NULL"); 578 } 579 } else if (!actual.equals(expectedFields[i])) { 580 581 throw new Exception ( 583 "Expected row " + (count + 1) 584 + " of the ResultSet to contain:\n" 585 + getExpectedRows()[count] 586 + "\nbut field " + j + " contained " 587 + results.getString(j)); 588 } 589 } 590 } else { 591 592 throw new Exception ( 594 "Expected the ResultSet to contain " 595 + expectedFields.length 596 + " fields, but it contained " 597 + results.getMetaData().getColumnCount() 598 + " fields."); 599 } 600 } 601 602 count++; 603 } 604 605 if (count != getExpectedRows().length) { 607 608 throw new Exception ("Expected the ResultSet to contain " 610 + getExpectedRows().length 611 + " rows, but it contained " + count 612 + " rows."); 613 } 614 } catch (Exception x) { 615 message = x.getMessage(); 616 617 return false; 618 } 619 620 return true; 621 } 622 623 private String [] getExpectedRows() { 624 return expectedRows; 625 } 626 } 627 628 629 class UpdateParsedSection extends ParsedSection { 630 631 int countWeWant; 633 634 protected UpdateParsedSection(String [] lines) { 635 636 super(lines); 637 638 type = 'u'; 639 countWeWant = Integer.parseInt(lines[0]); 640 } 641 642 protected String getResultString() { 643 return Integer.toString(getCountWeWant()); 644 } 645 646 private int getCountWeWant() { 647 return countWeWant; 648 } 649 650 protected boolean test(Statement aStatement) { 651 652 try { 653 try { 654 655 aStatement.execute(getSql()); 657 } catch (SQLException s) { 658 throw new Exception ("Expected an update count of " 659 + getCountWeWant() 660 + ", but got the error: " 661 + s.getMessage()); 662 } 663 664 if (aStatement.getUpdateCount() != getCountWeWant()) { 665 throw new Exception ("Expected an update count of " 666 + getCountWeWant() 667 + ", but got an update count of " 668 + aStatement.getUpdateCount() + "."); 669 } 670 } catch (Exception x) { 671 message = x.getMessage(); 672 673 return false; 674 } 675 676 return true; 677 } 678 } 679 680 681 class SilentParsedSection extends ParsedSection { 682 683 protected SilentParsedSection(String [] lines) { 684 685 super(lines); 686 687 type = 's'; 688 } 689 690 protected String getResultString() { 691 return null; 692 } 693 694 protected boolean test(Statement aStatement) { 695 696 try { 697 aStatement.execute(getSql()); 698 } catch (Exception x) {} 699 700 return true; 701 } 702 } 703 704 705 class CountParsedSection extends ParsedSection { 706 707 private int countWeWant; 709 710 protected CountParsedSection(String [] lines) { 711 712 super(lines); 713 714 type = 'c'; 715 countWeWant = Integer.parseInt(lines[0]); 716 } 717 718 protected String getResultString() { 719 return Integer.toString(getCountWeWant()); 720 } 721 722 private int getCountWeWant() { 723 return countWeWant; 724 } 725 726 protected boolean test(Statement aStatement) { 727 728 try { 729 730 try { 732 aStatement.execute(getSql()); 733 } catch (SQLException s) { 734 throw new Exception ("Expected a ResultSet containing " 735 + getCountWeWant() 736 + " rows, but got the error: " 737 + s.getMessage()); 738 } 739 740 if (aStatement.getUpdateCount() != -1) { 742 throw new Exception ( 743 "Expected a ResultSet, but got an update count of " 744 + aStatement.getUpdateCount()); 745 } 746 747 ResultSet results = aStatement.getResultSet(); 749 int count = 0; 750 751 while (results.next()) { 752 count++; 753 } 754 755 if (count != getCountWeWant()) { 757 758 throw new Exception ("Expected the ResultSet to contain " 760 + getCountWeWant() 761 + " rows, but it contained " + count 762 + " rows."); 763 } 764 } catch (Exception x) { 765 message = x.getMessage(); 766 767 return false; 768 } 769 770 return true; 771 } 772 } 773 774 775 class ExceptionParsedSection extends ParsedSection { 776 777 protected ExceptionParsedSection(String [] lines) { 778 779 super(lines); 780 781 type = 'e'; 782 } 783 784 protected String getResultString() { 785 return "SQLException"; 786 } 787 788 protected boolean test(Statement aStatement) { 789 790 try { 791 aStatement.execute(getSql()); 792 } catch (SQLException sqlX) { 793 return true; 794 } catch (Exception x) { 795 message = x.getMessage(); 796 797 return false; 798 } 799 800 return false; 801 } 802 } 803 804 805 class BlankParsedSection extends ParsedSection { 806 807 protected BlankParsedSection(String [] lines) { 808 809 super(lines); 810 811 type = ' '; 812 } 813 814 protected String getResultString() { 815 return "No result specified for this section"; 816 } 817 } 818 819 820 class IgnoreParsedSection extends ParsedSection { 821 822 protected IgnoreParsedSection(String [] inLines, char aType) { 823 824 827 828 super(inLines); 832 833 type = aType; 834 } 835 836 protected String getResultString() { 837 return "This section, of type '" + getType() + "' was ignored"; 838 } 839 } 840 841 842 class DisplaySection extends ParsedSection { 843 844 protected DisplaySection(String [] inLines) { 845 846 848 lines = inLines; 849 850 int firstSlash = lines[0].indexOf('/'); 851 852 lines[0] = lines[0].substring(firstSlash + 1); 853 } 854 855 protected String getResultString() { 856 857 StringBuffer sb = new StringBuffer (); 858 859 for (int i = 0; i < lines.length; i++) { 860 if (i > 0) { 861 sb.append('\n'); 862 } 863 864 sb.append("+ " + lines[i]); 865 } 866 867 return sb.toString(); 868 } 869 } 870 | Popular Tags |