xsdtest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if (noResolver)
  97. doc.XmlResolver = null;
  98. doc.Load (basePath + subdir + SEP + "tests-all.xml");
  99. if (reportAsXml) {
  100. XmlReport = new XmlTextWriter (ReportWriter);
  101. XmlReport.Formatting = Formatting.Indented;
  102. XmlReport.WriteStartElement ("test-results");
  103. }
  104. Console.WriteLine ("Started: " + DateTime.Now);
  105. foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {
  106. // Test schema
  107. string schemaFile = test.SelectSingleNode ("@schema").InnerText;
  108. if (specificTarget != null &&
  109. schemaFile.IndexOf (specificTarget) < 0)
  110. continue;
  111. if (schemaFile.Length > 2)
  112. schemaFile = schemaFile.Substring (2);
  113. if (verbose)
  114. Report (schemaFile, true, "compiling", "");
  115. bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";
  116. XmlSchema schema = null;
  117. XmlTextReader sxr = null;
  118. try {
  119. sxr = new XmlTextReader (basePath + schemaFile);
  120. if (noResolver)
  121. sxr.XmlResolver = null;
  122. schema = XmlSchema.Read (sxr, null);
  123. schema.Compile (noValidate ? noValidateHandler : null, noResolver ? null : new XmlUrlResolver ());
  124. if (!isValidSchema && !noValidate) {
  125. Report (schemaFile, true, "should fail", "");
  126. continue;
  127. }
  128. if (reportSuccess)
  129. Report (schemaFile, true, "OK", "");
  130. } catch (XmlSchemaException ex) {
  131. if (isValidSchema)
  132. Report (schemaFile, true, "should succeed",
  133. reportDetails ?
  134. ex.ToString () : ex.Message);
  135. else if (reportSuccess)
  136. Report (schemaFile, true, "OK", "");
  137. continue;
  138. } catch (Exception ex) {
  139. if (stopOnError)
  140. throw;
  141. Report (schemaFile, true, "unexpected",
  142. reportDetails ?
  143. ex.ToString () : ex.Message);
  144. continue;
  145. } finally {
  146. if (sxr != null)
  147. sxr.Close ();
  148. }
  149. // Test instances
  150. string instanceFile = test.SelectSingleNode ("@instance").InnerText;
  151. if (instanceFile.Length == 0)
  152. continue;
  153. else if (instanceFile.StartsWith ("./"))
  154. instanceFile = instanceFile.Substring (2);
  155. if (verbose)
  156. Report (instanceFile, false, "reading ", "");
  157. bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";
  158. XmlReader xvr = null;
  159. try {
  160. XmlTextReader ixtr = new XmlTextReader (
  161. Path.Combine (basePath, instanceFile));
  162. xvr = ixtr;
  163. #if NET_2_0
  164. if (version2) {
  165. XmlReaderSettings settings =
  166. new XmlReaderSettings ();
  167. settings.ValidationType = ValidationType.Schema;
  168. if (noValidate)
  169. settings.ValidationEventHandler +=
  170. noValidateHandler;
  171. if (noResolver)
  172. settings.Schemas.XmlResolver = null;
  173. settings.Schemas.Add (schema);
  174. if (noResolver)
  175. settings.XmlResolver = null;
  176. xvr = XmlReader.Create (ixtr, settings);
  177. } else {
  178. #endif
  179. XmlValidatingReader vr = new XmlValidatingReader (ixtr);
  180. if (noResolver)
  181. vr.XmlResolver = null;
  182. if (noValidate)
  183. vr.ValidationEventHandler += noValidateHandler;
  184. vr.Schemas.Add (schema);
  185. xvr = vr;
  186. #if NET_2_0
  187. }
  188. #endif
  189. while (!xvr.EOF)
  190. xvr.Read ();
  191. if (!isValidInstance && !noValidate)
  192. Report (instanceFile, false, "should fail", "");
  193. else if (reportSuccess)
  194. Report (instanceFile, false, "OK", "");
  195. } catch (XmlSchemaException ex) {
  196. if (isValidInstance)
  197. Report (instanceFile, false, "should succeed",
  198. reportDetails ?
  199. ex.ToString () : ex.Message);
  200. else if (reportSuccess)
  201. Report (instanceFile, false, "OK", "");
  202. } catch (Exception ex) {
  203. if (stopOnError)
  204. throw;
  205. Report (instanceFile, false, "unexpected",
  206. reportDetails ?
  207. ex.ToString () : ex.Message);
  208. } finally {
  209. if (xvr != null)
  210. xvr.Close ();
  211. }
  212. }
  213. if (reportAsXml) {
  214. XmlReport.WriteEndElement ();
  215. XmlReport.Flush ();
  216. }
  217. Console.WriteLine ("Finished: " + DateTime.Now);
  218. }
  219. void Report (string id, bool compile, string category, string s)
  220. {
  221. string phase = compile ? "compile" : "read";
  222. if (reportAsXml) {
  223. XmlReport.WriteStartElement ("testresult");
  224. XmlReport.WriteAttributeString ("id", id);
  225. XmlReport.WriteAttributeString ("phase", phase);
  226. XmlReport.WriteAttributeString ("category", category);
  227. XmlReport.WriteString (s);
  228. XmlReport.WriteEndElement ();
  229. }
  230. else
  231. ReportWriter.WriteLine ("{0}/{1} : {2} {3}",
  232. phase, category, id, s);
  233. }
  234. }