xmlconf.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. public 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. public 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. public class CleanTests : BaseTests
  82. {
  83. [Suite]
  84. static public TestSuite Suite{
  85. get {
  86. TestSuite suite = new TestSuite ("W3C_xmlconf.Clean");
  87. CleanTests tests = new CleanTests (suite);
  88. tests.BuildSuite ();
  89. return suite;
  90. }
  91. }
  92. CleanTests (TestSuite suite)
  93. : base (suite)
  94. {
  95. }
  96. protected override void ProcessTest(string testId, XmlElement test)
  97. {
  98. if (knownFailures.Contains (testId) || fixmeList.Contains (testId))
  99. return;
  100. base.ProcessTest (testId, test);
  101. }
  102. }
  103. public class KnownFailureTests : BaseTests
  104. {
  105. [Suite]
  106. static public TestSuite Suite{
  107. get {
  108. TestSuite suite = new TestSuite ("W3C_xmlconf.KnownFailures");
  109. KnownFailureTests tests = new KnownFailureTests (suite);
  110. tests.BuildSuite ();
  111. return suite;
  112. }
  113. }
  114. KnownFailureTests (TestSuite suite)
  115. : base (suite)
  116. {
  117. }
  118. protected override bool InverseResult {
  119. get {return true;}
  120. }
  121. protected override void ProcessTest(string testId, XmlElement test)
  122. {
  123. if (!knownFailures.Contains (testId) && !fixmeList.Contains (testId))
  124. return;
  125. base.ProcessTest (testId, test);
  126. }
  127. }
  128. public class TestFromCatalog : NUnit.Core.TestCase
  129. {
  130. XmlElement _test;
  131. string _errorString;
  132. bool _inverseResult;
  133. public TestFromCatalog (string testId, XmlElement test, bool inverseResult)
  134. :base (null, testId)
  135. {
  136. _test = test;
  137. _inverseResult = inverseResult;
  138. }
  139. bool TestNonValidating (string uri)
  140. {
  141. XmlTextReader trd = null;
  142. try {
  143. trd = new XmlTextReader (uri);
  144. new XmlDocument ().Load (trd);
  145. return true;
  146. }
  147. catch (Exception e) {
  148. _errorString = e.ToString ();
  149. return false;
  150. }
  151. finally {
  152. if (trd != null)
  153. trd.Close();
  154. }
  155. }
  156. bool TestValidating (string uri)
  157. {
  158. XmlTextReader rd = null;
  159. try {
  160. rd = new XmlTextReader (uri);
  161. XmlValidatingReader vrd = new XmlValidatingReader (rd);
  162. new XmlDocument ().Load (vrd);
  163. return true;
  164. }
  165. catch (Exception e) {
  166. _errorString = e.ToString (); //rewrites existing, possibly, but it's ok
  167. return false;
  168. }
  169. finally {
  170. if (rd != null)
  171. rd.Close();
  172. }
  173. }
  174. public override void Run (TestCaseResult res)
  175. {
  176. string type = _test.GetAttribute ("TYPE");
  177. if (type == "error")
  178. res.Success ();
  179. Uri baseUri = new Uri (_test.BaseURI);
  180. Uri testUri = new Uri (baseUri, _test.GetAttribute ("URI"));
  181. bool nonValidatingPassed = TestNonValidating (testUri.ToString ());
  182. bool validatingPassed = TestValidating (testUri.ToString ());
  183. bool isok = isOK (type, nonValidatingPassed, validatingPassed);
  184. string message="";
  185. if (_inverseResult) {
  186. isok = !isok;
  187. message = "The following test was FIXED:\n";
  188. }
  189. if (isok)
  190. res.Success ();
  191. else {
  192. message += "type:"+type;
  193. message += " non-validating passed:"+nonValidatingPassed.ToString();
  194. message += " validating passed:"+validatingPassed.ToString();
  195. message += " description:"+_test.InnerText;
  196. res.Failure (message, _errorString);
  197. }
  198. }
  199. static bool isOK (string type, bool nonValidatingPassed, bool validatingPassed)
  200. {
  201. switch (type) {
  202. case "valid":
  203. return nonValidatingPassed && validatingPassed;
  204. case "invalid":
  205. return nonValidatingPassed && !validatingPassed;
  206. case "not-wf":
  207. return !nonValidatingPassed && !validatingPassed;
  208. case "error":
  209. return true; //readers can optionally accept or reject errors
  210. default:
  211. throw new ArgumentException ("Wrong test type", "type");
  212. }
  213. }
  214. }
  215. }