1 30 31 32 package org.hsqldb.test; 33 34 import java.io.BufferedInputStream ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileNotFoundException ; 38 import java.io.IOException ; 39 import java.util.regex.Matcher ; 40 import java.util.regex.Pattern ; 41 42 44 48 public class SqlToolHarness { 49 50 private static final int MAX_SQLFILE_LEN = 10240; 51 private static final String SYNTAX_MSG = 52 "SYNTAX: java org.hsqldb.test.SqlToolHarness file1.sql [file2.sq...]"; 53 54 62 public static void main(String [] sa) 63 throws IOException , InterruptedException { 64 65 if (sa.length > 0 && sa[0].equals("-v")) { 66 sa = ExecHarness.shift(sa); 67 68 System.setProperty("VERBOSE", "true"); 69 } 70 71 if (sa.length < 1) { 72 System.err.println(SYNTAX_MSG); 73 System.exit(1); 74 } 75 76 SqlToolHarness harness = new SqlToolHarness(); 77 boolean result = true; 78 79 for (int i = 0; i < sa.length; i++) { 80 result = harness.execute(new File (sa[i])); 81 82 System.err.println(sa[i] + " ==> " + result); 83 } 84 85 System.exit(result ? 0 86 : 1); 87 } 88 89 ExecHarness execHarness; 90 91 public SqlToolHarness() { 92 93 execHarness = new ExecHarness("java"); 94 95 String tmp = System.getProperty("VERBOSE"); 96 97 Verbose = (tmp != null) && (tmp.trim().length() > 0); 98 } 99 100 private boolean Verbose = false; 101 102 107 public boolean execute(File file) 108 throws IOException , InterruptedException { 109 110 Metadata md = new Metadata(file); 111 112 if (Verbose) { 113 System.err.println("HARNESS METADATA:\n" + md); 114 } 115 116 execHarness.clear(); 117 118 String [] args = 119 new String [md.jvmargs.length + 1 + md.toolargs.length + (md.inputAsFile ? 1 120 : 0)]; 121 int argIndex = 0; 122 123 for (int i = 0; i < md.jvmargs.length; i++) { 124 args[argIndex++] = md.jvmargs[i]; 125 } 126 127 args[argIndex++] = org.hsqldb.util.SqlTool.class.getName(); 128 129 for (int i = 0; i < md.toolargs.length; i++) { 130 args[argIndex++] = md.toolargs[i]; 131 } 132 133 if (md.inputAsFile) { 134 args[argIndex++] = file.toString(); 135 } else { 136 execHarness.setInput(file); 137 } 138 139 if (Verbose) { 140 System.err.println("ALL ARGS: " 141 + ExecHarness.stringArrayToString(args)); 142 } 143 144 execHarness.setArgs(args); 145 execHarness.exec(); 146 147 if (Verbose) { 148 System.err.println( 149 "STDOUT ******************************************"); 150 System.out.print(execHarness.getStdout()); 151 System.err.println( 152 "ERROUT ******************************************"); 153 System.err.print(execHarness.getErrout()); 154 System.err.println( 155 "*************************************************"); 156 } 157 158 if (md.exitValue != null) { 159 if (md.exitValue.intValue() != execHarness.getExitValue()) { 160 if (Verbose) { 161 System.err.println("Failed exit value test"); 162 } 163 164 return false; 165 } 166 } 167 168 String stdout = execHarness.getStdout(); 169 String errout = execHarness.getErrout(); 170 171 for (int i = 0; i < md.rejectErroutPatterns.length; i++) { 172 if (md.rejectErroutPatterns[i].matcher(errout).find()) { 173 if (Verbose) { 174 System.err.println("Failed rejectErrOut regex '" 175 + md.rejectErroutPatterns[i].pattern() 176 + "'"); 177 } 178 179 return false; 180 } 181 } 182 183 for (int i = 0; i < md.rejectStdoutPatterns.length; i++) { 184 if (md.rejectStdoutPatterns[i].matcher(stdout).find()) { 185 if (Verbose) { 186 System.err.println("Failed rejectStdout regex '" 187 + md.rejectStdoutPatterns[i].pattern() 188 + "'"); 189 } 190 191 return false; 192 } 193 } 194 195 for (int i = 0; i < md.requireErroutPatterns.length; i++) { 196 if (!md.requireErroutPatterns[i].matcher(errout).find()) { 197 if (Verbose) { 198 System.err.println("Failed requireErrorOut regex '" 199 + md.requireErroutPatterns[i].pattern() 200 + "'"); 201 } 202 203 return false; 204 } 205 } 206 207 for (int i = 0; i < md.requireStdoutPatterns.length; i++) { 208 if (!md.requireStdoutPatterns[i].matcher(stdout).find()) { 209 if (Verbose) { 210 System.err.println("Failed requireStdOut regex '" 211 + md.requireStdoutPatterns[i].pattern() 212 + "'"); 213 } 214 215 return false; 216 } 217 } 218 219 return true; 220 } 221 222 private static String [] mtString = {}; 223 private static Pattern [] mtPattern = {}; 224 225 private class Metadata { 226 227 private byte[] ba = new byte[MAX_SQLFILE_LEN + 1]; 228 229 public Metadata(File inFile) 230 throws FileNotFoundException , IOException { 231 232 String name, val; 233 String metaBlock = getHarnessMetaBlock(inFile); 234 240 Pattern directivePattern = 241 Pattern.compile("(?m)^\\s*(\\w+)\\s+(.*\\S+)?"); 242 Matcher directiveMatcher = directivePattern.matcher(metaBlock); 243 244 while (directiveMatcher.find()) { 245 if (directiveMatcher.groupCount() != 2) { 246 throw new RuntimeException ("Pattern '" + directivePattern 247 + " matched " 248 + directiveMatcher.groupCount() 249 + " groups."); 250 } 251 252 name = directiveMatcher.group(1); 253 val = ((directiveMatcher.groupCount() == 2) 254 ? directiveMatcher.group(2) 255 : null); 256 257 if (name.equals("arg")) { 258 toolargs = ExecHarness.push(val, toolargs); 259 } else if (name.equals("jvmarg")) { 260 jvmargs = ExecHarness.push(val, jvmargs); 261 } else if (name.equals("requireStdoutRegex")) { 262 requireStdoutPatterns = push(Pattern.compile(val), 263 requireStdoutPatterns); 264 } else if (name.equals("rejectStdoutRegex")) { 265 rejectStdoutPatterns = push(Pattern.compile(val), 266 rejectStdoutPatterns); 267 } else if (name.equals("requireErroutRegex")) { 268 requireErroutPatterns = push(Pattern.compile(val), 269 requireErroutPatterns); 270 } else if (name.equals("rejectErroutRegex")) { 271 rejectErroutPatterns = push(Pattern.compile(val), 272 rejectErroutPatterns); 273 } else if (name.equals("inputAsFile")) { 274 inputAsFile = Boolean.valueOf(val).booleanValue(); 275 } else if (name.equals("exitValue")) { 276 exitValue = ((val == null) ? null 277 : Integer.valueOf(val)); 278 } else { 279 280 throw new IOException ("Unknown Metadata directive: " 282 + name); 283 } 284 } 285 } 286 287 private String [] toolargs = mtString; 288 private String [] jvmargs = mtString; 289 private Pattern [] requireStdoutPatterns = mtPattern; 290 private Pattern [] rejectStdoutPatterns = mtPattern; 291 private Pattern [] requireErroutPatterns = mtPattern; 292 private Pattern [] rejectErroutPatterns = mtPattern; 293 private boolean inputAsFile = false; 294 private Integer exitValue = new Integer (0); 295 296 public String toString() { 297 298 StringBuffer sb = new StringBuffer (); 299 300 sb.append(" TOOLARGS: " 301 + ExecHarness.stringArrayToString(toolargs) + '\n'); 302 sb.append(" JVMARGS: " 303 + ExecHarness.stringArrayToString(jvmargs) + '\n'); 304 sb.append(" REQUIRE_STDOUT_PATTERNS: " 305 + patternArrayToString(requireStdoutPatterns) + '\n'); 306 sb.append(" REJECT_STDOUT_PATTERNS: " 307 + patternArrayToString(rejectStdoutPatterns) + '\n'); 308 sb.append(" REQUIRE_ERROUT_PATTERNS: " 309 + patternArrayToString(requireErroutPatterns) + '\n'); 310 sb.append(" REJECT_ERROUT_PATTERNS: " 311 + patternArrayToString(rejectErroutPatterns) + '\n'); 312 sb.append(" INPUTASFILE: " + inputAsFile + '\n'); 313 sb.append(" EXITVALUE: " + exitValue + '\n'); 314 315 return sb.toString(); 316 } 317 318 private String getHarnessMetaBlock(File inFile) 319 throws FileNotFoundException , IOException { 320 321 int i; 324 int writePointer = 0; 325 BufferedInputStream bis = 326 new BufferedInputStream (new FileInputStream (inFile)); 327 328 while ((i = bis.read(ba, writePointer, ba.length - writePointer)) 329 > 0) { 330 writePointer += i; 331 } 332 333 if (i > -1) { 334 throw new IOException (inFile.toString() + " is larger than " 335 + (ba.length - 1) + " bytes."); 336 } 337 338 StringBuffer sb1 = new StringBuffer (); 339 StringBuffer sb2 = new StringBuffer (); 340 Pattern commentPattern = 341 Pattern.compile("(?s)(?<=/\\*).*?(?=\\*/)"); 342 Pattern mdPattern = Pattern.compile( 343 "(?m)(^\\s*HARNESS_METADATA\\s+BEGIN\\s*^)(?s)(.*?$)(?-s)" 344 + "(\\s*HARNESS_METADATA\\s+END\\s*$)"); 345 Matcher commentMatcher = commentPattern.matcher(new String (ba, 0, 346 writePointer)); 347 348 while (commentMatcher.find()) { 349 sb1.append(commentMatcher.group() + '\n'); 350 } 351 352 Matcher mdMatcher = mdPattern.matcher(sb1); 353 354 while (mdMatcher.find()) { 355 if (mdMatcher.groupCount() != 3) { 356 continue; 357 } 358 359 sb2.append(mdMatcher.group(2) + '\n'); 360 } 361 362 return sb2.toString(); 363 } 364 } 365 366 public static Pattern [] push(Pattern newTail, Pattern [] pataIn) { 367 368 Pattern [] pataOut = new Pattern [pataIn.length + 1]; 369 370 for (int i = 0; i < pataIn.length; i++) { 371 pataOut[i] = pataIn[i]; 372 } 373 374 pataOut[pataOut.length - 1] = newTail; 375 376 return pataOut; 377 } 378 379 public static String patternArrayToString(Pattern [] pata) { 380 381 StringBuffer sb = new StringBuffer ("{"); 382 383 for (int i = 0; i < pata.length; i++) { 384 if (i > 0) { 385 sb.append(','); 386 } 387 388 sb.append(pata[i].pattern()); 389 } 390 391 return sb.toString() + '}'; 392 } 393 } 394 | Popular Tags |