Project

Profile

Help

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

he / tags / 9.6.0.7 / hen / csource / samples / TestDriver.cs @ aa733b18

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

    
12

    
13
//using net.sf.saxon.Version;
14
using JFeatureKeys = net.sf.saxon.lib.FeatureKeys;
15

    
16
namespace TestRunner
17
{
18

    
19

    
20

    
21
public abstract class TestDriver {
22

    
23
    protected string resultsDir = null;
24
    public ResultsDocument resultsDoc;
25
    protected int successes = 0;
26
    protected int total = 200000;
27
    protected int failures = 0;
28
    protected int notrun = 0;
29
    protected int wrongErrorResults = 0;
30
    protected bool unfolded = false;
31
    protected bool saveResults = false;
32
    protected int generateByteCode = 0;
33
    protected TreeModel treeModel = TreeModel.TinyTree;
34
    protected bool debug = false;
35
    protected Regex testPattern = null;
36
    protected string requestedTestSet = null;
37
    protected string testSuiteDir;
38
    protected Processor driverProc = new Processor(true);
39
    protected Serializer driverSerializer;
40
	protected Spec spec;
41
    protected Dictionary<string, XdmNode> exceptionsMap = new Dictionary<string, XdmNode>();
42
    protected Dictionary<string, Environment> globalEnvironments = new Dictionary<string, Environment>();
43
    protected Dictionary<string, Environment> localEnvironments = new Dictionary<string, Environment>();
44
	protected IFeedbackListener feedback = new DefaultFeedbackListener();
45
	protected bool useXslt30Transformer = true;  // Temporary for controlling test processor
46

    
47

    
48
    public abstract string catalogNamespace();
49

    
50
    public int GenerateByteCode {
51
        get { return generateByteCode; }
52
        set { generateByteCode = value; }
53
    }
54

    
55
		public class DefaultFeedbackListener : IFeedbackListener
56
		{
57

    
58
			public void Feedback(int passed, int failed, int total)
59
			{
60
				Console.WriteLine("Done " + (passed + failed) + " of " + total);
61
		
62

    
63
			}
64

    
65

    
66
			public void Message(String message, bool popup)
67
			{
68
				Console.WriteLine (message);
69
			}
70
		}
71

    
72

    
73
		public Spec Spec {
74
			get{return spec;}
75
		}
76
    public void setFeedbackListener(IFeedbackListener f)
77
    {
78
        feedback = f;
79
    }
80

    
81
    public int Failures
82
    {
83
        get { return failures; }
84
        set { failures = value; }
85
    }
86

    
87
    public bool Unfolded
88
    {
89
        get { return unfolded; }
90
        set { unfolded = value; }
91
    }
92

    
93
    public TreeModel TreeModel
94
    {
95
        get { return treeModel; }
96
        set { treeModel = value; }
97
    }
98
    public void go(string[] args) {
99

    
100
    //    AutoActivate.activate(driverProc);
101
        driverSerializer = driverProc.NewSerializer();
102
        //testSuiteDir = args[0];
103

    
104

    
105

    
106
        testSuiteDir = args[0];
107
        if (testSuiteDir.EndsWith("/"))
108
        {
109
            testSuiteDir = testSuiteDir.Substring(0, testSuiteDir.Length - 1);
110
        }
111
				
112

    
113
        resultsDir = args[1];
114
        if (resultsDir.EndsWith("/"))
115
        {
116
            resultsDir = resultsDir.Substring(0, resultsDir.Length - 1);
117
        }
118
        string catalog = testSuiteDir + "/catalog.xml";
119
        string specStr = null;
120

    
121
        for (int i = 2; i < args.Length; i++) {
122
            if (args[i].StartsWith("-t:")) {
123
                testPattern = new Regex(args[i].Substring(3));
124
            }
125
            if (args[i].StartsWith("-s:")) {
126
                requestedTestSet = args[i].Substring(3);
127
            }
128
            if (args[i].StartsWith("-debug")) {
129
                debug = true;
130
            }
131
            if (args[i].Equals("-unfolded")) {
132
                unfolded = true;
133
            }
134
            if (args[i].Equals("-save"))
135
            {
136
                saveResults = true;
137
            }
138
            if (args[i].StartsWith("-bytecode"))
139
            {
140
                if (args[i].Substring(10).Equals("on"))
141
                {
142
                    generateByteCode = 1;
143
                }
144
                else if (args[i].Substring(10).Equals("debug"))
145
                {
146
                    generateByteCode = 2;
147
                }
148
                else
149
                {
150
                    generateByteCode = 0;
151
                }
152
            }
153
           /* if (args[i].StartsWith("-tree"))
154
            {
155
                if (args[i].Substring(6).EqualsIgnoreCase("dom"))
156
                {
157
                    treeModel = new DOMObjectModel();
158
                }
159
                else if (args[i].Substring(6).EqualsIgnoreCase("jdom"))
160
                {
161
                    treeModel = new JDOMObjectModel();
162
                }
163
                else if (args[i].Substring(6).EqualsIgnoreCase("jdom2"))
164
                {
165
                    treeModel = new JDOM2ObjectModel();
166
                }
167
                else if (args[i].Substring(6).EqualsIgnoreCase("dom4j"))
168
                {
169
                    treeModel = new DOM4JObjectModel();
170
                }
171
                else if (args[i].Substring(6).EqualsIgnoreCase("xom"))
172
                {
173
                    treeModel = new XOMObjectModel();
174
                }
175
                else if (args[i].Substring(6).EqualsIgnoreCase("axiom"))
176
                {
177
                    treeModel = new AxiomObjectModel();
178
                }
179
                else if (args[i].Substring(6).EqualsIgnoreCase("tinytree"))
180
                {
181
                    treeModel = TreeModel.TINY_TREE;
182
                }
183
                else if (args[i].Substring(6).EqualsIgnoreCase("condensed"))
184
                {
185
                    treeModel = TreeModel.TINY_TREE_CONDENSED;
186
                }
187
                else if (args[i].Substring(6).EqualsIgnoreCase("linked"))
188
                {
189
                    treeModel = TreeModel.LINKED_TREE;
190
                }
191
                else
192
                {
193
                    throw new Exception("The TreeModel specified does not exist");
194
                }
195
            }*/
196
            if (args[i].StartsWith("-lang"))
197
            {
198
                specStr = args[i].Substring(6);
199
                processSpec(specStr);
200
            }
201

    
202
			// Temporary for controlling test processor
203
			if (args[i].StartsWith("-xt30")) {
204
				if (args[i].Substring(6).Equals("on")) {
205
					useXslt30Transformer = true;
206
				} else if (args[i].Substring(6).Equals("off")) {
207
					useXslt30Transformer = false;
208
				}
209
			}
210
        }
211
        if (resultsDir == null)
212
        {
213
            feedback.Message("No results directory specified (use -o:dirname)", true);
214
            /*if (guiForm == null)
215
            {
216
                System.exit(2);
217
            }*/
218
        }
219
        if (resultsDoc == null)
220
        {
221
            feedback.Message("No results document specified (use -lang option)", true);
222
            /*if (guiForm == null)
223
            {
224
                System.exit(2);
225
            }*/
226
        }
227

    
228
        driverSerializer.SetOutputStream(System.Console.OpenStandardError());
229
        driverSerializer.SetOutputProperty(Serializer.OMIT_XML_DECLARATION, "yes");
230
        processCatalog(catalog);
231
        printResults(resultsDir + "/results" + driverProc.ProductVersion + ".xml");
232
    }
233

    
234
    public string getResultsDir() {
235
        return resultsDir;
236
    }
237

    
238
    public abstract void processSpec(string specStr);
239

    
240

    
241
    private void processCatalog(String catalogFile) {
242
       if (generateByteCode == 1) {
243
            driverProc.SetProperty(JFeatureKeys.GENERATE_BYTE_CODE, "true");
244
            driverProc.SetProperty(JFeatureKeys.DEBUG_BYTE_CODE, "false");
245
        } else if (generateByteCode == 2) {
246
            driverProc.SetProperty(JFeatureKeys.GENERATE_BYTE_CODE, "true");
247
            driverProc.SetProperty(JFeatureKeys.DEBUG_BYTE_CODE, "true");
248
        } else {
249
            driverProc.SetProperty(JFeatureKeys.GENERATE_BYTE_CODE, "false");
250
            driverProc.SetProperty(JFeatureKeys.DEBUG_BYTE_CODE, "false");
251
        }
252
        DocumentBuilder catbuilder = driverProc.NewDocumentBuilder();
253
        catbuilder.TreeModel = treeModel;
254
        XdmNode catalog = catbuilder.Build(new Uri(catalogFile.ToString())); 
255
        XPathCompiler xpc = driverProc.NewXPathCompiler();
256
        xpc.XPathLanguageVersion = "3.0";
257
        xpc.Caching = true;
258
        xpc.DeclareNamespace("", catalogNamespace());
259

    
260
        createGlobalEnvironments(catalog, xpc);
261

    
262
        try {
263
            writeResultFilePreamble(driverProc, catalog);
264
        } catch (Exception e) {
265
            System.Console.WriteLine(e.Message);
266
        }
267

    
268
        readExceptionsFile();
269

    
270

    
271
        if (requestedTestSet != null) {
272
            try {
273
                XdmNode funcSetNode = (XdmNode) xpc.EvaluateSingle("//test-set[@name='" + requestedTestSet + "']", catalog);
274
                if (funcSetNode == null) {
275
                    throw new Exception("Test-set " + requestedTestSet + " not found!");
276
                }
277
                processTestSet(catbuilder, xpc, funcSetNode);
278
            } catch (Exception e1) {
279
                //e1.printStackTrace();
280
                System.Console.WriteLine(e1.Message);
281
            }
282
        } else {
283
            foreach (XdmItem testSet in xpc.Evaluate("//test-set", catalog)) {
284
                processTestSet(catbuilder, xpc, ((XdmNode) testSet.Simplify));
285
            }
286
        }
287
        try {
288
            writeResultFilePostamble();
289
        } catch (Exception e) {
290
            System.Console.WriteLine(e.Message);
291
            //e.printStackTrace();
292
        }
293

    
294

    
295
    }
296

    
297
    /**
298
     * Look for an exceptions.xml document with the general format:
299
     * <p/>
300
     * <exceptions xmlns="...test catalog namespace...">
301
     * <exception test-set ="testset1" test-case="testcase" run="yes/no/not-unfolded"
302
     * bug="bug-reference" reason="">
303
     * <results>
304
     * ... alternative expected results ...
305
     * </results>
306
     * <optimization>
307
     * ... assertions about the "explain" tree
308
     * </optimization>
309
     * </exception>
310
     * </exceptions>
311
     */
312

    
313
    protected void readExceptionsFile() {
314

    
315
        XdmNode exceptionsDoc = null;
316
        DocumentBuilder exceptBuilder = driverProc.NewDocumentBuilder();
317
        QName testCase = new QName("", "test-case");
318
        try {
319
            exceptionsDoc = exceptBuilder.Build(new Uri(resultsDir + "/exceptions.xml"));
320
            IEnumerator iter = exceptionsDoc.EnumerateAxis(XdmAxis.Descendant, new QName("", "exception"));
321
            while (iter.Current != null) {
322
                XdmNode entry = (XdmNode) iter.Current;
323
                string test = entry.GetAttributeValue(testCase);
324
                if (test != null) {
325
                    exceptionsMap.Add(test, entry);
326
                }
327
                iter.MoveNext();
328
            }
329
        } catch (Exception e) {
330
            feedback.Message("*** Failed to process exceptions file: "+ e.Message, true);
331
            //printError("*** Failed to process exceptions file: ", e.getMessage()); //TODO - review this code later
332
        }
333

    
334
    }
335

    
336
    protected abstract void createGlobalEnvironments(
337
            XdmNode catalog, XPathCompiler xpc);
338

    
339
    protected void createLocalEnvironments(XdmNode testSetDocNode) {
340
        localEnvironments.Clear();
341
        Environment defaultEnvironment =
342
				Environment.createLocalEnvironment(testSetDocNode.BaseUri, generateByteCode, unfolded, spec);
343
        localEnvironments.Add("default", defaultEnvironment);
344
    }
345

    
346
    protected Environment getEnvironment(XdmNode testCase, XPathCompiler xpc) {
347
        string testCaseName = testCase.GetAttributeValue(new QName("name"));
348
        XdmNode environmentNode = (XdmNode) xpc.EvaluateSingle("environment", testCase);
349
        Environment env;
350
        if (environmentNode == null) {
351
            env = localEnvironments["default"];
352
        } else {
353
            string envName = environmentNode.GetAttributeValue(new QName("ref"));
354
            if (envName == null || envName.Equals("")) {
355
                env = Environment.processEnvironment(this, xpc, environmentNode, null, localEnvironments["default"]);
356
                bool baseUriCheck = ((XdmAtomicValue) xpc.EvaluateSingle("static-base-uri/@uri='#UNDEFINED'", environmentNode)).GetBooleanValue();
357
                if (baseUriCheck) {
358
                    //writeTestcaseElement(testCaseName, "notRun", "static-base-uri not supported", null);
359
                    return null;
360
                }
361
            } else {
362
                try
363
                {
364
                    env = localEnvironments[envName];
365
                }catch(Exception){
366
                    env = null;
367
                }
368
                if (env == null) {
369
                    try
370
                    {
371
                        env = globalEnvironments[envName];
372
                    }
373
                    catch (Exception e) { }
374
                }
375
                if (env == null) {
376
                    foreach (XdmItem e in xpc.Evaluate("//environment[@name='" + envName + "']", testCase)) {
377
                        Environment.processEnvironment(this, xpc, e, localEnvironments, localEnvironments["default"]);
378
                    }
379
                    try
380
                    {
381
                        env = localEnvironments[envName];
382
                    } catch(Exception e) {}
383
                }
384
                if (env == null) {
385
                    System.Console.WriteLine("*** Unknown environment " + envName);
386
                    //println("*** Unknown environment " + envName);
387
                    //writeTestcaseElement(testCaseName, "fail", "Environment " + envName + " not found", null);
388
                    failures++;
389
                    return null;
390
                }
391

    
392
            }
393
        }
394
        return env;
395
    }
396

    
397
    public void writeResultFilePreamble(Processor processor, XdmNode catalog) {
398
        resultsDoc.writeResultFilePreamble(processor, catalog);
399
    }
400

    
401
    public void writeResultFilePostamble(){
402
        resultsDoc.writeResultFilePostamble();
403
    }
404

    
405
    public void startTestSetElement(XdmNode testSetNode) {
406
        resultsDoc.startTestSetElement(testSetNode);
407
    }
408

    
409
    public void writeTestSetEndElement() {
410
        resultsDoc.endElement();
411
    }
412

    
413

    
414
    private void processTestSet(DocumentBuilder catbuilder, XPathCompiler xpc, XdmNode testSetNode) {
415
        string testName;
416
        string testSet;
417
        startTestSetElement(testSetNode);
418
        Uri testSetFile = new Uri(testSuiteDir + "/" + testSetNode.GetAttributeValue(new QName("file")));
419
        XdmNode testSetDocNode = catbuilder.Build(testSetFile);
420
        createLocalEnvironments(testSetDocNode);
421
        bool run = true;
422
        // TODO: this won't pick up any test-set level dependencies in the XSLT 3.0 catalog
423
        if (((XdmAtomicValue) xpc.Evaluate("exists(/test-set/dependency)", testSetDocNode).Simplify).GetBooleanValue()) {
424
            foreach (XdmItem dependency in xpc.Evaluate("/test-set/dependency", testSetDocNode)) {
425
                if (!dependencyIsSatisfied((XdmNode) dependency, localEnvironments["default"])) {
426
                    run = false;
427
                }
428
            }
429
        }
430
        if (run) {
431
            if (testPattern == null) {
432
                foreach (XdmItem env in xpc.Evaluate("//environment[@name]", testSetDocNode)) {
433
                    Environment.processEnvironment(this, xpc, env, localEnvironments, localEnvironments["default"]);
434
                }
435
            }
436
            testSet = xpc.EvaluateSingle("/test-set/@name", testSetDocNode).ToString();
437
            foreach (XdmItem testCase in xpc.Evaluate("//test-case", testSetDocNode)) {
438

    
439
                testName = xpc.EvaluateSingle("@name", testCase).ToString();
440

    
441
                ////
442
                //if (testName.Equals("type-0174"))
443
                //{
444
                //    int num = 0;
445
                //    System.Console.WriteLine("Test driver" + num);
446

    
447
                //}
448

    
449
                ///
450
                if (testPattern != null && !testPattern.IsMatch(testName)) {
451
                    continue;
452
                }
453
                println("-s:" + testSet + " -t:" + testName);
454

    
455
                runTestCase((XdmNode) testCase, xpc);
456
            }
457
        }
458
        writeTestSetEndElement();
459
    }
460

    
461
    protected abstract void runTestCase(XdmNode testCase, XPathCompiler catalogXpc);
462

    
463
    //public void setTestDriverForm(TestDriverForm gui) {
464
    //    guiForm = gui;
465
    //}
466

    
467
    public void println(string data)
468
    {
469
        //if (guiForm != null)
470
        //{
471
            //guiForm.setResultTextArea(data);
472
        //}
473
        //else
474
        {
475
            feedback.Message(data+"\n", false);
476
        }
477
    }
478

    
479
    public void printResults(string resultsFileStr)
480
    {
481
       /* if (guiForm != null)
482
        {
483
            guiForm.printResults("Result: " + successes + " successes, " + failures + " failures, " + wrongErrorResults + " incorrect ErrorCode, " + notrun + " not run", resultsFileStr, resultsDir);
484
        }
485
        else
486
        {*/
487
        feedback.Message(successes + " successes, " + failures + " failures, " + wrongErrorResults + " incorrect ErrorCode, " + notrun + " not run", false);
488
        //}
489
    }
490

    
491
    public void printError(string error, string message)
492
    {
493
        /*if (guiForm != null)
494
        {
495
            guiForm.errorPopup(error);
496
            System.err.println(error + message);
497
        }
498
        else
499
        {*/
500
        feedback.Message(error + message, true);
501
        //}
502
    }
503

    
504
    public abstract bool dependencyIsSatisfied(XdmNode dependency, Environment env);
505

    
506
}
507

    
508
//
509
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
510
// you may not use this file except in compliance with the License. You may obtain a copy of the
511
// License at http://www.mozilla.org/MPL/
512
//
513
// Software distributed under the License is distributed on an "AS IS" basis,
514
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
515
// See the License for the specific language governing rights and limitations under the License.
516
//
517
// The Original Code is: all this file.
518
//
519
// The Initial Developer of the Original Code is Saxonica Limited
520
//
521
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
522
//
523
// Contributor(s): none.
524
//
525

    
526
}
(14-14/24)