01: package gnu.xquery.testsuite;
02:
03: import java.util.Stack;
04: import gnu.lists.*;
05: import gnu.xml.*;
06: import gnu.mapping.CharArrayOutPort;
07: import gnu.kawa.xml.Document;
08:
09: /** Run a suite of XQuery tests, as read from an xml file. */
10:
11: public class TestSuite extends FilterConsumer {
12: public static void main(String[] args) {
13: gnu.xquery.lang.XQuery.registerEnvironment();
14: for (int i = 0; i < args.length; i++) {
15: try {
16: Document.parse(args[i], new TestSuite());
17: } catch (Throwable ex) {
18: System.err.println("caught " + ex
19: + " while processing " + args[i]);
20: }
21: }
22: TestMisc.printSummary();
23: }
24:
25: int nesting = 0;
26: boolean inTestSuite = false;
27: boolean inTest = false;
28: String currentTag;
29: /* #ifdef JAVA5 */
30: // Stack<Object> elementStack = new Stack<Object>();
31: /* #else */
32: Stack elementStack = new Stack();
33: /* #endif */
34:
35: CharArrayOutPort cout;
36: XMLPrinter xout;
37:
38: String query = null;
39: String expect = null;
40:
41: private TestSuite() {
42: this (new CharArrayOutPort());
43: }
44:
45: private TestSuite(CharArrayOutPort cout) {
46: this (cout, new XMLPrinter(cout));
47: xout.escapeText = false;
48: }
49:
50: private TestSuite(CharArrayOutPort cout, XMLPrinter xout) {
51: super (xout);
52: this .cout = cout;
53: this .xout = xout;
54: }
55:
56: public void startElement(Object type) {
57: String typeName = type.toString();
58: if ("testsuite".equals(typeName) && nesting == 0)
59: inTestSuite = true;
60: else if ("test".equals(typeName)
61: && (nesting == 0 || (inTestSuite && nesting == 1)))
62: inTest = true;
63: else if (inTestSuite ? nesting == 2 : nesting == 1) {
64: cout.setLength(0);
65: currentTag = typeName;
66: } else if (currentTag == null)
67: throw new RuntimeException("saw <" + typeName
68: + "> not in <test>");
69: else
70: base.startElement(type);
71: nesting++;
72: elementStack.push(type);
73: }
74:
75: public void endElement() {
76: nesting--;
77: Object type = elementStack.pop();
78: String typeName = type.toString();
79: if ("testsuite".equals(typeName) && nesting == 0)
80: inTestSuite = false;
81: else if ("test".equals(typeName)
82: && (nesting == 0 || (inTestSuite && nesting == 1))) {
83: inTest = false;
84: TestMisc.evalTest(query, expect);
85: } else if (inTestSuite ? nesting == 2 : nesting == 1) {
86: if ("query".equals(typeName)) {
87: xout.flush();
88: query = cout.toString();
89: } else if ("expect".equals(typeName)) {
90: xout.flush();
91: expect = cout.toString();
92: }
93: currentTag = null;
94: } else
95: base.endElement();
96: }
97: }
|