Project

Profile

Help

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

he / latest9.4 / hen / csource / samples / SchemaTestSuiteDriver.cs @ c3c84ba2

1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.IO;
5
using System.Text;
6
using System.Xml;
7
using Saxon.Api;
8

    
9
namespace TestRunner
10
{
11
         /**
12
         * This class runs the W3C XML Schema Test Suite, driven from the test catalog.
13
         */
14
        public class SchemaTestSuiteDriver {
15

    
16
            public static String testNS = "http://www.w3.org/XML/2004/xml-schema-test-suite/";
17
            /**
18
             * Run the testsuite using Saxon.
19
             *
20
             * @param args Array of parameters passed to the application
21
             * via the command line.
22
             */
23
 
24
            String testSuiteDir;
25
            IFeedbackListener feedback;
26
        
27
            bool onwards = false;
28

    
29
            StreamWriter results;
30
            QName xlinkHref;
31

    
32
            private XdmNode getChildElement(XdmNode parent, QName child)
33
            {
34
                IEnumerator e = parent.EnumerateAxis(XdmAxis.Child, child);
35
                return e.MoveNext() ? (XdmNode)e.Current : null;
36
            }
37

    
38
            public void setFeedbackListener(IFeedbackListener f)
39
            {
40
                feedback = f;
41
            }
42

    
43
            private XdmNode getLinkedDocument(XdmNode element, Processor processor, bool validate)
44
            {
45
                String href = element.GetAttributeValue(xlinkHref);
46
                DocumentBuilder builder = processor.NewDocumentBuilder();
47
                Uri target = new Uri(element.BaseUri, href);
48
                builder.IsLineNumbering = true;
49
                if (validate) {
50
                    builder.SchemaValidationMode = SchemaValidationMode.Strict;
51
                }
52
                return builder.Build(target);
53
            }
54

    
55

    
56
            /**
57
             * Run the tests
58
             * @param args command line arguments
59
             * @throws SAXException
60
             * @throws ParserConfigurationException
61
             * @throws XPathException
62
             * @throws IOException
63
             * @throws URISyntaxException
64
             */
65

    
66
            public void go(String[] args) {
67
                if (args.Length == 0 || args[0] == "-?")
68
                {
69
                    Console.WriteLine("SchemaTestSuiteDriver testDir [-w] [-onwards] -c:contributor? -s:setName? -g:groupName?");
70
                }
71
                Processor processor = new Processor(true);
72
                Console.WriteLine("Testing Saxon " + processor.ProductVersion);
73
                                
74
                testSuiteDir = args[0];
75
                if (testSuiteDir.EndsWith("/"))
76
                {
77
                    testSuiteDir = testSuiteDir.Substring(0, testSuiteDir.Length - 1);
78
                }
79
                String testSetPattern = null;   // TODO use a regex
80
                String testGroupPattern = null;
81
                String contributor = null;
82
                Hashtable exceptions = new Hashtable();
83

    
84
                for (int i=1; i<args.Length; i++) {
85
                    if (args[i] == ("-w")) {
86
                        //showWarnings = true;
87
                    } else if (args[i] == ("-onwards")) {
88
                        onwards = true;
89
                    } else if (args[i].StartsWith("-c:")) {
90
                        contributor = args[i].Substring(3);
91
                    } else if (args[i].StartsWith("-s:")) {
92
                        testSetPattern = args[i].Substring(3);
93
                    } else if (args[i].StartsWith("-g:")) {
94
                        testGroupPattern = args[i].Substring(3);
95
                    } else if (args[i] == "-?") {
96
                        Console.WriteLine("Usage: SchemaTestSuiteDriver testDir [-w] [-s:testSetPattern] [-g:testGroupPattern]");
97
                    }
98
                }
99

    
100
                int total = 39700;
101
                int passed = 0;
102
                int failed = 0;
103

    
104
                try {
105

    
106
                    xlinkHref = new QName("xlink", "http://www.w3.org/1999/xlink", "href");
107

    
108
                    QName testCaseNT = new QName("", "", "testcase");
109
                    QName commentNT = new QName("", "", "comment");
110

    
111
                    QName testSetRefNT = new QName(testNS, "testSetRef");
112
                    QName testGroupNT = new QName(testNS, "testGroup");
113
                    QName testSetNT = new QName(testNS, "testSet");
114
                    QName schemaTestNT = new QName(testNS, "schemaTest");
115
                    QName instanceTestNT = new QName(testNS, "instanceTest");
116
                    QName schemaDocumentNT = new QName(testNS, "schemaDocument");
117
                    QName instanceDocumentNT = new QName(testNS, "instanceDocument");
118
                    QName expectedNT = new QName(testNS, "expected");
119
                    QName currentNT = new QName(testNS, "current");
120

    
121
                    QName validityAtt = new QName("", "", "validity");
122
                    QName nameAtt = new QName("", "", "name");
123
                    QName contributorAtt = new QName("", "", "contributor");
124
                    QName setAtt = new QName("", "", "set");
125
                    QName groupAtt = new QName("", "", "group");
126
                    QName statusAtt = new QName("", "", "status");
127
                    QName bugzillaAtt = new QName("", "", "bugzilla");
128
                    QName targetNamespaceAtt = new QName("", "", "targetNamespace");
129
                    QName schemaVersion = new QName("saxon", "http://saxon.sf.net/", "schemaVersion");
130

    
131

    
132
                    DocumentBuilder builder = processor.NewDocumentBuilder();
133
                    builder.BaseUri = new Uri(testSuiteDir + "/");
134
                    XdmNode catalog = builder.Build(
135
                            new FileStream(testSuiteDir + "/suite.xml", FileMode.Open, FileAccess.Read, FileShare.Read));
136

    
137
                    results = new StreamWriter(testSuiteDir + "/saxon/SaxonResults"
138
                                + processor.ProductVersion + "n.xml");
139

    
140
                    results.Write("<testSuiteResults xmlns='" + testNS + "' xmlns:saxon='http://saxon.sf.net/' " +
141
                            "suite='TS_2006' " +
142
                            "processor='Saxon-SA (Java) 8.8++' submitDate='2007-01-05' publicationPermission='public'>\n");
143

    
144
                    XdmNode exceptionsDoc = builder.Build(
145
                            new FileStream(testSuiteDir + "/saxon/exceptions.xml", 
146
                                FileMode.Open, FileAccess.Read, FileShare.Read));
147

    
148
                  
149
                    IEnumerator exceptionTestCases = exceptionsDoc.EnumerateAxis(XdmAxis.Descendant, new QName("", "testcase"));
150
                    while (exceptionTestCases.MoveNext()) {
151
                        XdmNode testCase = (XdmNode)exceptionTestCases.Current;
152
                        String set = testCase.GetAttributeValue(setAtt);
153
                        String group = testCase.GetAttributeValue(groupAtt);
154
                        String comment = getChildElement(testCase, commentNT).StringValue;
155
                        exceptions[set + "#" + group] = comment;
156
                    }
157

    
158
                    IEnumerator testSets = catalog.EnumerateAxis(XdmAxis.Descendant, testSetRefNT);
159
                    while (testSets.MoveNext()) {
160

    
161
                        XdmNode testSetRef = (XdmNode)testSets.Current;
162
                        XdmNode testSetDoc = getLinkedDocument(testSetRef, processor, false);
163
                        XdmNode testSetElement = getChildElement(testSetDoc, testSetNT);
164

    
165
                        if (testSetElement == null) {
166
                            feedback.Message("test set doc has no TestSet child: " + testSetDoc.BaseUri, true);
167
                            continue;
168
                        }
169

    
170
                        String testSetName = testSetElement.GetAttributeValue(nameAtt);
171
                        if (testSetPattern != null && !testSetName.StartsWith(testSetPattern)) {
172
                            continue;
173
                        }
174
                        if (contributor != null && contributor != testSetElement.GetAttributeValue(contributorAtt)) {
175
                            continue;
176
                        }
177

    
178
                        bool needs11 = (testSetElement.GetAttributeValue(schemaVersion) == "1.1"); 
179

    
180
                        IEnumerator testGroups = testSetElement.EnumerateAxis(XdmAxis.Child, testGroupNT);
181
                        while (testGroups.MoveNext()) {
182
                            XdmNode testGroup = (XdmNode)testGroups.Current;
183
                            
184
                            String testGroupName = testGroup.GetAttributeValue(nameAtt);
185
                            String exception = (String)exceptions[testSetName + "#" + testGroupName];
186

    
187
                            if (testGroupPattern != null && !testGroupName.StartsWith(testGroupPattern)) {
188
                                continue;
189
                            }
190
                            Console.WriteLine("TEST SET " + testSetName + " GROUP " + testGroupName, false);
191
                            if (onwards) {
192
                                testGroupPattern = null;
193
                                testSetPattern = null;
194
                            }
195
                            Processor testConfig = new Processor(true);
196
                            if (needs11)
197
                            {
198
                                testConfig.SetProperty("http://saxon.sf.net/feature/xsd-version", "1.1");
199
                            }
200
                            SchemaManager schemaManager = testConfig.SchemaManager;
201
                            
202

    
203
                            //testConfig.setHostLanguage(Configuration.XML_SCHEMA);
204
                            testConfig.SetProperty("http://saxon.sf.net/feature/validation-warnings", "true");
205
                            IEnumerator schemaTests = testGroup.EnumerateAxis(XdmAxis.Child, schemaTestNT);
206
                            bool schemaQueried = false;
207
                            String bugzillaRef = null;
208

    
209
                            while (schemaTests.MoveNext()) {
210
                                XdmNode schemaTest = (XdmNode)schemaTests.Current;
211
                                if (schemaTest == null) {
212
                                    break;
213
                                }
214
                                bugzillaRef = null;
215
                                String testName = schemaTest.GetAttributeValue(nameAtt);
216
                                if (exception != null) {
217
                                    results.Write("<testResult set='" + testSetName +
218
                                            "' group='" + testGroupName +
219
                                            "' test='" + testName +
220
                                            "' validity='notKnown' saxon:outcome='notRun' saxon:comment='" + exception +
221
                                            "'/>\n");
222
                                    continue;
223
                                }
224
                                bool queried = false;
225
                                XdmNode statusElement = getChildElement(schemaTest, currentNT);
226
                                if (statusElement != null) {
227
                                    String status = statusElement.GetAttributeValue(statusAtt);
228
                                    queried = ("queried" == status);
229
                                    bugzillaRef = statusElement.GetAttributeValue(bugzillaAtt);
230
                                }
231
                                if (queried) {
232
                                    schemaQueried = true;
233
                                }
234
                                Console.WriteLine("TEST SCHEMA " + testName + (queried ? " (queried)" : ""));
235
                                bool success = true;
236
                                IEnumerator schemata = schemaTest.EnumerateAxis(XdmAxis.Child, schemaDocumentNT);
237
                                while (schemata.MoveNext()) {
238
                                    XdmNode schemaDocumentRef = (XdmNode)schemata.Current;
239
                                    if (schemaDocumentRef == null) {
240
                                        break;
241
                                    }
242
                                    Console.WriteLine("Loading schema at " + schemaDocumentRef.GetAttributeValue(xlinkHref));
243
                                    XdmNode schemaDoc = getLinkedDocument(schemaDocumentRef, testConfig, false);
244
                                    IEnumerator schemaDocKids = schemaDoc.EnumerateAxis(XdmAxis.Child);
245
                                    XdmNode schemaElement = null;
246
                                    while (schemaDocKids.MoveNext())
247
                                    {
248
                                        schemaElement = (XdmNode)schemaDocKids.Current;
249
                                        if (schemaElement.NodeKind == XmlNodeType.Element)
250
                                        {
251
                                            break;
252
                                        }
253
                                    }
254
                                    String targetNamespace = schemaElement.GetAttributeValue(targetNamespaceAtt);
255
                                    //if (targetNamespace != null && schemaManager. isSchemaAvailable(targetNamespace)) {
256
                                        // do nothing
257
                                        // TODO: this is the only way I can get MS additional test addB132 to work.
258
                                        // It's not ideal: addSchemaSource() ought to be a no-op if the schema components
259
                                        // are already loaded, but in fact recompiling the imported schema document on its
260
                                        // own is losing the substitution group membership that was defined in the
261
                                        // importing document.
262
                                    //} else {
263
                                    IList errorList = new ArrayList();
264
                                    schemaManager.ErrorList = errorList;
265
                                    try
266
                                    {
267
                                        schemaManager.Compile(schemaDoc);
268
                                    }
269
                                    catch (Exception e)
270
                                    {
271
                                        if (errorList.Count == 0)
272
                                        {
273
                                            feedback.Message("In " + testName + ", exception thrown but no errors in ErrorList\n", true);
274
                                            results.Write("<!--" + e.Message + "-->");
275
                                            success = false;
276
                                        }
277
                                    }
278
                                    for (int i = 0; i < errorList.Count; i++)
279
                                    {
280
                                        if (errorList[i] is StaticError)
281
                                        {
282
                                            StaticError err = (StaticError)errorList[i];
283
                                            if (!err.IsWarning)
284
                                            {
285
                                                success = false;
286
                                                break;
287
                                            }
288
                                        }
289
                                        else
290
                                        {
291
                                            feedback.Message("In " + testName + " wrong kind of error!" + errorList[i].GetType() + "\n", true);
292
                                        }
293
                                    }
294
                                }
295
                                XdmNode expected = getChildElement(schemaTest, expectedNT);
296
                                bool expectedSuccess = expected==null ||
297
                                        expected.GetAttributeValue(validityAtt) == "valid";
298
                                if (success == expectedSuccess)
299
                                {
300
                                    passed++;
301
                                }
302
                                else
303
                                {
304
                                    failed++;
305
                                }
306
                                feedback.Feedback(passed, failed, total);
307
                                results.Write("<testResult set='" + testSetName +
308
                                        "' group='" + testGroupName +
309
                                        "' test='" + testName +
310
                                        "' validity='" + (success ? "valid" : "invalid" ) +
311
                                        (queried ? "' saxon:queried='true' saxon:bugzilla='" + bugzillaRef : "") +
312
                                        "' saxon:outcome='" + (success==expectedSuccess ? "same" : "different") +
313
                                        "'/>\n");
314
                            }
315
                            IEnumerator instanceTests = testGroup.EnumerateAxis(XdmAxis.Child, instanceTestNT);
316
                            while (instanceTests.MoveNext()) {
317
                                XdmNode instanceTest = (XdmNode)instanceTests.Current;
318
                                String testName = instanceTest.GetAttributeValue(nameAtt);
319

    
320
                                if (exception != null) {
321
                                    results.Write("<testResult set='" + testSetName +
322
                                            "' group='" + testGroupName +
323
                                            "' test='" + testName +
324
                                            "' validity='notKnown' saxon:outcome='notRun' saxon:comment='" + exception +
325
                                            "'/>\n");
326
                                    continue;
327
                                }
328

    
329
                                bool queried = false;
330
                                XdmNode statusElement = getChildElement(instanceTest, currentNT);
331
                                if (statusElement != null) {
332
                                    String status = statusElement.GetAttributeValue(statusAtt);
333
                                    queried = ("queried" == status);
334
                                    String instanceBug = statusElement.GetAttributeValue(bugzillaAtt);
335
                                    if (instanceBug != null) {
336
                                        bugzillaRef = instanceBug;
337
                                    }
338
                                }
339
                                queried |= schemaQueried;
340

    
341
                                Console.WriteLine("TEST INSTANCE " + testName + (queried ? " (queried)" : ""));
342

    
343
                                XdmNode instanceDocument = getChildElement(instanceTest, instanceDocumentNT);
344

    
345
                                bool success = true;
346
                                try
347
                                {
348
                                    XdmNode instanceDoc = getLinkedDocument(instanceDocument, testConfig, true);
349
                                }
350
                                catch (Exception)
351
                                {
352
                                    success = false;
353
                                }
354
                                
355
                                XdmNode expected = getChildElement(instanceTest, expectedNT);
356
                                bool expectedSuccess = expected==null ||
357
                                        expected.GetAttributeValue(validityAtt) == "valid";
358
                                if (success == expectedSuccess)
359
                                {
360
                                    passed++;
361
                                }
362
                                else
363
                                {
364
                                    failed++;
365
                                }
366
                                feedback.Feedback(passed, failed, total);
367
                                results.Write("<testResult set='" + testSetName +
368
                                        "' group='" + testGroupName +
369
                                        "' test='" + testName +
370
                                        "' validity='" + (success ? "valid" : "invalid" ) +
371
                                        (queried ? "' saxon:queried='true' saxon:bugzilla='" + bugzillaRef : "") +
372
                                        "' saxon:outcome='" + (success==expectedSuccess ? "same" : "different") +
373
                                        "'/>\n");
374

    
375
                            }
376
                        }
377
                    }
378

    
379
                    results.Write("</testSuiteResults>");
380
                    results.Close();
381

    
382
                } catch (Exception e) {
383
                    feedback.Message("Test failed: " + e.Message, true);
384
                }
385
            }
386

    
387
            protected String getResultDirectoryName() {
388
                return "SaxonResults";
389
            }
390

    
391
            protected bool isExcluded(String testName) {
392
                return false;
393
            }
394

    
395

    
396
        }
397

    
398

    
399
    }
400

    
(9-9/14)