4 using System.Xml.Schema;
\r
5 using System.Xml.Serialization;
\r
9 static readonly char SEP = Path.DirectorySeparatorChar;
\r
10 static ValidationEventHandler noValidateHandler =
\r
11 new ValidationEventHandler (NoValidate);
\r
22 string specificTarget;
\r
23 TextWriter ReportWriter = Console.Out;
\r
24 XmlTextWriter XmlReport;
\r
26 public static void Main (string [] args)
\r
28 new XsdTest ().Run (args);
\r
33 Console.WriteLine (@"
\r
34 USAGE: mono xsdtest.exe options target-pattern
\r
37 --stoponerr: stops at unexpected error.
\r
38 --noresolve: don't resolve external resources.
\r
39 --novalidate: don't validate and continue reading.
\r
41 --verbose: includes processing status.
\r
42 --xml: report as XML format.
\r
43 --details: report stack trace for errors.
\r
44 --reportsuccess: report successful test as well.
\r
45 --testall: process NISTTest/SunTest as well as MSXsdTest.
\r
47 target-pattern: Part of the target schema file name.
\r
50 " --v2 use XmlReader.Create() [2.0 only]"
\r
55 void Run (string [] args)
\r
57 foreach (string s in args) {
\r
63 version2 = true; break;
\r
65 verbose = true; break;
\r
67 stopOnError = true; break;
\r
69 noResolver = true; break;
\r
70 case "--novalidate":
\r
71 noValidate = true; break;
\r
73 reportAsXml = true; break;
\r
75 reportDetails = true; break;
\r
76 case "--reportsuccess":
\r
77 reportSuccess = true; break;
\r
79 testAll = true; break;
\r
81 if (s.StartsWith ("--report:"))
\r
82 ReportWriter = new StreamWriter (
\r
89 RunTest ("msxsdtest");
\r
91 RunTest ("suntest");
\r
92 RunTest ("nisttest");
\r
94 ReportWriter.Close ();
\r
97 static void NoValidate (object o, ValidationEventArgs e)
\r
101 void RunTest (string subdir)
\r
103 string basePath = @"xsd-test-suite" + SEP;
\r
104 XmlDocument doc = new XmlDocument ();
\r
106 doc.XmlResolver = null;
\r
107 doc.Load (basePath + subdir + SEP + "tests-all.xml");
\r
110 XmlReport = new XmlTextWriter (ReportWriter);
\r
111 XmlReport.Formatting = Formatting.Indented;
\r
112 XmlReport.WriteStartElement ("test-results");
\r
115 Console.WriteLine ("Started: " + DateTime.Now);
\r
117 foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {
\r
119 string schemaFile = test.SelectSingleNode ("@schema").InnerText;
\r
120 if (specificTarget != null &&
\r
121 schemaFile.IndexOf (specificTarget) < 0)
\r
123 if (schemaFile.Length > 2)
\r
124 schemaFile = schemaFile.Substring (2);
\r
126 Report (schemaFile, true, "compiling", "");
\r
127 bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";
\r
128 XmlSchema schema = null;
\r
129 XmlTextReader sxr = null;
\r
131 sxr = new XmlTextReader (basePath + schemaFile);
\r
133 sxr.XmlResolver = null;
\r
134 schema = XmlSchema.Read (sxr, null);
\r
135 schema.Compile (noValidate ? noValidateHandler : null, noResolver ? null : new XmlUrlResolver ());
\r
136 if (!isValidSchema && !noValidate) {
\r
137 Report (schemaFile, true, "should fail", "");
\r
141 Report (schemaFile, true, "OK", "");
\r
142 } catch (XmlSchemaException ex) {
\r
144 Report (schemaFile, true, "should succeed",
\r
146 ex.ToString () : ex.Message);
\r
147 else if (reportSuccess)
\r
148 Report (schemaFile, true, "OK", "");
\r
150 } catch (Exception ex) {
\r
153 Report (schemaFile, true, "unexpected",
\r
155 ex.ToString () : ex.Message);
\r
163 string instanceFile = test.SelectSingleNode ("@instance").InnerText;
\r
164 if (instanceFile.Length == 0)
\r
166 else if (instanceFile.StartsWith ("./"))
\r
167 instanceFile = instanceFile.Substring (2);
\r
169 Report (instanceFile, false, "reading ", "");
\r
170 bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";
\r
171 XmlReader xvr = null;
\r
173 XmlTextReader ixtr = new XmlTextReader (
\r
174 Path.Combine (basePath, instanceFile));
\r
177 XmlReaderSettings settings =
\r
178 new XmlReaderSettings ();
\r
179 settings.ValidationType = ValidationType.Schema;
\r
181 settings.ValidationEventHandler +=
\r
184 settings.Schemas.XmlResolver = null;
\r
185 settings.Schemas.Add (schema);
\r
187 settings.XmlResolver = null;
\r
188 xvr = XmlReader.Create (ixtr, settings);
\r
190 XmlValidatingReader vr = new XmlValidatingReader (ixtr);
\r
192 vr.XmlResolver = null;
\r
194 vr.ValidationEventHandler += noValidateHandler;
\r
195 vr.Schemas.Add (schema);
\r
200 if (!isValidInstance && !noValidate)
\r
201 Report (instanceFile, false, "should fail", "");
\r
202 else if (reportSuccess)
\r
203 Report (instanceFile, false, "OK", "");
\r
204 } catch (XmlSchemaException ex) {
\r
205 if (isValidInstance)
\r
206 Report (instanceFile, false, "should succeed",
\r
208 ex.ToString () : ex.Message);
\r
209 else if (reportSuccess)
\r
210 Report (instanceFile, false, "OK", "");
\r
211 } catch (Exception ex) {
\r
214 Report (instanceFile, false, "unexpected",
\r
216 ex.ToString () : ex.Message);
\r
224 XmlReport.WriteEndElement ();
\r
225 XmlReport.Flush ();
\r
228 Console.WriteLine ("Finished: " + DateTime.Now);
\r
231 void Report (string id, bool compile, string category, string s)
\r
233 string phase = compile ? "compile" : "read";
\r
235 XmlReport.WriteStartElement ("testresult");
\r
236 XmlReport.WriteAttributeString ("id", id);
\r
237 XmlReport.WriteAttributeString ("phase", phase);
\r
238 XmlReport.WriteAttributeString ("category", category);
\r
239 XmlReport.WriteString (s);
\r
240 XmlReport.WriteEndElement ();
\r
243 ReportWriter.WriteLine ("{0}/{1} : {2} {3}",
\r
244 phase, category, id, s);
\r