public ErrorCollector(TestOutcome out1) : base() { outcome1 = out1; }
/** * Run a test case * * * @param testCase the test case element in the catalog * @param xpc the XPath compiler to be used for compiling XPath expressions against the catalog * @ */ protected override void runTestCase(XdmNode testCase, XPathCompiler xpc) { bool run = true; bool xpDependency = false; string hostLang; string langVersion; Spec specOpt = Spec.NULL; XPathCompiler xpath = driverProc.NewXPathCompiler(); string testCaseName = testCase.GetAttributeValue(new QName("name")); string testSetName = testCase.Parent.GetAttributeValue(new QName("name")); bool needSerializedResult = ((XdmAtomicValue)xpc.EvaluateSingle( "exists(./result//assert-serialization-error) or exists(./result//serialization-matches)", testCase)).GetBooleanValue(); bool needResultValue = true; if (needSerializedResult) { needResultValue = ((XdmAtomicValue)xpc.EvaluateSingle( "exists(./result//*[not(self::serialization-matches or self::assert-serialization-error or self::any-of or self::all-of)])", testCase)).GetBooleanValue(); } XdmNode alternativeResult = null; XdmNode optimization = null; hostLang = ((SpecAttr)spec.GetAttr()).sname; langVersion = ((SpecAttr)spec.GetAttr()).version; Environment env = getEnvironment(testCase, xpc); if (env == null) { notrun++; return; } env.xpathCompiler.BackwardsCompatible = false; env.processor.XmlVersion = (decimal)1.0; //test /*bool icuColCheck = net.sf.saxon.Version.platform.hasICUCollator (); * bool icuNumCheck = net.sf.saxon.Version.platform.hasICUNumberer(); * Console.WriteLine ("ICUCol: " + (icuColCheck ? "true" : "false")); * Console.WriteLine ("ICUNum: " + (icuNumCheck ? "true" : "false"));*/ //end of test foreach (XdmItem dependency in xpc.Evaluate("/*/dependency, ./dependency", testCase)) { string type = ((XdmNode)dependency).GetAttributeValue(new QName("type")); if (type == null) { // throw new IllegalStateException("dependency/@type is missing"); //TODO } string value = ((XdmNode)dependency).GetAttributeValue(new QName("value")); if (value == null) { //throw new IllegalStateException("dependency/@value is missing"); //TODO } if (type.Equals("spec")) { bool applicable = false; if (!value.Contains(((SpecAttr)spec.GetAttr()).sname)) { applicable = false; } else if (value.Contains(((SpecAttr)spec.GetAttr()).svname)) { applicable = true; } else if ((((SpecAttr)spec.GetAttr()).svname.Equals("XQ30") || ((SpecAttr)spec.GetAttr()).svname.Equals("XQ31")) && (value.Contains("XQ10+") || value.Contains("XQ30+"))) { applicable = true; } else if ((((SpecAttr)spec.GetAttr()).svname.Equals("XP30") || ((SpecAttr)spec.GetAttr()).svname.Equals("XP31")) && (value.Contains("XP20+") || value.Contains("XP30+"))) { applicable = true; } if (!applicable) { writeTestcaseElement(testCaseName, "n/a", "not" + ((SpecAttr)spec.GetAttr()).svname, spec); notrun++; return; } } if (langVersion.Equals("3.0")) { /* EnvironmentVariableResolver resolver = new EnvironmentVariableResolver() { * public Set<string> getAvailableEnvironmentVariables() { * Set<string> strings = new HashSet<string>(); * strings.add("QTTEST"); * strings.add("QTTEST2"); * strings.add("QTTESTEMPTY"); * return strings; * } * * public string getEnvironmentVariable(string name) { * if (name.Equals("QTTEST")) { * return "42"; * } else if (name.Equals("QTTEST2")) { * return "other"; * } else if (name.Equals("QTTESTEMPTY")) { * return ""; * } else { * return null; * } * } * }; */ //TODO //env.processor.SetProperty(JFeatureKeys.ENVIRONMENT_VARIABLE_RESOLVER, resolver); } /*if (type.Equals("feature") && value.Equals("xpath-1.0-compatibility")) * { * hostLang = "XP"; * langVersion = "3.0"; * xpDependency = true; * specOpt = Spec.XP30; * } * if (type.Equals("feature") && value.Equals("namespace-axis")) * { * hostLang = "XP"; * langVersion = "3.0"; * xpDependency = true; * specOpt = Spec.XP30; * }*/ if (!dependencyIsSatisfied((XdmNode)dependency, env)) { println("*** Dependency not satisfied: " + ((XdmNode)dependency).GetAttributeValue(new QName("type"))); writeTestcaseElement(testCaseName, "n/a", "Dependency not satisfied", spec); run = false; notrun++; return; } } XdmNode exceptionElement; try{ exceptionElement = exceptionsMap[testCaseName]; } catch (Exception) { exceptionElement = null; } if (exceptionElement != null) { XdmItem config = xpath.EvaluateSingle("configuration", exceptionElement); string runAtt = exceptionElement.GetAttributeValue(new QName("run")); string reasonMsg = xpath.EvaluateSingle("reason", exceptionElement).ToString(); string reportAtt = exceptionElement.GetAttributeValue(new QName("report")); if (config != null) { XdmItem paramValue = xpath.EvaluateSingle("param[@name='not-unfolded' and @value='yes']/@name", config); if (unfolded && paramValue != null) { writeTestcaseElement(testCaseName, "notRun", reasonMsg, spec); notrun++; return; } } if ("false".Equals(runAtt)) { writeTestcaseElement(testCaseName, reportAtt, reasonMsg, spec); notrun++; return; } alternativeResult = (XdmNode)xpc.EvaluateSingle("result", exceptionElement); optimization = (XdmNode)xpc.EvaluateSingle("optimization", exceptionElement); } if (run && (specOpt == Spec.NULL || specOpt == spec)) { TestOutcome outcome = new TestOutcome(this); string exp = null; try { exp = xpc.Evaluate("if (test/@file) then unparsed-text(resolve-uri(test/@file, base-uri(.))) else string(test)", testCase).ToString(); } catch (Exception err) { println("*** Failed to read query: " + err.Message); outcome.SetException((DynamicError)err); } //noinspection ThrowableResultOfMethodCallIgnored if (outcome.GetException() == null) { if (hostLang.Equals("XP") || hostLang.Equals("XT")) { XPathCompiler testXpc = env.xpathCompiler; testXpc.XPathLanguageVersion = langVersion; testXpc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions"); testXpc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema"); testXpc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map"); //testXpc.DeclareNamespace("math", NamespaceConstant.MATH); //testXpc.DeclareNamespace("Dictionary", NamespaceConstant.Dictionary_FUNCTIONS); try { XPathSelector selector = testXpc.Compile(exp).Load(); foreach (QName varName in env.params1.Keys) { selector.SetVariable(varName, env.params1[varName]); } if (env.contextItem != null) { selector.ContextItem = env.contextItem; } selector.InputXmlResolver = new TestUriResolver(env); if (env.unparsedTextResolver != null) { //selector.getUnderlyingXPathContext().setUnparsedTextURIResolver(env.unparsedTextResolver); //TODO } XdmValue result = selector.Evaluate(); outcome.SetPrincipalResult(result); } catch (Exception err) { println(err.Message); outcome.SetException(err); } } else if (hostLang.Equals("XQ")) { XQueryCompiler testXqc = env.xqueryCompiler; testXqc.XQueryLanguageVersion = langVersion; testXqc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions"); testXqc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema"); //testXqc.DeclareNamespace("math", NamespaceConstant.MATH); testXqc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map"); testXqc.DeclareNamespace("array", "http://www.w3.org/2005/xpath-functions/array"); // ErrorCollector errorCollector = new ErrorCollector(); testXqc.ErrorList = new List <StaticError>(); string decVars = env.paramDecimalDeclarations.ToString(); if (decVars.Length != 0) { int x = (exp.IndexOf("(:%DECL%:)")); if (x < 0) { exp = decVars + exp; } else { exp = exp.Substring(0, x) + decVars + exp.Substring(x + 13); } } string vars = env.paramDeclarations.ToString(); if (vars.Length != 0) { int x = (exp.IndexOf("(:%VARDECL%:)")); if (x < 0) { exp = vars + exp; } else { exp = exp.Substring(0, x) + vars + exp.Substring(x + 13); } } ModuleResolver mr = new ModuleResolver(xpc); mr.setTestCase(testCase); testXqc.QueryResolver = mr; // .setModuleURIResolver(mr); //TODO testXqc.QueryResolver = mr; try { XQueryExecutable q = testXqc.Compile(exp); if (optimization != null) { // Test whether required optimizations have been performed XdmDestination expDest = new XdmDestination(); JConfiguration config = driverProc.Implementation; //ExpressionPresenter presenter = new ExpressionPresenter(config, expDest.getReceiver(config)); //q.getUnderlyingCompiledQuery().explain(presenter); //presenter.close(); XdmNode explanation = expDest.XdmNode; XdmItem optResult = xpc.EvaluateSingle(optimization.GetAttributeValue(new QName("assert")), explanation); if (((XdmAtomicValue)optResult).GetBooleanValue()) { println("Optimization result OK"); } else { println("Failed optimization test"); Serializer ser = env.processor.NewSerializer(); ser.SetOutputStream((Stream)System.Console.OpenStandardError()); driverProc.WriteXdmValue(explanation, ser); writeTestcaseElement(testCaseName, "fail", "Failed optimization assertions", spec); failures++; return; } } XQueryEvaluator selector = q.Load(); foreach (QName varName in env.params1.Keys) { selector.SetExternalVariable(varName, env.params1[varName]); } if (env.contextItem != null) { selector.ContextItem = env.contextItem; } selector.InputXmlResolver = env; //selector.InputXmlResolver = .SetURIResolver(new TestURIResolver(env)); //TODO if (env.unparsedTextResolver != null) { selector.Implementation.getUnderlyingQueryContext().setUnparsedTextURIResolver(env.unparsedTextResolver);// TODO } if (needSerializedResult) { StringWriter sw = new StringWriter(); Serializer serializer = env.processor.NewSerializer(); serializer.SetOutputWriter(sw); selector.Run(serializer); outcome.SetPrincipalSerializedResult(sw.ToString()); } if (needResultValue) { XdmValue result = selector.Evaluate(); outcome.SetPrincipalResult(result); } } catch (Exception err) { println("in TestSet " + testSetName + err.StackTrace); println(err.Message); outcome.SetException(err); outcome.SetErrorsReported(testXqc.ErrorList); } } else { writeTestcaseElement(testCaseName, "notRun", "No processor found", spec); notrun++; return; } } if (env.resetAction != null) { env.resetAction.reset(env); } XdmNode assertion; if (alternativeResult != null) { assertion = (XdmNode)xpc.EvaluateSingle("*[1]", alternativeResult); } else { assertion = (XdmNode)xpc.EvaluateSingle("result/*[1]", testCase); } if (assertion == null) { println("*** No assertions found for test case " + testCaseName); writeTestcaseElement(testCaseName, "disputed", "No assertions in test case", spec); feedback.Feedback(successes, failures++, total); return; } XPathCompiler assertXpc = env.processor.NewXPathCompiler(); assertXpc.XPathLanguageVersion = "3.1"; assertXpc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions"); assertXpc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema"); assertXpc.DeclareNamespace("math", "http://www.w3.org/2005/xpath-functions/math"); assertXpc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map"); assertXpc.DeclareNamespace("array", "http://www.w3.org/2005/xpath-functions/array"); assertXpc.DeclareNamespace("j", "http://www.w3.org/2005/xpath-functions"); assertXpc.DeclareVariable(new QName("result")); bool b = outcome.TestAssertion(assertion, outcome.GetPrincipalResultDoc(), assertXpc, xpc, debug); if (b) { //println("OK"); writeTestcaseElement(testCaseName, "pass", null, spec); feedback.Feedback(successes++, failures, total); } else { if (outcome.IsException()) { XdmItem expectedError = xpc.EvaluateSingle("result//error/@code", testCase); if (expectedError == null) { // if (debug) { // outcome.getException().printStackTrace(System.out); // } writeTestcaseElement(testCaseName, "fail", "Expected success, got ", spec); println("*** fail, result " + outcome.GetException() + " Expected success."); feedback.Feedback(successes, failures++, total); } else { String exceptionMessage = outcome.GetException() == null?outcome.GetException().Message : ""; String errorCode = (outcome.GetErrorCode() == null ? "unknown - " + exceptionMessage : outcome.GetErrorCode().LocalName); writeTestcaseElement(testCaseName, "wrongError", "Expected error:" + expectedError.ToString() + ", got " + errorCode, spec); println("*** fail, result " + errorCode + " Expected error:" + expectedError.ToString()); wrongErrorResults++; feedback.Feedback(successes++, failures, total); } } else { writeTestcaseElement(testCaseName, "fail", "Wrong results, got " + truncate(outcome.Serialize(assertXpc.Processor, outcome.GetPrincipalResultDoc())), spec); feedback.Feedback(successes, failures++, total); if (debug) { try { println("Result:"); driverProc.WriteXdmValue(outcome.GetPrincipalResult(), driverSerializer); println("<======="); } catch (Exception err) { } //println(outcome.getResult()); } else { println("*** fail (use -debug to show actual result)"); //failures++; } } } } }
public OutputResolver(Processor proc, TestOutcome outcome, bool serialized) { this.proc = proc; this.outcome = outcome; this.serialized = serialized; }
protected override void runTestCase(XdmNode testCase, XPathCompiler xpath) { TestOutcome outcome = new TestOutcome(this); string testName = testCase.GetAttributeValue(new QName("name")); string testSetName = testCase.Parent.GetAttributeValue(new QName("name")); //// if (testName.Equals("type-0174")) { int num = 0; System.Console.WriteLine("Test driver" + num); } /// if (exceptionsMap.ContainsKey(testName)) { notrun++; resultsDoc.writeTestcaseElement(testName, "notRun", exceptionsMap[testName].GetAttributeValue(new QName("reason"))); return; } if (exceptionsMap.ContainsKey(testName) || isSlow(testName)) { notrun++; resultsDoc.writeTestcaseElement(testName, "notRun", "requires excessive resources"); return; } XdmValue specAtt = (XdmValue)(xpath.EvaluateSingle("(/test-set/dependencies/spec/@value, ./dependencies/spec/@value)[last()]", testCase)); string spec = specAtt.ToString(); Environment env = getEnvironment(testCase, xpath); if (env == null) { resultsDoc.writeTestcaseElement(testName, "notRun", "test catalog error"); return; } /*if(testName("environment-variable")) { * EnvironmentVariableResolver resolver = new EnvironmentVariableResolver() { * public Set<string> getAvailableEnvironmentVariables() { * Set<string> strings = new HashSet<string>(); * strings.add("QTTEST"); * strings.add("QTTEST2"); * strings.add("QTTESTEMPTY"); * return strings; * } * * public string getEnvironmentVariable(string name) { * if (name.Equals("QTTEST")) { * return "42"; * } else if (name.Equals("QTTEST2")) { * return "other"; * } else if (name.Equals("QTTESTEMPTY")) { * return ""; * } else { * return null; * } * } * }; //TODO * } */ // env.processor.SetProperty(JFeatureKeys.ENVIRONMENT_VARIABLE_RESOLVER, resolver); //TODO XdmNode testInput = (XdmNode)xpath.EvaluateSingle("test", testCase); XdmNode stylesheet = (XdmNode)xpath.EvaluateSingle("stylesheet", testInput); XdmNode pack = (XdmNode)xpath.EvaluateSingle("package", testInput); foreach (XdmItem dep in xpath.Evaluate("(/test-set/dependencies/*, ./dependencies/*)", testCase)) { if (!dependencyIsSatisfied((XdmNode)dep, env)) { notrun++; resultsDoc.writeTestcaseElement(testName, "notRun", "dependency not satisfied"); return; } } XsltExecutable sheet = env.xsltExecutable; //ErrorCollector collector = new ErrorCollector(); string baseOutputURI = resultsDir + "/results/output.xml"; ErrorCollector collector = new ErrorCollector(outcome); IList errorList = new List <StaticError> (); XmlUrlResolver res = new XmlUrlResolver(); string xsltLanguageVersion = spec.Contains("XSLT30") || spec.Contains("XSLT20+") ? "3.0" : "2.0"; if (stylesheet != null) { XsltCompiler compiler = env.xsltCompiler; compiler.ErrorList = errorList; Uri hrefFile = res.ResolveUri(stylesheet.BaseUri, stylesheet.GetAttributeValue(new QName("file"))); Stream stream = new FileStream(hrefFile.AbsolutePath, FileMode.Open, FileAccess.Read); compiler.BaseUri = hrefFile; compiler.XsltLanguageVersion = (spec.Contains("XSLT30") || spec.Contains("XSLT20+") ? "3.0" : "2.0"); foreach (XdmItem param in xpath.Evaluate("param[@static='yes']", testInput)) { String name = ((XdmNode)param).GetAttributeValue(new QName("name")); String select = ((XdmNode)param).GetAttributeValue(new QName("select")); XdmValue value; try { value = xpath.Evaluate(select, null); } catch (Exception e) { Console.WriteLine("*** Error evaluating parameter " + name + ": " + e.Message); //throw e; continue; } compiler.SetParameter(new QName(name), value); } try { sheet = compiler.Compile(stream); } catch (Exception err) { Console.WriteLine(err.Message); //Console.WriteLine(err.StackTrace); IEnumerator enumerator = errorList.GetEnumerator(); bool checkCur = enumerator.MoveNext(); /*if (checkCur && enumerator.Current != null) { * outcome.SetException ((Exception)(enumerator.Current)); * } else { * Console.WriteLine ("Error: Unknown exception thrown"); * }*/ outcome.SetErrorsReported(errorList); //outcome.SetErrorsReported(collector.GetErrorCodes); } // compiler.setErrorListener(collector); } else if (pack != null) { Uri hrefFile = res.ResolveUri(pack.BaseUri, pack.GetAttributeValue(new QName("file"))); Stream stream = new FileStream(hrefFile.AbsolutePath, FileMode.Open, FileAccess.Read); XsltCompiler compiler = env.xsltCompiler; compiler.ErrorList = errorList; compiler.XsltLanguageVersion = (spec.Contains("XSLT30") || spec.Contains("XSLT20+") ? "3.0" : "2.0"); //compiler.setErrorListener(collector); try { XsltPackage xpack = compiler.CompilePackage(stream); sheet = xpack.Link(); } catch (Exception err) { Console.WriteLine(err.Message); IEnumerator enumerator = errorList.GetEnumerator(); enumerator.MoveNext(); outcome.SetException((Exception)(enumerator.Current)); outcome.SetErrorsReported(errorList); } } if (sheet != null) { XdmItem contextItem = env.contextItem; XdmNode initialMode = (XdmNode)xpath.EvaluateSingle("initial-mode", testInput); XdmNode initialFunction = (XdmNode)xpath.EvaluateSingle("initial-function", testInput); XdmNode initialTemplate = (XdmNode)xpath.EvaluateSingle("initial-template", testInput); QName initialModeName = GetQNameAttribute(xpath, testInput, "initial-mode/@name"); QName initialTemplateName = GetQNameAttribute(xpath, testInput, "initial-template/@name"); if (useXslt30Transformer) { try { bool assertsSerial = xpath.Evaluate("result//(assert-serialization|assert-serialization-error|serialization-matches)", testCase).Count > 0; bool resultAsTree = env.outputTree; bool serializationDeclared = env.outputSerialize; XdmNode needsTree = (XdmNode)xpath.EvaluateSingle("output/@tree", testInput); if (needsTree != null) { resultAsTree = needsTree.StringValue.Equals("yes"); } XdmNode needsSerialization = (XdmNode)xpath.EvaluateSingle("output/@serialize", testInput); if (needsSerialization != null) { serializationDeclared = needsSerialization.StringValue.Equals("yes"); } bool resultSerialized = serializationDeclared || assertsSerial; if (assertsSerial) { String comment = outcome.GetComment(); comment = (comment == null ? "" : comment) + "*Serialization " + (serializationDeclared ? "declared* " : "required* "); outcome.SetComment(comment); } Xslt30Transformer transformer = sheet.Load30(); transformer.InputXmlResolver = env; if (env.unparsedTextResolver != null) { transformer.GetUnderlyingController.setUnparsedTextURIResolver(env.unparsedTextResolver); } Dictionary <QName, XdmValue> caseGlobalParams = GetNamedParameters(xpath, testInput, false, false); Dictionary <QName, XdmValue> caseStaticParams = GetNamedParameters(xpath, testInput, true, false); Dictionary <QName, XdmValue> globalParams = new Dictionary <QName, XdmValue>(env.params1); foreach (KeyValuePair <QName, XdmValue> entry in caseGlobalParams) { globalParams.Add(entry.Key, entry.Value); } foreach (KeyValuePair <QName, XdmValue> entry in caseStaticParams) { globalParams.Add(entry.Key, entry.Value); } transformer.SetStylesheetParameters(globalParams); if (contextItem != null) { transformer.GlobalContextItem = contextItem; } transformer.MessageListener = collector; transformer.BaseOutputURI = baseOutputURI; transformer.MessageListener = new TestOutcome.MessageListener(outcome); XdmValue result = null; TextWriter sw = new StringWriter(); Serializer serializer = env.processor.NewSerializer(); serializer.SetOutputWriter(sw); //serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); OutputResolver serializingOutput = new OutputResolver(env.processor, outcome, true); net.sf.saxon.Controller controller = transformer.GetUnderlyingController; controller.setOutputURIResolver(serializingOutput); XmlDestination dest = null; if (resultAsTree) { // If we want non-serialized, we need to accumulate any result documents as trees too controller.setOutputURIResolver( new OutputResolver(env.processor, outcome, false)); dest = new XdmDestination(); } if (resultSerialized) { dest = serializer; } Stream src = null; Uri srcBaseUri = new Uri("http://uri"); XdmNode srcNode = null; DocumentBuilder builder2 = env.processor.NewDocumentBuilder(); if (env.streamedPath != null) { src = new FileStream(env.streamedPath, FileMode.Open, FileAccess.Read); srcBaseUri = new Uri(env.streamedPath); } else if (env.streamedContent != null) { byte[] byteArray = Encoding.UTF8.GetBytes(env.streamedContent); src = new MemoryStream(byteArray); //, "inlineDoc"); builder2.BaseUri = new Uri("http://uri"); } else if (initialTemplate == null && contextItem != null) { srcNode = (XdmNode)(contextItem); } if (initialMode != null) { QName name = GetQNameAttribute(xpath, initialMode, "@name"); try { if (name != null) { transformer.InitialMode = name; } else { controller.getInitialMode(); /// has the side effect of setting to the unnamed } } catch (Exception e) { if (e.InnerException is net.sf.saxon.trans.XPathException) { Console.WriteLine(e.Message); outcome.SetException(e); //throw new SaxonApiException(e.getCause()); } else { throw e; } } } if (initialMode != null || initialTemplate != null) { XdmNode init = (XdmNode)(initialMode == null ? initialTemplate : initialMode); Dictionary <QName, XdmValue> params1 = GetNamedParameters(xpath, init, false, false); Dictionary <QName, XdmValue> tunnelledParams = GetNamedParameters(xpath, init, false, true); if (xsltLanguageVersion.Equals("2.0")) { if (!(params1.Count == 0 && tunnelledParams.Count == 0)) { Console.WriteLine("*** Initial template parameters ignored for XSLT 2.0"); } } else { transformer.SetInitialTemplateParameters(params1, false); transformer.SetInitialTemplateParameters(tunnelledParams, true); } } if (initialTemplate != null) { QName name = GetQNameAttribute(xpath, initialTemplate, "@name"); transformer.GlobalContextItem = contextItem; if (dest == null) { result = transformer.CallTemplate(name); } else { transformer.CallTemplate(name, dest); } } else if (initialFunction != null) { QName name = getQNameAttribute(xpath, initialFunction, "@name"); XdmValue[] params2 = getParameters(xpath, initialFunction); if (dest == null) { result = transformer.CallFunction(name, params2); } else { transformer.CallFunction(name, params2, dest); } } else { if (dest == null) { if (src != null) { result = transformer.ApplyTemplates(src, srcBaseUri); } else { result = transformer.ApplyTemplates(srcNode); } } else { if (src != null) { transformer.ApplyTemplates(src, dest); } else { transformer.ApplyTemplates(srcNode, dest); } } } //outcome.SetWarningsReported(collector.getFoundWarnings()); if (resultAsTree && !resultSerialized) { result = ((XdmDestination)(dest)).XdmNode; } if (resultSerialized) { outcome.SetPrincipalSerializedResult(sw.ToString()); } outcome.SetPrincipalResult(result); if (saveResults) { String s = sw.ToString(); // If a transform result is entirely xsl:result-document, then result will be null if (!resultSerialized && result != null) { StringWriter sw2 = new StringWriter(); Serializer se = env.processor.NewSerializer(sw2); se.SetOutputProperty(Serializer.OMIT_XML_DECLARATION, "yes"); env.processor.WriteXdmValue(result, se); se.Close(); s = sw2.ToString(); } // currently, only save the principal result file in the result directory saveResultsToFile(s, resultsDir + "/results/" + testSetName + "/" + testName + ".out"); Dictionary <Uri, TestOutcome.SingleResultDoc> xslResultDocuments = outcome.GetSecondaryResultDocuments(); foreach (KeyValuePair <Uri, TestOutcome.SingleResultDoc> entry in xslResultDocuments) { Uri key = entry.Key; String path = key.AbsolutePath; String serialization = outcome.Serialize(env.processor, entry.Value); saveResultsToFile(serialization, path); } } } catch (Exception err) { //if (err.getCause() is XPathException && //!((XPathException) err.getCause()).hasBeenReported()) { //System.err.println("Unreported ERROR: " + err.getCause()); //} outcome.SetException(err); if (collector.getErrorCodes().Count > 0) { outcome.SetErrorsReported((IList)collector.getErrorCodes()); } //Console.WriteLine(err.StackTrace); /*if(err.getErrorCode() == null) { * int b = 3 + 4; } * if(err.getErrorCode() != null) * outcome.AddReportedError(err.getErrorCode().getLocalName()); * } else { * outcome.SetErrorsReported(collector.getErrorCodes()); * }*/ } /*catch (Exception err) { * err.printStackTrace(); * failures++; * resultsDoc.writeTestcaseElement(testName, "fail", "*** crashed " + err.getClass() + ": " + err.getMessage()); * return; * }*/ } else { try { XsltTransformer transformer = sheet.Load(); //transformer.SetURIResolver(env); //TODO if (env.unparsedTextResolver != null) { transformer.Implementation.setUnparsedTextURIResolver(env.unparsedTextResolver); } if (initialTemplate != null) { transformer.InitialTemplate = initialTemplateName; } if (initialMode != null) { transformer.InitialMode = initialModeName; } foreach (XdmItem param in xpath.Evaluate("param", testInput)) { string name = ((XdmNode)param).GetAttributeValue(new QName("name")); string select = ((XdmNode)param).GetAttributeValue(new QName("select")); XdmValue value = xpath.Evaluate(select, null); transformer.SetParameter(new QName(name), value); } if (contextItem != null) { transformer.InitialContextNode = (XdmNode)contextItem; } if (env.streamedPath != null) { transformer.SetInputStream(new FileStream(env.streamedPath, FileMode.Open, FileAccess.Read), testCase.BaseUri); } foreach (QName varName in env.params1.Keys) { transformer.SetParameter(varName, env.params1[varName]); } //transformer.setErrorListener(collector); transformer.BaseOutputUri = new Uri(resultsDir + "/results/output.xml"); /*transformer.MessageListener = (new MessageListener() { * public void message(XdmNode content, bool terminate, SourceLocator locator) { * outcome.addXslMessage(content); * } * });*/ // Run the transformation twice, once for serialized results, once for a tree. // TODO: we could be smarter about this and capture both // run with serialization StringWriter sw = new StringWriter(); Serializer serializer = env.processor.NewSerializer(sw); transformer.Implementation.setOutputURIResolver(new OutputResolver(driverProc, outcome, true)); transformer.Run(serializer); outcome.SetPrincipalSerializedResult(sw.ToString()); if (saveResults) { // currently, only save the principal result file saveResultsToFile(sw.ToString(), resultsDir + "/results/" + testSetName + "/" + testName + ".out"); } transformer.MessageListener = new TestOutcome.MessageListener(outcome); // run without serialization if (env.streamedPath != null) { transformer.SetInputStream(new FileStream(env.streamedPath, FileMode.Open, FileAccess.Read), testCase.BaseUri); } XdmDestination destination = new XdmDestination(); transformer.Implementation.setOutputURIResolver( new OutputResolver(env.processor, outcome, false)); transformer.Run(destination); //transformer. .transform(); outcome.SetPrincipalResult(destination.XdmNode); //} } catch (Exception err) { outcome.SetException(err); //outcome.SetErrorsReported(collector.getErrorCodes()); // err.printStackTrace(); // failures++; //resultsDoc.writeTestcaseElement(testName, "fail", "*** crashed " + err.Message); //return; } } XdmNode assertion = (XdmNode)xpath.EvaluateSingle("result/*", testCase); if (assertion == null) { failures++; resultsDoc.writeTestcaseElement(testName, "fail", "No test assertions found"); return; } XPathCompiler assertionXPath = env.processor.NewXPathCompiler(); //assertionXPath.setLanguageVersion("3.0"); bool success = outcome.TestAssertion(assertion, outcome.GetPrincipalResultDoc(), assertionXPath, xpath, debug); if (success) { if (outcome.GetWrongErrorMessage() != null) { outcome.SetComment(outcome.GetWrongErrorMessage()); wrongErrorResults++; } else { successes++; } resultsDoc.writeTestcaseElement(testName, "pass", outcome.GetComment()); } else { failures++; resultsDoc.writeTestcaseElement(testName, "fail", outcome.GetComment()); } } }
public MessageListener(TestRunner.TestOutcome out1) : base() { outcome1 = out1; }
private bool TestAssertion2(TestOutcome outcome, XdmNode assertion, SingleResultDoc result, XPathCompiler assertXpc, XPathCompiler catalogXpc, bool debug) { string tag = assertion.NodeName.LocalName; if (tag.Equals("assert-eq")) { return(assertEq(assertion, result, assertXpc)); } else if (tag.Equals("assert-deep-eq")) { return(assertDeepEq(assertion, result, assertXpc)); } else if (tag.Equals("assert-permutation")) { return(assertPermutation(assertion, result, assertXpc)); } else if (tag.Equals("assert-xml")) { return(assertXml(assertion, result, assertXpc, catalogXpc, debug)); } else if (tag.Equals("serialization-matches")) { return(AssertSerializationMatches(assertion, result, catalogXpc)); } else if (tag.Equals("assert-serialization-error")) { return(AssertSerializationError(assertion, result, assertXpc)); } else if (tag.Equals("assert-empty")) { return(assertEmpty(result.value)); } else if (tag.Equals("assert-count")) { return(assertCount(assertion, result)); } else if (tag.Equals("assert")) { return(AssertXPath(assertion, result, assertXpc, debug)); } else if (tag.Equals("assert-string-value")) { return(AssertStringValue(assertion, result, debug)); } else if (tag.Equals("assert-serialization")) { return(AssertSerialization(assertion, result, catalogXpc, debug)); } else if (tag.Equals("assert-type")) { return(AssertType(assertion, result, assertXpc)); } else if (tag.Equals("assert-true")) { return(AssertTrue(result)); } else if (tag.Equals("assert-false")) { return(AssertFalse(result)); } else if (tag.Equals("assert-warning")) { return(AssertWarning()); } else if (tag.Equals("assert-message")) { XdmNode subAssertion = (XdmNode)catalogXpc.EvaluateSingle("*", assertion); foreach (XdmNode message in xslMessages) { if (TestAssertion2(outcome, subAssertion, new SingleResultDoc(message, "", assertXpc.Processor), assertXpc, catalogXpc, debug)) { return(true); } } return(false); } else if (tag.Equals("assert-result-document")) { XdmNode subAssertion = (XdmNode)catalogXpc.EvaluateSingle("*", assertion); XmlUrlResolver res = new XmlUrlResolver(); // Uri uri = new Uri(outcome.BaseOutputUri + "/results/output.xml"); Uri uri = null; try { uri = res.ResolveUri(outcome.BaseOutputUri, assertion.GetAttributeValue(new QName("uri"))); } catch (Exception ex) { System.Console.WriteLine("**** Invalid output uri " + outcome.BaseOutputUri.AbsolutePath); return(false); } SingleResultDoc doc = GetSecondaryResult(uri); if (doc == null) { System.Console.WriteLine("**** No output document found for " + uri); return(false); } bool ok = TestAssertion2(outcome, subAssertion, doc, assertXpc, catalogXpc, debug); if (!ok) { System.Console.WriteLine("**** Assertion failed for result-document " + uri); } return(ok); } else if (tag.Equals("error")) { bool b = false; try { b = IsException() && compareExpectedError(assertion); } catch (Exception) { if (GetException() is StaticError) { string expectedError = assertion.GetAttributeValue(new QName("code")); QName expectedErrorQ; if (expectedError.Equals("*")) { expectedErrorQ = null; } else if (expectedError.StartsWith("Q{")) { expectedErrorQ = QName.FromEQName(expectedError); } else { expectedErrorQ = new QName("err", JNamespaceConstant.ERR, expectedError); } JFastStringBuffer fsb = new JFastStringBuffer(100); fsb.append("Expected "); fsb.append(expectedErrorQ.LocalName); fsb.append("; got "); fsb.append("err:XXX"); fsb.setLength(fsb.length() - 1); wrongError = fsb.ToString(); return(true); } if (GetException() is DynamicError) { string expectedError = assertion.GetAttributeValue(new QName("code")); QName expectedErrorQ; if (expectedError.Equals("*")) { expectedErrorQ = null; } else if (expectedError.StartsWith("Q{")) { expectedErrorQ = QName.FromEQName(expectedError); } else { expectedErrorQ = new QName("err", JNamespaceConstant.ERR, expectedError); } JFastStringBuffer fsb = new JFastStringBuffer(100); fsb.append("Expected "); fsb.append(expectedErrorQ.LocalName); fsb.append("; got "); fsb.append("err:XXX"); fsb.setLength(fsb.length() - 1); wrongError = fsb.ToString(); return(true); } } return(b); } else if (tag.Equals("all-of")) { foreach (XdmItem child in catalogXpc.Evaluate("*", assertion)) { if (!TestAssertion(outcome, (XdmNode)child, result, assertXpc, catalogXpc, debug)) { return(false); } } return(true); } else if (tag.Equals("any-of")) { bool partialSuccess = false; foreach (XdmItem child in catalogXpc.Evaluate("*", assertion)) { if (TestAssertion(outcome, (XdmNode)child, result, assertXpc, catalogXpc, debug)) { if (wrongError != null) { partialSuccess = true; continue; } return(true); } } return(partialSuccess); } else if (tag.Equals("not")) { XdmNode subAssertion = (XdmNode)catalogXpc.EvaluateSingle("*", assertion); return(!TestAssertion(outcome, subAssertion, result, assertXpc, catalogXpc, debug)); } throw new Exception("Unknown assertion element " + tag); }
protected override void runTestCase(XdmNode testCase, XPathCompiler xpath) { TestOutcome outcome = new TestOutcome(this); string testName = testCase.GetAttributeValue(new QName("name")); string testSetName = testCase.Parent.GetAttributeValue(new QName("name")); //// if (testName.Equals("type-0174")) { int num = 0; System.Console.WriteLine("Test driver" + num); } /// if (exceptionsMap.ContainsKey(testName)) { notrun++; resultsDoc.writeTestcaseElement(testName, "notRun", exceptionsMap[testName].GetAttributeValue(new QName("reason"))); return; } if (exceptionsMap.ContainsKey(testName) || isSlow(testName)) { notrun++; resultsDoc.writeTestcaseElement(testName, "notRun", "requires excessive resources"); return; } XdmValue specAtt = (XdmValue)(xpath.EvaluateSingle("(/test-set/dependencies/spec/@value, ./dependencies/spec/@value)[last()]", testCase)); string spec = specAtt.ToString(); Environment env = getEnvironment(testCase, xpath); if (env == null) { resultsDoc.writeTestcaseElement(testName, "notRun", "test catalog error"); return; } /*if(testName("environment-variable")) { * EnvironmentVariableResolver resolver = new EnvironmentVariableResolver() { * public Set<string> getAvailableEnvironmentVariables() { * Set<string> strings = new HashSet<string>(); * strings.add("QTTEST"); * strings.add("QTTEST2"); * strings.add("QTTESTEMPTY"); * return strings; * } * * public string getEnvironmentVariable(string name) { * if (name.Equals("QTTEST")) { * return "42"; * } else if (name.Equals("QTTEST2")) { * return "other"; * } else if (name.Equals("QTTESTEMPTY")) { * return ""; * } else { * return null; * } * } * }; //TODO * } */ // env.processor.SetProperty(JFeatureKeys.ENVIRONMENT_VARIABLE_RESOLVER, resolver); //TODO XdmNode testInput = (XdmNode)xpath.EvaluateSingle("test", testCase); XdmNode stylesheet = (XdmNode)xpath.EvaluateSingle("stylesheet", testInput); foreach (XdmItem dep in xpath.Evaluate("(/test-set/dependencies/*, ./dependencies/*)", testCase)) { if (!dependencyIsSatisfied((XdmNode)dep, env)) { notrun++; resultsDoc.writeTestcaseElement(testName, "notRun", "dependency not satisfied"); return; } } XsltExecutable sheet = env.xsltExecutable; //ErrorCollector collector = new ErrorCollector(); XmlUrlResolver res = new XmlUrlResolver(); if (stylesheet != null) { XsltCompiler compiler = env.xsltCompiler; Uri hrefFile = res.ResolveUri(stylesheet.BaseUri, stylesheet.GetAttributeValue(new QName("file"))); Stream stream = new FileStream(hrefFile.AbsolutePath, FileMode.Open, FileAccess.Read); compiler.BaseUri = hrefFile; compiler.XsltLanguageVersion = (spec.Contains("XSLT30") || spec.Contains("XSLT20+") ? "3.0" : "2.0"); try { sheet = compiler.Compile(stream); } catch (Exception err) { outcome.SetException(err); //outcome.SetErrorsReported(collector.GetErrorCodes); } // compiler.setErrorListener(collector); } if (sheet != null) { XdmItem contextItem = env.contextItem; QName initialMode = getQNameAttribute(xpath, testInput, "initial-mode/@name"); QName initialTemplate = getQNameAttribute(xpath, testInput, "initial-template/@name"); try { XsltTransformer transformer = sheet.Load(); //transformer.SetURIResolver(env); //TODO if (env.unparsedTextResolver != null) { transformer.Implementation.setUnparsedTextURIResolver(env.unparsedTextResolver); } if (initialTemplate != null) { transformer.InitialTemplate = initialTemplate; } if (initialMode != null) { transformer.InitialMode = initialMode; } foreach (XdmItem param in xpath.Evaluate("param", testInput)) { string name = ((XdmNode)param).GetAttributeValue(new QName("name")); string select = ((XdmNode)param).GetAttributeValue(new QName("select")); XdmValue value = xpath.Evaluate(select, null); transformer.SetParameter(new QName(name), value); } if (contextItem != null) { transformer.InitialContextNode = (XdmNode)contextItem; } if (env.streamedPath != null) { transformer.SetInputStream(new FileStream(env.streamedPath, FileMode.Open, FileAccess.Read), testCase.BaseUri); } foreach (QName varName in env.params1.Keys) { transformer.SetParameter(varName, env.params1[varName]); } //transformer.setErrorListener(collector); transformer.BaseOutputUri = new Uri(resultsDir + "/results/output.xml"); /*transformer.MessageListener = (new MessageListener() { * public void message(XdmNode content, bool terminate, SourceLocator locator) { * outcome.addXslMessage(content); * } * });*/ // Run the transformation twice, once for serialized results, once for a tree. // TODO: we could be smarter about this and capture both // run with serialization StringWriter sw = new StringWriter(); Serializer serializer = env.processor.NewSerializer(sw); transformer.Implementation.setOutputURIResolver(new OutputResolver(driverProc, outcome, true)); transformer.Run(serializer); outcome.SetPrincipalSerializedResult(sw.ToString()); if (saveResults) { // currently, only save the principal result file saveResultsToFile(sw.ToString(), resultsDir + "/results/" + testSetName + "/" + testName + ".out"); } transformer.MessageListener = new TestOutcome.MessageListener(outcome); // run without serialization if (env.streamedPath != null) { transformer.SetInputStream(new FileStream(env.streamedPath, FileMode.Open, FileAccess.Read), testCase.BaseUri); } XdmDestination destination = new XdmDestination(); transformer.Implementation.setOutputURIResolver( new OutputResolver(env.processor, outcome, false)); transformer.Run(destination); //transformer. .transform(); outcome.SetPrincipalResult(destination.XdmNode); //} } catch (Exception err) { outcome.SetException(err); //outcome.SetErrorsReported(collector.getErrorCodes()); // err.printStackTrace(); // failures++; //resultsDoc.writeTestcaseElement(testName, "fail", "*** crashed " + err.Message); //return; } } XdmNode assertion = (XdmNode)xpath.EvaluateSingle("result/*", testCase); if (assertion == null) { failures++; resultsDoc.writeTestcaseElement(testName, "fail", "No test assertions found"); return; } XPathCompiler assertionXPath = env.processor.NewXPathCompiler(); //assertionXPath.setLanguageVersion("3.0"); bool success = outcome.TestAssertion(assertion, outcome.GetPrincipalResultDoc(), assertionXPath, xpath, debug); if (success) { if (outcome.GetWrongErrorMessage() != null) { outcome.SetComment(outcome.GetWrongErrorMessage()); wrongErrorResults++; } else { successes++; } resultsDoc.writeTestcaseElement(testName, "pass", outcome.GetComment()); } else { failures++; resultsDoc.writeTestcaseElement(testName, "fail", outcome.GetComment()); } }