if (args.length == 0) args = new String[] { "../xqts-0.9.4" };
File rootDir = new File(args[0]);
if (!rootDir.exists() || !rootDir.isDirectory()) {
throw new IllegalArgumentException("xqts dir does not exist: " + rootDir);
}
Document catalog = buildDocument(new File(rootDir, "XQTSCatalog.xml"));
String ns = "declare namespace ns = 'http://www.w3.org/2005/02/query-test-XQTSCatalog'; ";
// String version = XQueryUtil.xquery(catalog, ns + "ns:test-suite/@version").get(0).getValue();
File expectedDir = new File(rootDir,
XQueryUtil.xquery(catalog, ns + "ns:test-suite/@ResultOffsetPath").get(0).getValue());
File queryDir = new File(rootDir,
XQueryUtil.xquery(catalog, ns + "ns:test-suite/@XQueryQueryOffsetPath").get(0).getValue());
File testSourcesDir = new File(rootDir, "TestSources");
Nodes testCases = XQueryUtil.xquery(catalog, ns + "//ns:test-case");
for (int i=0; i < testCases.size(); i++) {
Node testCase = testCases.get(i);
// String groupTitle = XQueryUtil.xquery(testCase, ns + "../ns:GroupInfo/ns:title").get(0).getValue();
String path = XQueryUtil.xquery(testCase, "@FilePath").get(0).getValue();
File query = new File(new File(queryDir, path),
XQueryUtil.xquery(testCase, ns + "ns:query/@name").get(0).getValue() + ".xq");
String squery = readQuery(query);
System.out.println(i + ": " + query + " ...");
if (XQueryUtil.xquery(testCase, ns + "ns:spec-citation[@section-pointer='id-validate']").size() > 0) {
System.out.println(" ************* IGNORED SCHEMA AWARE FUNCTIONALITY *****");
continue; // ignore validate() function (nux is not schema aware)
}
if (squery == null) {
System.out.println(" ************* IGNORED *****");
continue;
}
Nodes inputs = XQueryUtil.xquery(testCase, ns + "ns:input-file");
Map vars = new HashMap();
for (int j=0; j < inputs.size(); j++) {
File input = new File(testSourcesDir, inputs.get(j).getValue() + ".xml");
String varName = ((Element) inputs.get(j)).getAttributeValue("variable");
Document inputDoc = buildDocument(input);
// System.out.println(inputDoc.getBaseURI());
if (true) XOMUtil.Normalizer.STRIP.normalize(inputDoc);
vars.put(varName, inputDoc);
}
Nodes expectedErrors = XQueryUtil.xquery(testCase, ns + "ns:expected-error");
Nodes expectedOutputs = XQueryUtil.xquery(testCase, ns + "ns:output-file");
boolean inspect = false;
for (int k=0; !inspect && k < expectedOutputs.size(); k++) {
String compare = ((Element)expectedOutputs.get(k)).getAttributeValue("compare");
if ("Inspect".equals(compare)) inspect = true;
}
Nodes results = null;
try { // here's where the query is actually executed
XQuery xquery = new XQuery(squery, testSourcesDir.toURI());
// XQuery xquery = new XQuery(squery, query.toURI());
// XQuery xquery = XQueryPool.GLOBAL_POOL.getXQuery(squery, query.toURI());
results = xquery.execute(null, null, vars).toNodes();
} catch (Throwable t) {
if (!inspect && expectedErrors.size() == 0) {
System.out.println(XOMUtil.toPrettyXML(testCase));
throw t;
}
if (!(t instanceof XQueryException)) throw t;
// System.out.println("expected error:" + t);
continue;
}
for (int k=0; k < expectedOutputs.size(); k++) {
File expectedOutput = new File(
new File(expectedDir, path), expectedOutputs.get(k).getValue());
String compare = ((Element)expectedOutputs.get(k)).getAttributeValue("compare");
if ("Text".equals(compare)) compare = "Fragment"; // see http://www.w3.org/Bugs/Public/show_bug.cgi?id=2476
try {
if (compare.equals("Text")) {
String expected = FileUtil.toString(
new FileInputStream(expectedOutput), UTF8);
String actual = serialize(results);
assertEquals(expected, actual);
} else if (compare.equals("XML")) {
Document expected = buildDocument(expectedOutput);
if (query.toString().indexOf("XQuery/UseCase/") >= 0) {
// input doc should not have whitespace
XOMUtil.Normalizer.STRIP.normalize(expected); // ???
}
Document actual = XOMUtil.toDocument(serialize(results));
assertEquals(expected, actual);
} else if (compare.equals("Fragment")) {
Document expected = buildFromSequence(expectedOutput);
Document actual = buildFromSequence(results);
assertEquals(expected, actual);
} else if (compare.equals("Ignore")) {
; // nothing to do
} else if (compare.equals("Inspect")) {
System.out.println("****************** Inspect output?");