2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / standalone_tests / xsdtest.cs
1 using System;\r
2 using System.IO;\r
3 using System.Xml;\r
4 using System.Xml.Schema;\r
5 using System.Xml.Serialization;\r
6 \r
7 public class XsdTest\r
8 {\r
9         static readonly char SEP = Path.DirectorySeparatorChar;\r
10         static ValidationEventHandler noValidateHandler =\r
11                 new ValidationEventHandler (NoValidate);\r
12 \r
13         bool version2;\r
14         bool verbose;\r
15         bool stopOnError;\r
16         bool noResolver;\r
17         bool reportAsXml;\r
18         bool reportDetails;\r
19         bool reportSuccess;\r
20         bool testAll;\r
21         bool noValidate;\r
22         string specificTarget;\r
23         TextWriter ReportWriter = Console.Out;\r
24         XmlTextWriter XmlReport;\r
25 \r
26         public static void Main (string [] args)\r
27         {\r
28                 new XsdTest ().Run (args);\r
29         }\r
30 \r
31         void Usage ()\r
32         {\r
33                 Console.WriteLine (@"\r
34 USAGE: mono xsdtest.exe options target-pattern\r
35 \r
36         options:\r
37         --stoponerr:            stops at unexpected error.\r
38         --noresolve:            don't resolve external resources.\r
39         --novalidate:           don't validate and continue reading.\r
40 {0}\r
41         --verbose:              includes processing status.\r
42         --xml:                  report as XML format.\r
43         --details:              report stack trace for errors.\r
44         --reportsuccess:        report successful test as well.\r
45         --testall:              process NISTTest/SunTest as well as MSXsdTest.\r
46 \r
47         target-pattern:         Part of the target schema file name.\r
48                                 (No Regex support.)\r
49 ",\r
50 "       --v2                    use XmlReader.Create() [2.0 only]"\r
51         );\r
52                 return;\r
53         }\r
54 \r
55         void Run (string [] args)\r
56         {\r
57                 foreach (string s in args) {\r
58                         switch (s) {\r
59                         case "--help":\r
60                                 Usage ();\r
61                                 return;\r
62                         case "--v2":\r
63                                 version2 = true; break;\r
64                         case "--verbose":\r
65                                 verbose = true; break;\r
66                         case "--stoponerr":\r
67                                 stopOnError = true; break;\r
68                         case "--noresolve":\r
69                                 noResolver = true; break;\r
70                         case "--novalidate":\r
71                                 noValidate = true; break;\r
72                         case "--xml":\r
73                                 reportAsXml = true; break;\r
74                         case "--details":\r
75                                 reportDetails = true; break;\r
76                         case "--reportsuccess":\r
77                                 reportSuccess = true; break;\r
78                         case "--testall":\r
79                                 testAll = true; break;\r
80                         default:\r
81                                 if (s.StartsWith ("--report:"))\r
82                                         ReportWriter = new StreamWriter (\r
83                                                 s.Substring (9));\r
84                                 else\r
85                                         specificTarget = s;\r
86                                 break;\r
87                         }\r
88                 }\r
89                 RunTest ("msxsdtest");\r
90                 if (testAll) {\r
91                         RunTest ("suntest");\r
92                         RunTest ("nisttest");\r
93                 }\r
94                 ReportWriter.Close ();\r
95         }\r
96 \r
97         static void NoValidate (object o, ValidationEventArgs e)\r
98         {\r
99         }\r
100         \r
101         void RunTest (string subdir)\r
102         {\r
103                 string basePath = @"Xsd-Test-Suite" + SEP;\r
104                 XmlDocument doc = new XmlDocument ();\r
105                 doc.Load (basePath + subdir + SEP + "tests-all.xml");\r
106 \r
107                 if (reportAsXml) {\r
108                         XmlReport = new XmlTextWriter (ReportWriter);\r
109                         XmlReport.Formatting = Formatting.Indented;\r
110                         XmlReport.WriteStartElement ("test-results");\r
111                 }\r
112 \r
113                 Console.WriteLine ("Started:  " + DateTime.Now);\r
114 \r
115                 foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {\r
116                         // Test schema\r
117                         string schemaFile = test.SelectSingleNode ("@schema").InnerText;\r
118                         if (specificTarget != null &&\r
119                                 schemaFile.IndexOf (specificTarget) < 0)\r
120                                 continue;\r
121                         if (schemaFile.Length > 2)\r
122                                 schemaFile = schemaFile.Substring (2);\r
123                         if (verbose)\r
124                                 Report (schemaFile, false, "compiling", "");\r
125                         bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";\r
126                         XmlSchema schema = null;\r
127                         XmlTextReader sxr = null;\r
128                         try {\r
129                                 sxr = new XmlTextReader (basePath + schemaFile);\r
130                                 if (noResolver)\r
131                                         sxr.XmlResolver = null;\r
132                                 schema = XmlSchema.Read (sxr, null);\r
133                                 schema.Compile (noValidate ? noValidateHandler : null);\r
134                                 if (!isValidSchema && !noValidate) {\r
135                                         Report (schemaFile, true, "should fail", "");\r
136                                         continue;\r
137                                 }\r
138                                 if (reportSuccess)\r
139                                         Report (schemaFile, true, "OK", "");\r
140                         } catch (XmlSchemaException ex) {\r
141                                 if (isValidSchema) {\r
142                                         Report (schemaFile, true, "should succeed", \r
143                                                 reportDetails ?\r
144                                                 ex.ToString () : ex.Message);\r
145                                         continue;\r
146                                 }\r
147                         } catch (Exception ex) {\r
148                                 if (stopOnError)\r
149                                         throw;\r
150                                 Report (schemaFile, true, "unexpected",\r
151                                                 reportDetails ?\r
152                                                 ex.ToString () : ex.Message);\r
153                                 continue;\r
154                         } finally {\r
155                                 if (sxr != null)\r
156                                         sxr.Close ();\r
157                         }\r
158 \r
159                         // Test instances\r
160                         string instanceFile = test.SelectSingleNode ("@instance").InnerText;\r
161                         if (instanceFile.Length == 0)\r
162                                 continue;\r
163                         else if (instanceFile.StartsWith ("./"))\r
164                                 instanceFile = instanceFile.Substring (2);\r
165                         if (verbose)\r
166                                 Report (instanceFile, false, "reading ", "");\r
167                         bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";\r
168                         XmlReader xvr = null;\r
169                         try {\r
170                                 XmlTextReader ixtr = new XmlTextReader (\r
171                                         Path.Combine (basePath, instanceFile));\r
172 #if NET_2_0\r
173                                 if (version2) {\r
174                                         XmlReaderSettings settings =\r
175                                                 new XmlReaderSettings ();\r
176                                         settings.ValidationType = ValidationType.Schema;\r
177                                         settings.ValidationFlags =\r
178                                                 XmlSchemaValidationFlags.IgnoreValidationWarnings;\r
179                                         if (noValidate)\r
180                                                 settings.ValidationEventHandler +=\r
181                                                         noValidateHandler;\r
182                                         if (noResolver)\r
183                                                 settings.Schemas.XmlResolver = null;\r
184                                         settings.Schemas.Add (schema);\r
185                                         if (noResolver)\r
186                                                 xvr = XmlReader.Create (ixtr, null, settings);\r
187                                         else\r
188                                                 xvr = XmlReader.Create (ixtr, settings);\r
189                                 } else {\r
190 #endif\r
191                                         XmlValidatingReader vr = new XmlValidatingReader (ixtr);\r
192                                         if (noResolver)\r
193                                                 vr.XmlResolver = null;\r
194                                         if (noValidate)\r
195                                                 vr.ValidationEventHandler += noValidateHandler;\r
196                                         vr.Schemas.Add (schema);\r
197                                         xvr = vr;\r
198 #if NET_2_0\r
199                                 }\r
200 #endif\r
201                                 while (!xvr.EOF)\r
202                                         xvr.Read ();\r
203                                 if (!isValidInstance && !noValidate)\r
204                                         Report (instanceFile, false, "should fail", "");\r
205                                 if (reportSuccess)\r
206                                         Report (instanceFile, false, "OK", "");\r
207                         } catch (XmlSchemaException ex) {\r
208                                 if (isValidInstance)\r
209                                         Report (instanceFile, false, "should succeed",\r
210                                                 reportDetails ?\r
211                                                 ex.ToString () : ex.Message);\r
212                         } catch (Exception ex) {\r
213                                 if (stopOnError)\r
214                                         throw;\r
215                                 Report (instanceFile, false, "unexpected",\r
216                                         reportDetails ?\r
217                                         ex.ToString () : ex.Message);\r
218                         } finally {\r
219                                 if (xvr != null)\r
220                                         xvr.Close ();\r
221                         }\r
222                 }\r
223 \r
224                 if (reportAsXml) {\r
225                         XmlReport.WriteEndElement ();\r
226                         XmlReport.Flush ();\r
227                 }\r
228 \r
229                 Console.WriteLine ("Finished: " + DateTime.Now);\r
230         }\r
231 \r
232         void Report (string id, bool compile, string category, string s)\r
233         {\r
234                 string phase = compile ? "compile" : "read";\r
235                 if (reportAsXml) {\r
236                         XmlReport.WriteStartElement ("testresult");\r
237                         XmlReport.WriteAttributeString ("id", id);\r
238                         XmlReport.WriteAttributeString ("phase", phase);\r
239                         XmlReport.WriteAttributeString ("category", category);\r
240                         XmlReport.WriteString (s);\r
241                         XmlReport.WriteEndElement ();\r
242                 }\r
243                 else\r
244                         ReportWriter.WriteLine ("{0}/{1} : {2} {3}",\r
245                                 phase, category, id, s);\r
246         }\r
247 }\r