xmlconf.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Xml;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Text;
  6. namespace MonoTests.W3C_xmlconf {
  7. using NUnit.Core;
  8. using NUnit.Framework;
  9. abstract class BaseTests
  10. {
  11. TestSuite _suite;
  12. #region test list fields
  13. protected readonly ArrayList ignoredTests = new ArrayList ();
  14. protected readonly ArrayList knownFailures = new ArrayList ();
  15. protected readonly ArrayList fixmeList = new ArrayList ();
  16. protected readonly ArrayList netFailures = new ArrayList ();
  17. #endregion
  18. #region ReadStrings ()
  19. static void ReadStrings (ArrayList array, string filename) {
  20. if (!File.Exists (filename))
  21. return;
  22. using (StreamReader reader = new StreamReader (filename)) {
  23. foreach (string s_ in reader.ReadToEnd ().Split ("\n".ToCharArray ())) {
  24. string s = s_.Trim ();
  25. if (s.Length > 0)
  26. array.Add (s);
  27. }
  28. }
  29. }
  30. #endregion
  31. protected BaseTests (TestSuite suite)
  32. :this ()
  33. {
  34. _suite = suite;
  35. }
  36. private BaseTests ()
  37. {
  38. ReadStrings (ignoredTests, "ignored.lst");
  39. ReadStrings (knownFailures, "knownFailures.lst");
  40. ReadStrings (fixmeList, "fixme.lst");
  41. ReadStrings (netFailures, "net-failed.lst");
  42. }
  43. protected void BuildSuite ()
  44. {
  45. XmlDocument catalog = new XmlDocument ();
  46. catalog.Load ("xmlconf/xmlconf.xml");
  47. foreach (XmlElement test in catalog.SelectNodes ("//TEST")) {
  48. string testId = test.GetAttribute ("ID");
  49. ProcessTest (testId, test);
  50. }
  51. }
  52. protected virtual bool InverseResult {
  53. get {return false;}
  54. }
  55. protected virtual void ProcessTest (string testId, XmlElement test)
  56. {
  57. if (ignoredTests.Contains (testId))
  58. return;
  59. if (netFailures.Contains (testId))
  60. return;
  61. _suite.Add (new TestFromCatalog (testId, test, InverseResult));
  62. }
  63. }
  64. class AllTests: BaseTests
  65. {
  66. [Suite]
  67. static public TestSuite Suite{
  68. get {
  69. TestSuite suite = new TestSuite ("W3C_xmlconf.All");
  70. AllTests tests = new AllTests (suite);
  71. tests.BuildSuite ();
  72. return suite;
  73. }
  74. }
  75. AllTests (TestSuite suite)
  76. : base (suite)
  77. {
  78. }
  79. }
  80. class CleanTests: BaseTests {
  81. [Suite]
  82. static public TestSuite Suite{
  83. get {
  84. TestSuite suite = new TestSuite ("W3C_xmlconf.Clean");
  85. CleanTests tests = new CleanTests (suite);
  86. tests.BuildSuite ();
  87. return suite;
  88. }
  89. }
  90. CleanTests (TestSuite suite)
  91. : base (suite)
  92. {
  93. }
  94. protected override void ProcessTest(string testId, XmlElement test)
  95. {
  96. if (knownFailures.Contains (testId) || fixmeList.Contains (testId))
  97. return;
  98. base.ProcessTest (testId, test);
  99. }
  100. }
  101. class KnownFailureTests: BaseTests {
  102. [Suite]
  103. static public TestSuite Suite{
  104. get {
  105. TestSuite suite = new TestSuite ("W3C_xmlconf.KnownFailures");
  106. KnownFailureTests tests = new KnownFailureTests (suite);
  107. tests.BuildSuite ();
  108. return suite;
  109. }
  110. }
  111. KnownFailureTests (TestSuite suite)
  112. : base (suite)
  113. {
  114. }
  115. protected override bool InverseResult {
  116. get {return true;}
  117. }
  118. protected override void ProcessTest(string testId, XmlElement test)
  119. {
  120. if (!knownFailures.Contains (testId) && !fixmeList.Contains (testId))
  121. return;
  122. base.ProcessTest (testId, test);
  123. }
  124. }
  125. class TestFromCatalog: NUnit.Core.TestCase
  126. {
  127. XmlElement _test;
  128. string _stackTrace;
  129. bool _inverseResult;
  130. public TestFromCatalog (string testId, XmlElement test, bool inverseResult)
  131. :base (null, testId)
  132. {
  133. _test = test;
  134. _inverseResult = inverseResult;
  135. }
  136. bool TestNonValidating (string uri)
  137. {
  138. try {
  139. XmlTextReader trd = new XmlTextReader (uri);
  140. new XmlDocument ().Load (trd);
  141. return true;
  142. }
  143. catch (Exception e) {
  144. _stackTrace = e.StackTrace;
  145. return false;
  146. }
  147. }
  148. bool TestValidating (string uri)
  149. {
  150. try {
  151. XmlTextReader rd = new XmlTextReader (uri);
  152. XmlValidatingReader vrd = new XmlValidatingReader (rd);
  153. new XmlDocument ().Load (vrd);
  154. return true;
  155. }
  156. catch (Exception e) {
  157. _stackTrace = e.StackTrace; //rewrites existing, possibly, but it's ok
  158. return false;
  159. }
  160. }
  161. public override void Run (TestCaseResult res)
  162. {
  163. string type = _test.GetAttribute ("TYPE");
  164. if (type == "error")
  165. res.Success ();
  166. Uri baseUri = new Uri (_test.BaseURI);
  167. Uri testUri = new Uri (baseUri, _test.GetAttribute ("URI"));
  168. bool nonValidatingPassed = TestNonValidating (testUri.ToString ());
  169. bool validatingPassed = TestValidating (testUri.ToString ());
  170. bool isok = isOK (type, nonValidatingPassed, validatingPassed);
  171. string message="";
  172. if (_inverseResult) {
  173. isok = !isok;
  174. message = "The following test was FIXED:\n";
  175. }
  176. if (isok)
  177. res.Success ();
  178. else {
  179. message += "type:"+type;
  180. message += " non-validating passed:"+nonValidatingPassed.ToString();
  181. message += " validating passed:"+validatingPassed.ToString();
  182. message += " description:"+_test.InnerText;
  183. res.Failure (message, _stackTrace);
  184. }
  185. }
  186. static bool isOK (string type, bool nonValidatingPassed, bool validatingPassed)
  187. {
  188. switch (type) {
  189. case "valid":
  190. return nonValidatingPassed && validatingPassed;
  191. case "invalid":
  192. return nonValidatingPassed && !validatingPassed;
  193. case "not-wf":
  194. return !nonValidatingPassed && !validatingPassed;
  195. case "error":
  196. return true; //readers can optionally accept or reject errors
  197. default:
  198. throw new ArgumentException ("Wrong test type", "type");
  199. }
  200. }
  201. }
  202. }