| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.testdriver; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 13 |
import net.sf.saxon.functions.ResolveURI; |
| 14 |
import net.sf.saxon.lib.*; |
| 15 |
import net.sf.saxon.om.Sequence; |
| 16 |
import net.sf.saxon.om.TreeModel; |
| 17 |
import net.sf.saxon.s9api.*; |
| 18 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 19 |
import net.sf.saxon.trans.LicenseException; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
|
| 22 |
import javax.xml.stream.XMLStreamException; |
| 23 |
import javax.xml.transform.Source; |
| 24 |
import javax.xml.transform.TransformerException; |
| 25 |
import javax.xml.transform.URIResolver; |
| 26 |
import javax.xml.transform.stream.StreamSource; |
| 27 |
import java.io.File; |
| 28 |
import java.io.IOException; |
| 29 |
import java.io.StringWriter; |
| 30 |
import java.net.URI; |
| 31 |
import java.net.URISyntaxException; |
| 32 |
import java.util.*; |
| 33 |
|
| 34 |
import static net.sf.saxon.s9api.streams.Predicates.*; |
| 35 |
import static net.sf.saxon.s9api.streams.Steps.*; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class QT3TestDriverHE extends TestDriver { |
| 41 |
|
| 42 |
|
| 43 |
public static String RNS = "http://www.w3.org/2012/08/qt-fots-results"; |
| 44 |
public static String CNS = "http://www.w3.org/2010/09/qt-fots-catalog"; |
| 45 |
|
| 46 |
public Map<String, Dependency> getDependencyMap() { |
| 47 |
return dependencyMap; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public String catalogNamespace() { |
| 54 |
return CNS; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void go(String[] args) throws Exception { |
| 59 |
super.go(args); |
| 60 |
} |
| 61 |
|
| 62 |
public void processSpec(String specStr) { |
| 63 |
|
| 64 |
switch (specStr) { |
| 65 |
case "XP20": |
| 66 |
spec = Spec.XP20; |
| 67 |
break; |
| 68 |
case "XP30": |
| 69 |
spec = Spec.XP30; |
| 70 |
break; |
| 71 |
case "XP31": |
| 72 |
spec = Spec.XP31; |
| 73 |
break; |
| 74 |
case "XQ10": |
| 75 |
spec = Spec.XQ10; |
| 76 |
break; |
| 77 |
case "XQ30": |
| 78 |
spec = Spec.XQ30; |
| 79 |
break; |
| 80 |
case "XQ31": |
| 81 |
spec = Spec.XQ31; |
| 82 |
break; |
| 83 |
case "XT30": |
| 84 |
spec = Spec.XT30; |
| 85 |
break; |
| 86 |
default: |
| 87 |
throw new IllegalArgumentException("The specific language must be one of the following: XP20, XP30, XP31, XQ10, XQ30, XQ31, XT30"); |
| 88 |
} |
| 89 |
resultsDoc = new QT3TestReport(this, spec); |
| 90 |
} |
| 91 |
|
| 92 |
public static void main(String[] args) throws Exception { |
| 93 |
if (args.length == 0 || args[0].equals("-?")) { |
| 94 |
usage(); |
| 95 |
return; |
| 96 |
} |
| 97 |
new QT3TestDriverHE().go(args); |
| 98 |
} |
| 99 |
|
| 100 |
public static void usage() { |
| 101 |
System.err.println("java net.sf.saxon.testdriver.QT3TestSuiteDriverHE testsuiteDir catalog [-o:resultsdir] [-s:testSetName]" + |
| 102 |
" [-t:testNamePattern] [-unfolded] [-bytecode:on|off|debug] [-tree] [-lang:XP20|XP30|XQ10|XQ30]"); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
@Override |
| 108 |
protected void createGlobalEnvironments(XdmNode catalog, XPathCompiler xpc) throws SaxonApiException { |
| 109 |
Environment environment = null; |
| 110 |
for (XdmNode env : catalog.select(descendant("environment")).asList()) { |
| 111 |
try { |
| 112 |
environment = Environment.processEnvironment( |
| 113 |
this, xpc, env, globalEnvironments, localEnvironments.get("default")); |
| 114 |
} catch (NullPointerException ex) { |
| 115 |
ex.printStackTrace(); |
| 116 |
System.err.println("Failed to load environment"); |
| 117 |
} |
| 118 |
} |
| 119 |
try { |
| 120 |
buildDependencyMap(driverProc, catalog, environment); |
| 121 |
} catch (Exception ex) { |
| 122 |
System.err.println("Environment map error" + ex.getMessage()); |
| 123 |
ex.printStackTrace(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public boolean ensureDependencySatisfied(XdmNode dependency, Environment env) { |
| 137 |
String type = dependency.attribute("type"); |
| 138 |
String value = dependency.attribute("value"); |
| 139 |
boolean inverse = "false".equals(dependency.attribute("satisfied")); |
| 140 |
if ("saxon:config".equals(type)) { |
| 141 |
|
| 142 |
final String name = dependency.attribute("name"); |
| 143 |
final Object oldValue = env.processor.getConfigurationProperty(name); |
| 144 |
env.processor.setConfigurationProperty(name, value); |
| 145 |
env.resetActions.add(new Environment.ResetAction() { |
| 146 |
@Override |
| 147 |
public void reset(Environment env) { |
| 148 |
env.processor.setConfigurationProperty(name, oldValue); |
| 149 |
} |
| 150 |
}); |
| 151 |
return true; |
| 152 |
} else if ("xml-version".equals(type)) { |
| 153 |
if (value.equals("1.0:4-") && !inverse) { |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
if (value.contains("1.1") && !inverse) { |
| 158 |
if (treeModel.getName().equals("JDOM") || treeModel.getName().equals("JDOM2") || treeModel.getName().equals("XOM") || treeModel.getName().equals("Axiom")) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
if (env != null) { |
| 162 |
env.processor.setXmlVersion("1.1"); |
| 163 |
} else { |
| 164 |
return false; |
| 165 |
} |
| 166 |
} else if (value.contains("1.0") && !inverse) { |
| 167 |
if (env != null) { |
| 168 |
env.processor.setXmlVersion("1.0"); |
| 169 |
} else { |
| 170 |
return false; |
| 171 |
} |
| 172 |
} |
| 173 |
return true; |
| 174 |
} else if ("xsd-version".equals(type)) { |
| 175 |
final String old = env.processor.getConfigurationProperty(Feature.XSD_VERSION); |
| 176 |
if ("1.1".equals(value)) { |
| 177 |
if (env != null) { |
| 178 |
env.processor.setConfigurationProperty(Feature.XSD_VERSION, inverse ? "1.0" : "1.1"); |
| 179 |
} else { |
| 180 |
return false; |
| 181 |
} |
| 182 |
} else if ("1.0".equals(value)) { |
| 183 |
if (env != null) { |
| 184 |
env.processor.setConfigurationProperty(Feature.XSD_VERSION, inverse ? "1.1" : "1.0"); |
| 185 |
} else { |
| 186 |
return false; |
| 187 |
} |
| 188 |
} |
| 189 |
env.resetActions.add(new Environment.ResetAction() { |
| 190 |
public void reset(Environment env) { |
| 191 |
env.processor.setConfigurationProperty(Feature.XSD_VERSION, old); |
| 192 |
} |
| 193 |
}); |
| 194 |
return true; |
| 195 |
} else if ("limits".equals(type)) { |
| 196 |
return ("year_lt_0".equals(value) || "big_integer".equals(value)) && !inverse; |
| 197 |
} else if ("spec".equals(type)) { |
| 198 |
return isApplicableToSpecVersion(value, spec); |
| 199 |
} else if ("collection-stability".equals(type)) { |
| 200 |
|
| 201 |
return "false".equals(value) != inverse; |
| 202 |
} else if ("default-language".equals(type)) { |
| 203 |
final String old = (String) env.processor.getConfigurationProperty(Feature.DEFAULT_LANGUAGE); |
| 204 |
if (!value.equals(old)){ |
| 205 |
env.processor.setConfigurationProperty(Feature.DEFAULT_LANGUAGE, value); |
| 206 |
env.resetActions.add(new Environment.ResetAction() { |
| 207 |
public void reset(Environment env) { |
| 208 |
env.processor.setConfigurationProperty(Feature.DEFAULT_LANGUAGE, old); |
| 209 |
} |
| 210 |
}); |
| 211 |
} |
| 212 |
return true; |
| 213 |
} else if ("directory-as-collection-uri".equals(type)) { |
| 214 |
return "true".equals(value) != inverse; |
| 215 |
} else if ("language".equals(type)) { |
| 216 |
return isSupportedLanguage(value); |
| 217 |
|
| 218 |
} else if ("calendar".equals(type)) { |
| 219 |
return ("AD".equals(value) || "ISO".equals(value)) != inverse; |
| 220 |
} else if ("format-integer-sequence".equals(type)) { |
| 221 |
return !inverse; |
| 222 |
} else if ("unicode-normalization-form".equals(type)) { |
| 223 |
return value.equalsIgnoreCase("FULLY-NORMALIZED") == inverse; |
| 224 |
} else if ("feature".equals(type)) { |
| 225 |
String edition = env.processor.getSaxonEdition(); |
| 226 |
if ("namespace-axis".equals(value)) { |
| 227 |
return !inverse; |
| 228 |
} else if ("higherOrderFunctions".equals(value)) { |
| 229 |
return (edition.equals("PE") || edition.equals("EE")) ^ inverse; |
| 230 |
} else if ("schemaImport".equals(value) || "schemaValidation".equals(value) || "schemaAware".equals(value)) { |
| 231 |
return makeSchemaAware(env, inverse); |
| 232 |
} else if ("xpath-1.0-compatibility".equals(value)) { |
| 233 |
if (env != null && !env.processor.getSaxonEdition().equals("HE")) { |
| 234 |
env.xpathCompiler.setBackwardsCompatible(!inverse); |
| 235 |
return true; |
| 236 |
} else { |
| 237 |
return false; |
| 238 |
} |
| 239 |
} else if ("staticTyping".equals(value)) { |
| 240 |
return inverse; |
| 241 |
} else if ("remote_http".equals(value)) { |
| 242 |
return !inverse; |
| 243 |
} else if ("moduleImport".equals(value)) { |
| 244 |
return !inverse; |
| 245 |
} else if ("schema-location-hint".equals(value)) { |
| 246 |
return (edition.equals("PE") || edition.equals("EE")) ^ inverse; |
| 247 |
} else if ("infoset-dtd".equals(value)) { |
| 248 |
if (treeModel == TreeModel.TINY_TREE || treeModel == TreeModel.LINKED_TREE || treeModel == TreeModel.TINY_TREE_CONDENSED) { |
| 249 |
return !inverse; |
| 250 |
} else { |
| 251 |
return inverse; |
| 252 |
} |
| 253 |
} else if ("serialization".equals(value)) { |
| 254 |
return true; |
| 255 |
} else if ("non_unicode_codepoint_collation".equals(value)) { |
| 256 |
return !inverse; |
| 257 |
} else if ("non_empty_sequence_collection".equals(value)) { |
| 258 |
return !inverse; |
| 259 |
} else if ("fn-transform-XSLT".equals(value)) { |
| 260 |
return !inverse; |
| 261 |
} else if ("fn-transform-XSLT30".equals(value)) { |
| 262 |
return !inverse; |
| 263 |
} else if ("fn-load-xquery-module".equals(value)) { |
| 264 |
return edition.equals("EE") ^ inverse; |
| 265 |
} else if ("fn-format-integer-CLDR".equals(value)) { |
| 266 |
return (edition.equals("PE") || edition.equals("EE")) ^ inverse; |
| 267 |
} else if ("simple-uca-fallback".equals(value)) { |
| 268 |
return !inverse; |
| 269 |
} else if ("advanced-uca-fallback".equals(value)) { |
| 270 |
return (edition.equals("PE") || edition.equals("EE")) ^ inverse; |
| 271 |
} else { |
| 272 |
println("**** feature = " + value + " ????"); |
| 273 |
return false; |
| 274 |
} |
| 275 |
} else { |
| 276 |
println("**** dependency not recognized: " + type); |
| 277 |
return false; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
protected boolean makeSchemaAware(Environment env, boolean inverse) { |
| 282 |
|
| 283 |
return inverse; |
| 284 |
} |
| 285 |
|
| 286 |
protected boolean isSupportedLanguage(String language) { |
| 287 |
return "en".equals(language); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored"}) |
| 299 |
protected void runTestCase(XdmNode testCase, XPathCompiler xpc) throws SaxonApiException { |
| 300 |
boolean run = true; |
| 301 |
String hostLang; |
| 302 |
String langVersion; |
| 303 |
Spec specOpt = null; |
| 304 |
XPathCompiler xpath = driverProc.newXPathCompiler(); |
| 305 |
String testCaseName = testCase.attribute("name"); |
| 306 |
String testSetName = testCase.getParent().attribute("name"); |
| 307 |
XdmNode resultElement = testCase.select(child("result")).asNode(); |
| 308 |
boolean needSerializedResult = resultElement.select(descendant("assert-serialization-error")).exists() |
| 309 |
|| resultElement.select(descendant("serialization-matches")).exists(); |
| 310 |
boolean needResultValue = true; |
| 311 |
if (needSerializedResult) { |
| 312 |
needResultValue = resultElement.select(descendant(isElement()).where(not( |
| 313 |
hasLocalName("serialization-matches") |
| 314 |
.or(hasLocalName("assert-serialization-error")) |
| 315 |
.or(hasLocalName("any-of")) |
| 316 |
.or(hasLocalName("all-of"))))).exists(); |
| 317 |
} |
| 318 |
|
| 319 |
XdmNode alternativeResult = null; |
| 320 |
XdmNode optimization = null; |
| 321 |
|
| 322 |
|
| 323 |
hostLang = spec.shortSpecName; |
| 324 |
langVersion = spec.version; |
| 325 |
|
| 326 |
|
| 327 |
Environment env = getEnvironment(testCase, xpc); |
| 328 |
if (env == null) { |
| 329 |
writeTestcaseElement(testCaseName, "notRun", "test catalog error"); |
| 330 |
notrun++; |
| 331 |
return; |
| 332 |
} |
| 333 |
if (env.failedToBuild) { |
| 334 |
writeTestcaseElement(testCaseName, "fail", "unable to build environment"); |
| 335 |
noteFailure(testSetName, testCaseName); |
| 336 |
return; |
| 337 |
} |
| 338 |
if (!env.usable) { |
| 339 |
writeTestcaseElement(testCaseName, "n/a", "environment dependencies not satisfied"); |
| 340 |
notrun++; |
| 341 |
return; |
| 342 |
} |
| 343 |
env.xpathCompiler.setBackwardsCompatible(false); |
| 344 |
env.processor.setXmlVersion("1.0"); |
| 345 |
|
| 346 |
List<XdmNode> dependencies = testCase.getParent().select(child("dependency")).asList(); |
| 347 |
dependencies.addAll(testCase.select(child("dependency")).asList()); |
| 348 |
for (XdmNode dependency : dependencies) { |
| 349 |
String type = dependency.attribute("type"); |
| 350 |
if (type == null) { |
| 351 |
throw new IllegalStateException("dependency/@type is missing"); |
| 352 |
} |
| 353 |
String value = dependency.attribute("value"); |
| 354 |
if (value == null) { |
| 355 |
throw new IllegalStateException("dependency/@value is missing"); |
| 356 |
} |
| 357 |
|
| 358 |
if (type.equals("spec")) { |
| 359 |
boolean applicable = isApplicableToSpecVersion(value, spec); |
| 360 |
if (!applicable) { |
| 361 |
writeTestcaseElement(testCaseName, "n/a", "not" + spec.specAndVersion); |
| 362 |
notrun++; |
| 363 |
return; |
| 364 |
} |
| 365 |
} |
| 366 |
if (langVersion.equals("3.0") || langVersion.equals("3.1")) { |
| 367 |
EnvironmentVariableResolver resolver = new EnvironmentVariableResolver() { |
| 368 |
public Set<String> getAvailableEnvironmentVariables() { |
| 369 |
Set<String> strings = new HashSet<String>(); |
| 370 |
strings.add("QTTEST"); |
| 371 |
strings.add("QTTEST2"); |
| 372 |
strings.add("QTTESTEMPTY"); |
| 373 |
return strings; |
| 374 |
} |
| 375 |
|
| 376 |
public String getEnvironmentVariable(String name) { |
| 377 |
if (name.equals("QTTEST")) { |
| 378 |
return "42"; |
| 379 |
} else if (name.equals("QTTEST2")) { |
| 380 |
return "other"; |
| 381 |
} else if (name.equals("QTTESTEMPTY")) { |
| 382 |
return ""; |
| 383 |
} else { |
| 384 |
return null; |
| 385 |
} |
| 386 |
} |
| 387 |
}; |
| 388 |
env.processor.setConfigurationProperty(FeatureKeys.ENVIRONMENT_VARIABLE_RESOLVER, resolver); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
if (!ensureDependencySatisfied(dependency, env)) { |
| 403 |
if ("false".equals(dependency.attribute("satisfied"))) { |
| 404 |
type = "!" + type; |
| 405 |
} |
| 406 |
println("*** Dependency not satisfied: " + type + ":" + value); |
| 407 |
writeTestcaseElement(testCaseName, "n/a", "dependency not satisfied: " + type + ":" + value); |
| 408 |
|
| 409 |
notrun++; |
| 410 |
return; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
XdmNode exceptionElement = exceptionsMap.get(testCaseName); |
| 415 |
if (exceptionElement == null) { |
| 416 |
exceptionElement = exceptionsMap.get("$" + testSetName); |
| 417 |
} |
| 418 |
if (exceptionElement != null) { |
| 419 |
Optional<XdmNode> config = exceptionElement.select(child("configuration")).asOptionalNode(); |
| 420 |
|
| 421 |
String runAtt = exceptionElement.attribute("run"); |
| 422 |
Optional<String> reasonMsg = exceptionElement.select(child("reason")).asOptionalString(); |
| 423 |
String reportAtt = exceptionElement.attribute("report"); |
| 424 |
if (reportAtt == null) { |
| 425 |
reportAtt = ""; |
| 426 |
} |
| 427 |
|
| 428 |
if (config.isPresent()) { |
| 429 |
XdmItem paramValue = xpath.evaluateSingle("param[@name='not-unfolded' and @value='yes']/@name", config.get()); |
| 430 |
if (unfolded && paramValue != null) { |
| 431 |
writeTestcaseElement(testCaseName, "notRun", reasonMsg.orElse("no reason given")); |
| 432 |
notrun++; |
| 433 |
return; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
if ("false".equals(runAtt)) { |
| 438 |
writeTestcaseElement(testCaseName, reportAtt, reasonMsg.orElse("no reason given")); |
| 439 |
notrun++; |
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
alternativeResult = exceptionElement.select(child("result")).asOptionalNode().orElse(null); |
| 444 |
optimization = exceptionElement.select(child("optimization")).asOptionalNode().orElse(null); |
| 445 |
} |
| 446 |
|
| 447 |
if (run && (specOpt == null || specOpt == spec)) { |
| 448 |
|
| 449 |
TestOutcome outcome = new TestOutcome(this); |
| 450 |
String exp = null; |
| 451 |
try { |
| 452 |
exp = xpc.evaluate("if (test/@file) then unparsed-text(resolve-uri(test/@file, base-uri(.))) else string(test)", testCase).toString(); |
| 453 |
} catch (SaxonApiException err) { |
| 454 |
println("*** Failed to read query: " + err.getMessage()); |
| 455 |
if (err.getErrorCode().getLocalName().equals("FOUT1190")) { |
| 456 |
outcome.setException(new SaxonApiException(new XPathException(err.getMessage(), "XPST0003"))); |
| 457 |
} else { |
| 458 |
outcome.setException(err); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
if (exp != null && env.processor.getSaxonEdition().equals("HE") && testSetName.equals("app-spec-examples")) { |
| 463 |
|
| 464 |
|
| 465 |
if (exp.contains("http://www.w3.org/2013/collation/UCA?lang=en;alternate=blanked;strength=primary")) { |
| 466 |
writeTestcaseElement(testCaseName, "notRun", "requires ICU"); |
| 467 |
notrun++; |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
if (exp.contains("validate lax")) { |
| 472 |
writeTestcaseElement(testCaseName, "notRun", "requires schema-awareness"); |
| 473 |
notrun++; |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
if (exp.contains("'fallback':function($s)")) { |
| 478 |
writeTestcaseElement(testCaseName, "notRun", "requires HOF"); |
| 479 |
notrun++; |
| 480 |
return; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
if (outcome.getException() == null) { |
| 486 |
switch (hostLang) { |
| 487 |
case "XP": |
| 488 |
case "XT": { |
| 489 |
XPathCompiler testXpc = env.xpathCompiler; |
| 490 |
testXpc.setLanguageVersion(langVersion); |
| 491 |
testXpc.declareNamespace("fn", NamespaceConstant.FN); |
| 492 |
testXpc.declareNamespace("xs", NamespaceConstant.SCHEMA); |
| 493 |
|
| 494 |
testXpc.declareNamespace("map", NamespaceConstant.MAP_FUNCTIONS); |
| 495 |
testXpc.declareNamespace("array", NamespaceConstant.ARRAY_FUNCTIONS); |
| 496 |
|
| 497 |
copySchemaNamespaces(env, testXpc); |
| 498 |
|
| 499 |
|
| 500 |
ModuleResolver mr = new ModuleResolver(xpc); |
| 501 |
|
| 502 |
mr.setTestCase(testCase); |
| 503 |
testXpc.getProcessor().getUnderlyingConfiguration().setModuleURIResolver(mr); |
| 504 |
|
| 505 |
try { |
| 506 |
XPathSelector selector = testXpc.compile(exp).load(); |
| 507 |
for (QName varName : env.params.keySet()) { |
| 508 |
selector.setVariable(varName, env.params.get(varName)); |
| 509 |
} |
| 510 |
if (env.contextItem != null) { |
| 511 |
selector.setContextItem(env.contextItem); |
| 512 |
} |
| 513 |
selector.setURIResolver(new TestURIResolver(env)); |
| 514 |
|
| 515 |
if (env.unparsedTextResolver != null) { |
| 516 |
selector.getUnderlyingXPathContext().setUnparsedTextURIResolver(env.unparsedTextResolver); |
| 517 |
} |
| 518 |
XdmValue result = selector.evaluate(); |
| 519 |
outcome.setPrincipalResult(result); |
| 520 |
} catch (SaxonApiException err) { |
| 521 |
println(err.getMessage()); |
| 522 |
outcome.setException(err); |
| 523 |
} catch (Exception err) { |
| 524 |
println(err.getMessage()); |
| 525 |
err.printStackTrace(); |
| 526 |
writeTestcaseElement(testCaseName, "fail", "*** crashed: " + err.getMessage()); |
| 527 |
|
| 528 |
|
| 529 |
} |
| 530 |
break; |
| 531 |
} |
| 532 |
case "XQ": { |
| 533 |
XQueryCompiler testXqc = env.xqueryCompiler; |
| 534 |
testXqc.setLanguageVersion(langVersion); |
| 535 |
testXqc.declareNamespace("fn", NamespaceConstant.FN); |
| 536 |
testXqc.declareNamespace("xs", NamespaceConstant.SCHEMA); |
| 537 |
|
| 538 |
testXqc.declareNamespace("map", NamespaceConstant.MAP_FUNCTIONS); |
| 539 |
testXqc.declareNamespace("array", NamespaceConstant.ARRAY_FUNCTIONS); |
| 540 |
ErrorCollector errorCollector = new ErrorCollector(); |
| 541 |
testXqc.setErrorListener(errorCollector); |
| 542 |
String decVars = env.paramDecimalDeclarations.toString(); |
| 543 |
if (!decVars.isEmpty()) { |
| 544 |
int x = exp.indexOf("(:%DECL%:)"); |
| 545 |
if (x < 0) { |
| 546 |
exp = decVars + exp; |
| 547 |
} else { |
| 548 |
exp = exp.substring(0, x) + decVars + exp.substring(x + 13); |
| 549 |
} |
| 550 |
} |
| 551 |
String vars = env.paramDeclarations.toString(); |
| 552 |
if (!vars.isEmpty()) { |
| 553 |
int x = exp.indexOf("(:%VARDECL%:)"); |
| 554 |
if (x < 0) { |
| 555 |
exp = vars + exp; |
| 556 |
} else { |
| 557 |
exp = exp.substring(0, x) + vars + exp.substring(x + 13); |
| 558 |
} |
| 559 |
} |
| 560 |
ModuleResolver mr = new ModuleResolver(xpc); |
| 561 |
mr.setTestCase(testCase); |
| 562 |
testXqc.getProcessor().getUnderlyingConfiguration().setModuleURIResolver(mr); |
| 563 |
testXqc.setModuleURIResolver(mr); |
| 564 |
|
| 565 |
try { |
| 566 |
testXqc.getUnderlyingStaticContext().setModuleLocation( |
| 567 |
new ExplicitLocation(testXqc.getBaseURI().toString(), testCase.getLineNumber(), -1)); |
| 568 |
XQueryExecutable q = null; |
| 569 |
try { |
| 570 |
q = testXqc.compile(exp); |
| 571 |
} catch (SaxonApiException e) { |
| 572 |
println("Static error in query"); |
| 573 |
throw e; |
| 574 |
} |
| 575 |
if (optimization != null) { |
| 576 |
|
| 577 |
XdmDestination expDest = new XdmDestination(); |
| 578 |
Configuration config = driverProc.getUnderlyingConfiguration(); |
| 579 |
try { |
| 580 |
Receiver receiver = expDest.getReceiver(config.makePipelineConfiguration(), config.obtainDefaultSerializationProperties()); |
| 581 |
ExpressionPresenter presenter = new ExpressionPresenter(config, receiver); |
| 582 |
q.getUnderlyingCompiledQuery().explain(presenter); |
| 583 |
presenter.close(); |
| 584 |
} catch (XPathException e) { |
| 585 |
e.printStackTrace(); |
| 586 |
} |
| 587 |
XdmNode explanation = expDest.getXdmNode(); |
| 588 |
XPathCompiler assertXpc = xpc.getProcessor().newXPathCompiler(); |
| 589 |
XdmItem optResult = assertXpc.evaluateSingle(optimization.attribute("assert"), explanation); |
| 590 |
if (((XdmAtomicValue) optResult).getBooleanValue()) { |
| 591 |
println("Optimization result OK"); |
| 592 |
} else { |
| 593 |
println("Failed optimization test {" + optimization.attribute("assert") + "}"); |
| 594 |
driverProc.writeXdmValue(explanation, driverProc.newSerializer(System.err)); |
| 595 |
writeTestcaseElement(testCaseName, "fail", "Failed optimization assertions"); |
| 596 |
noteFailure(testSetName, testCaseName); |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
} |
| 601 |
XQueryEvaluator selector = q.load(); |
| 602 |
for (QName varName : env.params.keySet()) { |
| 603 |
selector.setExternalVariable(varName, env.params.get(varName)); |
| 604 |
} |
| 605 |
if (env.contextItem != null) { |
| 606 |
selector.setContextItem(env.contextItem); |
| 607 |
} |
| 608 |
selector.setURIResolver(new TestURIResolver(env)); |
| 609 |
if (env.unparsedTextResolver != null) { |
| 610 |
selector.getUnderlyingQueryContext().setUnparsedTextURIResolver(env.unparsedTextResolver); |
| 611 |
} |
| 612 |
if (needSerializedResult) { |
| 613 |
StringWriter sw = new StringWriter(); |
| 614 |
Serializer serializer = env.processor.newSerializer(sw); |
| 615 |
selector.setDestination(serializer); |
| 616 |
selector.run(); |
| 617 |
outcome.setPrincipalSerializedResult(sw.toString()); |
| 618 |
} |
| 619 |
if (needResultValue) { |
| 620 |
XdmValue result = selector.evaluate(); |
| 621 |
outcome.setPrincipalResult(result); |
| 622 |
} |
| 623 |
} catch (SaxonApiException err) { |
| 624 |
println("in TestSet " + testSetName); |
| 625 |
println(err.getMessage()); |
| 626 |
outcome.setException(err); |
| 627 |
outcome.setErrorsReported(errorCollector.getErrorCodes()); |
| 628 |
} catch (LicenseException err) { |
| 629 |
|
| 630 |
println("in TestSet " + testSetName); |
| 631 |
println(err.getMessage()); |
| 632 |
XPathException xe = new XPathException(err.getMessage(), "XPST0075"); |
| 633 |
outcome.setException(new SaxonApiException(xe)); |
| 634 |
try { |
| 635 |
errorCollector.error(xe); |
| 636 |
} catch (Exception e3) { |
| 637 |
|
| 638 |
} |
| 639 |
outcome.setErrorsReported(errorCollector.getErrorCodes()); |
| 640 |
} |
| 641 |
break; |
| 642 |
} |
| 643 |
default: |
| 644 |
writeTestcaseElement(testCaseName, "notRun", "No processor found"); |
| 645 |
notrun++; |
| 646 |
return; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
for (Environment.ResetAction action : env.resetActions) { |
| 651 |
action.reset(env); |
| 652 |
} |
| 653 |
env.resetActions.clear(); |
| 654 |
XdmNode assertion; |
| 655 |
if (alternativeResult != null) { |
| 656 |
assertion = (XdmNode) xpc.evaluateSingle("*[1]", alternativeResult); |
| 657 |
} else { |
| 658 |
assertion = (XdmNode) xpc.evaluateSingle("result/*[1]", testCase); |
| 659 |
} |
| 660 |
if (assertion == null) { |
| 661 |
println("*** No assertions found for test case " + testCaseName); |
| 662 |
writeTestcaseElement(testCaseName, "disputed", "No assertions in test case"); |
| 663 |
noteFailure(testSetName, testCaseName); |
| 664 |
return; |
| 665 |
} |
| 666 |
XPathCompiler assertXpc = env.processor.newXPathCompiler(); |
| 667 |
assertXpc.setLanguageVersion("3.1"); |
| 668 |
assertXpc.declareNamespace("fn", NamespaceConstant.FN); |
| 669 |
assertXpc.declareNamespace("xs", NamespaceConstant.SCHEMA); |
| 670 |
assertXpc.declareNamespace("math", NamespaceConstant.MATH); |
| 671 |
assertXpc.declareNamespace("map", NamespaceConstant.MAP_FUNCTIONS); |
| 672 |
assertXpc.declareNamespace("array", NamespaceConstant.ARRAY_FUNCTIONS); |
| 673 |
assertXpc.declareNamespace("j", NamespaceConstant.FN); |
| 674 |
assertXpc.declareVariable(new QName("result")); |
| 675 |
assertXpc.setBaseURI(assertion.getBaseURI()); |
| 676 |
|
| 677 |
copySchemaNamespaces(env, assertXpc); |
| 678 |
|
| 679 |
boolean success = outcome.testAssertion(assertion, outcome, outcome.getPrincipalResultDoc(), assertXpc, xpath, debug); |
| 680 |
if (success) { |
| 681 |
successes++; |
| 682 |
writeTestcaseElement(testCaseName, "pass", null); |
| 683 |
} else { |
| 684 |
XdmItem expectedError = xpc.evaluateSingle("result//error/@code", testCase); |
| 685 |
if (outcome.isException()){ |
| 686 |
if (expectedError != null){ |
| 687 |
wrongErrorResults++; |
| 688 |
successes++; |
| 689 |
if (outcome.getWrongErrorMessage() != null) { |
| 690 |
outcome.setComment(outcome.getWrongErrorMessage()); |
| 691 |
writeTestcaseElement(testCaseName, "wrongError", outcome.getComment()); |
| 692 |
} else { |
| 693 |
writeTestcaseElement(testCaseName, "wrongError", |
| 694 |
"Expected error:" + expectedError.getStringValue() + ", got " + outcome.getException().getErrorCode()); |
| 695 |
} |
| 696 |
println("*** TEST WRONG ERRORCODE: result " + outcome.getException().getErrorCode() + |
| 697 |
" Expected error:" + expectedError.getStringValue()); |
| 698 |
} else { |
| 699 |
noteFailure(testSetName, testCaseName); |
| 700 |
writeTestcaseElement(testCaseName, "fail", "Expected success, got " + outcome.getException().getErrorCode()); |
| 701 |
println("*** TEST-FAILURE: result " + outcome.getException().getErrorCode() + |
| 702 |
" Expected success."); |
| 703 |
} |
| 704 |
} else { |
| 705 |
noteFailure(testSetName, testCaseName); |
| 706 |
if (expectedError == null){ |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
writeTestcaseElement(testCaseName, "fail", "Wrong results, got " + |
| 711 |
truncate(outcome.serialize(assertXpc.getProcessor(), outcome.getPrincipalResultDoc()))); |
| 712 |
} else { |
| 713 |
writeTestcaseElement(testCaseName, "fail", |
| 714 |
"Expected error:" + expectedError.getStringValue() + ", got " + |
| 715 |
truncate(outcome.serialize(assertXpc.getProcessor(), outcome.getPrincipalResultDoc()))); |
| 716 |
} |
| 717 |
if (debug) { |
| 718 |
try { |
| 719 |
println("*** TEST-FAILURE. Result:"); |
| 720 |
StringWriter sw = new StringWriter(); |
| 721 |
driverSerializer.setOutputWriter(sw); |
| 722 |
driverSerializer.serializeXdmValue(outcome.getPrincipalResult()); |
| 723 |
println(sw.toString()); |
| 724 |
println("<======="); |
| 725 |
} catch (Exception err) { |
| 726 |
|
| 727 |
} |
| 728 |
|
| 729 |
} else { |
| 730 |
println("*** TEST-FAILURE (use -debug to show actual result)"); |
| 731 |
|
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
private static boolean isApplicableToSpecVersion(String value, Spec spec) { |
| 739 |
boolean applicable = false; |
| 740 |
if (!value.contains(spec.shortSpecName)) { |
| 741 |
applicable = false; |
| 742 |
} else if (value.contains(spec.specAndVersion)) { |
| 743 |
applicable = true; |
| 744 |
} else if ((spec.specAndVersion.equals("XQ30") || spec.specAndVersion.equals("XQ31")) && |
| 745 |
(value.contains("XQ10+") || value.contains("XQ30+"))) { |
| 746 |
applicable = true; |
| 747 |
} else if ((spec.specAndVersion.equals("XP30") || spec.specAndVersion.equals("XP31")) && |
| 748 |
(value.contains("XP20+") || value.contains("XP30+"))) { |
| 749 |
applicable = true; |
| 750 |
} |
| 751 |
return applicable; |
| 752 |
} |
| 753 |
|
| 754 |
private String truncate(String in) { |
| 755 |
if (in.length() > 80) { |
| 756 |
return in.substring(0, 80) + "..."; |
| 757 |
} else { |
| 758 |
return in; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
protected void writeResultFilePreamble(Processor processor, XdmNode catalog) throws IOException, SaxonApiException, XMLStreamException { |
| 764 |
resultsDoc.writeResultFilePreamble(processor, catalog); |
| 765 |
} |
| 766 |
|
| 767 |
protected void writeResultFilePostamble() throws XMLStreamException { |
| 768 |
resultsDoc.writeResultFilePostamble(); |
| 769 |
if (requestedTestSet != null) { |
| 770 |
System.err.println("Failing tests: "); |
| 771 |
for (Map.Entry<String, Integer> entry : failSummary.entrySet()) { |
| 772 |
System.err.println(" " + entry.getKey()); |
| 773 |
} |
| 774 |
} else { |
| 775 |
System.err.println("Failures by Test Set: "); |
| 776 |
for (Map.Entry<String, Integer> entry : failSummary.entrySet()) { |
| 777 |
System.err.println(" " + entry.getKey() + " : " + entry.getValue()); |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
protected void startTestSetElement(XdmNode testSetNode) { |
| 784 |
resultsDoc.startTestSetElement(testSetNode); |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
protected void writeTestSetEndElement() { |
| 789 |
resultsDoc.endElement(); |
| 790 |
} |
| 791 |
|
| 792 |
private void writeTestcaseElement(String name, String result, String comment) { |
| 793 |
resultsDoc.writeTestcaseElement(name, result, comment); |
| 794 |
} |
| 795 |
|
| 796 |
public static class ModuleResolver extends StandardModuleURIResolver { |
| 797 |
|
| 798 |
XPathCompiler catXPC; |
| 799 |
XdmNode testCase; |
| 800 |
|
| 801 |
public ModuleResolver(XPathCompiler xpc) { |
| 802 |
super(xpc.getProcessor().getUnderlyingConfiguration()); |
| 803 |
this.catXPC = xpc; |
| 804 |
} |
| 805 |
|
| 806 |
public void setTestCase(XdmNode testCase) { |
| 807 |
this.testCase = testCase; |
| 808 |
} |
| 809 |
|
| 810 |
public StreamSource[] resolve(String moduleURI, String baseURI, String[] locations) throws XPathException { |
| 811 |
try { |
| 812 |
XdmValue files = testCase.select(child("module").where(attributeEq("uri", moduleURI)) |
| 813 |
.then(attribute("file")).then(atomize())).asXdmValue(); |
| 814 |
if (files.size() == 0) { |
| 815 |
throw new XPathException("Failed to find module entry for " + moduleURI); |
| 816 |
} |
| 817 |
StreamSource[] ss = new StreamSource[files.size()]; |
| 818 |
for (int i = 0; i < files.size(); i++) { |
| 819 |
URI uri = testCase.getBaseURI().resolve(files.itemAt(i).toString()); |
| 820 |
ss[i] = getQuerySource(uri); |
| 821 |
} |
| 822 |
return ss; |
| 823 |
} catch (SaxonApiUncheckedException e) { |
| 824 |
throw new XPathException(e); |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
public static class TestURIResolver implements URIResolver { |
| 830 |
Environment env; |
| 831 |
|
| 832 |
public TestURIResolver(Environment env) { |
| 833 |
this.env = env; |
| 834 |
} |
| 835 |
|
| 836 |
public Source resolve(String href, String base) throws TransformerException { |
| 837 |
try { |
| 838 |
String abs = ResolveURI.makeAbsolute(href, base).toString(); |
| 839 |
XdmNode node = env.sourceDocs.get(abs); |
| 840 |
if (node == null) { |
| 841 |
node = env.sourceDocs.get(href); |
| 842 |
if (node == null) { |
| 843 |
return null; |
| 844 |
} |
| 845 |
} |
| 846 |
return node.asSource(); |
| 847 |
} catch (URISyntaxException e) { |
| 848 |
throw new XPathException("Invalid URI: " + e.getMessage(), "FODC0005"); |
| 849 |
} |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
public static Sequence<?> lazyLiteral(Sequence value) { |
| 864 |
return value; |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
private void buildDependencyMap(Processor processor, XdmNode catalog, Environment env) throws SaxonApiException { |
| 876 |
XQueryCompiler xqCompiler = processor.newXQueryCompiler(); |
| 877 |
xqCompiler.setBaseURI(new File(System.getProperty("user.dir")).toURI()); |
| 878 |
XQueryEvaluator eval = xqCompiler.compile( |
| 879 |
" declare namespace fots = \"http://www.w3.org/2010/09/qt-fots-catalog\";\n" + |
| 880 |
" let $testsets := //fots:test-set/@file/doc(resolve-uri(., exactly-one(base-uri(.))))\n" + |
| 881 |
" for $dependencyTS in $testsets//fots:dependency\n" + |
| 882 |
" let $type := $dependencyTS/@type\n" + |
| 883 |
" let $name := $dependencyTS/@name\n" + |
| 884 |
" let $value := $dependencyTS/@value\n" + |
| 885 |
" group by $type, $value\n" + |
| 886 |
" order by $type, $value\n" + |
| 887 |
" return <dependency type='{$type}' name='{$name}' value='{$value}' />").load(); |
| 888 |
eval.setContextItem(catalog); |
| 889 |
XdmValue result = eval.evaluate(); |
| 890 |
for (XdmItem item : result) { |
| 891 |
XdmNode node = (XdmNode) item; |
| 892 |
String type = node.attribute("type"); |
| 893 |
String value = node.attribute("value"); |
| 894 |
addDependency(type, value, ensureDependencySatisfied(node, env)); |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
protected class Dependency { |
| 902 |
public String dType; |
| 903 |
public boolean satisfied; |
| 904 |
} |
| 905 |
|
| 906 |
private Map<String, Dependency> dependencyMap = new HashMap<String, Dependency>(); |
| 907 |
|
| 908 |
public void addDependency(String depStr, String value, boolean satisfied) { |
| 909 |
if (!dependencyMap.containsKey(value)) { |
| 910 |
Dependency dep = new Dependency(); |
| 911 |
dep.dType = depStr; |
| 912 |
dep.satisfied = satisfied; |
| 913 |
dependencyMap.put(value, dep); |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
} |
| 919 |
|