merge -r 61110:61111
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebTest / NunitWebTest.cs
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
38 namespace MonoTests.stand_alone.WebHarness
39 {
40         public abstract class XmlComparableTest
41         {
42                 public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
43         }
44
45         public class WebTest : XmlComparableTest
46         {
47                 public const string BEGIN_TAG = "begint";
48                 public const string END_TAG = "endt";
49
50                 private XmlDocument _xmlIgnoreList = null;
51                 private string _compareStatus = "";
52                 private static string _compareActual = "";
53                 private static string _compareExpect = "";
54                 private string _ignoreListFile = "";
55
56                 
57
58                 public WebTest()
59                 {
60                 }
61
62                 public string IgnoreListFile
63                 {
64                         get {return _ignoreListFile;}
65                         set {_ignoreListFile = value;}
66                 }
67
68                 public string CompareStatus
69                 {
70                         get {return _compareStatus.ToString();}
71                 }
72
73                 public static string GetControlFromPageHtml (string str)
74                 {
75                         if (string.IsNullOrEmpty (str))
76                                 throw new ArgumentException ("internal error: str is null or empty");
77                         int beginPos = str.IndexOf (BEGIN_TAG);
78                         int endPos = str.IndexOf (END_TAG);
79                         if (beginPos == -1)
80                                 throw new Exception ("internal error: BEGIN_TAG is missing. Full source: "+str);
81                         if (endPos == -1)
82                                 throw new Exception ("internal error: END_TAG is missing. Full source: "+str);
83                                 
84                         StringBuilder sb = new StringBuilder ();
85                         sb.Append (str.Substring (beginPos + BEGIN_TAG.Length, endPos - beginPos - BEGIN_TAG.Length));
86                         return sb.ToString ();
87                 }
88
89                 public static void AssertAreEqual (string origin, string derived, string msg)
90                 {
91                         bool test = HtmlComparer (origin, derived);
92                         if (!test) {
93                                 Assert.AreEqual (_compareActual, _compareExpect, msg);
94                                        
95                         }
96                 }
97
98                 public static bool HtmlComparer (string origin, string derived)
99                 {
100                         XmlDocument or = new XmlDocument ();
101                         MonoTests.stand_alone.WebHarness.WebTest helper = new MonoTests.stand_alone.WebHarness.WebTest ();
102                         or.LoadXml (helper.HtmltoXml (origin));
103                         XmlDocument dr = new XmlDocument ();
104                         dr.LoadXml (helper.HtmltoXml (derived));
105                         return helper.XmlCompare (or, dr, false);
106                 }
107
108                 public override bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost)
109                 {
110                         XmlComparer comparer = new XmlComparer();
111                         if (ignoreAlmost == false)
112                         {
113                                 DoAlmost(d1);
114                                 DoAlmost(d2);
115                         }
116                         bool c = comparer.AreEqual(d1, d2);
117                         _compareStatus = comparer.LastCompare;
118                         _compareActual = comparer.Actual;
119                         _compareExpect = comparer.Expected;
120                         return c;
121                 }
122
123                 public string HtmltoXml(string html)
124                 {
125                         HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
126                         doc.LoadHtml(html);
127
128                         StringBuilder fixedxml = new StringBuilder();
129                         StringWriter sw = new StringWriter(fixedxml);
130
131                         try
132                         {
133                                 StringBuilder tempxml = new StringBuilder();
134                                 StringWriter tsw = new StringWriter(tempxml);
135
136                                 doc.OptionOutputAsXml = true;
137                                 doc.Save(tsw);
138
139                                 // fix style attribute
140                                 // the reason is that style attribute name-value pairs come in different order
141                                 // in .NET and GH
142                                 // Here I will sort the values of style attribute
143                                 XmlDocument tempDoc = new XmlDocument();
144                                 tempDoc.LoadXml(tempxml.ToString());
145
146                                 XmlNodeList allNodes = tempDoc.SelectNodes("//*");
147                                 foreach (XmlNode n in allNodes)
148                                 {
149                                         if (n.Attributes["style"] != null)
150                                         {
151                                                 string att = n.Attributes["style"].Value;
152                                                 string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
153
154                                                 for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
155                                                 {
156                                                         style[styleIndex] = FixStyleNameValue(style[styleIndex]);
157                                                 }
158                                                 Array.Sort(style);
159                                                 n.Attributes["style"].Value = string.Join(";", style);
160                                         }
161                                 }
162                                 tempDoc.Save(sw);
163                         }
164                         catch (Exception)
165                         {
166                                 Console.WriteLine("Error parsing html response...");
167                                 Console.WriteLine("Test case aborted");
168                                 return "<TestCaseAborted></TestCaseAborted>";
169                         }
170                         return fixedxml.ToString();
171                 }
172
173                 private string FixStyleNameValue(string nameValue)
174                 {
175                         string [] nv = nameValue.Split(':');
176                         // value may contain spaces in case of
177                         // multiple values for one key
178                         string [] nvalue = nv[1].Trim().Split(' ');
179                         Array.Sort(nvalue);
180                         nv[1] = string.Join(" ", nvalue);
181                         return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
182                 }
183
184                 private void DoAlmost(XmlDocument xmlDocument)
185                 {
186                         XmlNode XmlIgnoreNode;
187                         IEnumerator xmlIgnoreEnum;
188
189                         if (_xmlIgnoreList == null)
190                         {
191                                 _xmlIgnoreList = new XmlDocument();
192                                 string xml;
193                                 using (Stream source = Assembly.GetExecutingAssembly()
194                                         .GetManifestResourceStream ("nunitweb_config.xml")) {
195                                         using (StreamReader sr = new StreamReader (source))
196                                                 xml = sr.ReadToEnd ();
197                                 }
198                                 _xmlIgnoreList.LoadXml (xml);
199                         }
200
201                         // Remove by Id or Name
202                         // search by tag and if id or name match, remove all attributes
203                         // must be the first almost since the following almost delete the id and name
204                         xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
205                         while (xmlIgnoreEnum.MoveNext())
206                         {
207                                 XmlNodeList DocNodeList;
208                                 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
209                                 DocNodeList = xmlDocument.GetElementsByTagName("*");
210                                 if (DocNodeList != null)
211                                 {
212                                         foreach (XmlElement tmpXmlElement in DocNodeList)
213                                         {
214                                                 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
215                                                 {
216                                                         if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower()) 
217                                                         {
218                                                                 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
219                                                                 {
220                                                                         if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
221                                                                         {
222                                                                                 tmpXmlElement.RemoveAllAttributes();
223                                                                         }
224                                                                 }
225                                                         }
226                                                 }
227                                         }
228                                 }       
229                         }
230                         // remove ignored attributes
231                         // search for tag and remove it's attributes
232                         xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
233                         while (xmlIgnoreEnum.MoveNext())
234                         {
235                                 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
236                                 XmlNodeList DocNodeList;
237                                 //clean specific element
238
239                                 DocNodeList = xmlDocument.GetElementsByTagName("*");
240                                 if (DocNodeList != null)
241                                 {
242                                         foreach (XmlElement tmpXmlElement in DocNodeList)
243                                         {
244                                                 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower()) 
245                                                 {
246                                                         foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
247                                                         {
248                                                                 tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
249                                                         }
250                                                 }
251                                         }
252                                 }
253                         }
254
255                         // clean javascript attribute value
256                         xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
257                         while (xmlIgnoreEnum.MoveNext())
258                         {
259                                 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
260                                 XmlNodeList DocNodeList;
261                                 //clean Java Script attribute values
262                                 DocNodeList = xmlDocument.GetElementsByTagName("*");
263                                 if (DocNodeList != null)
264                                 {
265                                         foreach (XmlElement tmpXmlElement in DocNodeList)
266                                         {
267                                                 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower()) 
268                                                 {
269                                                         foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
270                                                         {
271                                                                 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
272                                                                 {
273                                                                         if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
274                                                                         {
275                                                                                 tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");
276                                                                         }
277                                                                 }
278                                                         }
279                                                 }
280                                         }
281                                 }
282                         }
283                 }
284         }
285 }