xsdtest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Schema;
  5. using System.Xml.Serialization;
  6. public class XsdTest
  7. {
  8. static readonly char SEP = Path.DirectorySeparatorChar;
  9. static ValidationEventHandler noValidateHandler =
  10. new ValidationEventHandler (NoValidate);
  11. bool version2;
  12. bool verbose;
  13. bool stopOnError;
  14. bool noResolver;
  15. bool reportAsXml;
  16. bool reportDetails;
  17. bool reportSuccess;
  18. bool testAll;
  19. bool noValidate;
  20. string specificTarget;
  21. TextWriter ReportWriter = Console.Out;
  22. XmlTextWriter XmlReport;
  23. public static void Main (string [] args)
  24. {
  25. new XsdTest ().Run (args);
  26. }
  27. void Usage ()
  28. {
  29. Console.WriteLine (@"
  30. USAGE: mono xsdtest.exe options target-pattern
  31. options:
  32. --stoponerr: stops at unexpected error.
  33. --noresolve: don't resolve external resources.
  34. --novalidate: don't validate and continue reading.
  35. {0}
  36. --verbose: includes processing status.
  37. --xml: report as XML format.
  38. --details: report stack trace for errors.
  39. --reportsuccess: report successful test as well.
  40. --testall: process NISTTest/SunTest as well as MSXsdTest.
  41. target-pattern: Part of the target schema file name.
  42. (No Regex support.)
  43. ",
  44. " --v2 use XmlReader.Create() [2.0 only]"
  45. );
  46. return;
  47. }
  48. void Run (string [] args)
  49. {
  50. foreach (string s in args) {
  51. switch (s) {
  52. case "--help":
  53. Usage ();
  54. return;
  55. case "--v2":
  56. version2 = true; break;
  57. case "--verbose":
  58. verbose = true; break;
  59. case "--stoponerr":
  60. stopOnError = true; break;
  61. case "--noresolve":
  62. noResolver = true; break;
  63. case "--novalidate":
  64. noValidate = true; break;
  65. case "--xml":
  66. reportAsXml = true; break;
  67. case "--details":
  68. reportDetails = true; break;
  69. case "--reportsuccess":
  70. reportSuccess = true; break;
  71. case "--testall":
  72. testAll = true; break;
  73. default:
  74. if (s.StartsWith ("--report:"))
  75. ReportWriter = new StreamWriter (
  76. s.Substring (9));
  77. else
  78. specificTarget = s;
  79. break;
  80. }
  81. }
  82. RunTest ("msxsdtest");
  83. if (testAll) {
  84. RunTest ("suntest");
  85. RunTest ("nisttest");
  86. }
  87. ReportWriter.Close ();
  88. }
  89. static void NoValidate (object o, ValidationEventArgs e)
  90. {
  91. }
  92. void RunTest (string subdir)
  93. {
  94. string basePath = @"Xsd-Test-Suite" + SEP;
  95. XmlDocument doc = new XmlDocument ();
  96. doc.Load (basePath + subdir + SEP + "tests-all.xml");
  97. if (reportAsXml) {
  98. XmlReport = new XmlTextWriter (ReportWriter);
  99. XmlReport.Formatting = Formatting.Indented;
  100. XmlReport.WriteStartElement ("test-results");
  101. }
  102. Console.WriteLine ("Started: " + DateTime.Now);
  103. foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {
  104. // Test schema
  105. string schemaFile = test.SelectSingleNode ("@schema").InnerText;
  106. if (specificTarget != null &&
  107. schemaFile.IndexOf (specificTarget) < 0)
  108. continue;
  109. if (schemaFile.Length > 2)
  110. schemaFile = schemaFile.Substring (2);
  111. if (verbose)
  112. Report (schemaFile, false, "compiling", "");
  113. bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";
  114. XmlSchema schema = null;
  115. XmlTextReader sxr = null;
  116. try {
  117. sxr = new XmlTextReader (basePath + schemaFile);
  118. if (noResolver)
  119. sxr.XmlResolver = null;
  120. schema = XmlSchema.Read (sxr, null);
  121. schema.Compile (noValidate ? noValidateHandler : null);
  122. if (!isValidSchema && !noValidate) {
  123. Report (schemaFile, true, "should fail", "");
  124. continue;
  125. }
  126. if (reportSuccess)
  127. Report (schemaFile, true, "OK", "");
  128. } catch (XmlSchemaException ex) {
  129. if (isValidSchema) {
  130. Report (schemaFile, true, "should succeed",
  131. reportDetails ?
  132. ex.ToString () : ex.Message);
  133. continue;
  134. }
  135. } catch (Exception ex) {
  136. if (stopOnError)
  137. throw;
  138. Report (schemaFile, true, "unexpected",
  139. reportDetails ?
  140. ex.ToString () : ex.Message);
  141. continue;
  142. } finally {
  143. if (sxr != null)
  144. sxr.Close ();
  145. }
  146. // Test instances
  147. string instanceFile = test.SelectSingleNode ("@instance").InnerText;
  148. if (instanceFile.Length == 0)
  149. continue;
  150. else if (instanceFile.StartsWith ("./"))
  151. instanceFile = instanceFile.Substring (2);
  152. if (verbose)
  153. Report (instanceFile, false, "reading ", "");
  154. bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";
  155. XmlReader xvr = null;
  156. try {
  157. XmlTextReader ixtr = new XmlTextReader (
  158. Path.Combine (basePath, instanceFile));
  159. #if NET_2_0
  160. if (version2) {
  161. XmlReaderSettings settings =
  162. new XmlReaderSettings ();
  163. settings.ValidationType = ValidationType.Schema;
  164. settings.ValidationFlags =
  165. XmlSchemaValidationFlags.IgnoreValidationWarnings;
  166. if (noValidate)
  167. settings.ValidationEventHandler +=
  168. noValidateHandler;
  169. if (noResolver)
  170. settings.Schemas.XmlResolver = null;
  171. settings.Schemas.Add (schema);
  172. if (noResolver)
  173. xvr = XmlReader.Create (ixtr, null, settings);
  174. else
  175. xvr = XmlReader.Create (ixtr, settings);
  176. } else {
  177. #endif
  178. XmlValidatingReader vr = new XmlValidatingReader (ixtr);
  179. if (noResolver)
  180. vr.XmlResolver = null;
  181. if (noValidate)
  182. vr.ValidationEventHandler += noValidateHandler;
  183. vr.Schemas.Add (schema);
  184. xvr = vr;
  185. #if NET_2_0
  186. }
  187. #endif
  188. while (!xvr.EOF)
  189. xvr.Read ();
  190. if (!isValidInstance && !noValidate)
  191. Report (instanceFile, false, "should fail", "");
  192. if (reportSuccess)
  193. Report (instanceFile, false, "OK", "");
  194. } catch (XmlSchemaException ex) {
  195. if (isValidInstance)
  196. Report (instanceFile, false, "should succeed",
  197. reportDetails ?
  198. ex.ToString () : ex.Message);
  199. } catch (Exception ex) {
  200. if (stopOnError)
  201. throw;
  202. Report (instanceFile, false, "unexpected",
  203. reportDetails ?
  204. ex.ToString () : ex.Message);
  205. } finally {
  206. if (xvr != null)
  207. xvr.Close ();
  208. }
  209. }
  210. if (reportAsXml) {
  211. XmlReport.WriteEndElement ();
  212. XmlReport.Flush ();
  213. }
  214. Console.WriteLine ("Finished: " + DateTime.Now);
  215. }
  216. void Report (string id, bool compile, string category, string s)
  217. {
  218. string phase = compile ? "compile" : "read";
  219. if (reportAsXml) {
  220. XmlReport.WriteStartElement ("testresult");
  221. XmlReport.WriteAttributeString ("id", id);
  222. XmlReport.WriteAttributeString ("phase", phase);
  223. XmlReport.WriteAttributeString ("category", category);
  224. XmlReport.WriteString (s);
  225. XmlReport.WriteEndElement ();
  226. }
  227. else
  228. ReportWriter.WriteLine ("{0}/{1} : {2} {3}",
  229. phase, category, id, s);
  230. }
  231. }