XsltTestUtils.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System.Xml;
  2. using System.Xml.Xsl;
  3. using System.Xml.XPath;
  4. using System.Collections;
  5. using System.IO;
  6. namespace MonoTests.oasis_xslt {
  7. class EnvOptions {
  8. static readonly bool useDomStyle;
  9. static readonly bool useDomInstance;
  10. static readonly string outputDir;
  11. static readonly bool whitespaceStyle;
  12. static readonly bool whitespaceInstance;
  13. static readonly bool inverseResults;
  14. public static bool UseDomStyle {
  15. get {return useDomStyle;}
  16. }
  17. public static bool UseDomInstance {
  18. get {return useDomInstance;}
  19. }
  20. public static string OutputDir {
  21. get {return outputDir;}
  22. }
  23. public static bool WhitespaceStyle {
  24. get {return whitespaceStyle;}
  25. }
  26. public static bool WhitespaceInstance {
  27. get {return whitespaceInstance;}
  28. }
  29. public static bool InverseResults {
  30. get {return inverseResults;}
  31. }
  32. static EnvOptions () {
  33. IDictionary env = System.Environment.GetEnvironmentVariables();
  34. if (env.Contains ("XSLTTEST_DOM")) {
  35. useDomStyle = true;
  36. useDomInstance = true;
  37. }
  38. if (env.Contains ("XSLTTEST_DOMXSL"))
  39. useDomStyle = true;
  40. if (env.Contains ("XSLTTEST_DOMINSTANCE"))
  41. useDomInstance = true;
  42. if (env.Contains ("XSLTTEST_WS")) {
  43. whitespaceStyle = true;
  44. whitespaceInstance = true;
  45. }
  46. if (env.Contains ("XSLTTEST_WSXSL"))
  47. whitespaceStyle = true;
  48. if (env.Contains ("XSLTTEST_WSSRC"))
  49. whitespaceInstance = true;
  50. if (env.Contains ("XSLTTEST_INVERSE_RESULTS"))
  51. inverseResults = true;
  52. if (useDomStyle || useDomInstance)
  53. outputDir = "domresults";
  54. else
  55. outputDir = "results";
  56. }
  57. }
  58. class Helpers
  59. {
  60. public static void ReadStrings (ArrayList array, string filename)
  61. {
  62. if (!File.Exists (filename))
  63. return;
  64. using (StreamReader reader = new StreamReader (filename)) {
  65. foreach (string s_ in reader.ReadToEnd ().Split ("\n".ToCharArray ())) {
  66. string s = s_.Trim ();
  67. if (s.Length > 0)
  68. array.Add (s);
  69. }
  70. }
  71. }
  72. }
  73. class CatalogTestCase
  74. {
  75. string _stylesheet;
  76. string _srcxml;
  77. string _outfile;
  78. public enum CompareType {
  79. Text,
  80. HTML,
  81. XML
  82. }
  83. CompareType _compare;
  84. XmlElement _testCase;
  85. string _outputDir;
  86. public CatalogTestCase (string outputDir, XmlElement testCase)
  87. {
  88. _testCase = testCase;
  89. _outputDir = outputDir;
  90. }
  91. public bool Process ()
  92. {
  93. string relPath = GetRelPath ();
  94. string path = Path.Combine (Path.Combine ("testsuite", "TESTS"), relPath);
  95. string outputPath = Path.Combine (_outputDir, relPath);
  96. if (!Directory.Exists (outputPath))
  97. Directory.CreateDirectory (outputPath);
  98. //FIXME: this ignores negative tests. Read README if you want to fix it
  99. XmlNode scenario = _testCase.SelectSingleNode ("scenario[@operation='standard']");
  100. if (scenario == null)
  101. return false;
  102. ProcessScenario (path, outputPath, scenario);
  103. return true;
  104. }
  105. string GetRelPath ()
  106. {
  107. string filePath = _testCase.SelectSingleNode ("file-path").InnerText;
  108. string submitter = _testCase.SelectSingleNode ("./parent::test-catalog/@submitter").InnerText;
  109. if (submitter == "Lotus")
  110. return Path.Combine ("Xalan_Conformance_Tests", filePath);
  111. else if (submitter == "Microsoft")
  112. return Path.Combine ("MSFT_Conformance_Tests", filePath);
  113. else
  114. throw new System.Exception ("unknown submitter in the catalog");
  115. }
  116. void ProcessScenario (string path, string outputPath, XmlNode scenario)
  117. {
  118. string stylesheetBase = scenario.SelectSingleNode ("input-file[@role='principal-stylesheet']").InnerText;
  119. _stylesheet = Path.Combine (path, stylesheetBase);
  120. if (!File.Exists (_stylesheet)) {
  121. using (StreamWriter wr = new StreamWriter ("missing.lst", true))
  122. wr.WriteLine (_stylesheet);
  123. }
  124. _srcxml = Path.Combine (path, scenario.SelectSingleNode ("input-file[@role='principal-data']").InnerText);
  125. XmlNode outputNode = scenario.SelectSingleNode ("output-file[@role='principal']");
  126. if (outputNode != null) {
  127. _outfile = Path.Combine (outputPath, outputNode.InnerText);
  128. switch (outputNode.Attributes ["compare"].Value) {
  129. case "XML":
  130. _compare = CompareType.XML;
  131. break;
  132. case "HTML":
  133. _compare = CompareType.HTML;
  134. break;
  135. default:
  136. _compare = CompareType.Text;
  137. break;
  138. }
  139. }
  140. else {
  141. _outfile = null;
  142. _compare = CompareType.Text;
  143. }
  144. }
  145. public CompareType Compare {
  146. get {return _compare;}
  147. }
  148. public string StyleSheet {
  149. get {return _stylesheet;}
  150. }
  151. public string SrcXml {
  152. get {return _srcxml;}
  153. }
  154. public string OutFile {
  155. get {return _outfile;}
  156. }
  157. }
  158. class SingleTestTransform
  159. {
  160. CatalogTestCase _testCase;
  161. public SingleTestTransform (CatalogTestCase testCase)
  162. {
  163. _testCase = testCase;
  164. }
  165. string _result;
  166. public string Result {
  167. get {return _result;}
  168. }
  169. System.Exception _exception;
  170. public System.Exception Exception {
  171. get {return _exception;}
  172. }
  173. public bool Succeeded {
  174. get {return this.Exception == null;}
  175. }
  176. public CatalogTestCase TestCase {
  177. get {return _testCase;}
  178. }
  179. XslTransform LoadTransform ()
  180. {
  181. XslTransform trans = new XslTransform ();
  182. if (EnvOptions.UseDomStyle) {
  183. XmlDocument styledoc = new XmlDocument ();
  184. if (EnvOptions.WhitespaceStyle)
  185. styledoc.PreserveWhitespace = true;
  186. styledoc.Load (_testCase.StyleSheet);
  187. trans.Load (styledoc, null, null);
  188. } else
  189. trans.Load (new XPathDocument (
  190. _testCase.StyleSheet,
  191. EnvOptions.WhitespaceStyle ? XmlSpace.Preserve :
  192. XmlSpace.Default),
  193. null, null);
  194. return trans;
  195. }
  196. IXPathNavigable LoadInput ()
  197. {
  198. XmlTextReader xtr=null;
  199. try {
  200. xtr = new XmlTextReader (_testCase.SrcXml);
  201. XmlValidatingReader xvr = new XmlValidatingReader (xtr);
  202. xvr.ValidationType = ValidationType.None;
  203. IXPathNavigable input = null;
  204. if (EnvOptions.UseDomInstance) {
  205. XmlDocument dom = new XmlDocument ();
  206. if (EnvOptions.WhitespaceInstance)
  207. dom.PreserveWhitespace = true;
  208. dom.Load (xvr);
  209. input = dom;
  210. } else {
  211. input = new XPathDocument (xvr,
  212. EnvOptions.WhitespaceStyle ? XmlSpace.Preserve :
  213. XmlSpace.Default);
  214. }
  215. return input;
  216. }
  217. finally {
  218. if (xtr!=null)
  219. xtr.Close ();
  220. }
  221. }
  222. public void RunTest ()
  223. {
  224. try {
  225. XslTransform trans = LoadTransform ();
  226. IXPathNavigable input = LoadInput ();
  227. using (StringWriter sw = new StringWriter ()) {
  228. trans.Transform (input, null, sw, null);
  229. _result = sw.ToString ();
  230. }
  231. }
  232. catch (System.Exception e) {
  233. _exception = e;
  234. }
  235. }
  236. }
  237. }