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