PageRenderTime 86ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web/Test/mainsoft/MainsoftWebTest/NunitWebTest.cs

https://github.com/iainlane/mono
C# | 301 lines | 222 code | 35 blank | 44 comment | 48 complexity | fb69beadeb0a32fc256677e89cff2a4a MD5 | raw file
  1. //
  2. // Authors:
  3. // Rafael Mizrahi <rafim@mainsoft.com>
  4. // Erez Lotan <erezl@mainsoft.com>
  5. // Vladimir Krasnov <vladimirk@mainsoft.com>
  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.IO;
  31. using System.Xml;
  32. using System.Net;
  33. using System.Text;
  34. using System.Collections;
  35. using System.Reflection;
  36. using NUnit.Framework;
  37. namespace MonoTests.stand_alone.WebHarness
  38. {
  39. public class HtmlDiff
  40. {
  41. public const string BEGIN_TAG = "begint";
  42. public const string END_TAG = "endt";
  43. private XmlDocument _xmlIgnoreList = null;
  44. private string _compareStatus = "";
  45. private static string _compareActual = "";
  46. private static string _compareExpect = "";
  47. private string _ignoreListFile = "";
  48. public HtmlDiff()
  49. {
  50. }
  51. public string IgnoreListFile
  52. {
  53. get {return _ignoreListFile;}
  54. set {_ignoreListFile = value;}
  55. }
  56. public string CompareStatus
  57. {
  58. get {return _compareStatus.ToString();}
  59. }
  60. public static string GetControlFromPageHtml (string str)
  61. {
  62. return GetControlFromPageHtml (str, BEGIN_TAG, END_TAG);
  63. }
  64. public static string GetControlFromPageHtml (string str, string beginTag, string endTag)
  65. {
  66. if (str == null || str.Length == 0)
  67. throw new ArgumentException ("internal error: str is null or empty");
  68. if (beginTag == null || beginTag.Length == 0)
  69. throw new ArgumentNullException ("beginTag");
  70. if (endTag == null || endTag.Length == 0)
  71. throw new ArgumentNullException ("endTag");
  72. int beginPos = str.IndexOf (beginTag);
  73. int endPos = str.IndexOf (endTag);
  74. if (beginPos == -1)
  75. throw new InvalidOperationException (String.Format ("internal error: begin tag ('{0}') is missing. Full source: {1}", beginTag, str));
  76. if (endPos == -1)
  77. throw new InvalidOperationException (String.Format ("internal error: end tag ('{0}') is missing. Full source: {1}", endTag, str));
  78. StringBuilder sb = new StringBuilder ();
  79. sb.Append (str.Substring (beginPos + beginTag.Length, endPos - beginPos - beginTag.Length));
  80. return sb.ToString ();
  81. }
  82. public static void AssertAreEqual (string origin, string derived, string msg)
  83. {
  84. bool test = false;
  85. try {
  86. test = HtmlComparer (origin, derived);
  87. }
  88. catch (Exception e) {
  89. //swallow e when there is XML error and fallback
  90. //to the text comparison
  91. Assert.AreEqual (origin, derived, msg);
  92. }
  93. if (!test) {
  94. Assert.AreEqual (_compareExpect, _compareActual, msg);
  95. }
  96. }
  97. private static bool HtmlComparer (string origin, string derived)
  98. {
  99. XmlDocument or = new XmlDocument ();
  100. MonoTests.stand_alone.WebHarness.HtmlDiff helper = new MonoTests.stand_alone.WebHarness.HtmlDiff ();
  101. or.LoadXml (helper.HtmltoXml (origin));
  102. XmlDocument dr = new XmlDocument ();
  103. dr.LoadXml (helper.HtmltoXml (derived));
  104. return helper.XmlCompare (or, dr, false);
  105. }
  106. private bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
  107. {
  108. XmlComparer comparer = new XmlComparer();
  109. if (ignoreAlmost == false)
  110. {
  111. DoAlmost(expected);
  112. DoAlmost(actual);
  113. }
  114. bool c = comparer.AreEqual(expected, actual);
  115. _compareStatus = comparer.LastCompare;
  116. _compareActual = comparer.Actual;
  117. _compareExpect = comparer.Expected;
  118. return c;
  119. }
  120. public string HtmltoXml (string html) //throws XmlException
  121. {
  122. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument ();
  123. doc.LoadHtml (html.Trim (new char[] { '\r', '\n', ' ' })); // bug in HtmlAgilityPack
  124. StringBuilder fixedxml = new StringBuilder ();
  125. StringWriter sw = new StringWriter (fixedxml);
  126. StringBuilder tempxml = new StringBuilder ();
  127. StringWriter tsw = new StringWriter (tempxml);
  128. doc.OptionOutputAsXml = true;
  129. doc.Save (tsw);
  130. // fix style attribute
  131. // the reason is that style attribute name-value pairs come in different order
  132. // in .NET and GH
  133. // Here I will sort the values of style attribute
  134. XmlDocument tempDoc = new XmlDocument ();
  135. tempDoc.LoadXml (tempxml.ToString ());
  136. XmlNodeList allNodes = tempDoc.SelectNodes ("//*");
  137. foreach (XmlNode n in allNodes) {
  138. if (n.Attributes["style"] != null) {
  139. string att = n.Attributes["style"].Value;
  140. string[] style = att.Trim (new char[] { ' ', ';' }).Split (';');
  141. for (int styleIndex = 0; styleIndex < style.Length; styleIndex++) {
  142. style[styleIndex] = FixStyleNameValue (style[styleIndex]);
  143. }
  144. Array.Sort (style);
  145. n.Attributes["style"].Value = string.Join (";", style);
  146. }
  147. }
  148. tempDoc.Save (sw);
  149. return fixedxml.ToString ();
  150. }
  151. private string FixStyleNameValue(string nameValue)
  152. {
  153. string [] nv = nameValue.Split(':');
  154. // value may contain spaces in case of
  155. // multiple values for one key
  156. string [] nvalue = nv[1].Trim().Split(' ');
  157. Array.Sort(nvalue);
  158. nv[1] = string.Join(" ", nvalue);
  159. return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
  160. }
  161. private void DoAlmost(XmlDocument xmlDocument)
  162. {
  163. XmlNode XmlIgnoreNode;
  164. IEnumerator xmlIgnoreEnum;
  165. if (_xmlIgnoreList == null)
  166. {
  167. _xmlIgnoreList = new XmlDocument();
  168. string xml;
  169. Stream source = Assembly.GetExecutingAssembly ()
  170. .GetManifestResourceStream ("HtmlCompare.nunitweb_config.xml");
  171. if (source == null) {
  172. source = Assembly.GetExecutingAssembly ()
  173. .GetManifestResourceStream ("nunitweb_config.xml");
  174. }
  175. try {
  176. using (StreamReader sr = new StreamReader (source))
  177. xml = sr.ReadToEnd ();
  178. }
  179. finally {
  180. source.Close ();
  181. }
  182. _xmlIgnoreList.LoadXml (xml);
  183. }
  184. // Remove by Id or Name
  185. // search by tag and if id or name match, remove all attributes
  186. // must be the first almost since the following almost delete the id and name
  187. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
  188. while (xmlIgnoreEnum.MoveNext())
  189. {
  190. XmlNodeList DocNodeList;
  191. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  192. DocNodeList = xmlDocument.GetElementsByTagName("*");
  193. if (DocNodeList != null)
  194. {
  195. foreach (XmlElement tmpXmlElement in DocNodeList)
  196. {
  197. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  198. {
  199. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  200. {
  201. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  202. {
  203. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
  204. {
  205. tmpXmlElement.RemoveAllAttributes();
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. // remove ignored attributes
  214. // search for tag and remove it's attributes
  215. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
  216. while (xmlIgnoreEnum.MoveNext())
  217. {
  218. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  219. XmlNodeList DocNodeList;
  220. //clean specific element
  221. DocNodeList = xmlDocument.GetElementsByTagName("*");
  222. if (DocNodeList != null)
  223. {
  224. foreach (XmlElement tmpXmlElement in DocNodeList)
  225. {
  226. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  227. {
  228. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  229. {
  230. tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. // clean javascript attribute value
  237. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
  238. while (xmlIgnoreEnum.MoveNext())
  239. {
  240. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  241. XmlNodeList DocNodeList;
  242. //clean Java Script attribute values
  243. DocNodeList = xmlDocument.GetElementsByTagName("*");
  244. if (DocNodeList != null)
  245. {
  246. foreach (XmlElement tmpXmlElement in DocNodeList)
  247. {
  248. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  249. {
  250. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  251. {
  252. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  253. {
  254. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
  255. {
  256. tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }