xsdtest.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Schema;
  5. using System.Xml.Serialization;
  6. public class Test
  7. {
  8. static char SEP = Path.DirectorySeparatorChar;
  9. public static void Main ()
  10. {
  11. RunTest ("msxsdtest");
  12. RunTest ("nisttest");
  13. RunTest ("suntest");
  14. }
  15. static void RunTest (string subdir)
  16. {
  17. Console.WriteLine ("Started: " + DateTime.Now);
  18. string basePath = @"Xsd-Test-Suite" + SEP;
  19. XmlDocument doc = new XmlDocument ();
  20. doc.Load (basePath + subdir + SEP + "tests-all.xml");
  21. foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {
  22. // Test schema
  23. string schemaFile = test.SelectSingleNode ("@schema").InnerText;
  24. if (schemaFile.Length > 2)
  25. schemaFile = schemaFile.Substring (2);
  26. bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";
  27. XmlSchema schema = null;
  28. XmlTextReader sxr = null;
  29. try {
  30. sxr = new XmlTextReader (basePath + schemaFile);
  31. schema = XmlSchema.Read (sxr, null);
  32. sxr.Close ();
  33. schema.Compile (null);
  34. if (!isValidSchema) {
  35. Console.WriteLine ("Incorrectly Valid schema : " + schemaFile);
  36. continue;
  37. }
  38. } catch (XmlSchemaException ex) {
  39. if (isValidSchema) {
  40. Console.WriteLine ("Incorrectly Invalid schema : " + schemaFile + " " + ex.Message);
  41. continue;
  42. }
  43. } catch (Exception ex) {
  44. Console.WriteLine ("Unexpected Exception on schema: " + schemaFile + " " + ex.Message);
  45. continue;
  46. } finally {
  47. sxr.Close ();
  48. }
  49. // Test instances
  50. string instanceFile = test.SelectSingleNode ("@instance").InnerText;
  51. if (instanceFile.Length == 0)
  52. continue;
  53. else if (instanceFile.StartsWith ("./"))
  54. instanceFile = instanceFile.Substring (2);
  55. bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";
  56. XmlValidatingReader xvr = null;
  57. try {
  58. xvr = new XmlValidatingReader (new XmlTextReader (basePath + "\\" + instanceFile));
  59. xvr.Schemas.Add (schema);
  60. while (!xvr.EOF)
  61. xvr.Read ();
  62. if (!isValidInstance)
  63. Console.WriteLine ("Incorrectly Valid instance: " + schemaFile);
  64. } catch (XmlSchemaException ex) {
  65. if (isValidInstance)
  66. Console.WriteLine ("Incorrectly Invalid instance: " + schemaFile + " " + ex.Message);
  67. } catch (Exception ex) {
  68. Console.WriteLine ("Unexpected Exception on instance: " + schemaFile + " " + ex.Message);
  69. } finally {
  70. xvr.Close ();
  71. }
  72. }
  73. Console.WriteLine ("Finished: " + DateTime.Now);
  74. }
  75. }