New test.
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebTest / NunitWebTest.cs
index 2a721a2cf941c4cdabd552356f7783ae30497a41..d6c7eba1132b16211723ddb2c3bbcf6fdfffd170 100644 (file)
@@ -26,6 +26,9 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
+
+
+
 using System;
 using System.IO;
 using System.Xml;
@@ -37,12 +40,7 @@ using NUnit.Framework;
 
 namespace MonoTests.stand_alone.WebHarness
 {
-       public abstract class XmlComparableTest
-       {
-               public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
-       }
-
-       public class WebTest : XmlComparableTest
+       public class HtmlDiff
        {
                public const string BEGIN_TAG = "begint";
                public const string END_TAG = "endt";
@@ -55,7 +53,7 @@ namespace MonoTests.stand_alone.WebHarness
 
                
 
-               public WebTest()
+               public HtmlDiff()
                {
                }
 
@@ -72,93 +70,96 @@ namespace MonoTests.stand_alone.WebHarness
 
                public static string GetControlFromPageHtml (string str)
                {
+                       if (str == null || str == string.Empty)
+                               throw new ArgumentException ("internal error: str is null or empty");
+                       int beginPos = str.IndexOf (BEGIN_TAG);
+                       int endPos = str.IndexOf (END_TAG);
+                       if (beginPos == -1)
+                               throw new Exception ("internal error: BEGIN_TAG is missing. Full source: "+str);
+                       if (endPos == -1)
+                               throw new Exception ("internal error: END_TAG is missing. Full source: "+str);
+                               
                        StringBuilder sb = new StringBuilder ();
-                       sb.Append (str.Substring (str.IndexOf (BEGIN_TAG) + BEGIN_TAG.Length, str.IndexOf (END_TAG) - str.IndexOf (BEGIN_TAG) - BEGIN_TAG.Length));
+                       sb.Append (str.Substring (beginPos + BEGIN_TAG.Length, endPos - beginPos - BEGIN_TAG.Length));
                        return sb.ToString ();
                }
 
                public static void AssertAreEqual (string origin, string derived, string msg)
                {
-                       bool test = HtmlComparer (origin, derived);
-                       if (!test) {
-                               Assert.AreEqual (_compareActual, _compareExpect, msg);
-                                      
+                       bool test = false;\r
+                       try {\r
+                               test = HtmlComparer (origin, derived);\r
+                       }\r
+                       catch (Exception e) {\r
+                               //swallow e when there is XML error and fallback\r
+                               //to the text comparison\r
                        }
-               }
-
-               public static bool HtmlComparer (string origin, string derived)
+                       if (!test) {\r
+                               Assert.AreEqual (_compareExpect, _compareActual, msg);
+                       }
+               }\r
+\r
+               private static bool HtmlComparer (string origin, string derived)
                {
                        XmlDocument or = new XmlDocument ();
-                       MonoTests.stand_alone.WebHarness.WebTest helper = new MonoTests.stand_alone.WebHarness.WebTest ();
+                       MonoTests.stand_alone.WebHarness.HtmlDiff helper = new MonoTests.stand_alone.WebHarness.HtmlDiff ();
                        or.LoadXml (helper.HtmltoXml (origin));
                        XmlDocument dr = new XmlDocument ();
                        dr.LoadXml (helper.HtmltoXml (derived));
                        return helper.XmlCompare (or, dr, false);
                }
 
-               public override bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost)
+               private bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
                {
                        XmlComparer comparer = new XmlComparer();
                        if (ignoreAlmost == false)
                        {
-                               DoAlmost(d1);
-                               DoAlmost(d2);
+                               DoAlmost(expected);
+                               DoAlmost(actual);
                        }
-                       bool c = comparer.AreEqual(d1, d2);
+                       bool c = comparer.AreEqual(expected, actual);
                        _compareStatus = comparer.LastCompare;
                        _compareActual = comparer.Actual;
                        _compareExpect = comparer.Expected;
                        return c;
-               }
-
-               public string HtmltoXml(string html)
-               {
-                       HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
-                       doc.LoadHtml(html);
-
-                       StringBuilder fixedxml = new StringBuilder();
-                       StringWriter sw = new StringWriter(fixedxml);
-
-                       try
-                       {
-                               StringBuilder tempxml = new StringBuilder();
-                               StringWriter tsw = new StringWriter(tempxml);
-
-                               doc.OptionOutputAsXml = true;
-                               doc.Save(tsw);
-
-                               // fix style attribute
-                               // the reason is that style attribute name-value pairs come in different order
-                               // in .NET and GH
-                               // Here I will sort the values of style attribute
-                               XmlDocument tempDoc = new XmlDocument();
-                               tempDoc.LoadXml(tempxml.ToString());
-
-                               XmlNodeList allNodes = tempDoc.SelectNodes("//*");
-                               foreach (XmlNode n in allNodes)
-                               {
-                                       if (n.Attributes["style"] != null)
-                                       {
-                                               string att = n.Attributes["style"].Value;
-                                               string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
-
-                                               for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
-                                               {
-                                                       style[styleIndex] = FixStyleNameValue(style[styleIndex]);
-                                               }
-                                               Array.Sort(style);
-                                               n.Attributes["style"].Value = string.Join(";", style);
-                                       }
-                               }
-                               tempDoc.Save(sw);
-                       }
-                       catch (Exception)
-                       {
-                               Console.WriteLine("Error parsing html response...");
-                               Console.WriteLine("Test case aborted");
-                               return "<TestCaseAborted></TestCaseAborted>";
-                       }
-                       return fixedxml.ToString();
+               }\r
+\r
+               public string HtmltoXml (string html) //throws XmlException\r
+               {\r
+                       HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument ();\r
+                       doc.LoadHtml (html.Trim (new char[] { '\r', '\n', ' ' })); // bug in HtmlAgilityPack\r
+\r
+                       StringBuilder fixedxml = new StringBuilder ();\r
+                       StringWriter sw = new StringWriter (fixedxml);\r
+\r
+                       StringBuilder tempxml = new StringBuilder ();\r
+                       StringWriter tsw = new StringWriter (tempxml);\r
+\r
+                       doc.OptionOutputAsXml = true;\r
+                       doc.Save (tsw);\r
+\r
+                       // fix style attribute\r
+                       // the reason is that style attribute name-value pairs come in different order\r
+                       // in .NET and GH\r
+                       // Here I will sort the values of style attribute\r
+                       XmlDocument tempDoc = new XmlDocument ();\r
+                       tempDoc.LoadXml (tempxml.ToString ());\r
+\r
+                       XmlNodeList allNodes = tempDoc.SelectNodes ("//*");\r
+                       foreach (XmlNode n in allNodes) {\r
+                               if (n.Attributes["style"] != null) {\r
+                                       string att = n.Attributes["style"].Value;\r
+                                       string[] style = att.Trim (new char[] { ' ', ';' }).Split (';');\r
+\r
+                                       for (int styleIndex = 0; styleIndex < style.Length; styleIndex++) {\r
+                                               style[styleIndex] = FixStyleNameValue (style[styleIndex]);\r
+                                       }\r
+                                       Array.Sort (style);\r
+                                       n.Attributes["style"].Value = string.Join (";", style);\r
+                               }\r
+                       }\r
+                       tempDoc.Save (sw);\r
+                       return fixedxml.ToString ();\r
                }
 
                private string FixStyleNameValue(string nameValue)
@@ -177,21 +178,33 @@ namespace MonoTests.stand_alone.WebHarness
                        XmlNode XmlIgnoreNode;
                        IEnumerator xmlIgnoreEnum;
 
+
                        if (_xmlIgnoreList == null)
                        {
                                _xmlIgnoreList = new XmlDocument();
-                               string xml;
-                               using (Stream source = Assembly.GetExecutingAssembly()
-                                       .GetManifestResourceStream ("HTMLComparer.nunitweb_config.xml")) {
-                                       using (StreamReader sr = new StreamReader (source))
-                                               xml = sr.ReadToEnd ();
-                               }
+                               string xml;\r
+\r
+                               Stream source = Assembly.GetExecutingAssembly ()\r
+                                       .GetManifestResourceStream ("HtmlCompare.nunitweb_config.xml");\r
+                               if (source == null) {\r
+                                       source = Assembly.GetExecutingAssembly ()\r
+                                       .GetManifestResourceStream ("nunitweb_config.xml");\r
+                               }\r
+                                                               \r
+                               try {\r
+                                       using (StreamReader sr = new StreamReader (source))\r
+                                               xml = sr.ReadToEnd ();\r
+                               }\r
+                               finally {\r
+                                       source.Close ();\r
+                               }\r
+                               \r
                                _xmlIgnoreList.LoadXml (xml);
                        }
-
                        // Remove by Id or Name
                        // search by tag and if id or name match, remove all attributes
                        // must be the first almost since the following almost delete the id and name
+                       
                        xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
                        while (xmlIgnoreEnum.MoveNext())
                        {