Project

Profile

Help

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

he / src / samples / cs / TestRunner10.0 / QT3TestSuiteDriver.cs @ be17ef52

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
using System.Linq;
18
using java.util;
19

    
20
namespace TestRunner
21
{
22

    
23

    
24

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

    
31

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

    
35

    
36
        public object getDependencyDictionary()
37
        {
38
            return dependencyDictionary;
39
        }
40

    
41
        private Dictionary<string, Dependency> dependencyDictionary = new Dictionary<string, Dependency>();
42

    
43

    
44
        private Spec spec;
45
        //private FotsResultsDocument resultsDoc;
46

    
47
        public override string catalogNamespace()
48
        {
49
            return CNS;
50
        }
51
			
52

    
53
        public override void processSpec(string specStr)
54
        {
55

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

    
83

    
84
        //C:/work/repositories/hg/qt3-testsuite/ C:/work/repositories/hg/qt3-testsuite/results/saxon/ -lang:XQ31
85
        //file:////C:/work/repositories/hg/qt3tests/ C:/work/repositories/hg/qt3tests/results/saxon/ -lang:XQ30
86
        public static void Main(string[] args)
87
        {
88

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

    
99

    
100

    
101
        protected override void createGlobalEnvironments(XdmNode catalog, XPathCompiler xpc)
102
        {
103
            Environment environment = null;
104

    
105
            Environment defaultEnv = null;
106
            try
107
            {
108
                defaultEnv = localEnvironments["default"];
109
            }
110
            catch (Exception) { }
111
            foreach (XdmNode env in catalog.Select(Steps.Descendant("environment")))
112
            {
113
                environment = Environment.processEnvironment(
114
                        this, xpc, env, globalEnvironments, defaultEnv);
115
            }
116
            buildDependencyDictionary(driverProc, environment);
117
        }
118

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

    
344
        /**
345
         * Run a test case
346
         *
347
         *
348
         * @param testCase the test case element in the catalog
349
         * @param xpc      the XPath compiler to be used for compiling XPath expressions against the catalog
350
         * @
351
         */
352

    
353
        protected override void runTestCase(XdmNode testCase, XPathCompiler xpc)
354
        {
355
            bool run = true;
356
            bool xpDependency = false;
357
            string hostLang;
358
            string langVersion;
359
            Spec specOpt = Spec.NULL;
360
            XPathCompiler xpath = driverProc.NewXPathCompiler();
361
            string testCaseName = testCase.GetAttributeValue(new QName("name"));
362
            string testSetName = testCase.Parent.GetAttributeValue(new QName("name"));
363

    
364
            XdmNode resultElement = testCase.Select(Steps.Child("result")).FirstOrDefault(); ;
365
            bool needSerializedResult = resultElement.Select(Steps.Descendant("assert-serialization-error")).Count() > 0
366
                    || resultElement.Select(Steps.Descendant("serialization-matches")).Count() > 0;
367
            bool needResultValue = true;
368
            if (needSerializedResult)
369
            {
370
                needResultValue = resultElement.Select(Steps.Descendant(Predicates.IsElement()).Where(Predicates.Not(
371
                        Predicates.HasLocalName("serialization-matches")
372
                                .Or(Predicates.HasLocalName("assert-serialization-error"))
373
                                .Or(Predicates.HasLocalName("any-of"))
374
                                .Or(Predicates.HasLocalName("all-of"))))).Count() > 0;
375
            }
376

    
377
            
378
            XdmNode alternativeResult = null;
379
            XdmNode optimization = null;
380

    
381

    
382
            hostLang = ((SpecAttr)spec.GetAttr()).sname;
383
            langVersion = ((SpecAttr)spec.GetAttr()).version;
384

    
385

    
386
            Environment env = getEnvironment(testCase, xpc);
387
            if (env == null)
388
            {
389
                notrun++;
390
                return;
391
            }
392
            env.xpathCompiler.BackwardsCompatible = false;
393
            env.processor.XmlVersion = (decimal)1.0;
394

    
395

    
396
            //test
397
            /*bool icuColCheck = net.sf.saxon.Version.platform.hasICUCollator ();
398
			bool icuNumCheck = net.sf.saxon.Version.platform.hasICUNumberer();
399
			Console.WriteLine ("ICUCol: " + (icuColCheck ? "true" : "false"));
400
			Console.WriteLine ("ICUNum: " + (icuNumCheck ? "true" : "false"));*/
401
            //end of test
402
            IList<XdmNode> dependencies = new List<XdmNode>(testCase.Select(Steps.Child("dependency")));
403
            dependencies.Concat(testCase.Select(Steps.Child("dependency")));
404
            foreach (XdmNode dependency in dependencies)
405
            {
406
                string type = (dependency).GetAttributeValue(new QName("type"));
407
                if (type == null)
408
                {
409
                    // throw new IllegalStateException("dependency/@type is missing"); //TODO
410
                }
411
                string value = (dependency).GetAttributeValue(new QName("value"));
412
                if (value == null)
413
                {
414
                    //throw new IllegalStateException("dependency/@value is missing"); //TODO
415
                }
416

    
417
                if (type.Equals("spec"))
418
                {
419
                    bool applicable = false;
420
                    if (!value.Contains(((SpecAttr)spec.GetAttr()).sname))
421
                    {
422
                        applicable = false;
423
                    }
424
                    else if (value.Contains(((SpecAttr)spec.GetAttr()).svname))
425
                    {
426
                        applicable = true;
427
                    }
428
                    else if ( ( ((SpecAttr)spec.GetAttr()).svname.Equals("XQ30") || ((SpecAttr)spec.GetAttr()).svname.Equals("XQ31")) && (value.Contains("XQ10+") || value.Contains("XQ30+")) )
429
                    {
430
                        applicable = true;
431
                    }
432
                    else if ( ( ((SpecAttr)spec.GetAttr()).svname.Equals("XP30") || ((SpecAttr)spec.GetAttr()).svname.Equals("XP31")) && (value.Contains("XP20+") || value.Contains("XP30+")) )
433
                    {
434
                        applicable = true;
435
                    }
436
                    if (!applicable)
437
                    {
438
                        writeTestcaseElement(testCaseName, "n/a", "not" + ((SpecAttr)spec.GetAttr()).svname, spec);
439
                        notrun++;
440
                        return;
441
                    }
442
                }
443
                if (langVersion.Equals("3.0") || langVersion.Equals("3.1"))
444
                {
445
                  
446
                    env.processor.Implementation.setConfigurationProperty(JFeatureKeys.ENVIRONMENT_VARIABLE_RESOLVER, new EnvironmentVariableResolver());
447
                }
448
                /*if (type.Equals("feature") && value.Equals("xpath-1.0-compatibility"))
449
                {
450
                    hostLang = "XP";
451
                    langVersion = "3.0";
452
                    xpDependency = true;
453
                    specOpt = Spec.XP30;
454
                }
455
                if (type.Equals("feature") && value.Equals("namespace-axis"))
456
                {
457
                    hostLang = "XP";
458
                    langVersion = "3.0";
459
                    xpDependency = true;
460
                    specOpt = Spec.XP30;
461
                }*/
462
                if (!EnsureDependencySatisfied((XdmNode)dependency, env))
463
                {
464
                    println("*** Dependency not satisfied: " + ((XdmNode)dependency).GetAttributeValue(new QName("type")));
465
                    writeTestcaseElement(testCaseName, "n/a", "Dependency not satisfied", spec);
466
                    run = false;
467
                    notrun++;
468
                    return;
469
                }
470
            }
471

    
472
            XdmNode exceptionElement;
473
            
474
            exceptionsMap.TryGetValue(testCaseName, out exceptionElement);
475

    
476
            
477
            if (exceptionElement != null)
478
            {
479
                XdmItem config = xpath.EvaluateSingle("configuration", exceptionElement);
480

    
481
                string runAtt = exceptionElement.GetAttributeValue(new QName("run"));
482
                string reasonMsg = xpath.EvaluateSingle("reason", exceptionElement).ToString();
483
                string reportAtt = exceptionElement.GetAttributeValue(new QName("report"));
484

    
485
                if (config != null)
486
                {
487
                    XdmItem paramValue = xpath.EvaluateSingle("param[@name='not-unfolded' and @value='yes']/@name", config);
488
                    if (unfolded && paramValue != null)
489
                    {
490
                        writeTestcaseElement(testCaseName, "notRun", reasonMsg, spec);
491
                        notrun++;
492
                        return;
493
                    }
494
                }
495

    
496
                if ("false".Equals(runAtt))
497
                {
498
                    writeTestcaseElement(testCaseName, reportAtt, reasonMsg, spec);
499
                    notrun++;
500
                    return;
501
                }
502

    
503
                alternativeResult = (XdmNode)xpc.EvaluateSingle("result", exceptionElement);
504
                optimization = (XdmNode)xpc.EvaluateSingle("optimization", exceptionElement);
505
            }
506

    
507
            if (run && (specOpt == Spec.NULL || specOpt == spec))
508
            {
509

    
510
                TestOutcome outcome = new TestOutcome(this);
511
                string exp = null;
512
                try
513
                {
514
                    exp = xpc.Evaluate("if (test/@file) then unparsed-text(resolve-uri(test/@file, base-uri(.))) else string(test)", testCase).ToString();
515
                }
516
                catch (Exception err)
517
                {
518
                    println("*** Failed to read query: " + err.Message);
519
                    outcome.SetException((DynamicError)err);
520
                }
521

    
522
                //noinspection ThrowableResultOfMethodCallIgnored
523
                if (outcome.GetException() == null)
524
                {
525
                    if (hostLang.Equals("XP") || hostLang.Equals("XT"))
526
                    {
527
                        XPathCompiler testXpc = env.xpathCompiler;
528
                        testXpc.XPathLanguageVersion = langVersion;
529
                        testXpc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
530
                        testXpc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema");
531
                        testXpc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map");
532
                        //testXpc.DeclareNamespace("math", NamespaceConstant.MATH);
533
                        //testXpc.DeclareNamespace("Dictionary", NamespaceConstant.Dictionary_FUNCTIONS);
534

    
535
                        try
536
                        {
537
                            XPathSelector selector = testXpc.Compile(exp).Load();
538
                            
539
                        foreach (QName varName in env.params1.Keys)
540
                            {
541
                                selector.SetVariable(varName, env.params1[varName]);
542
                            }
543
                            if (env.contextItem != null)
544
                            {
545
                                selector.ContextItem = env.contextItem;
546
                            }
547
                            selector.InputXmlResolver = new TestUriResolver(env);
548

    
549
                            if (env.unparsedTextResolver != null)
550
                            {
551
                                //selector.getUnderlyingXPathContext().setUnparsedTextURIResolver(env.unparsedTextResolver); //TODO
552
                            }
553
                            XdmValue result = selector.Evaluate();
554
                            outcome.SetPrincipalResult(result);
555
                        }
556
                        catch (Exception err)
557
                        {
558
                            println(err.Message);
559
                            
560
                           outcome.SetException(err);
561
                            
562

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

    
634
                            }
635
                            XQueryEvaluator selector = q.Load();
636
                            foreach (QName varName in env.params1.Keys)
637
                            {
638
                                selector.SetExternalVariable(varName, env.params1[varName]);
639
                            }
640
                            if (env.contextItem != null)
641
                            {
642
                                selector.ContextItem = env.contextItem;
643
                            }
644
							selector.InputXmlResolver = env;
645
                            //selector.InputXmlResolver =  .SetURIResolver(new TestURIResolver(env)); //TODO
646
                            if (env.unparsedTextResolver != null)
647
                            {
648
                                selector.Implementation.getUnderlyingQueryContext().setUnparsedTextURIResolver(env.unparsedTextResolver);// TODO
649
                            }
650
                            if (needSerializedResult)
651
                            {
652
                                StringWriter sw = new StringWriter();
653
                                Serializer serializer =env.processor.NewSerializer();
654
                                serializer.SetOutputWriter(sw);
655
                                selector.Run(serializer);
656
                                outcome.SetPrincipalSerializedResult(sw.ToString());
657
                            }
658
                            if (needResultValue)
659
                            {
660
                                try
661
                                {
662
                                    XdmValue result = selector.Evaluate();
663

    
664
                                    outcome.SetPrincipalResult(result);
665
                                }
666
                                catch (StackOverflowException err)
667
                                {
668

    
669
                                    println("in TestSet " + testSetName + err.StackTrace);
670
                                    println(err.Message);
671
                                    outcome.SetException(err);
672
                                }
673
                                }
674
                        }
675
                        catch(StackOverflowException err)
676
                        {
677

    
678
                            println("in TestSet " + testSetName + err.StackTrace);
679
                            println(err.Message);
680
                            outcome.SetException(err);
681

    
682

    
683
                        }
684
                        catch (Exception err)
685
                        {
686
							println("in TestSet " + testSetName + err.StackTrace);
687
                            println(err.Message);
688
                            outcome.SetException(err);
689
						//	outcome.SetErrorsReported(testXqc.ErrorList); //TODO
690
                        }
691
                    }
692
                    else
693
                    {
694
                        writeTestcaseElement(testCaseName, "notRun", "No processor found", spec);
695
                        notrun++;
696
                        return;
697
                    }
698
                }
699

    
700
                if (env.resetActions != null)
701
                {
702
                    env.resetActions.Clear();
703
                }
704
                XdmNode assertion;
705
                if (alternativeResult != null)
706
                {
707
                    assertion = (XdmNode)xpc.EvaluateSingle("*[1]", alternativeResult);
708
                }
709
                else
710
                {
711
                    assertion = (XdmNode)xpc.EvaluateSingle("result/*[1]", testCase);
712
                }
713
                if (assertion == null)
714
                {
715
                    println("*** No assertions found for test case " + testCaseName);
716
                    writeTestcaseElement(testCaseName, "disputed", "No assertions in test case", spec);
717
                    feedback.Feedback(successes, failures++, total);
718
                    return;
719
                }
720
                XPathCompiler assertXpc = env.processor.NewXPathCompiler();
721
                assertXpc.XPathLanguageVersion = "3.1";
722
                assertXpc.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
723
                assertXpc.DeclareNamespace("xs", "http://www.w3.org/2001/XMLSchema");
724
                assertXpc.DeclareNamespace("math", "http://www.w3.org/2005/xpath-functions/math");
725
                assertXpc.DeclareNamespace("map", "http://www.w3.org/2005/xpath-functions/map");
726
                assertXpc.DeclareNamespace("array", "http://www.w3.org/2005/xpath-functions/array");
727
                assertXpc.DeclareNamespace("j", "http://www.w3.org/2005/xpath-functions");
728
                assertXpc.DeclareVariable(new QName("result"));
729

    
730
                bool b = outcome.TestAssertion(outcome, assertion, outcome.GetPrincipalResultDoc(), assertXpc, xpc, debug);
731
                if (b)
732
                {
733
                    //println("OK");
734
                    writeTestcaseElement(testCaseName, "pass", null, spec);
735
                    feedback.Feedback(successes++, failures, total);
736

    
737
                }
738
                else
739
                {
740

    
741
                    if (outcome.IsException())
742
                    {
743
                        XdmItem expectedError = xpc.EvaluateSingle("result//error/@code", testCase);
744

    
745
                        if (expectedError == null)
746
                        {
747
                            //                        if (debug) {
748
                            //                            outcome.getException().printStackTrace(System.out);
749
                            //                        }
750
                          
751
                            writeTestcaseElement(testCaseName, "fail", "Expected success, got ", spec);
752
                            println("*** fail, result " + outcome.GetException() +
753
                                    " Expected success.");
754
                            feedback.Feedback(successes, failures++, total);
755
                        }
756
                        else
757
                        {
758
                            String exceptionMessage = outcome.GetException() == null ? outcome.GetException().Message : "";
759
                            String errorCode = (outcome.GetErrorCode() == null ? "unknown - "+ exceptionMessage : outcome.GetErrorCode().LocalName);
760
                            writeTestcaseElement(testCaseName, "wrongError",
761
                                    "Expected error:" + expectedError.ToString() + ", got " + errorCode, spec);
762
                            println("*** fail, result " + errorCode +
763
                                    " Expected error:" + expectedError.ToString());
764
                            wrongErrorResults++;
765
                            feedback.Feedback(successes++, failures, total);
766
                        }
767

    
768
                    }
769
                    else
770
                    {
771
                        writeTestcaseElement(testCaseName, "fail", "Wrong results, got " +
772
                                truncate(outcome.Serialize(assertXpc.Processor, outcome.GetPrincipalResultDoc())), spec);
773
                        feedback.Feedback(successes, failures++, total);
774
                        if (debug)
775
                        {
776
                            try
777
                            {
778
                                println("Result:");
779
                                driverProc.WriteXdmValue(outcome.GetPrincipalResult(), driverSerializer);
780
                                println("<=======");
781
                            }
782
                            catch (Exception err)
783
                            {
784
                            }
785
                            //println(outcome.getResult());
786
                        }
787
                        else
788
                        {
789
                            println("*** fail (use -debug to show actual result)");
790
                            //failures++;
791
                        }
792
                    }
793
                }
794
            }
795
        }
796

    
797
        private string truncate(string in1)
798
        {
799
            if (in1.Length > 80)
800
            {
801
                return in1.Substring(0, 80) + "...";
802
            }
803
            else
804
            {
805
                return in1;
806
            }
807
        }
808

    
809

    
810

    
811
        public void writeResultFilePreamble(Processor processor, XdmNode catalog)
812
        {
813
            resultsDoc.writeResultFilePreamble(processor, catalog);
814
        }
815

    
816
        public void writeResultFilePostamble()
817
        {
818
            resultsDoc.writeResultFilePostamble();
819
        }
820

    
821

    
822
        public void startTestSetElement(XdmNode testSetNode)
823
        {
824
            resultsDoc.startTestSetElement(testSetNode);
825
        }
826

    
827

    
828
        protected void writeTestSetEndElement()
829
        {
830
            resultsDoc.endElement();
831
        }
832

    
833
        private void writeTestcaseElement(string name, string result, string comment, Spec specOpt)
834
        {
835
            resultsDoc.writeTestcaseElement(name, result, comment);
836
        }
837

    
838
		public class ModuleResolver : IQueryResolver {
839

    
840
            XPathCompiler catXPC;
841
            XdmNode testCase;
842

    
843
            public ModuleResolver(XPathCompiler xpc)
844
            {
845
                this.catXPC = xpc;
846
            }
847

    
848
            public void setTestCase(XdmNode testCase)
849
            {
850
                this.testCase = testCase;
851
            }
852

    
853

    
854
			public Object GetEntity(Uri absoluteUri) {
855
				String fullPath = absoluteUri.AbsolutePath;
856
				Stream stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
857
				//javax.xml.transform.Source sourcei = new javax.xml.transform.stream.StreamSource(uri);
858

    
859
				return stream;
860
			}
861

    
862
			internal Uri resolve(Uri basei, String child) { 
863
			
864
				return (new XmlUrlResolver ()).ResolveUri (testCase.BaseUri, child);
865
			
866
			
867
			}
868

    
869

    
870
			public Uri[] GetModules(String moduleUri, Uri baseUri, String[] locationHints){
871
				XdmValue files = catXPC.Evaluate("./module[@uri='" + moduleUri + "']/@file/string()", testCase);
872
				if (files.Count == 0) {
873
					throw new Exception("Failed to find module entry for " + moduleUri);
874
				}
875

    
876
				Uri[] fullPaths = new Uri[1];
877

    
878

    
879
				fullPaths [0] = resolve (baseUri, files.Unwrap().head().getStringValue());
880
				
881
			
882
				return fullPaths;
883
			}
884

    
885
            /*public StreamSource[] resolve(string moduleURI, string baseURI, string[] locations) {
886
                try {
887
                    XdmValue files = catXPC.Evaluate("./module[@uri='" + moduleURI + "']/@file/string()", testCase);
888
                    if (files.size() == 0) {
889
                        throw new XPathException("Failed to find module entry for " + moduleURI);
890
                    }
891
                    StreamSource[] ss = new StreamSource[files.size()];
892
                    for (int i = 0; i < files.size(); i++) {
893
                        URI uri = testCase.getBaseURI().resolve(files.itemAt(i).ToString());
894
                        ss[i] = getQuerySource(uri);
895
                    }
896
                    return ss;
897
                } catch (SaxonApiException e) {
898
                    throw new XPathException(e);
899
                }
900
            }*/
901
        }
902

    
903
        public class EnvironmentVariableResolver : net.sf.saxon.lib.EnvironmentVariableResolver
904
        {
905

    
906
            public EnvironmentVariableResolver() { }
907
            
908
            public Set getAvailableEnvironmentVariables()
909
            {
910
                Set strings = new HashSet();
911
                strings.add("QTTEST");
912
                strings.add("QTTEST2");
913
                strings.add("QTTESTEMPTY");
914
                return strings;
915
            }
916

    
917
            public string getEnvironmentVariable(string name)
918
            {
919
                if (name.Equals("QTTEST"))
920
                {
921
                    return "42";
922
                }
923
                else if (name.Equals("QTTEST2"))
924
                {
925
                    return "other";
926
                }
927
                else if (name.Equals("QTTESTEMPTY"))
928
                {
929
                    return "";
930
                }
931
                else
932
                {
933
                    return null;
934
                }
935
            }
936

    
937
            
938
        }
939

    
940
        public class TestUriResolver : XmlUrlResolver
941
        {
942
            Environment env1;
943

    
944
            public TestUriResolver(Environment env)
945
            {
946
                this.env1 = env;
947
            }
948

    
949
            public override Uri ResolveUri(Uri base1, string href)
950
            {
951
                XdmNode node = null;
952
                try
953
                {
954
                    node = env1.sourceDocs[href];
955
                }
956
                catch (Exception)
957
                {
958
                    try
959
                    {
960
                        return (new XmlUrlResolver()).ResolveUri(base1, href);
961
                    }
962
                    catch (Exception) {
963
                        return null;
964
                    }
965

    
966
                }
967
                if (node == null)
968
                {
969
                    return null;
970
                }
971
                else
972
                {
973
                    return node.DocumentUri;
974
                }
975
            }
976
        }
977

    
978

    
979

    
980
        /**
981
         * Static method called as an external function call to evaluate a literal when running in "unfolded" mode.
982
         * The function simply returns the value of its argument - but the optimizer doesn't know that, so it
983
         * can't pre-evaluate the call at compile time.
984
         *
985
         * @param value the value to be returned
986
         * @return the supplied value, unchanged
987
         */
988

    
989
        /*public static Sequence lazyLiteral(Sequence value) {
990
            return value;
991
        } */
992

    
993
        /**
994
         * Collect together information about all the dependencies of tests that use a given environment
995
         * @param processor the Saxon processor
996
         * @param env the environment for which dependency information is to be gathered
997
         * @
998
         */
999

    
1000
        private void buildDependencyDictionary(Processor processor, Environment env)
1001
        {
1002
            XQueryCompiler xqCompiler = processor.NewXQueryCompiler();
1003
            xqCompiler.XQueryLanguageVersion = "3.0";
1004
			xqCompiler.BaseUri = testSuiteDir;
1005
            XdmValue result = xqCompiler.Compile(
1006
			"        declare namespace fots = \"http://www.w3.org/2010/09/qt-fots-catalog\";\n" +
1007
			"        let $testsets := doc('" + testSuiteDir + "/catalog.xml')//fots:test-set/@file/doc(resolve-uri(., exactly-one(base-uri(.))))\n" +
1008
			"        for $dependencyTS in $testsets//fots:dependency\n" +
1009
			"        let $name := $dependencyTS/@type\n" +
1010
			"        let $value := $dependencyTS/@value\n" +
1011
			"        group by $name, $value\n" +
1012
			"        order by $name, $value\n" +
1013
			"        return <dependency type='{$name}' value='{$value}' />").Load().Evaluate();
1014

    
1015

    
1016
            foreach (XdmItem item in result)
1017
            {
1018
                XdmNode node = (XdmNode)item;
1019
                string type = node.GetAttributeValue(new QName("type"));
1020
                string value = node.GetAttributeValue(new QName("value"));
1021
                addDependency(type, value, EnsureDependencySatisfied(node, env));
1022
            }
1023

    
1024

    
1025
        }
1026

    
1027

    
1028
        protected class Dependency
1029
        {
1030
            public string dType;
1031
            public bool satisfied;
1032
        }
1033

    
1034

    
1035
        public void addDependency(string depStr, string value, bool satisfied)
1036
        {
1037
            if (!dependencyDictionary.ContainsKey(value))
1038
            {
1039
                Dependency dep = new Dependency();
1040
                dep.dType = depStr;
1041
                dep.satisfied = satisfied;
1042
                dependencyDictionary.Add(value, dep);
1043
            }
1044
        }
1045

    
1046
       
1047
            
1048

    
1049

    
1050
        
1051
    }
1052
}
1053

    
1054
//
1055
// The contents of this file are subject to the Mozilla Public License Version
1056
// 1.0 (the "License");
1057
// you may not use this file except in compliance with the License. You may
1058
// obtain a copy of the
1059
// License at http://www.mozilla.org/MPL/
1060
//
1061
// Software distributed under the License is distributed on an "AS IS" basis,
1062
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
1063
// See the License for the specific language governing rights and limitations
1064
// under the License.
1065
//
1066
// The Original Code is: all this file.
1067
//
1068
// The Initial Developer of the Original Code is Michael Kay,
1069
//
1070
// Portions created by (your name) are Copyright (C) (your legal entity). All
1071
// Rights Reserved.
1072
//
1073
// Contributor(s): none.
1074
//
1075

    
1076

    
(6-6/19)