Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mcs / class / System.XML / Test / System.Xml / W3C / xmlconf.cs
1 using System;
2 using System.Xml;
3 using System.IO;
4 using System.Collections;
5 using System.Text;
6
7 namespace MonoTests.W3C_xmlconf {
8         using NUnit.Core;
9         using NUnit.Framework;
10
11         public abstract class BaseTests
12         {
13                 TestSuite _suite;
14
15                 #region test list fields
16                 protected readonly ArrayList ignoredTests = new ArrayList ();
17                 protected readonly ArrayList knownFailures = new ArrayList ();
18                 protected readonly ArrayList fixmeList = new ArrayList ();
19                 protected readonly ArrayList netFailures = new ArrayList ();
20                 #endregion
21
22                 #region ReadStrings ()
23                 static void ReadStrings (ArrayList array, string filename) {
24                         if (!File.Exists (filename))
25                                 return;
26
27                         using (StreamReader reader = new StreamReader (filename)) {
28                                 foreach (string s_ in reader.ReadToEnd ().Split ("\n".ToCharArray ())) {
29                                         string s = s_.Trim ();
30                                         if (s.Length > 0)
31                                                 array.Add (s);
32                                 }
33                                 reader.Close();
34                         }
35                 }
36                 #endregion
37
38                 protected BaseTests (TestSuite suite)
39                         :this ()
40                 {
41                         _suite = suite;
42                 }
43
44                 private BaseTests ()
45                 {
46                         ReadStrings (ignoredTests, "ignored.lst");
47                         ReadStrings (knownFailures, "knownFailures.lst");
48                         ReadStrings (fixmeList, "fixme.lst");
49                         ReadStrings (netFailures, "net-failed.lst");
50                 }
51
52                 protected void BuildSuite ()
53                 {
54                         XmlDocument catalog = new XmlDocument ();
55                         catalog.Load ("xmlconf/xmlconf.xml");
56                         
57                         foreach (XmlElement test in catalog.SelectNodes ("//TEST")) {
58                                 string testId = test.GetAttribute ("ID");
59                                 
60                                 ProcessTest (testId, test);
61                         }
62                 }
63
64                 protected virtual bool InverseResult {
65                         get {return false;}
66                 }
67
68                 protected virtual void ProcessTest (string testId, XmlElement test)
69                 {
70                         if (ignoredTests.Contains (testId))
71                                 return;
72
73                         if (netFailures.Contains (testId))
74                                 return;
75
76                         _suite.Add (new TestFromCatalog (testId, test, InverseResult));
77                 }
78         }
79
80         public class AllTests: BaseTests
81         {
82                 [Suite]
83                 static public TestSuite Suite{
84                         get {
85                                 TestSuite suite = new TestSuite ("W3C_xmlconf.All");
86                                 AllTests tests = new AllTests (suite);
87                                 tests.BuildSuite ();
88                                 return suite;
89                         }
90                 }
91
92                 AllTests (TestSuite suite)
93                         : base (suite)
94                 {
95                 }
96         }
97
98         public class CleanTests : BaseTests
99         {
100                 [Suite]
101                 static public TestSuite Suite{
102                         get {
103                                 TestSuite suite = new TestSuite ("W3C_xmlconf.Clean");
104                                 CleanTests tests = new CleanTests (suite);
105                                 tests.BuildSuite ();
106                                 return suite;
107                         }
108                 }
109
110                 CleanTests (TestSuite suite)
111                         : base (suite)
112                 {
113                 }
114
115                 protected override void ProcessTest(string testId, XmlElement test)
116                 {
117                         if (knownFailures.Contains (testId) || fixmeList.Contains (testId))
118                                 return;
119
120                         base.ProcessTest (testId, test);
121                 }
122         }
123
124         public class KnownFailureTests : BaseTests
125         {
126                 [Suite]
127                 static public TestSuite Suite{
128                         get {
129                                 TestSuite suite = new TestSuite ("W3C_xmlconf.KnownFailures");
130                                 KnownFailureTests tests = new KnownFailureTests (suite);
131                                 tests.BuildSuite ();
132                                 return suite;
133                         }
134                 }
135
136                 KnownFailureTests (TestSuite suite)
137                         : base (suite)
138                 {
139                 }
140
141                 protected override bool InverseResult {
142                         get {return true;}
143                 }
144
145                 protected override void ProcessTest(string testId, XmlElement test)
146                 {
147                         if (!knownFailures.Contains (testId) && !fixmeList.Contains (testId))
148                                 return;
149
150                         base.ProcessTest (testId, test);
151                 }
152         }
153
154         public class TestFromCatalog : NUnit.Core.TestCase
155         {
156                 XmlElement _test;
157                 string _errorString;
158                 bool _inverseResult;
159
160                 public TestFromCatalog (string testId, XmlElement test, bool inverseResult)
161                         :base (null, testId)
162                 {
163                         _test = test;
164                         _inverseResult = inverseResult;
165                 }
166
167                 bool TestNonValidating (string uri)
168                 {
169                         XmlTextReader trd = null;
170                         try {
171                                 trd = new XmlTextReader (uri);
172                                 new XmlDocument ().Load (trd);
173                                 return true;
174                         }
175                         catch (Exception e) {
176                                 _errorString = e.ToString ();
177                                 return false;
178                         }
179                         finally {
180                                 if (trd != null)
181                                         trd.Close();
182                         }
183                 }
184
185                 bool TestValidating (string uri)
186                 {
187                         XmlTextReader rd = null;
188                         try {
189                                 rd = new XmlTextReader (uri);
190                                 XmlValidatingReader vrd = new XmlValidatingReader (rd);
191                                 new XmlDocument ().Load (vrd);
192                                 return true;
193                         }
194                         catch (Exception e) {
195                                 _errorString = e.ToString (); //rewrites existing, possibly, but it's ok
196                                 return false;
197                         }
198                         finally {
199                                 if (rd != null) 
200                                         rd.Close();                             
201                         }
202                 }
203
204                 public override void Run (TestCaseResult res)
205                 {
206                         string type = _test.GetAttribute ("TYPE");
207                         if (type == "error")
208                                 res.Success ();
209
210                         Uri baseUri = new Uri (_test.BaseURI);
211                         Uri testUri = new Uri (baseUri, _test.GetAttribute ("URI"));
212
213                         bool nonValidatingPassed = TestNonValidating (testUri.ToString ());
214                         bool validatingPassed = TestValidating (testUri.ToString ());
215                 
216                         bool isok = isOK (type, nonValidatingPassed, validatingPassed);
217                         string message="";
218                         if (_inverseResult) {
219                                 isok = !isok;
220                                 message = "The following test was FIXED:\n";
221                         }
222
223                         if (isok)
224                                 res.Success ();
225                         else {
226                                 message += "type:"+type;
227                                 message += " non-validating passed:"+nonValidatingPassed.ToString();
228                                 message += " validating passed:"+validatingPassed.ToString();
229                                 message += " description:"+_test.InnerText;
230                                 res.Failure (message, _errorString);
231                         }
232                 }
233
234                 static bool isOK (string type, bool nonValidatingPassed, bool validatingPassed)
235                 {
236                         switch (type) {
237                         case "valid":
238                                 return nonValidatingPassed && validatingPassed;
239                         case "invalid":
240                                 return nonValidatingPassed && !validatingPassed;
241                         case "not-wf":
242                                 return !nonValidatingPassed && !validatingPassed;
243                         case "error":
244                                 return true; //readers can optionally accept or reject errors
245                         default:
246                                 throw new ArgumentException ("Wrong test type", "type");
247                         }
248                 }
249         }
250 }