KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > SqlToolHarness


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.test;
33
34 import java.io.BufferedInputStream JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileNotFoundException JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
41
42 // $Id: SqlToolHarness.java,v 1.13 2005/10/23 19:25:13 fredt Exp $
43

44 /**
45  * Runs SqlTool tests based upon metacommands embedded in comments in SQL
46  * files.
47  */

48 public class SqlToolHarness {
49
50     private static final int MAX_SQLFILE_LEN = 10240;
51     private static final String JavaDoc SYNTAX_MSG =
52         "SYNTAX: java org.hsqldb.test.SqlToolHarness file1.sql [file2.sq...]";
53
54     /**
55      * To test the SqlToolHarness class itself.
56      * (Basically, a sanity check).
57      *
58      * @param sa Each argument is a SQL file to process.
59      * @returns Exits with 0 or 1 depending on whether the last
60      * SqlToolHarness.execute() returned true or false (correspondingly).
61      */

62     public static void main(String JavaDoc[] sa)
63     throws IOException JavaDoc, InterruptedException JavaDoc {
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 JavaDoc(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 JavaDoc tmp = System.getProperty("VERBOSE");
96
97         Verbose = (tmp != null) && (tmp.trim().length() > 0);
98     }
99
100     private boolean Verbose = false;
101
102     /**
103      * Run SqlTool according to metacommands embedded in given SQL file.
104      *
105      * @param file SQL file
106      */

107     public boolean execute(File JavaDoc file)
108     throws IOException JavaDoc, InterruptedException JavaDoc {
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 JavaDoc[] args =
119             new String JavaDoc[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 JavaDoc stdout = execHarness.getStdout();
169         String JavaDoc 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 JavaDoc[] mtString = {};
223     private static Pattern JavaDoc[] mtPattern = {};
224
225     private class Metadata {
226
227         private byte[] ba = new byte[MAX_SQLFILE_LEN + 1];
228
229         public Metadata(File JavaDoc inFile)
230         throws FileNotFoundException JavaDoc, IOException JavaDoc {
231
232             String JavaDoc name, val;
233             String JavaDoc metaBlock = getHarnessMetaBlock(inFile);
234             /* This really only needed for debugging this class itself
235              * (SqlToolHarness).
236             if (Verbose) {
237                 System.err.println("METABLOCK {\n" + metaBlock + "}");
238             }
239             */

240             Pattern JavaDoc directivePattern =
241                 Pattern.compile("(?m)^\\s*(\\w+)\\s+(.*\\S+)?");
242             Matcher JavaDoc directiveMatcher = directivePattern.matcher(metaBlock);
243
244             while (directiveMatcher.find()) {
245                 if (directiveMatcher.groupCount() != 2) {
246                     throw new RuntimeException JavaDoc("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                     // TODO: Use a custom Exception class.
281
throw new IOException JavaDoc("Unknown Metadata directive: "
282                                           + name);
283                 }
284             }
285         }
286
287         private String JavaDoc[] toolargs = mtString;
288         private String JavaDoc[] jvmargs = mtString;
289         private Pattern JavaDoc[] requireStdoutPatterns = mtPattern;
290         private Pattern JavaDoc[] rejectStdoutPatterns = mtPattern;
291         private Pattern JavaDoc[] requireErroutPatterns = mtPattern;
292         private Pattern JavaDoc[] rejectErroutPatterns = mtPattern;
293         private boolean inputAsFile = false;
294         private Integer JavaDoc exitValue = new Integer JavaDoc(0);
295
296         public String JavaDoc toString() {
297
298             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc getHarnessMetaBlock(File JavaDoc inFile)
319         throws FileNotFoundException JavaDoc, IOException JavaDoc {
320
321             // The extra 1 is so we can request 1 more byte than we want.
322
// If that read is satisfied, we know that we read > MAX_SQLFILE_LEN
323
int i;
324             int writePointer = 0;
325             BufferedInputStream JavaDoc bis =
326                 new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(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 JavaDoc(inFile.toString() + " is larger than "
335                                       + (ba.length - 1) + " bytes.");
336             }
337
338             StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
339             StringBuffer JavaDoc sb2 = new StringBuffer JavaDoc();
340             Pattern JavaDoc commentPattern =
341                 Pattern.compile("(?s)(?<=/\\*).*?(?=\\*/)");
342             Pattern JavaDoc mdPattern = Pattern.compile(
343                 "(?m)(^\\s*HARNESS_METADATA\\s+BEGIN\\s*^)(?s)(.*?$)(?-s)"
344                 + "(\\s*HARNESS_METADATA\\s+END\\s*$)");
345             Matcher JavaDoc commentMatcher = commentPattern.matcher(new String JavaDoc(ba, 0,
346                 writePointer));
347
348             while (commentMatcher.find()) {
349                 sb1.append(commentMatcher.group() + '\n');
350             }
351
352             Matcher JavaDoc 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 JavaDoc[] push(Pattern JavaDoc newTail, Pattern JavaDoc[] pataIn) {
367
368         Pattern JavaDoc[] pataOut = new Pattern JavaDoc[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 JavaDoc patternArrayToString(Pattern JavaDoc[] pata) {
380
381         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("{");
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