Harness.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // Authors:
  3. // Rafael Mizrahi <[email protected]>
  4. // Erez Lotan <[email protected]>
  5. // Vladimir Krasnov <[email protected]>
  6. //
  7. //
  8. // Copyright (c) 2002-2005 Mainsoft Corporation.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Configuration;
  31. using System.Xml;
  32. using System.Collections;
  33. using System.Net;
  34. using System.IO;
  35. #if NUNIT
  36. using NUnit.Framework;
  37. using NUnit.Core;
  38. #endif
  39. namespace MonoTests.stand_alone.WebHarness
  40. {
  41. public class Harness
  42. {
  43. static string _ignoreListFile = "";
  44. static string _catalogFile = "";
  45. static string _outputPath = "";
  46. static string _baseUrlExp = "";
  47. static string _baseUrlTst = "";
  48. static bool _disableAlmost = false;
  49. static bool _runExcluded = false;
  50. static void Main(string[] args)
  51. {
  52. ParseCommandLineAgrs(args);
  53. if (_baseUrlExp != "")
  54. {
  55. //baseUrl = "http://localhost:80/System_Web_dll";
  56. CreateExpectedResults(_baseUrlExp);
  57. }
  58. if (_baseUrlTst != "")
  59. {
  60. //baseUrl = "http://localhost:8080/System_Web_dll";
  61. RunTests(_baseUrlTst);
  62. }
  63. }
  64. #if NUNIT
  65. [Suite]
  66. static public TestSuite Suite
  67. {
  68. get
  69. {
  70. ParseAppConfigFile();
  71. TestSuite suite = new TestSuite ("SystemWebTests");
  72. TestsCatalog tc = new TestsCatalog();
  73. foreach (TestInfo ti in tc)
  74. {
  75. suite.Add(new SingleWebTest(ti, _baseUrlTst));
  76. }
  77. return suite;
  78. }
  79. }
  80. #endif
  81. #region "Cmd line"
  82. static string GetParameterByName(string name, string[] args)
  83. {
  84. int i = Array.IndexOf(args, name);
  85. if (i >= 0)
  86. {
  87. if (i < args.Length - 1)
  88. {
  89. return args[i+1];
  90. }
  91. }
  92. return "";
  93. }
  94. static bool IsParameterSet(string name, string[] args)
  95. {
  96. int i = Array.IndexOf(args, name);
  97. return (i >= 0);
  98. }
  99. static void ParseAppConfigFile()
  100. {
  101. _disableAlmost = Convert.ToBoolean(ConfigurationSettings.AppSettings["DisableAlmost"]);
  102. _runExcluded = Convert.ToBoolean(ConfigurationSettings.AppSettings["RunExcluded"]);
  103. _ignoreListFile = ConfigurationSettings.AppSettings["AlmostList"];
  104. _catalogFile = ConfigurationSettings.AppSettings["TestCatalog"];
  105. _outputPath = ConfigurationSettings.AppSettings["OutputDir"];
  106. if ((!_outputPath.EndsWith("\\")) && (!_outputPath.EndsWith("/")))
  107. {
  108. _outputPath += Path.DirectorySeparatorChar;
  109. }
  110. _baseUrlExp = ConfigurationSettings.AppSettings["ExpResBaseUrl"];
  111. _baseUrlTst = ConfigurationSettings.AppSettings["TestBaseUrl"];
  112. }
  113. static void ParseCommandLineAgrs(string [] args)
  114. {
  115. _disableAlmost = IsParameterSet("-na", args);
  116. _runExcluded = IsParameterSet("-x", args);
  117. // specifies the almost config xml file
  118. // default is almost_config.xml in current folder
  119. _ignoreListFile = GetParameterByName("-i", args);
  120. if (_ignoreListFile == "")
  121. {
  122. _ignoreListFile = "almost_config.xml";
  123. }
  124. // specifies tests catalog xml file
  125. // default is test_catalog.xml in current folder
  126. _catalogFile = GetParameterByName("-c", args);
  127. if (_catalogFile == "")
  128. {
  129. _catalogFile = "test_catalog.xml";
  130. }
  131. // specifies the folder where expected results will be stored
  132. // by default is current folder
  133. _outputPath = GetParameterByName("-o", args);
  134. if (_outputPath != "")
  135. {
  136. if ((!_outputPath.EndsWith("\\")) && (!_outputPath.EndsWith("/")))
  137. {
  138. _outputPath += Path.DirectorySeparatorChar;
  139. }
  140. }
  141. // specifies the base url for all tests
  142. // no default value
  143. _baseUrlExp = GetParameterByName("-e", args);
  144. _baseUrlTst = GetParameterByName("-t", args);
  145. }
  146. #endregion
  147. #region "Tests run routines"
  148. static void CreateExpectedResults(string baseUrl)
  149. {
  150. TestsCatalog tc = new TestsCatalog(_catalogFile, _runExcluded);
  151. HtmlDiff wt = new HtmlDiff();
  152. wt.TestsBaseUrl = baseUrl;
  153. wt.IgnoreListFile = _ignoreListFile;
  154. if ((_outputPath != "") && (!Directory.Exists(_outputPath)))
  155. {
  156. Directory.CreateDirectory(_outputPath);
  157. }
  158. Console.WriteLine("Running expected results...");
  159. foreach (TestInfo ti in tc)
  160. {
  161. Console.WriteLine("Running... " + ti.Url);
  162. XmlDocument d = wt.GetTestXml( ti );
  163. d.Save(_outputPath + ti.Url.Replace("/", "_") + ".xml");
  164. }
  165. }
  166. static void RunTests(string baseUrl)
  167. {
  168. TestsCatalog tc = new TestsCatalog(_catalogFile, _runExcluded);
  169. foreach (TestInfo ti in tc)
  170. {
  171. try {
  172. RunSingleTest(baseUrl, ti);
  173. }
  174. catch (Exception e) {
  175. Console.WriteLine(e.Message);
  176. }
  177. }
  178. }
  179. public static bool RunSingleTest(string baseUrl, TestInfo ti)
  180. {
  181. HtmlDiff wt = new HtmlDiff();
  182. wt.TestsBaseUrl = baseUrl;
  183. wt.IgnoreListFile = _ignoreListFile;
  184. XmlDocument d1 = new XmlDocument();
  185. d1.Load(_outputPath + ti.Url.Replace("/", "_") + ".xml");
  186. XmlDocument d2 = wt.GetTestXml( ti );
  187. bool fp = wt.XmlCompare(d1, d2, _disableAlmost);
  188. if (fp == false) {
  189. throw new Exception("Url: " + ti.Url + "\nCompare failed:\n" + wt.CompareStatus + "\n");
  190. }
  191. return fp;
  192. }
  193. #endregion
  194. }
  195. #region "NUnit"
  196. #if NUNIT
  197. public class SingleWebTest : NUnit.Core.TestCase
  198. {
  199. TestInfo _testInfo = null;
  200. string _baseUrl = "";
  201. public SingleWebTest (TestInfo testInfo, string baseUrl) : base (null, testInfo.Url)
  202. {
  203. _testInfo = testInfo;
  204. _baseUrl = baseUrl;
  205. }
  206. public override void Run (TestCaseResult res)
  207. {
  208. try
  209. {
  210. Harness.RunSingleTest(_baseUrl, _testInfo);
  211. }
  212. catch (Exception e)
  213. {
  214. res.Failure(e.Message, null);
  215. return;
  216. }
  217. res.Success();
  218. }
  219. }
  220. #endif
  221. #endregion
  222. }