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
105 doc.Load (basePath + subdir + SEP + "tests-all.xml");
\r
108 XmlReport = new XmlTextWriter (ReportWriter);
\r
109 XmlReport.Formatting = Formatting.Indented;
\r
110 XmlReport.WriteStartElement ("test-results");
\r
113 Console.WriteLine ("Started: " + DateTime.Now);
\r
115 foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {
\r
117 string schemaFile = test.SelectSingleNode ("@schema").InnerText;
\r
118 if (specificTarget != null &&
\r
119 schemaFile.IndexOf (specificTarget) < 0)
\r
121 if (schemaFile.Length > 2)
\r
122 schemaFile = schemaFile.Substring (2);
\r
124 Report (schemaFile, false, "compiling", "");
\r
125 bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";
\r
126 XmlSchema schema = null;
\r
127 XmlTextReader sxr = null;
\r
129 sxr = new XmlTextReader (basePath + schemaFile);
\r
131 sxr.XmlResolver = null;
\r
132 schema = XmlSchema.Read (sxr, null);
\r
133 schema.Compile (noValidate ? noValidateHandler : null);
\r
134 if (!isValidSchema && !noValidate) {
\r
135 Report (schemaFile, true, "should fail", "");
\r
139 Report (schemaFile, true, "OK", "");
\r
140 } catch (XmlSchemaException ex) {
\r
141 if (isValidSchema) {
\r
142 Report (schemaFile, true, "should succeed",
\r
144 ex.ToString () : ex.Message);
\r
147 } catch (Exception ex) {
\r
150 Report (schemaFile, true, "unexpected",
\r
152 ex.ToString () : ex.Message);
\r
160 string instanceFile = test.SelectSingleNode ("@instance").InnerText;
\r
161 if (instanceFile.Length == 0)
\r
163 else if (instanceFile.StartsWith ("./"))
\r
164 instanceFile = instanceFile.Substring (2);
\r
166 Report (instanceFile, false, "reading ", "");
\r
167 bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";
\r
168 XmlReader xvr = null;
\r
170 XmlTextReader ixtr = new XmlTextReader (
\r
171 Path.Combine (basePath, instanceFile));
\r
174 XmlReaderSettings settings =
\r
175 new XmlReaderSettings ();
\r
176 settings.ValidationType = ValidationType.Schema;
\r
177 settings.ValidationFlags =
\r
178 XmlSchemaValidationFlags.IgnoreValidationWarnings;
\r
180 settings.ValidationEventHandler +=
\r
183 settings.Schemas.XmlResolver = null;
\r
184 settings.Schemas.Add (schema);
\r
186 xvr = XmlReader.Create (ixtr, null, settings);
\r
188 xvr = XmlReader.Create (ixtr, settings);
\r
191 XmlValidatingReader vr = new XmlValidatingReader (ixtr);
\r
193 vr.XmlResolver = null;
\r
195 vr.ValidationEventHandler += noValidateHandler;
\r
196 vr.Schemas.Add (schema);
\r
203 if (!isValidInstance && !noValidate)
\r
204 Report (instanceFile, false, "should fail", "");
\r
206 Report (instanceFile, false, "OK", "");
\r
207 } catch (XmlSchemaException ex) {
\r
208 if (isValidInstance)
\r
209 Report (instanceFile, false, "should succeed",
\r
211 ex.ToString () : ex.Message);
\r
212 } catch (Exception ex) {
\r
215 Report (instanceFile, false, "unexpected",
\r
217 ex.ToString () : ex.Message);
\r
225 XmlReport.WriteEndElement ();
\r
226 XmlReport.Flush ();
\r
229 Console.WriteLine ("Finished: " + DateTime.Now);
\r
232 void Report (string id, bool compile, string category, string s)
\r
234 string phase = compile ? "compile" : "read";
\r
236 XmlReport.WriteStartElement ("testresult");
\r
237 XmlReport.WriteAttributeString ("id", id);
\r
238 XmlReport.WriteAttributeString ("phase", phase);
\r
239 XmlReport.WriteAttributeString ("category", category);
\r
240 XmlReport.WriteString (s);
\r
241 XmlReport.WriteEndElement ();
\r
244 ReportWriter.WriteLine ("{0}/{1} : {2} {3}",
\r
245 phase, category, id, s);
\r