1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.derbyTesting.functionTests.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;
/**
* Convert a SQL script to a JUnit test.
*
* Usage: java org.apache.derbyTesting.functionTests.util.SQLToJUnit <embedded_sql_out_file>
*/
public class SQLToJUnit
{
// Default left margin is 8 spaces.
private static final int DEFAULT_LEFT_MARGIN = 8;
private static String leftMargin;
private static final String IJ_COMMENT = "--";
private static final String IJ_WARNING = "ij warning";
private static final String JAVA_COMMENT = "//";
private static final String RS_META_OBJECT_NAME = "rsmd";
private static final String RS_OBJECT_NAME = "rs";
private static final String CSTMT_OBJECT_NAME = "cSt";
private static final String PSTMT_OBJECT_NAME = "pSt";
private static final String SQL_WARN_OBJECT_NAME = "sqlWarn";
private static String IJ_PROMPT = "ij>";
private static String CONN_OBJECT_NAME = "conn";
private static String STMT_OBJECT_NAME = "st";
private static String USER_NAME = "";
private static String getWarningLogic;
// DDL statements for which we will NOT generate row count assertions.
private static final String [] DDL_NO_RC_COMMANDS =
{
"create", "drop", "alter", "insert", "rename", "grant ", "set schema",
"revoke", "lock table"
};
// DDL statements for which we WILL generate row count assertions.
private static final String [] DDL_RC_COMMANDS =
{
"update", "delete", "declare"
};
// SQL statements that return a result set.
private static final String [] QUERY_COMMANDS =
{
"select", "values"
};
// IJ-only (non-SQL) commands
private static final String [] IJ_COMMANDS =
{
"show", "describe"
};
/* Positive numbers (and zero) indicate that we need to
* generate some kind of JDBC call and (potentially)
* assert something (result set, row count, etc.); negative
* numbers represent a "result" from executing some
* statement--ex. error, warning, row count, etc.
*/
private static int EXEC_QUERY = 0;
private static int EXEC_DDL_NO_ROW_COUNT = 1;
private static int EXEC_DDL_WITH_ROW_COUNT = 2;
private static int PREPARE = 3;
private static int P_EXECUTE = 4;
private static int CALL_STMT = 5;
private static int COMMENT = 6;
private static int AUTOCOMMIT = 7;
private static int COMMIT = 8;
private static int ROLLBACK = 9;
private static int SET_SCHEMA = 10;
private static int REMOVE = 11;
private static int GRANT = 12;
private static int GET_CURSOR = 13;
private static int CURSOR_NEXT = 14;
private static int CURSOR_CLOSE = 15;
private static int CONNECT = 16;
private static int SET_CONNECTION = 17;
private static int REVOKE = 18;
private static int IJ_COMMAND = 19;
private static int ROW_COUNT = -1;
private static int BLANK_LINE = -3;
private static int SQL_ERROR = -4;
private static int SQL_WARNING = -5;
private static int IWARNING = -7;
/* Note that the result set produced by execution of a
* QUERY_COMMAND falls into the category of "UNKNOWN
* LINE" because there is no fixed "prefix" that matches
* the start of all result sets.
*/
private static int UNKNOWN_LINE = -999;
// Maximum length of a line to print to the JUnit output
// (with some exceptions).
private static int LINE_LENGTH = 60;
private int prevLineType;
private String jTestName;
private BufferedReader ijScript;
private BufferedWriter junit;
// tmpBuf is used as a pushback buffer to store lines in case we
// go too far reading in result set lines.
private StringBuffer tmpBuf;
// used to skip commands in runtime statistics calls across calls to getNextIjCommand
private boolean gotRuntimeStatistics = false;
private int numIgnored = 0;
private int numUnconverted = 0;
/* This is set if connect .. user .. as statements are found in
* the test to decide if we should alter our search patterns to
* account for ij's multiple connection behavior. The hashtable
* is used to store the user and connection names.
*/
private boolean multipleUserConnections = false;
private Properties userConnections = new Properties();
private int usersConnected;
private int anonymousCount = 0;
public static void main(String [] args)
{
try {
(new SQLToJUnit()).convert(args);
} catch (Exception e) {
System.out.println("OOPS, top-level error:");
e.printStackTrace();
}
}
/**
* Convert .sql test output to JUnit JDBC code.
*
* This keeps two lines/blocks of output at a time in currLineType
* and nextLineType so that the result of statement executions can be
* handled along with the executing statement, for example to generate
* the necessary asserts for errors or resultsets.
*/
public void convert(String [] args) throws Exception
{
if (args.length < 1)
{
System.out.println("\n Usage: java SQLToJUnit <embedded_sql_out_file>\n");
return;
}
if (!loadIJScript(args[0]))
return;
writePrologue();
tmpBuf = new StringBuffer();
try {
leftMargin = "";
for (int i = 0; i < DEFAULT_LEFT_MARGIN; i++)
leftMargin += " ";
writeJUnitEOL();
int nextLineType;
boolean writeCurrLine;
boolean done = false;
StringBuffer currLine = new StringBuffer();
StringBuffer nextLine = new StringBuffer();
StringBuffer str = new StringBuffer();
while (!done && getNextIjCommand(str))
{
nextLineType = getLineType(str);
// if the next line is a set connection, make the switch to
// the object names before the next line after is read.
//
// TODO: not sure this is the exactly right place for this,
// some comments are recorded with their ij prompt intact.
if (nextLineType == SET_CONNECTION)
{
CONN_OBJECT_NAME = str.substring(str.indexOf("set connection ") + 15, str.length());
STMT_OBJECT_NAME = "st_" + CONN_OBJECT_NAME;
IJ_PROMPT = "ij(" + CONN_OBJECT_NAME.toUpperCase()+ ")>";
USER_NAME = (String)userConnections.getProperty(CONN_OBJECT_NAME);
junit.write("// set connection " + CONN_OBJECT_NAME);
writeJUnitEOL();
}
// Ignore IJ-specific warnings for now.
while (ignorableLine(nextLineType))
{
if (nextLineType != BLANK_LINE)
{
junit.write("[**:: IGNORED ::**] ");
junit.write(str.toString().trim());
writeJUnitEOL();
writeJUnitEOL();
numIgnored++;
}
str.delete(0, str.length());
if (!getNextIjCommand(str))
{
done = true;
break;
}
nextLineType = getLineType(str);
}
writeCurrLine = true;
// Condense multiple comment lines into uniform
// blocks of Java comments.
if (nextLineType == COMMENT)
{
if (prevLineType == COMMENT)
{
nextLine.append(strip(str, IJ_COMMENT));
writeCurrLine = false;
}
else
{
nextLine.append(str.toString().trim());
writeCurrLine = (currLine.length() > 0);
}
}
if (writeCurrLine && ((nextLineType != SQL_ERROR)
|| (prevLineType != SQL_ERROR)))
{
if (nextLineType != COMMENT)
nextLine.append(str);
writeJavaLine(currLine, nextLine);
if (nextLine.length() == 0)
writeJUnitEOL();
}
// Clear out the lines that have been processed and shuffle
// the buffers to prepare for the next incoming line.
// NOTE: prevLineType should maybe be named currLineType?
prevLineType = nextLineType;
str.delete(0, str.length());
currLine.append(nextLine.toString());
nextLine.delete(0, nextLine.length());
}
if (currLine.length() > 0)
writeJavaLine(currLine, nextLine);
// TODO: need cleanup for multiple connections
writeJUnitEOL();
junit.write("getConnection().rollback();");
writeJUnitEOL();
junit.write("st.close();");
junit.write("\n }\n}");
System.out.println("\n ==> Ignored " + numIgnored + " lines and left " +
numUnconverted + " lines unconverted.\n ==> Output is in '" +
jTestName + ".junit'.\n\n");
if (multipleUserConnections) {
System.out.print("Found multiple users: ");
for (Enumeration e = userConnections.elements(); e.hasMoreElements(); )
{
System.out.print("\"");
System.out.print(e.nextElement());
System.out.print("\"");
System.out.print((e.hasMoreElements() ? ", " : ""));
}
}
System.out.println("\n\nDone.\n");
junit.flush();
} catch (Exception e) {
// If something went wrong flush the junit output so that
// user can see whereabouts the problem occurred.
if (junit != null)
junit.flush();
throw e;
}
}
private boolean loadIJScript(String fName)
throws Exception
{
try {
ijScript = new BufferedReader(
new InputStreamReader(
new FileInputStream(fName)));
if (fName.endsWith(".out"))
jTestName = fName.substring(0, fName.indexOf("."));
else
jTestName = fName;
jTestName = jTestName.replaceAll("[.]", "_");
junit = new BufferedWriter(new FileWriter(jTestName + ".junit"));
} catch (IOException ioe) {
System.out.println("-=- Could not find IJ master: '" +
fName + "'");
return false;
}
return true;
}
private void writeJavaLine(StringBuffer lineToWrite,
StringBuffer followupLine) throws Exception
{
if ((lineToWrite == null) || (lineToWrite.length() == 0))
return;
boolean writeEOL = true;
strip(lineToWrite, IJ_PROMPT);
if (prevLineType == COMMENT)
{
strip(lineToWrite, IJ_COMMENT);
junit.write(JAVA_COMMENT);
writeMaxLenLine(lineToWrite, JAVA_COMMENT + " ", null);
writeJUnitEOL();
}
else if (prevLineType == IJ_COMMAND)
{
junit.write("[**:: UNCONVERTED ::**] ");
junit.write(lineToWrite.toString());
writeJUnitEOL();
numUnconverted++;
}
else if (prevLineType >= 0)
writeJDBCCode(lineToWrite, followupLine);
else if (prevLineType == SQL_WARNING)
{
writeAssertWarning(lineToWrite);
writeJUnitEOL();
}
else
{
String str = lineToWrite.toString();
if (str.trim().length() > 0)
{
junit.write("[**:: UNCONVERTED ::**] ");
junit.write(str);
writeJUnitEOL();
numUnconverted++;
}
else
writeEOL = false;
}
if (writeEOL)
writeJUnitEOL();
lineToWrite.delete(0, lineToWrite.length());
return;
}
private void writeJDBCCode(StringBuffer stmt,
StringBuffer result) throws Exception
{
int resultType = getLineType(result);
boolean success = (resultType != SQL_ERROR);
String indent = "";
if (!success)
indent = " ";
if (prevLineType == EXEC_QUERY)
{
if (success)
{
junit.write("rs = ");
junit.write(STMT_OBJECT_NAME);
junit.write(".executeQuery(");
writeJUnitEOL();
writeQuotedLine(stmt, indent);
writeJUnitEOL();
writeAssertResultSet(result);
}
else
writeFailStatement(stmt, null, result, false);
}
else if ((prevLineType == EXEC_DDL_WITH_ROW_COUNT)
|| (prevLineType == EXEC_DDL_NO_ROW_COUNT))
{
if (success)
{
if ((resultType == ROW_COUNT) &&
(prevLineType == EXEC_DDL_WITH_ROW_COUNT))
{
writeAssertDDLCount(stmt, result, STMT_OBJECT_NAME);
}
else
{
junit.write(STMT_OBJECT_NAME);
junit.write(".executeUpdate(");
writeJUnitEOL();
writeQuotedLine(stmt, indent);
result.delete(0, result.length());
}
}
else
writeFailStatement(stmt, null, result, false);
}
else if (prevLineType == PREPARE)
{
stmt.delete(0, stmt.indexOf("'") + 1);
stmt.delete(stmt.lastIndexOf("'"), stmt.length());
if (success)
{
junit.write(indent);
junit.write(PSTMT_OBJECT_NAME);
junit.write(" = ");
junit.write("prepareStatement(");
writeJUnitEOL();
writeQuotedLine(stmt, indent);
writeJUnitEOL();
}
else
writeFailStatement(stmt, null, result, true);
}
else if (prevLineType == P_EXECUTE)
{
// Bind the parameters.
if (stmt.indexOf("'") != -1)
{
stmt.delete(0, stmt.indexOf("'") + 1);
stmt.delete(stmt.lastIndexOf("'"), stmt.length());
junit.write(RS_OBJECT_NAME);
junit.write(" = ");
junit.write(STMT_OBJECT_NAME);
junit.write(".executeQuery(");
writeJUnitEOL();
collapseQuotes(stmt, '\'');
writeQuotedLine(stmt, indent);
writeJUnitEOL();
writeJUnitEOL();
junit.write(RS_OBJECT_NAME);
junit.write(".next();");
writeJUnitEOL();
junit.write(RS_META_OBJECT_NAME);
junit.write(" = ");
junit.write(RS_OBJECT_NAME);
junit.write(".getMetaData();");
writeJUnitEOL();
junit.write("for (int i = 1; i <= ");
junit.write(RS_META_OBJECT_NAME);
junit.write(".getColumnCount(); i++)");
writeJUnitEOL();
junit.write(" ");
junit.write(PSTMT_OBJECT_NAME);
junit.write(".setObject(i, ");
junit.write(RS_OBJECT_NAME);
junit.write(".getObject(i));");
writeJUnitEOL();
writeJUnitEOL();
}
// Now execute.
if (success)
{
if (resultType == ROW_COUNT)
writeAssertDDLCount(null, result, PSTMT_OBJECT_NAME);
else
{
junit.write("rs = ");
junit.write(PSTMT_OBJECT_NAME);
junit.write(".executeQuery();");
writeAssertResultSet(result);
}
}
else
{
writeFailStatement(null, PSTMT_OBJECT_NAME,
result, false);
}
}
else if (prevLineType == CALL_STMT)
{
junit.write(CSTMT_OBJECT_NAME);
junit.write(" = prepareCall(");
writeJUnitEOL();
writeQuotedLine(stmt, "");
if (success) {
writeJUnitEOL();
writeAssertDDLCount(null, result, CSTMT_OBJECT_NAME);
}
else
{
writeJUnitEOL();
writeFailStatement(null, CSTMT_OBJECT_NAME,
result, false);
}
}
else if (prevLineType == AUTOCOMMIT)
{
junit.write(CONN_OBJECT_NAME + ".setAutoCommit(");
junit.write(stmt.indexOf(" off") == -1 ? "true" : "false");
junit.write(");");
writeJUnitEOL();
}
else if (prevLineType == COMMIT)
junit.write(CONN_OBJECT_NAME + ".commit();");
else if (prevLineType == ROLLBACK)
{
junit.write(CONN_OBJECT_NAME + ".rollback();");
writeJUnitEOL();
}
else if (prevLineType == GET_CURSOR)
writeGetCursor(stmt, indent);
else if (prevLineType == CURSOR_NEXT)
{
junit.write(stmt.substring(stmt.lastIndexOf(" ") + 1));
junit.write(".next();");
writeJUnitEOL();
}
else if (prevLineType == CURSOR_CLOSE)
{
String cName = stmt.substring(stmt.lastIndexOf(" ") + 1).trim();
junit.write(cName);
junit.write(".close();");
writeJUnitEOL();
junit.write("ps_");
junit.write(cName);
junit.write(".close();");
writeJUnitEOL();
} else if (prevLineType == CONNECT)
{
multipleUserConnections = true;
int userpos = 0;
boolean anonymous = false;
if (stmt.indexOf("user=") > 0) {
userpos = stmt.indexOf("user=") + 4;
if (stmt.indexOf(" as ") < 0) {
anonymous = true;
}
} else
{
userpos = stmt.indexOf("'", stmt.indexOf("user") + 1);
}
if (anonymous) {
CONN_OBJECT_NAME = "CONNECTION" + anonymousCount;
anonymousCount++;
} else {
int connpos = stmt.indexOf(" as ");
CONN_OBJECT_NAME = stmt.substring(connpos + 4, stmt.length());
}
STMT_OBJECT_NAME = "st_" + CONN_OBJECT_NAME;
USER_NAME = stmt.substring(userpos + 1, stmt.indexOf("'", userpos + 1));
if (userConnections.get(USER_NAME) == null) {
junit.write("Connection ");
junit.write(CONN_OBJECT_NAME);
junit.write(" = openUserConnection(\"");
junit.write(USER_NAME);
junit.write("\");");
writeJUnitEOL();
junit.write("Statement ");
junit.write(STMT_OBJECT_NAME);
junit.write(" = ");
junit.write(CONN_OBJECT_NAME);
junit.write(".createStatement();");
writeJUnitEOL();
}
userConnections.setProperty(CONN_OBJECT_NAME, USER_NAME);
usersConnected++;
if (usersConnected > 1)
IJ_PROMPT = "ij(" + CONN_OBJECT_NAME.toUpperCase()+ ")>";
}
}
private void writeFailStatement(StringBuffer stmt,
String stmtName, StringBuffer result, boolean compileTime)
throws Exception
{
String sqlState = extractSQLState(result);
if (compileTime)
junit.write("assertCompileError(\"");
else
junit.write("assertStatementError(\"");
junit.write(sqlState);
junit.write("\", ");
if (stmt != null)
{
if (!compileTime)
{
junit.write(STMT_OBJECT_NAME);
junit.write(",");
}
writeJUnitEOL();
writeQuotedLine(stmt, "");
}
else
{
junit.write(stmtName);
junit.write(");");
}
}
private void writeAssertDDLCount(StringBuffer stmt,
StringBuffer ddlCount, String stmtName)
throws Exception
{
junit.write("assertUpdateCount(");
junit.write(stmtName);
junit.write(", ");
junit.write(extractRowCount(ddlCount));
if (stmt != null)
{
junit.write(",");
writeJUnitEOL();
writeQuotedLine(stmt, "");
}
else
junit.write(");");
}
private String extractRowCount(StringBuffer sBuf)
throws Exception
{
int lineType = getLineType(sBuf);
if (lineType != ROW_COUNT)
{
System.out.println("OOPS, tried to extract row count from " + sBuf);
return "";
}
String rowCount = sBuf.substring(0, sBuf.indexOf(" ")).trim();
sBuf.delete(0, sBuf.length());
return rowCount;
}
/**
* Write JDBC code for an ij statement of the form:
*
* get cursor c1 as 'select * from t1'
*/
private void writeGetCursor(StringBuffer stmt, String indent)
throws Exception
{
int pos_1 = stmt.indexOf("cursor") + 7;
if (pos_1 < 0)
pos_1 = stmt.indexOf("CURSOR") + 7;
int pos_2 = stmt.indexOf(" as ");
if (pos_2 < 0)
pos_2 = stmt.indexOf(" AS ");
String cName = stmt.substring(pos_1, pos_2).trim();
junit.write("PreparedStatement ps_");
junit.write(cName);
junit.write(" = prepareStatement(");
writeJUnitEOL();
stmt.delete(0, stmt.indexOf("'") + 1);
stmt.delete(stmt.lastIndexOf("'"), stmt.length());
collapseQuotes(stmt, '\'');
writeQuotedLine(stmt, indent);
writeJUnitEOL();
writeJUnitEOL();
junit.write("ResultSet ");
junit.write(cName);
junit.write(" = ps_");
junit.write(cName);
junit.write(".executeQuery();");
writeJUnitEOL();
}
private void writeMaxLenLine(StringBuffer aLine,
String prefix, String suffix) throws Exception
{
if (aLine.length() <= LINE_LENGTH)
{
junit.write(aLine.toString());
return;
}
int prefixLen = (prefix == null) ? 0 : prefix.length();
int suffixLen = (suffix == null) ? 0 : suffix.length();
String s;
int pos = -1;
boolean firstLine = true;
while (aLine.length() > 0)
{
if (!firstLine)
{
writeJUnitEOL();
if (prefixLen > 0)
junit.write(prefix);
}
pos = aLine.indexOf("\n");
if (pos != -1)
{
writeMaxLenLine(new StringBuffer(aLine.substring(0, pos)),
prefix, suffix);
pos++;
}
else if (aLine.length() <= LINE_LENGTH)
{
junit.write(aLine.toString());
pos = aLine.length();
}
else
{
s = aLine.toString().substring(
0, LINE_LENGTH - prefixLen - suffixLen);
pos = s.lastIndexOf(" ") + 1;
if (pos == 0)
pos = s.length();
junit.write(s.substring(0, pos));
}
aLine.delete(0, pos);
if ((suffixLen > 0) && (aLine.length() > 0))
junit.write(suffix);
firstLine = false;
}
return;
}
private void escapeQuotes(StringBuffer aLine)
{
if ((aLine == null) || (aLine.length() == 0))
return;
int len = aLine.length();
for (int i = len - 1; i >= 0; i--)
{
if (aLine.charAt(i) == '"')
aLine.replace(i, i+1, "\\\"");
}
return;
}
private void collapseQuotes(StringBuffer aLine, char quote)
{
if ((aLine == null) || (aLine.length() == 0))
return;
int len = aLine.length();
for (int i = len - 1; i >= 1; i--)
{
if ((aLine.charAt(i) == quote) &&
(aLine.charAt(i-1) == quote))
{
aLine.deleteCharAt(i);
i--;
}
}
return;
}
private String strip(String str, String toStrip)
{
if ((toStrip == null) || (toStrip.length() == 0))
return str;
if (!str.trim().startsWith(toStrip))
return str;
return str.substring(
str.indexOf(toStrip) + toStrip.length()).trim();
}
private StringBuffer strip(StringBuffer sBuf, String toStrip)
{
if ((toStrip == null) || (toStrip.length() == 0))
return sBuf;
if (sBuf.length() == 0)
return sBuf;
// Move past the beginning whitespace, if any.
int pos = 0;
while (Character.isWhitespace(sBuf.charAt(pos)))
pos++;
int len = toStrip.length();
boolean okayToStrip = true;
for (int i = 0; (i < len) && okayToStrip; i++)
{
if (sBuf.charAt(pos+i) != toStrip.charAt(i))
okayToStrip = false;
}
if (okayToStrip)
sBuf.delete(0, pos + len);
return sBuf;
}
private int getLineType(StringBuffer sBuf)
{
return getLineType(sBuf.toString());
}
private int getLineType(String str)
{
str = strip(str, IJ_PROMPT).toLowerCase().trim();
if (str.length() == 0)
return BLANK_LINE;
else if (str.startsWith(IJ_WARNING))
return IWARNING;
else if (str.startsWith(IJ_COMMENT))
return COMMENT;
else if (str.startsWith("error"))
return SQL_ERROR;
else if (str.startsWith("warning"))
return SQL_WARNING;
else if (isIjCommand(str))
return IJ_COMMAND;
else if (isQueryStatement(str))
return EXEC_QUERY;
else if (isDDLWithRowCount(str))
return EXEC_DDL_WITH_ROW_COUNT;
else if (isDDLNoRowCount(str))
return EXEC_DDL_NO_ROW_COUNT;
else if (str.startsWith("prepare"))
return PREPARE;
else if (str.startsWith("execute"))
return P_EXECUTE;
else if (str.startsWith("call "))
return CALL_STMT;
else if (Character.isDigit(str.charAt(0)))
{
int pos = str.indexOf(" ");
if ((pos > 0) && str.substring(pos).trim().startsWith("row"))
return ROW_COUNT;
return UNKNOWN_LINE;
}
else if (str.startsWith("autocommit"))
return AUTOCOMMIT;
else if (str.startsWith("commit"))
return COMMIT;
else if (str.startsWith("rollback"))
return ROLLBACK;
else if (str.startsWith("set schema"))
return SET_SCHEMA;
else if (str.startsWith("grant "))
return GRANT;
else if (str.startsWith("revoke"))
return REVOKE;
else if (str.startsWith("remove"))
return REMOVE;
else if (str.startsWith("get cursor"))
return GET_CURSOR;
else if (str.startsWith("next "))
return CURSOR_NEXT;
else if (str.startsWith("close"))
return CURSOR_CLOSE;
else if (str.startsWith("connect"))
return CONNECT;
else if (str.startsWith("set connection"))
return SET_CONNECTION;
else
return UNKNOWN_LINE;
}
/* Note: the next four methods could probably be condensed into one
* method: e.g. void isMember(String[], String)
*/
private boolean isDDLWithRowCount(String str)
{
for (int i = 0; i < DDL_RC_COMMANDS.length; i++)
{
if (str.startsWith(DDL_RC_COMMANDS[i]))
return true;
}
return false;
}
private boolean isDDLNoRowCount(String str)
{
for (int i = 0; i < DDL_NO_RC_COMMANDS.length; i++)
{
if (str.startsWith(DDL_NO_RC_COMMANDS[i]))
return true;
}
return false;
}
private boolean isQueryStatement(String str)
{
for (int i = 0; i < QUERY_COMMANDS.length; i++)
{
if (str.startsWith(QUERY_COMMANDS[i]))
return true;
}
return false;
}
private boolean isIjCommand(String str)
{
for (int i = 0; i < IJ_COMMANDS.length; i++)
{
if (str.startsWith(IJ_COMMANDS[i]))
return true;
}
return false;
}
private void writeAssertResultSet(StringBuffer rsAsText)
throws Exception
{
while ((rsAsText.length() > 0) &&
Character.isWhitespace(rsAsText.charAt(0)))
{
rsAsText.deleteCharAt(0);
}
BufferedReader rsReader =
new BufferedReader(
new StringReader(rsAsText.toString()));
int rowCount = 0;
int colCount = 0;
boolean wroteDecl = false;
StringBuffer sBuf = new StringBuffer();
StringTokenizer tkzr = null;
for (String row = rsReader.readLine(); row != null;
row = rsReader.readLine(), rowCount++)
{
// Second row is just "underlining" of the column names,
// so skip it.
if (rowCount == 1)
continue;
// ignore last line ROW_COUNTs
// continue to write out assert statement.
if (getLineType(row) == ROW_COUNT) {
rowCount--;
continue;
}
// First row is column names.
if (rowCount == 0)
{
writeJUnitEOL();
junit.write("expColNames = new String [] {");
}
else if (!wroteDecl)
{
writeJUnitEOL();
junit.write("expRS = new String [][]");
writeJUnitEOL();
junit.write("{");
wroteDecl = true;
}
if (rowCount > 2)
junit.write(",");
colCount = 0;
tkzr = new StringTokenizer(row, "|");
if (rowCount > 0)
{
writeJUnitEOL();
junit.write(" {");
}
while (tkzr.hasMoreTokens())
{
sBuf.append(tkzr.nextToken().trim());
escapeQuotes(sBuf);
if (colCount > 0)
junit.write(", ");
if (sBuf.toString().equals("NULL"))
junit.write("null");
else
{
junit.write("\"");
writeMaxLenLine(sBuf, " + \"", "\"");
junit.write("\"");
}
colCount++;
sBuf.delete(0, sBuf.length());
}
junit.write("}");
if (rowCount == 0)
{
junit.write(";");
writeJUnitEOL();
junit.write("JDBC.assertColumnNames(rs, expColNames);");
writeJUnitEOL();
}
}
// Row count of 2 means we had an empty result set.
if (rowCount == 2)
junit.write("JDBC.assertDrainResults(rs, 0);");
else
{
writeJUnitEOL();
junit.write("};");
writeJUnitEOL();
writeJUnitEOL();
junit.write("JDBC.assertFullResultSet(rs, expRS, true);");
}
rsAsText.delete(0, rsAsText.length());
}
/**
* Extract out the SQLSTATE. Messages are of the
* form "ERROR <SQLSTATE>: ..." or "WARNING <SQLSTATE>: ...".
* or "ERROR ... SQLSTATE <SQLSTATE>"
*/
private String extractSQLState(StringBuffer errString)
throws Exception
{
String sqlState;
if (errString.indexOf("SQLSTATE") > 0)
{
sqlState = errString.delete(0, errString.indexOf(" ", errString.indexOf("SQLSTATE")) + 1).toString().substring(0,5);
errString.delete(0, 6);
} else {
sqlState =
errString.substring(
errString.indexOf(" ") + 1, errString.indexOf(":"));
errString.delete(0, errString.length());
}
return sqlState;
}
private void writeAssertSQLState(String sqlState,
String objName) throws Exception
{
junit.write("assertSQLState(\"");
junit.write(sqlState);
junit.write("\", ");
junit.write(objName);
junit.write(");");
return;
}
private void writeAssertWarning(StringBuffer warnString)
throws Exception
{
String indent = " ";
if (getWarningLogic == null)
{
getWarningLogic =
"if (usingEmbedded())\n" + leftMargin + "{\n" +
leftMargin + indent + "if ((" + SQL_WARN_OBJECT_NAME +
" == null) && (" + STMT_OBJECT_NAME + " != null))\n"
+ leftMargin + indent + indent + SQL_WARN_OBJECT_NAME
+ " = " + STMT_OBJECT_NAME + ".getWarnings();\n" +
leftMargin + indent + "if (" + SQL_WARN_OBJECT_NAME +
" == null)\n" + leftMargin + indent + indent +
SQL_WARN_OBJECT_NAME + " = " +
(multipleUserConnections ? CONN_OBJECT_NAME : "getConnection()")
+ ".getWarnings();\n" + leftMargin;
}
junit.write(getWarningLogic);
junit.write(indent);
junit.write("assertNotNull(\"Expected warning but found none\", ");
junit.write(SQL_WARN_OBJECT_NAME);
junit.write(");");
writeJUnitEOL();
junit.write(indent);
writeAssertSQLState(
extractSQLState(warnString), SQL_WARN_OBJECT_NAME);
writeJUnitEOL();
junit.write(indent);
junit.write("sqlWarn = null;");
writeJUnitEOL();
junit.write("}");
}
private void writeQuotedLine(StringBuffer line, String indent)
throws Exception
{
junit.write(indent);
junit.write(" \"");
escapeQuotes(line);
writeMaxLenLine(line, indent + " + \"", "\"");
junit.write("\");");
}
private void writeJUnitEOL()
throws IOException
{
junit.write("\n");
junit.write(leftMargin);
}
/**
* Return one ij command, or if it's a result set, return the entire result set
* in the StringBuffer.
*/
private boolean getNextIjCommand(StringBuffer aLine)
throws IOException
{
int c = 0;
boolean done = false;
StringBuffer targetBuf = aLine;
String nextline = null;
boolean insideRS = false;
boolean gotFirstComment = false;
boolean gotCommand = false;
boolean readFromTmpBuf = false;
if (tmpBuf.length() > 0)
{
// get pushed back command
nextline = tmpBuf.toString();
tmpBuf.delete(0, tmpBuf.length());
readFromTmpBuf = true;
}
while(!done)
{
if (!readFromTmpBuf) {
nextline = ijScript.readLine();
} else {
readFromTmpBuf = false;
}
int linetype;
// Skip entirely blank lines:
while (nextline != null)
{
nextline = nextline.trim();
if (nextline.length() == 0)
nextline = ijScript.readLine();
else
break;
}
if (nextline == null) {
c = -1;
break;
} else {
linetype = getLineType(nextline);
// multiple lines of SQL comments will be condensed in convert().
// If we are inside a result set, allow the first line of a command
// to be pushed back, otherwise Once we are accumulating lines for a command,
// don't stop until we find a semicolon.
if (!insideRS)
gotCommand = gotCommand || (linetype > -1 && linetype != COMMENT);
}
if (!insideRS && (nextline.charAt(nextline.length() - 1) == ';'))
{
// most likely a single-line command, chomp the semicolon and return
targetBuf.append(nextline.substring(0, nextline.length() - 1));
// if we're getting runtime statistics, skip the next command inside a
// result set, it will be the command that we're getting RS for.
if (nextline.indexOf("GET_RUNTIMESTATISTICS()") > 0)
gotRuntimeStatistics = true;
break;
} else {
// unknown lines are assumed to be a part of a command if we got one previously.
// otherwise, must be part of a result set.
if (!gotCommand) {
// must be part of a result set
if (linetype == UNKNOWN_LINE || (linetype == COMMENT && !gotFirstComment && insideRS)) {
insideRS = true;
if (linetype == COMMENT) gotFirstComment = true;
targetBuf.append(nextline);
targetBuf.append("\n");
continue;
}
}
if (insideRS) {
if (linetype > -1) {
// got a command, comment or query. Result
// set is over and we went one line too far, hold it for
// the next call.
// put the first command found into the resulset if this is a
// RuntimeStatistics resultset.
if (gotRuntimeStatistics) {
gotRuntimeStatistics = false;
targetBuf.append(nextline);
targetBuf.append("\n");
continue;
}
tmpBuf.append(nextline);
break;
} else {
// got a row count or error, return it with the result set.
targetBuf.append(nextline);
break;
}
} else {
targetBuf.append(nextline);
if (gotCommand){
// multi-line command or comment, keep going till
// we get a semicolon. Add a space to avoid glomming
// multiple trimmed strings together.
targetBuf.append(" ");
continue;
} else{
// single line warning or error, finished
break;
}
}
}
}
return (c != -1);
}
private boolean haveNonCommand(String aLine)
{
int lType = getLineType(aLine);
return ((lType < 0) || (lType == COMMENT));
}
private boolean ignorableLine(int lineType)
{
return (lineType == IWARNING)
|| (lineType == BLANK_LINE);
}
/* TODO: It would be nice to automatically write the prologue to
* contain the proper sqlAuthorizationDecorator if we have
* multiple connections in the test, but this would require
* rethinking how the output is written, since the prologue
* could not be constructed until the rest of the output has
* been processed.
*/
private void writePrologue() throws IOException
{
final String prologueText =
"\npublic final class IJToJUnitTest extends BaseJDBCTestCase {\n\n" +
" /**\n" +
" * Public constructor required for running test as standalone JUnit.\n" +
" */\n" +
" public IJToJUnitTest(String name)\n" +
" {\n" +
" super(name);\n" +
" }\n\n" +
" public static Test suite()\n" +
" {\n" +
" BaseTestSuite suite = " +
"new BaseTestSuite(\"IJToJUnitTest Test\");\n" +
" suite.addTest(TestConfiguration.defaultSuite(IJToJUnitTest.class));\n" +
" return suite;\n" +
" }\n\n" +
" public void test_IJToJUnitTest() throws Exception\n" +
" {\n" +
" ResultSet rs = null;\n" +
" ResultSetMetaData rsmd;\n" +
" SQLWarning sqlWarn = null;\n\n" +
" PreparedStatement pSt;\n" +
" CallableStatement cSt;\n" +
" Statement st = createStatement();\n\n" +
" String [][] expRS;\n" +
" String [] expColNames;\n\n";
junit.write(prologueText.replaceAll("IJToJUnitTest", jTestName));
}
}
|