Sequence results = xquery.execute(query, null, AccessContext.TEST);
Assert.assertFalse("", !results.hasOne());
ElementImpl TC = (ElementImpl) results.toNodeSet().get(0).getNode();
//collect test case information
String folder = "";
String scenario = "";
String script = "";
//DateTimeValue scriptDateTime = null;
NodeListImpl inputFiles = new NodeListImpl();
NodeListImpl outputFiles = new NodeListImpl();
ElementImpl contextItem = null;
NodeListImpl modules = new NodeListImpl();
String expectedError = "";
String name = null;
NodeList childNodes = TC.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
switch (child.getNodeType()) {
case Node.ATTRIBUTE_NODE:
name = ((Attr)child).getName();
if (name.equals("FilePath"))
folder = ((Attr)child).getValue();
else if (name.equals("scenario"))
scenario = ((Attr)child).getValue();
break;
case Node.ELEMENT_NODE:
name = ((ElementImpl) child).getLocalName();
if (name.equals("query")) {
ElementImpl el = ((ElementImpl) child);
script = el.getAttribute("name");
//scriptDateTime = new DateTimeValue(el.getAttribute("date"));
}
else if (name.equals("input-file"))
inputFiles.add(child);
else if (name.equals("output-file"))
outputFiles.add(child);
else if (name.equals("contextItem"))
contextItem = (ElementImpl)child;
else if (name.equals("module"))
modules.add(child);
else if (name.equals("expected-error"))
expectedError = ((ElementImpl) child).getNodeValue();
break;
default :
;
}
}
Sequence result = null;
//compile & evaluate
File caseScript = new File(XQTS_folder+"Queries/XQuery/"+folder, script+".xq");
try {
XQueryContext context;
context = xquery.newContext(AccessContext.TEST);
//map modules' namespaces to location
Map<String, String> moduleMap = (Map<String, String>)broker.getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP);
for (int i = 0; i < modules.getLength(); i++) {
ElementImpl module = (ElementImpl)modules.item(i);
String id = module.getNodeValue();
moduleMap.put(module.getAttribute("namespace"), moduleSources.get(id));
}
broker.getConfiguration().setProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP, moduleMap);
//declare variable
for (int i = 0; i < inputFiles.getLength(); i++) {
ElementImpl inputFile = (ElementImpl)inputFiles.item(i);
String id = inputFile.getNodeValue();
//use DocUtils
//context.declareVariable(
//inputFile.getAttribute("variable"),
//DocUtils.getDocument(context, sources.get(id))
//);
//in-memory nodes
context.declareVariable(inputFile.getAttribute("variable"), loadVarFromURI(context, sources.get(id)));
}
Sequence contextSequence = null;
//set context item
if (contextItem != null) {
String id = contextItem.getNodeValue();
contextSequence = loadVarFromURI(context, sources.get(id));
}
fixBrokenTests(context, testGroup, testCase);
//compare result with one provided by test case
boolean ok = false;
Exception error = null;
if ("runtime-error".equals(scenario)) {
try {
//compile
CompiledXQuery compiled = xquery.compile(context, new FileSource(caseScript, "UTF8", true));
//execute
result = xquery.execute(compiled, contextSequence);
if (outputFiles.getLength() != 0) {
//can be answered
for (int i = 0; i < outputFiles.getLength(); i++) {
ElementImpl outputFile = (ElementImpl)outputFiles.item(i);
String compare = outputFile.getAttribute("compare");
if (compare != null && compare.equalsIgnoreCase("IGNORE")) {
ok = true;
break;
}
if (compareResult(script, "XQTS_1_0_3/ExpectedTestResults/"+folder, outputFile, result)) {
ok = true;
break;
}
}
} else {
error = catchError(result);
}
} catch (Exception e) {
error = e;
}
if (!ok && error != null && expectedError != null) {// error.getMessage().contains(expectedError)) {
ok = true;
}
} else {
//compile
CompiledXQuery compiled = xquery.compile(context, new FileSource(caseScript, "UTF8", true));
//execute
result = xquery.execute(compiled, contextSequence);
//check answer
for (int i = 0; i < outputFiles.getLength(); i++) {
ElementImpl outputFile = (ElementImpl)outputFiles.item(i);
String compare = outputFile.getAttribute("compare");
if (compare != null && compare.equalsIgnoreCase("IGNORE")) {
ok = true;
break;
}
if (compareResult(script, "XQTS_1_0_3/ExpectedTestResults/"+folder, outputFile, result)) {
ok = true;
break;
}
}
}
//collect information if result is wrong
if (!ok) {
StringBuilder message = new StringBuilder();
StringBuffer exp = new StringBuffer();
try {
for (int i = 0; i < outputFiles.getLength(); i++) {
ElementImpl outputFile = (ElementImpl)outputFiles.item(i);
String compare = outputFile.getAttribute("compare");
if (compare != null && compare.equalsIgnoreCase("IGNORE"))
continue;
File expectedResult = new File(XQTS_folder+"ExpectedTestResults/"+folder, outputFile.getNodeValue());
if (!expectedResult.canRead())
Assert.fail("can't read expected result");
//avoid to big output
if (expectedResult.length() >= 1024) {
exp = new StringBuffer();
exp.append("{TOO BIG}");
break;
} else {
exp.append("{'");
Reader reader = new BufferedReader(new FileReader(expectedResult));
char ch;
try {
while (reader.ready()) {
ch = (char)reader.read();
if (ch == '\r')
ch = (char)reader.read();
exp.append(String.valueOf(ch));
}
} finally {
reader.close();
}
exp.append("'}");
}
}
} catch (Exception e) {
exp.append(e.getMessage());
}
String res = "{NOTHING}";
if (result != null)
res = sequenceToString(result);
if (exp.length() == 0)
exp.append("error "+ expectedError);
StringBuilder data = new StringBuilder();
for (int i = 0; i < inputFiles.getLength(); i++) {
ElementImpl inputFile = (ElementImpl)inputFiles.item(i);
String id = inputFile.getNodeValue();
data.append(inputFile.getAttribute("variable"));
data.append(" = \n");
data.append(readFileAsString(new File(sources.get(id)), 1024));
data.append("\n");
}
message.append("\n");