Project

Profile

Help

How to connect?
Download (42 KB) Statistics
| Branch: | Tag: | Revision:

he / latest9.9 / samples / cs / TestRunner9.9 / QT3TestSuiteDriver.cs @ 063bf4dc

1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Text;
5
using System.Xml;
6
using System.IO;
7
using System.Text.RegularExpressions;
8
using System.Globalization;
9
using Saxon.Api;
10
using TestRunner;
11

    
12
// file:////C:/work/repositories/hg/qt3-testsuite/ C:/work/repositories/hg/qt3-testsuite/results/saxon/ -lang:XQ30
13
//using net.sf.saxon.Version;
14
using JFeatureKeys = net.sf.saxon.lib.FeatureKeys;
15
using JConfiguration = net.sf.saxon.Configuration;
16
using JVersion = net.sf.saxon.Version;
17

    
18
namespace TestRunner
19
{
20

    
21

    
22

    
23
    /**
24
     * Test Driver for the QT3 test suite
25
     */
26
    public class QT3TestSuiteDriver : TestDriver
27
    {
28

    
29

    
30
        public static string RNS = "http://www.w3.org/2012/08/qt-fots-results";
31
        public static string CNS = "http://www.w3.org/2010/09/qt-fots-catalog";
32

    
33

    
34
        public object getDependencyDictionary()
35
        {
36
            return dependencyDictionary;
37
        }
38

    
39
        private Dictionary<string, Dependency> dependencyDictionary = new Dictionary<string, Dependency>();
40

    
41

    
42
        private Spec spec;
43
        //private FotsResultsDocument resultsDoc;
44

    
45
        public override string catalogNamespace()
46
        {
47
            return CNS;
48
        }
49
			
50

    
51
        public override void processSpec(string specStr)
52
        {
53

    
54
            if (specStr.Equals("XP20"))
55
            {
56
                spec = Spec.XP20;
57
            }
58
            else if (specStr.Equals("XP30"))
59
            {
60
                spec = Spec.XP30;
61
            }
62
            else if (specStr.Equals("XQ10"))
63
            {
64
                spec = Spec.XQ10;
65
            }
66
            else if (specStr.Equals("XQ30"))
67
            {
68
                spec = Spec.XQ30;
69
            }
70
            else if (specStr.Equals("XQ31"))
71
            {
72
                spec = Spec.XQ31;
73
            }
74
            else
75
            {
76
                System.Console.WriteLine("The specific language must be one of the following: XP20, XP30, XQ10, XQ30 or XQ31");
77
            }
78
            resultsDoc = new FotsResultsDocument(this.getResultsDir(), spec);
79
        }
80

    
81

    
82
        //C:/work/repositories/hg/qt3-testsuite/ C:/work/repositories/hg/qt3-testsuite/results/saxon/ -lang:XQ31
83
        public static void Main(string[] args)
84
        {
85

    
86
            if (args.Length == 0 || args[0].Equals("-?"))
87
            {
88
                System.Console.WriteLine("testsuiteDir catalog [-o:resultsdir] [-s:testSetName]" +
89
                        " [-t:testNamePattern] [-unfolded] [-bytecode:on|off|debug] [-tree] [-lang:XP20|XP30|XQ10|XQ30|XQ31]");
90
                return; 
91
            }
92
            System.Console.WriteLine("Testing Saxon " + (new Processor()).ProductVersion);
93
            new QT3TestSuiteDriver().go(args);
94
        }
95

    
96

    
97

    
98
        protected override void createGlobalEnvironments(XdmNode catalog, XPathCompiler xpc)
99
        {
100
            Environment environment = null;
101

    
102
            Environment defaultEnv = null;
103
            try
104
            {
105
                defaultEnv = localEnvironments["default"];
106
            }
107
            catch (Exception) { }
108
            foreach (XdmItem env in xpc.Evaluate("//environment", catalog))
109
            {
110
                environment = Environment.processEnvironment(
111
                        this, xpc, env, globalEnvironments, defaultEnv);
112
            }
113
            buildDependencyDictionary(driverProc, environment);
114
        }
115

    
116
        /**
117
         * Decide whether a dependency is satisfied
118
         *
119
         * @param dependency the dependency element in the catalog
120
         * @param env        an environment in the catalog, which can be modified to satisfy the dependency if necessary.
121
         *                   May be null.
122
         * @return true if the environment satisfies the dependency, else false
123
         */
124
        
125
        public override bool dependencyIsSatisfied(XdmNode dependency, Environment env)
126
        {
127
            string type = dependency.GetAttributeValue(new QName("type"));
128
            string value = dependency.GetAttributeValue(new QName("value"));
129
            bool inverse = "false".Equals(dependency.GetAttributeValue(new QName("satisfied")));
130
            if ("xml-version".Equals(type))
131
            {
132
                if (value.Equals("1.0:4-") && !inverse)
133
                {
134
                    // we don't support XML 1.0 4th edition or earlier
135
                    return false;
136
                }
137
                if (value.Contains("1.1") && !inverse)
138
                {
139
                    if (env != null)
140
                    {
141
                        env.processor.SetProperty(JFeatureKeys.XML_VERSION, "1.1");
142
                    }
143
                    else
144
                    {
145
                        return false;
146
                    }
147
                }
148
                else if (value.Contains("1.0") && !inverse)
149
                {
150
                    if (env != null)
151
                    {
152
                        env.processor.SetProperty(JFeatureKeys.XML_VERSION, "1.0");
153
                    }
154
                    else
155
                    {
156
                        return false;
157
                    }
158
                }
159
                return true;
160
            }
161
            else if ("xsd-version".Equals(type))
162
            {
163
                if ("1.1".Equals(value))
164
                {
165
                    if (env != null)
166
                    {
167
                        env.processor.SetProperty(JFeatureKeys.XSD_VERSION, (inverse ? "1.0" : "1.1"));
168
                    }
169
                    else
170
                    {
171
                        return false;
172
                    }
173
                }
174
                else if ("1.0".Equals(value))
175
                {
176
                    if (env != null)
177
                    {
178
                        env.processor.SetProperty(JFeatureKeys.XSD_VERSION, (inverse ? "1.1" : "1.0"));
179
                    }
180
                    else
181
                    {
182
                        return false;
183
                    }
184
                }
185
                return true;
186
            }
187
            else if ("limits".Equals(type))
188
            {
189
                return "year_lt_0".Equals(value) && !inverse;
190
            }
191
            else if ("spec".Equals(type))
192
            {
193
                return true;
194
            }
195
            else if ("collection-stability".Equals(type))
196
            {
197
                // SAXON has a problem here - we don't support stable collections
198
                return ("false".Equals(value) != inverse);
199
            }
200
            else if ("default-language".Equals(type))
201
            {
202
                return ("en".Equals(value) != inverse);
203
            }
204
            else if ("directory-as-collection-uri".Equals(type))
205
            {
206
                return ("true".Equals(value) != inverse);
207
            }
208
            else if ("language".Equals(type))
209
            {
210
                return (("en".Equals(value) || "de".Equals(value) || "fr".Equals(value)) != inverse);
211
            }
212
            else if ("calendar".Equals(type))
213
            {
214
                return (("AD".Equals(value) || "ISO".Equals(value)) != inverse);
215
            }
216
            else if ("format-integer-sequence".Equals(type))
217
            {
218
                return !inverse;
219
            }
220
            else if ("unicode-normalization-form".Equals(type))
221
            {
222
                return value.Equals("FULLY-NORMALIZED", StringComparison.OrdinalIgnoreCase) ? inverse : !inverse;
223
            }
224
            else if ("feature".Equals(type))
225
            {
226
                if ("namespace-axis".Equals(value))
227
                {
228
                    return !inverse;
229
                }
230
                else if ("higherOrderFunctions".Equals(value))
231
                {
232
                    return !inverse;
233
                }
234
                else if ("schemaImport".Equals(value) || "schemaValidation".Equals(value) || "schemaAware".Equals(value))
235
                {
236
                    //if (!treeModel.isSchemaAware()) {//TODO
237
                    //  return false; // cannot use the selected tree model for schema-aware tests
238
                    //}
239
                    // Need to reset these after use for this query??
240
                    if (env != null)
241
                    {
242
                        if (inverse)
243
                        {
244
                            // force use of a non-schema-aware processor by creating a ProfessionalConfiguration
245
                            /* ProfessionalConfiguration pConfig = new ProfessionalConfiguration();
246
                             pConfig.setNamePool(env.processor.getUnderlyingConfiguration().getNamePool());
247
                             final Processor savedProcessor = env.processor;
248
                             final XPathCompiler savedXPathCompiler = env.xpathCompiler;
249
                             final XQueryCompiler savedXQueryCompiler = env.xqueryCompiler;
250
                             final XsltCompiler savedXsltCompiler = env.xsltCompiler;
251
                             env.processor = new Processor(pConfig);
252
                             env.xpathCompiler = env.processor.newXPathCompiler();
253
                             env.xqueryCompiler = env.processor.newXQueryCompiler();
254
                             env.xsltCompiler = env.processor.newXsltCompiler();
255
                             env.xpathCompiler.setSchemaAware(false);
256
                             env.xqueryCompiler.setSchemaAware(false);
257
                             env.xsltCompiler.setSchemaAware(false);
258
                             env.resetAction = new Environment.ResetAction() {
259
                                 public void reset(Environment env) {
260
                                     env.processor = savedProcessor;
261
                                     env.xpathCompiler = savedXPathCompiler;
262
                                     env.xqueryCompiler = savedXQueryCompiler;
263
                                     env.xsltCompiler = savedXsltCompiler;
264
                                 }
265
                             }; */
266
                        }
267
                        else
268
                        {
269
                            env.xpathCompiler.SchemaAware = true;
270
                            env.xqueryCompiler.SchemaAware = true;
271
                            if (env.xsltCompiler != null) {
272
                                env.xsltCompiler.SchemaAware = true;
273
                            }
274
                        }
275
                    }
276
                    return true;
277
                }
278
                else if ("xpath-1.0-compatibility".Equals(value))
279
                {
280
                    if (env != null)
281
                    {
282
                        env.xpathCompiler.BackwardsCompatible = !inverse;
283
                        return true;
284
                    }
285
                    else
286
                    {
287
                        return false;
288
                    }
289
                }
290
                else if ("staticTyping".Equals(value))
291
                {
292
                    return inverse;
293
                }
294
                else if ("moduleImport".Equals(value))
295
                {
296
                    return !inverse;
297
                }
298
                else if ("schema-location-hint".Equals(value))
299
                {
300
                    return !inverse;
301
                } else if ("info-dtd".Equals(value)) {
302
                    if (treeModel == TreeModel.TinyTree || treeModel == TreeModel.LinkedTree || treeModel == TreeModel.TinyTreeCondensed)
303
                    {
304
                        return !inverse;
305
                    }
306
                    else {
307
                        return inverse;
308
                    }
309
                } else if ("serialization".Equals(value)) {
310
                    return true;
311
                } else if ("non_codepoint_collation".Equals(value)) {
312
                    return !inverse;
313
                } else if ("non_empty_sequence_collection".Equals(value)) {
314
                    return !inverse;
315
                } else if ("fn-transform-XSLT".Equals(value)) {
316
                    return !inverse;
317
                } else if ("fn-transform-XSLT30".Equals(value)) {
318
                    String edition = env.processor.Edition;
319
                    return (edition.Equals("PE") || edition.Equals("EE")) ^ inverse;
320
                } else if ("fn-format-integer-CLDR".Equals(value)) {
321
                    String edition = env.processor.Edition;
322
                    return (edition.Equals("PE") || edition.Equals("EE")) ^ inverse;
323
                } else if ("unicode-version".Equals(value)) {
324
                    return true;
325
                } else
326
                {
327
                    println("**** feature = " + value + "  ????");
328
                    return false;
329
                }
330
            }
331
            else
332
            {
333
                println("**** dependency not recognized: " + type);
334
                return false;
335
            }
336
        }
337

    
338
        /**
339
         * Run a test case
340
         *
341
         *
342
         * @param testCase the test case element in the catalog
343
         * @param xpc      the XPath compiler to be used for compiling XPath expressions against the catalog
344
         * @
345
         */
346

    
347
        protected override void runTestCase(XdmNode testCase, XPathCompiler xpc)
348
        {
349
            bool run = true;
350
            bool xpDependency = false;
351
            string hostLang;
352
            string langVersion;
353
            Spec specOpt = Spec.NULL;
354
            XPathCompiler xpath = driverProc.NewXPathCompiler();
355
            string testCaseName = testCase.GetAttributeValue(new QName("name"));
356
            string testSetName = testCase.Parent.GetAttributeValue(new QName("name"));
357
            bool needSerializedResult = ((XdmAtomicValue)xpc.EvaluateSingle(
358
                    "exists(./result//assert-serialization-error) or exists(./result//serialization-matches)", testCase)).GetBooleanValue();
359
            bool needResultValue = true;
360
            if (needSerializedResult)
361
            {
362
                needResultValue = ((XdmAtomicValue)xpc.EvaluateSingle(
363
                        "exists(./result//*[not(self::serialization-matches or self::assert-serialization-error or self::any-of or self::all-of)])", testCase)).GetBooleanValue();
364
            }
365

    
366
            XdmNode alternativeResult = null;
367
            XdmNode optimization = null;
368

    
369

    
370
            hostLang = ((SpecAttr)spec.GetAttr()).sname;
371
            langVersion = ((SpecAttr)spec.GetAttr()).version;
372

    
373

    
374
            Environment env = getEnvironment(testCase, xpc);
375
            if (env == null)
376
            {
377
                notrun++;
378
                return;
379
            }
380
            env.xpathCompiler.BackwardsCompatible = false;
381
            env.processor.XmlVersion = (decimal)1.0;
382

    
383

    
384
			//test
385
			/*bool icuColCheck = net.sf.saxon.Version.platform.hasICUCollator ();
386
			bool icuNumCheck = net.sf.saxon.Version.platform.hasICUNumberer();
387
			Console.WriteLine ("ICUCol: " + (icuColCheck ? "true" : "false"));
388
			Console.WriteLine ("ICUNum: " + (icuNumCheck ? "true" : "false"));*/
389
			//end of test
390
            foreach (XdmItem dependency in xpc.Evaluate("/*/dependency, ./dependency", testCase))
391
            {
392
                string type = ((XdmNode)dependency).GetAttributeValue(new QName("type"));
393
                if (type == null)
394
                {
395
                    // throw new IllegalStateException("dependency/@type is missing"); //TODO
396
                }
397
                string value = ((XdmNode)dependency).GetAttributeValue(new QName("value"));
398
                if (value == null)
399
                {
400
                    //throw new IllegalStateException("dependency/@value is missing"); //TODO
401
                }
402

    
403
                if (type.Equals("spec"))
404
                {
405
                    bool applicable = false;
406
                    if (!value.Contains(((SpecAttr)spec.GetAttr()).sname))
407
                    {
408
                        applicable = false;
409
                    }
410
                    else if (value.Contains(((SpecAttr)spec.GetAttr()).svname))
411
                    {
412
                        applicable = true;
413
                    }
414
                    else if ( ( ((SpecAttr)spec.GetAttr()).svname.Equals("XQ30") || ((SpecAttr)spec.GetAttr()).svname.Equals("XQ31")) && (value.Contains("XQ10+") || value.Contains("XQ30+")) )
415
                    {
416
                        applicable = true;
417
                    }
418
                    else if ( ( ((SpecAttr)spec.GetAttr()).svname.Equals("XP30") || ((SpecAttr)spec.GetAttr()).svname.Equals("XP31")) && (value.Contains("XP20+") || value.Contains("XP30+")) )
419
                    {
420
                        applicable = true;
421
                    }
422
                    if (!applicable)
423
                    {
424
                        writeTestcaseElement(testCaseName, "n/a", "not" + ((SpecAttr)spec.GetAttr()).svname, spec);
425
                        notrun++;
426
                        return;
427
                    }
428
                }
429
                if (langVersion.Equals("3.0"))
430
                {
431
                    /* EnvironmentVariableResolver resolver = new EnvironmentVariableResolver() {
432
                         public Set<string> getAvailableEnvironmentVariables() {
433
                             Set<string> strings = new HashSet<string>();
434
                             strings.add("QTTEST");
435
                             strings.add("QTTEST2");
436
                             strings.add("QTTESTEMPTY");
437
                             return strings;
438
                         }
439

    
440
                         public string getEnvironmentVariable(string name) {
441
                             if (name.Equals("QTTEST")) {
442
                                 return "42";
443
                             } else if (name.Equals("QTTEST2")) {
444
                                 return "other";
445
                             } else if (name.Equals("QTTESTEMPTY")) {
446
                                 return "";
447
                             } else {
448
                                 return null;
449
                             }
450
                         }
451
                     }; */
452
                    //TODO
453
                    //env.processor.SetProperty(JFeatureKeys.ENVIRONMENT_VARIABLE_RESOLVER, resolver);
454
                }
455
                /*if (type.Equals("feature") && value.Equals("xpath-1.0-compatibility"))
456
                {
457
                    hostLang = "XP";
458
                    langVersion = "3.0";
459
                    xpDependency = true;
460
                    specOpt = Spec.XP30;
461
                }
462
                if (type.Equals("feature") && value.Equals("namespace-axis"))
463
                {
464
                    hostLang = "XP";
465
                    langVersion = "3.0";
466
                    xpDependency = true;
467
                    specOpt = Spec.XP30;
468
                }*/
469
                if (!dependencyIsSatisfied((XdmNode)dependency, env))
470
                {
471
                    println("*** Dependency not satisfied: " + ((XdmNode)dependency).GetAttributeValue(new QName("type")));
472
                    writeTestcaseElement(testCaseName, "n/a", "Dependency not satisfied", spec);
473
                    run = false;
474
                    notrun++;
475
                    return;
476
                }
477
            }
478

    
479
            XdmNode exceptionElement;
480
            
481
            try{
482
                exceptionElement = exceptionsMap[testCaseName];
483

    
484
            } catch(Exception) {
485
                exceptionElement = null;
486
            }
487
            if (exceptionElement != null)
488
            {
489
                XdmItem config = xpath.EvaluateSingle("configuration", exceptionElement);
490

    
491
                string runAtt = exceptionElement.GetAttributeValue(new QName("run"));
492
                string reasonMsg = xpath.EvaluateSingle("reason", exceptionElement).ToString();
493
                string reportAtt = exceptionElement.GetAttributeValue(new QName("report"));
494

    
495
                if (config != null)
496
                {
497
                    XdmItem paramValue = xpath.EvaluateSingle("param[@name='not-unfolded' and @value='yes']/@name", config);
498
                    if (unfolded && paramValue != null)
499
                    {
500
                        writeTestcaseElement(testCaseName, "notRun", reasonMsg, spec);
501
                        notrun++;
502
                        return;
503
                    }
504
                }
505

    
506
                if ("false".Equals(runAtt))
507
                {
508
                    writeTestcaseElement(testCaseName, reportAtt, reasonMsg, spec);
509
                    notrun++;
510
                    return;
511
                }
512

    
513
                alternativeResult = (XdmNode)xpc.EvaluateSingle("result", exceptionElement);
514
                optimization = (XdmNode)xpc.EvaluateSingle("optimization", exceptionElement);
515
            }
516

    
517
            if (run && (specOpt == Spec.NULL || specOpt == spec))
518
            {
519

    
520
                TestOutcome outcome = new TestOutcome(this);
521
                string exp = null;
522
                try
523
                {
524
                    exp = xpc.Evaluate("if (test/@file) then unparsed-text(resolve-uri(test/@file, base-uri(.))) else string(test)", testCase).ToString();
525
                }
526
                catch (Exception err)
527
                {
528
                    println("*** Failed to read query: " + err.Message);
529
                    outcome.SetException((DynamicError)err);
530
                }
531

    
532
                //noinspection ThrowableResultOfMethodCallIgnored
533
                if (outcome.GetException() == null)
534
                {
535
                    if (hostLang.Equals("XP") || hostLang.Equals("XT"))
536
                    {
537
                        XPathCompiler testXpc = env.xpathCompiler;
538
                        testXpc.XPathLanguageVersion = langVersion;
539
                        testXpc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
540
                        testXpc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema");
541
                        testXpc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map");
542
                        //testXpc.DeclareNamespace("math", NamespaceConstant.MATH);
543
                        //testXpc.DeclareNamespace("Dictionary", NamespaceConstant.Dictionary_FUNCTIONS);
544

    
545
                        try
546
                        {
547
                            XPathSelector selector = testXpc.Compile(exp).Load();
548
                            
549
foreach (QName varName in env.params1.Keys)
550
                            {
551
                                selector.SetVariable(varName, env.params1[varName]);
552
                            }
553
                            if (env.contextItem != null)
554
                            {
555
                                selector.ContextItem = env.contextItem;
556
                            }
557
                            selector.InputXmlResolver = new TestUriResolver(env);
558

    
559
                            if (env.unparsedTextResolver != null)
560
                            {
561
                                //selector.getUnderlyingXPathContext().setUnparsedTextURIResolver(env.unparsedTextResolver); //TODO
562
                            }
563
                            XdmValue result = selector.Evaluate();
564
                            outcome.SetPrincipalResult(result);
565
                        }
566
                        catch (Exception err)
567
                        {
568
                            println(err.Message);
569
                            
570
                           outcome.SetException(err);
571
                            
572

    
573
                        }
574
                    }
575
                    else if (hostLang.Equals("XQ"))
576
                    {
577
                        XQueryCompiler testXqc = env.xqueryCompiler;
578
                        testXqc.XQueryLanguageVersion = langVersion;
579
                        testXqc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
580
                        testXqc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema");
581
                        //testXqc.DeclareNamespace("math", NamespaceConstant.MATH);
582
                        testXqc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map");
583
                        testXqc.DeclareNamespace("array", "http://www.w3.org/2005/xpath-functions/array");
584
                        // ErrorCollector errorCollector = new ErrorCollector();
585
                        testXqc.ErrorList = new List<StaticError>();
586
                        string decVars = env.paramDecimalDeclarations.ToString();
587
                        if (decVars.Length != 0)
588
                        {
589
                            int x = (exp.IndexOf("(:%DECL%:)"));
590
                            if (x < 0)
591
                            {
592
                                exp = decVars + exp;
593
                            }
594
                            else
595
                            {
596
                                exp = exp.Substring(0, x) + decVars + exp.Substring(x + 13);
597
                            }
598
                        }
599
                        string vars = env.paramDeclarations.ToString();
600
                        if (vars.Length != 0)
601
                        {
602
                            int x = (exp.IndexOf("(:%VARDECL%:)"));
603
                            if (x < 0)
604
                            {
605
                                exp = vars + exp;
606
                            }
607
                            else
608
                            {
609
                                exp = exp.Substring(0, x) + vars + exp.Substring(x + 13);
610
                            }
611
                        }
612
                         ModuleResolver mr = new ModuleResolver(xpc);
613
                         mr.setTestCase(testCase);
614
                          testXqc.QueryResolver = mr;// .setModuleURIResolver(mr); //TODO
615
						testXqc.QueryResolver = mr;
616
                        try
617
                        {
618
                            XQueryExecutable q = testXqc.Compile(exp);
619
                            if (optimization != null)
620
                            {
621
                                // Test whether required optimizations have been performed
622
                                XdmDestination expDest = new XdmDestination();
623
                                JConfiguration config = driverProc.Implementation;
624
                                //ExpressionPresenter presenter = new ExpressionPresenter(config, expDest.getReceiver(config));
625
                                //q.getUnderlyingCompiledQuery().explain(presenter);
626
                                //presenter.close();
627
                                XdmNode explanation = expDest.XdmNode;
628
                                XdmItem optResult = xpc.EvaluateSingle(optimization.GetAttributeValue(new QName("assert")), explanation);
629
                                if (((XdmAtomicValue)optResult).GetBooleanValue())
630
                                {
631
                                    println("Optimization result OK");
632
                                }
633
                                else
634
                                {
635
                                    println("Failed optimization test");
636
                                    Serializer ser = env.processor.NewSerializer();
637
                                    ser.SetOutputStream((Stream)System.Console.OpenStandardError());
638
                                    driverProc.WriteXdmValue(explanation, ser);
639
                                    writeTestcaseElement(testCaseName, "fail", "Failed optimization assertions", spec);
640
                                    failures++;
641
                                    return;
642
                                }
643

    
644
                            }
645
                            XQueryEvaluator selector = q.Load();
646
                            foreach (QName varName in env.params1.Keys)
647
                            {
648
                                selector.SetExternalVariable(varName, env.params1[varName]);
649
                            }
650
                            if (env.contextItem != null)
651
                            {
652
                                selector.ContextItem = env.contextItem;
653
                            }
654
							selector.InputXmlResolver = env;
655
                            //selector.InputXmlResolver =  .SetURIResolver(new TestURIResolver(env)); //TODO
656
                            if (env.unparsedTextResolver != null)
657
                            {
658
                                selector.Implementation.getUnderlyingQueryContext().setUnparsedTextURIResolver(env.unparsedTextResolver);// TODO
659
                            }
660
                            if (needSerializedResult)
661
                            {
662
                                StringWriter sw = new StringWriter();
663
                                Serializer serializer =env.processor.NewSerializer();
664
                                serializer.SetOutputWriter(sw);
665
                                selector.Run(serializer);
666
                                outcome.SetPrincipalSerializedResult(sw.ToString());
667
                            }
668
                            if (needResultValue)
669
                            {
670
                                XdmValue result = selector.Evaluate();
671
                                outcome.SetPrincipalResult(result);
672
                            }
673
                        }
674
                        catch (Exception err)
675
                        {
676
							println("in TestSet " + testSetName + err.StackTrace);
677
                            println(err.Message);
678
                            outcome.SetException(err);
679
							outcome.SetErrorsReported(testXqc.ErrorList);
680
                        }
681
                    }
682
                    else
683
                    {
684
                        writeTestcaseElement(testCaseName, "notRun", "No processor found", spec);
685
                        notrun++;
686
                        return;
687
                    }
688
                }
689

    
690
                if (env.resetAction != null)
691
                {
692
                    env.resetAction.reset(env);
693
                }
694
                XdmNode assertion;
695
                if (alternativeResult != null)
696
                {
697
                    assertion = (XdmNode)xpc.EvaluateSingle("*[1]", alternativeResult);
698
                }
699
                else
700
                {
701
                    assertion = (XdmNode)xpc.EvaluateSingle("result/*[1]", testCase);
702
                }
703
                if (assertion == null)
704
                {
705
                    println("*** No assertions found for test case " + testCaseName);
706
                    writeTestcaseElement(testCaseName, "disputed", "No assertions in test case", spec);
707
                    feedback.Feedback(successes, failures++, total);
708
                    return;
709
                }
710
                XPathCompiler assertXpc = env.processor.NewXPathCompiler();
711
                assertXpc.XPathLanguageVersion = "3.1";
712
                assertXpc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
713
                assertXpc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema");
714
                assertXpc.DeclareNamespace("math", "http://www.w3.org/2005/xpath-functions/math");
715
                assertXpc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map");
716
                assertXpc.DeclareNamespace("array", "http://www.w3.org/2005/xpath-functions/array");
717
                assertXpc.DeclareNamespace("j", "http://www.w3.org/2005/xpath-functions");
718
                assertXpc.DeclareVariable(new QName("result"));
719

    
720
                bool b = outcome.TestAssertion(assertion, outcome.GetPrincipalResultDoc(), assertXpc, xpc, debug);
721
                if (b)
722
                {
723
                    //println("OK");
724
                    writeTestcaseElement(testCaseName, "pass", null, spec);
725
                    feedback.Feedback(successes++, failures, total);
726

    
727
                }
728
                else
729
                {
730

    
731
                    if (outcome.IsException())
732
                    {
733
                        XdmItem expectedError = xpc.EvaluateSingle("result//error/@code", testCase);
734

    
735
                        if (expectedError == null)
736
                        {
737
                            //                        if (debug) {
738
                            //                            outcome.getException().printStackTrace(System.out);
739
                            //                        }
740
                          
741
                            writeTestcaseElement(testCaseName, "fail", "Expected success, got ", spec);
742
                            println("*** fail, result " + outcome.GetException() +
743
                                    " Expected success.");
744
                            feedback.Feedback(successes, failures++, total);
745
                        }
746
                        else
747
                        {
748
                            String exceptionMessage = outcome.GetException() == null ? outcome.GetException().Message : "";
749
                            String errorCode = (outcome.GetErrorCode() == null ? "unknown - "+ exceptionMessage : outcome.GetErrorCode().LocalName);
750
                            writeTestcaseElement(testCaseName, "wrongError",
751
                                    "Expected error:" + expectedError.ToString() + ", got " + errorCode, spec);
752
                            println("*** fail, result " + errorCode +
753
                                    " Expected error:" + expectedError.ToString());
754
                            wrongErrorResults++;
755
                            feedback.Feedback(successes++, failures, total);
756
                        }
757

    
758
                    }
759
                    else
760
                    {
761
                        writeTestcaseElement(testCaseName, "fail", "Wrong results, got " +
762
                                truncate(outcome.Serialize(assertXpc.Processor, outcome.GetPrincipalResultDoc())), spec);
763
                        feedback.Feedback(successes, failures++, total);
764
                        if (debug)
765
                        {
766
                            try
767
                            {
768
                                println("Result:");
769
                                driverProc.WriteXdmValue(outcome.GetPrincipalResult(), driverSerializer);
770
                                println("<=======");
771
                            }
772
                            catch (Exception err)
773
                            {
774
                            }
775
                            //println(outcome.getResult());
776
                        }
777
                        else
778
                        {
779
                            println("*** fail (use -debug to show actual result)");
780
                            //failures++;
781
                        }
782
                    }
783
                }
784
            }
785
        }
786

    
787
        private string truncate(string in1)
788
        {
789
            if (in1.Length > 80)
790
            {
791
                return in1.Substring(0, 80) + "...";
792
            }
793
            else
794
            {
795
                return in1;
796
            }
797
        }
798

    
799

    
800

    
801
        public void writeResultFilePreamble(Processor processor, XdmNode catalog)
802
        {
803
            resultsDoc.writeResultFilePreamble(processor, catalog);
804
        }
805

    
806
        public void writeResultFilePostamble()
807
        {
808
            resultsDoc.writeResultFilePostamble();
809
        }
810

    
811

    
812
        public void startTestSetElement(XdmNode testSetNode)
813
        {
814
            resultsDoc.startTestSetElement(testSetNode);
815
        }
816

    
817

    
818
        protected void writeTestSetEndElement()
819
        {
820
            resultsDoc.endElement();
821
        }
822

    
823
        private void writeTestcaseElement(string name, string result, string comment, Spec specOpt)
824
        {
825
            resultsDoc.writeTestcaseElement(name, result, comment);
826
        }
827

    
828
		public class ModuleResolver : IQueryResolver {
829

    
830
            XPathCompiler catXPC;
831
            XdmNode testCase;
832

    
833
            public ModuleResolver(XPathCompiler xpc)
834
            {
835
                this.catXPC = xpc;
836
            }
837

    
838
            public void setTestCase(XdmNode testCase)
839
            {
840
                this.testCase = testCase;
841
            }
842

    
843

    
844
			public Object GetEntity(Uri absoluteUri) {
845
				String fullPath = absoluteUri.AbsolutePath;
846
				Stream stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
847
				//javax.xml.transform.Source sourcei = new javax.xml.transform.stream.StreamSource(uri);
848

    
849
				return stream;
850
			}
851

    
852
			internal Uri resolve(Uri basei, String child) { 
853
			
854
				return (new XmlUrlResolver ()).ResolveUri (testCase.BaseUri, child);
855
			
856
			
857
			}
858

    
859

    
860
			public Uri[] GetModules(String moduleUri, Uri baseUri, String[] locationHints){
861
				XdmValue files = catXPC.Evaluate("./module[@uri='" + moduleUri + "']/@file/string()", testCase);
862
				if (files.Count == 0) {
863
					throw new Exception("Failed to find module entry for " + moduleUri);
864
				}
865

    
866
				Uri[] fullPaths = new Uri[1];
867

    
868

    
869
				fullPaths [0] = resolve (baseUri, files.Unwrap().head().getStringValue());
870
				
871
			
872
				return fullPaths;
873
			}
874

    
875
            /*public StreamSource[] resolve(string moduleURI, string baseURI, string[] locations) {
876
                try {
877
                    XdmValue files = catXPC.Evaluate("./module[@uri='" + moduleURI + "']/@file/string()", testCase);
878
                    if (files.size() == 0) {
879
                        throw new XPathException("Failed to find module entry for " + moduleURI);
880
                    }
881
                    StreamSource[] ss = new StreamSource[files.size()];
882
                    for (int i = 0; i < files.size(); i++) {
883
                        URI uri = testCase.getBaseURI().resolve(files.itemAt(i).ToString());
884
                        ss[i] = getQuerySource(uri);
885
                    }
886
                    return ss;
887
                } catch (SaxonApiException e) {
888
                    throw new XPathException(e);
889
                }
890
            }*/
891
        }
892

    
893
        public class TestUriResolver : XmlUrlResolver
894
        {
895
            Environment env1;
896

    
897
            public TestUriResolver(Environment env)
898
            {
899
                this.env1 = env;
900
            }
901

    
902
            public override Uri ResolveUri(Uri base1, string href)
903
            {
904
                XdmNode node = null;
905
                try
906
                {
907
                    node = env1.sourceDocs[href];
908
                }
909
                catch (Exception)
910
                {
911
                    try
912
                    {
913
                        return (new XmlUrlResolver()).ResolveUri(base1, href);
914
                    }
915
                    catch (Exception) {
916
                        return null;
917
                    }
918

    
919
                }
920
                if (node == null)
921
                {
922
                    return null;
923
                }
924
                else
925
                {
926
                    return node.DocumentUri;
927
                }
928
            }
929
        }
930

    
931

    
932

    
933
        /**
934
         * Static method called as an external function call to evaluate a literal when running in "unfolded" mode.
935
         * The function simply returns the value of its argument - but the optimizer doesn't know that, so it
936
         * can't pre-evaluate the call at compile time.
937
         *
938
         * @param value the value to be returned
939
         * @return the supplied value, unchanged
940
         */
941

    
942
        /*public static Sequence lazyLiteral(Sequence value) {
943
            return value;
944
        } */
945

    
946
        /**
947
         * Collect together information about all the dependencies of tests that use a given environment
948
         * @param processor the Saxon processor
949
         * @param env the environment for which dependency information is to be gathered
950
         * @
951
         */
952

    
953
        private void buildDependencyDictionary(Processor processor, Environment env)
954
        {
955
            XQueryCompiler xqCompiler = processor.NewXQueryCompiler();
956
            xqCompiler.XQueryLanguageVersion = "3.0";
957
			xqCompiler.BaseUri = testSuiteDir;
958
            XdmValue result = xqCompiler.Compile(
959
			"        declare namespace fots = \"http://www.w3.org/2010/09/qt-fots-catalog\";\n" +
960
			"        let $testsets := doc('" + testSuiteDir + "/catalog.xml')//fots:test-set/@file/doc(resolve-uri(., exactly-one(base-uri(.))))\n" +
961
			"        for $dependencyTS in $testsets//fots:dependency\n" +
962
			"        let $name := $dependencyTS/@type\n" +
963
			"        let $value := $dependencyTS/@value\n" +
964
			"        group by $name, $value\n" +
965
			"        order by $name, $value\n" +
966
			"        return <dependency type='{$name}' value='{$value}' />").Load().Evaluate();
967

    
968

    
969
            foreach (XdmItem item in result)
970
            {
971
                XdmNode node = (XdmNode)item;
972
                string type = node.GetAttributeValue(new QName("type"));
973
                string value = node.GetAttributeValue(new QName("value"));
974
                addDependency(type, value, dependencyIsSatisfied(node, env));
975
            }
976

    
977

    
978
        }
979

    
980

    
981
        protected class Dependency
982
        {
983
            public string dType;
984
            public bool satisfied;
985
        }
986

    
987

    
988
        public void addDependency(string depStr, string value, bool satisfied)
989
        {
990
            if (!dependencyDictionary.ContainsKey(value))
991
            {
992
                Dependency dep = new Dependency();
993
                dep.dType = depStr;
994
                dep.satisfied = satisfied;
995
                dependencyDictionary.Add(value, dep);
996
            }
997
        }
998

    
999
       
1000
            
1001

    
1002

    
1003
        
1004
    }
1005
}
1006

    
1007
//
1008
// The contents of this file are subject to the Mozilla Public License Version
1009
// 1.0 (the "License");
1010
// you may not use this file except in compliance with the License. You may
1011
// obtain a copy of the
1012
// License at http://www.mozilla.org/MPL/
1013
//
1014
// Software distributed under the License is distributed on an "AS IS" basis,
1015
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
1016
// See the License for the specific language governing rights and limitations
1017
// under the License.
1018
//
1019
// The Original Code is: all this file.
1020
//
1021
// The Initial Developer of the Original Code is Michael Kay,
1022
//
1023
// Portions created by (your name) are Copyright (C) (your legal entity). All
1024
// Rights Reserved.
1025
//
1026
// Contributor(s): none.
1027
//
1028

    
1029

    
(6-6/19)