0761338aef3ce0f865af73d31135df098bb27a12
[mono.git] / mcs / class / System.XML / Test / System.Xml.Xsl / standalone_tests / xslttest.cs
1 using System;
2 using System.Collections;
3 using System.IO;
4 using System.Text;
5 using System.Xml;
6 using System.Xml.XPath;
7 using System.Xml.Xsl;
8 using NUnit.Core;
9 using NUnit.Framework;
10
11 namespace MonoTests.oasis_xslt {
12         public class SuiteBuilder {
13                 #region test list fields
14                 IDictionary expectedExceptions = new Hashtable ();
15                 ArrayList skipTargets = new ArrayList ();
16                 ArrayList knownFailures = new ArrayList ();
17                 ArrayList fixmeList = new ArrayList ();
18                 #endregion
19         
20                 public SuiteBuilder ()
21                 {
22                 }
23
24                 void ReadLists ()
25                 {
26                         string exceptionsFilename = Path.Combine (EnvOptions.OutputDir, "res-exceptions.lst");
27
28                         Helpers.ReadStrings (skipTargets, "ignore.lst");
29 #if TARGET_JVM
30                         Helpers.ReadStrings (knownFailures, "knownFailures.jvm.lst");
31 #else
32                         Helpers.ReadStrings (knownFailures, "knownFailures.lst");
33 #endif
34                         Helpers.ReadStrings (fixmeList, "fixme.lst");
35                         ArrayList exceptionsArray = new ArrayList();
36                         Helpers.ReadStrings (exceptionsArray, exceptionsFilename);
37                         foreach (string s in exceptionsArray) {
38                                 string [] halves = s.Split ('\t');
39                                 expectedExceptions [halves[0]] = halves[1];
40                         }
41                 }
42
43                 void Build (TestSuite suite)
44                 {
45 //                      if (Environment.GetEnvironmentVariables().Contains("START_DEBUG"))
46 //                              System.Diagnostics.Debugger.Launch ();
47                         ReadLists ();
48                         XmlDocument whole = new XmlDocument ();
49                         whole.Load (@"testsuite/TESTS/catalog-fixed.xml");
50
51                         foreach (XmlElement testCase in whole.SelectNodes ("test-suite/test-catalog/test-case")) {
52                                 string testid = testCase.GetAttribute ("id");
53
54                                 if (skipTargets.Contains (testid))
55                                         continue;
56
57                                 CatalogTestCase ctc = new CatalogTestCase(EnvOptions.OutputDir, testCase);
58                                 if (!ctc.Process ())
59                                         continue;
60
61                                 SingleTestTransform stt = new SingleTestTransform (ctc);
62
63                                 string expectedException = (string) expectedExceptions[testid];
64                                 bool isKnownFailure = knownFailures.Contains (testid) || fixmeList.Contains (testid);
65
66                                 suite.Add (new TestFromCatalog (testid, stt, expectedException,
67                                         EnvOptions.InverseResults, isKnownFailure));
68                         }
69                 }
70
71                 static object lock_obj = new object ();
72                 static TestSuite _suite;
73
74                 [Suite]
75                 public static TestSuite Suite { 
76                         get {
77                                 if (_suite == null) { lock (lock_obj) {
78                                 TestSuite suite = new TestSuite ("MonoTests.oasis_xslt.SuiteBuilder");
79                                 new SuiteBuilder ().Build (suite);
80                                 _suite = suite;
81                                 } }
82                                 return _suite;
83                         }
84                 }
85         }
86
87         class TestFromCatalog: NUnit.Core.TestCase {
88                 bool _inverseResult;
89                 string _testid;
90                 string _expectedException;
91                 SingleTestTransform _transform;
92
93                 public TestFromCatalog (string testid, SingleTestTransform transform,
94                         string expectedException, bool inverseResult, bool isKnownFailure)
95                         :base (null, testid)
96                 {
97                         _testid = testid;
98                         _expectedException = expectedException;
99                         _transform = transform;
100                         _inverseResult = inverseResult;
101                         
102                         ArrayList arr = new ArrayList ();
103                         if (isKnownFailure) {
104                                 arr.Add ("KnownFailures");
105                                 //this.IsExplicit = true;
106                         }
107                         else
108                                 arr.Add ("Clean");
109                         Categories = arr;
110                 }
111
112                 static string EscapeString (string res)
113                 {
114                         MemoryStream s = new MemoryStream ();
115                         XmlTextWriter w = new XmlTextWriter (s, System.Text.Encoding.ASCII);
116                         w.WriteString (res);
117                         w.Close ();     
118                         
119                         StringBuilder sb = new StringBuilder (res.Length);
120                         byte [] arr = s.ToArray ();
121                         foreach (byte b in arr)
122                                 sb.Append (Convert.ToChar (b));
123
124                         return sb.ToString ();
125                 }
126
127                 string CompareResult (string actual, string expected, CatalogTestCase.CompareType compare)
128                 {
129                         //TODO: add html comparison
130                         if (compare== CatalogTestCase.CompareType.XML) {
131                                 try {
132                                         XmlDocument actDoc = new XmlDocument();
133                                         XmlDocument expDoc = new XmlDocument();
134                                         actDoc.LoadXml (actual);
135                                         expDoc.LoadXml (expected);
136                                         XmlCompare.XmlCompare cmp = new XmlCompare.XmlCompare(XmlCompare.XmlCompare.Flags.IgnoreAttribOrder);
137                                         if (cmp.AreEqual (actDoc, expDoc)) {
138                                                 return null;
139                                         }
140                                 }
141                                 catch (Exception ex) {
142                                         //could not compare as xml, fallback to text
143                                         if (actual == expected)
144                                                 return null;
145                                 }
146                         }
147                         else
148                                 if (actual == expected)
149                                         return null;
150
151                         string res = "Different.\nActual*****\n"+actual+"\nReference*****\n"+expected;
152                         return EscapeString (res);
153                 }
154
155                 string CompareException (Exception actual, string testid)
156                 {
157                         if (_expectedException == null)
158                                 return "Unexpected exception: " + actual.ToString ();
159
160                         string actualType = actual.GetType ().ToString ();
161                         if (actualType != _expectedException)
162                                 return "Different exception thrown.\nActual*****\n"+actualType+
163                                         "\nReference*****\n"+_expectedException;
164
165             return null;
166                 }
167
168                 void ReportResult (string failureMessage, string stackTrace, TestCaseResult res)
169                 {
170                         if (_inverseResult) {
171                                 if (failureMessage != null)
172                                         res.Success ();
173                                 else
174                                         res.Failure ("The following test was FIXED: "+_testid, null);
175                         }
176                         else {
177                                 if (failureMessage != null)
178                                         res.Failure (failureMessage, stackTrace);
179                                 else
180                                         res.Success ();
181                         }
182                 }
183
184                 public override void Run (TestCaseResult res)
185                 {
186                         _transform.RunTest ();
187
188                         string failureMessage;
189                         string stackTrace = null;
190                         if (_transform.Succeeded) {
191                                 try {
192                                         using (StreamReader sr = new StreamReader (_transform.TestCase.OutFile))
193                                                 failureMessage = CompareResult (_transform.Result, sr.ReadToEnd ().Replace ("\r\n", "\n"), _transform.TestCase.Compare);
194                                 }
195                                 catch {
196                                         //if there is no reference result because of expectedException, we
197                                         //are OK, otherwise, rethrow
198                                         if (_expectedException!=null)
199                                                 failureMessage = null;
200                                         else {
201                                                 Console.WriteLine (_transform.TestCase.OutFile);
202                                                 Console.WriteLine ("ERROR: No reference result, and no expected exception.");
203                                                 throw;
204                                         }
205                                 }
206                         }
207                         else {
208                                 failureMessage = CompareException (_transform.Exception, _testid);
209                                 stackTrace = _transform.Exception.StackTrace;
210                         }
211
212                         ReportResult (failureMessage, stackTrace, res);
213                 }
214         }
215 }