Merge pull request #2323 from esdrubal/servicemodel
[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                 if (noResolver)\r
106                         doc.XmlResolver = null;\r
107                 doc.Load (basePath + subdir + SEP + "tests-all.xml");\r
108 \r
109                 if (reportAsXml) {\r
110                         XmlReport = new XmlTextWriter (ReportWriter);\r
111                         XmlReport.Formatting = Formatting.Indented;\r
112                         XmlReport.WriteStartElement ("test-results");\r
113                 }\r
114 \r
115                 Console.WriteLine ("Started:  " + DateTime.Now);\r
116 \r
117                 foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {\r
118                         // Test schema\r
119                         string schemaFile = test.SelectSingleNode ("@schema").InnerText;\r
120                         if (specificTarget != null &&\r
121                                 schemaFile.IndexOf (specificTarget) < 0)\r
122                                 continue;\r
123                         if (schemaFile.Length > 2)\r
124                                 schemaFile = schemaFile.Substring (2);\r
125                         if (verbose)\r
126                                 Report (schemaFile, true, "compiling", "");\r
127                         bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";\r
128                         XmlSchema schema = null;\r
129                         XmlTextReader sxr = null;\r
130                         try {\r
131                                 sxr = new XmlTextReader (basePath + schemaFile);\r
132                                 if (noResolver)\r
133                                         sxr.XmlResolver = null;\r
134                                 schema = XmlSchema.Read (sxr, null);\r
135                                 schema.Compile (noValidate ? noValidateHandler : null, noResolver ? null : new XmlUrlResolver ());\r
136                                 if (!isValidSchema && !noValidate) {\r
137                                         Report (schemaFile, true, "should fail", "");\r
138                                         continue;\r
139                                 }\r
140                                 if (reportSuccess)\r
141                                         Report (schemaFile, true, "OK", "");\r
142                         } catch (XmlSchemaException ex) {\r
143                                 if (isValidSchema)\r
144                                         Report (schemaFile, true, "should succeed", \r
145                                                 reportDetails ?\r
146                                                 ex.ToString () : ex.Message);\r
147                                 else if (reportSuccess)\r
148                                         Report (schemaFile, true, "OK", "");\r
149                                 continue;\r
150                         } catch (Exception ex) {\r
151                                 if (stopOnError)\r
152                                         throw;\r
153                                 Report (schemaFile, true, "unexpected",\r
154                                                 reportDetails ?\r
155                                                 ex.ToString () : ex.Message);\r
156                                 continue;\r
157                         } finally {\r
158                                 if (sxr != null)\r
159                                         sxr.Close ();\r
160                         }\r
161 \r
162                         // Test instances\r
163                         string instanceFile = test.SelectSingleNode ("@instance").InnerText;\r
164                         if (instanceFile.Length == 0)\r
165                                 continue;\r
166                         else if (instanceFile.StartsWith ("./"))\r
167                                 instanceFile = instanceFile.Substring (2);\r
168                         if (verbose)\r
169                                 Report (instanceFile, false, "reading ", "");\r
170                         bool isValidInstance = test.SelectSingleNode ("@out_x").InnerText == "1";\r
171                         XmlReader xvr = null;\r
172                         try {\r
173                                 XmlTextReader ixtr = new XmlTextReader (\r
174                                         Path.Combine (basePath, instanceFile));\r
175                                 xvr = ixtr;\r
176                                 if (version2) {\r
177                                         XmlReaderSettings settings =\r
178                                                 new XmlReaderSettings ();\r
179                                         settings.ValidationType = ValidationType.Schema;\r
180                                         if (noValidate)\r
181                                                 settings.ValidationEventHandler +=\r
182                                                         noValidateHandler;\r
183                                         if (noResolver)\r
184                                                 settings.Schemas.XmlResolver = null;\r
185                                         settings.Schemas.Add (schema);\r
186                                         if (noResolver)\r
187                                                 settings.XmlResolver = null;\r
188                                         xvr = XmlReader.Create (ixtr, settings);\r
189                                 } else {\r
190                                         XmlValidatingReader vr = new XmlValidatingReader (ixtr);\r
191                                         if (noResolver)\r
192                                                 vr.XmlResolver = null;\r
193                                         if (noValidate)\r
194                                                 vr.ValidationEventHandler += noValidateHandler;\r
195                                         vr.Schemas.Add (schema);\r
196                                         xvr = vr;\r
197                                 }\r
198                                 while (!xvr.EOF)\r
199                                         xvr.Read ();\r
200                                 if (!isValidInstance && !noValidate)\r
201                                         Report (instanceFile, false, "should fail", "");\r
202                                 else if (reportSuccess)\r
203                                         Report (instanceFile, false, "OK", "");\r
204                         } catch (XmlSchemaException ex) {\r
205                                 if (isValidInstance)\r
206                                         Report (instanceFile, false, "should succeed",\r
207                                                 reportDetails ?\r
208                                                 ex.ToString () : ex.Message);\r
209                                 else if (reportSuccess)\r
210                                         Report (instanceFile, false, "OK", "");\r
211                         } catch (Exception ex) {\r
212                                 if (stopOnError)\r
213                                         throw;\r
214                                 Report (instanceFile, false, "unexpected",\r
215                                         reportDetails ?\r
216                                         ex.ToString () : ex.Message);\r
217                         } finally {\r
218                                 if (xvr != null)\r
219                                         xvr.Close ();\r
220                         }\r
221                 }\r
222 \r
223                 if (reportAsXml) {\r
224                         XmlReport.WriteEndElement ();\r
225                         XmlReport.Flush ();\r
226                 }\r
227 \r
228                 Console.WriteLine ("Finished: " + DateTime.Now);\r
229         }\r
230 \r
231         void Report (string id, bool compile, string category, string s)\r
232         {\r
233                 string phase = compile ? "compile" : "read";\r
234                 if (reportAsXml) {\r
235                         XmlReport.WriteStartElement ("testresult");\r
236                         XmlReport.WriteAttributeString ("id", id);\r
237                         XmlReport.WriteAttributeString ("phase", phase);\r
238                         XmlReport.WriteAttributeString ("category", category);\r
239                         XmlReport.WriteString (s);\r
240                         XmlReport.WriteEndElement ();\r
241                 }\r
242                 else\r
243                         ReportWriter.WriteLine ("{0}/{1} : {2} {3}",\r
244                                 phase, category, id, s);\r
245         }\r
246 }\r