xmlconf.cs 5.5 KB

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