Add testcase for multiple missing IDs
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / XmlSchemaValidatorTests.cs
1 //
2 // XmlSchemaValidatorTests.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2008 Novell Inc.
8 //
9
10 #if NET_2_0
11
12 using System;
13 using System.Collections;
14 using System.IO;
15 using System.Xml;
16 using System.Xml.Schema;
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Xml
20 {
21         [TestFixture]
22         public class XmlSchemaValidatorTests
23         {
24                 void Validate (string xml, string xsd)
25                 {
26                         XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
27                         XmlReaderSettings settings = new XmlReaderSettings ();
28                         settings.ValidationType = ValidationType.Schema;
29                         settings.Schemas.Add (schema);
30                         XmlReader reader = XmlReader.Create (new StringReader (xml), settings);
31                         while (reader.Read ())
32                                 ;
33                 }
34
35                 [Test]
36                 public void XsdAnyToSkipAttributeValidation ()
37                 {
38                         // bug #358408
39                         XmlSchemaSet schemas = new XmlSchemaSet ();
40                         schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd");
41                         XmlSchemaValidator v = new XmlSchemaValidator (
42                                 new NameTable (),
43                                 schemas,
44                                 new XmlNamespaceManager (new NameTable ()),
45                                 XmlSchemaValidationFlags.ProcessIdentityConstraints);
46                         v.Initialize ();
47                         v.ValidateWhitespace (" ");
48                         XmlSchemaInfo info = new XmlSchemaInfo ();
49                         ArrayList list = new ArrayList ();
50
51                         v.ValidateElement ("configuration", "", info, null, null, null, null);
52                         v.GetUnspecifiedDefaultAttributes (list);
53                         v.ValidateEndOfAttributes (info);
54
55                         v.ValidateWhitespace (" ");
56
57                         v.ValidateElement ("host", "", info, null, null, null, null);
58                         v.ValidateAttribute ("auto-start", "", "true", info);
59                         list.Clear ();
60                         v.GetUnspecifiedDefaultAttributes (list);
61                         v.ValidateEndOfAttributes (info);
62                         v.ValidateEndElement (null);//info);
63
64                         v.ValidateWhitespace (" ");
65
66                         v.ValidateElement ("service-managers", "", info, null, null, null, null);
67                         list.Clear ();
68                         v.GetUnspecifiedDefaultAttributes (list);
69                         v.ValidateEndOfAttributes (info);
70
71                         v.ValidateWhitespace (" ");
72
73                         v.ValidateElement ("service-manager", "", info, null, null, null, null);
74                         list.Clear ();
75                         v.GetUnspecifiedDefaultAttributes (list);
76                         v.ValidateEndOfAttributes (info);
77
78                         v.ValidateWhitespace (" ");
79
80                         v.ValidateElement ("foo", "", info, null, null, null, null);
81                         v.ValidateAttribute ("bar", "", "", info);
82                 }
83
84                 [Test]
85                 public void SkipInvolved () // bug #422581
86                 {
87                         XmlReader schemaReader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xsd");
88                         XmlSchema schema = XmlSchema.Read (schemaReader, null);
89                         XmlReaderSettings settings = new XmlReaderSettings ();
90                         settings.ValidationType = ValidationType.Schema;
91                         settings.Schemas.Add (schema);
92                         XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xml", settings);
93                         while (reader.Read ());
94                 }
95
96                 [Test]
97                 public void Bug433774 ()
98                 {
99                         string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
100   <xs:element name='Root'>
101     <xs:complexType>
102       <xs:sequence></xs:sequence>
103       <xs:attribute name='version' type='xs:string' fixed='3' />
104     </xs:complexType>
105   </xs:element>
106 </xs:schema>";
107                         XmlDocument doc = new XmlDocument ();
108                         doc.LoadXml ("<Root version='3' xmlns='urn:foo'/>");
109                         XmlSchemaSet schemaSet = new XmlSchemaSet();
110                         schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
111                         doc.Schemas = schemaSet;
112                         XmlNode root = doc.DocumentElement;
113                         doc.Validate (null, root);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (XmlSchemaValidationException))]
118                 public void Bug435206 ()
119                 {
120                         string xsd = @"<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
121   <xs:element name='myDoc'>
122     <xs:complexType>
123       <xs:attribute name='foo' type='xs:unsignedLong' use='required' />
124       <xs:attribute name='bar' type='xs:dateTime' use='required' />
125     </xs:complexType>
126   </xs:element>
127 </xs:schema>";
128                         string xml = @"<myDoc foo='12' bar='January 1st 1900'/>";
129                         Validate (xml, xsd);
130                 }
131
132                 [Test]
133                 public void Bug469713 ()
134                 {
135                         string xsd = @"<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
136   <xs:element name='Message'>
137     <xs:complexType>
138       <xs:all>
139         <xs:element name='MyDateTime' nillable='true' type='xs:dateTime' />
140       </xs:all>
141     </xs:complexType>
142   </xs:element>
143 </xs:schema>";
144                         string xml = @"<Message xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='test.xsd'>
145         <MyDateTime xsi:nil='true'></MyDateTime>
146 </Message>";
147                         Validate (xml, xsd);
148                 }
149
150                 [Test]
151                 public void Bug496192_496205 ()
152                 {
153                         using (var xmlr = new StreamReader ("Test/XmlFiles/496192.xml"))
154                                 using (var xsdr = new StreamReader ("Test/XmlFiles/496192.xsd"))
155                                         Validate (xmlr.ReadToEnd (), xsdr.ReadToEnd ());
156                 }
157                 
158                 [Test]          
159                 public void Bug501666 ()
160                 {
161                         string xsd = @"
162                         <xs:schema id='Settings'
163                                 targetNamespace='foo'                
164                                 xmlns='foo'
165                                 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
166
167                                 <xs:element name='Settings' type='Settings'/>
168
169                                 <xs:complexType name='Settings'>
170                                         <xs:attribute name='port' type='PortNumber' use='required'/>
171                                 </xs:complexType>
172                 
173                                 <xs:simpleType name='PortNumber'>
174                                         <xs:restriction base='xs:positiveInteger'>
175                                                 <xs:minInclusive value='1'/>
176                                                 <xs:maxInclusive value='65535'/>
177                                         </xs:restriction>
178                                 </xs:simpleType>
179                         </xs:schema>";
180
181                         string xml = @"<Settings port='1337' xmlns='foo'/>";
182
183                         XmlDocument doc = new XmlDocument ();
184                         doc.LoadXml (xml);
185                         doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
186                         doc.Validate (null);
187                 }
188
189                 public void Bug502251 ()
190                 {
191                         string xsd = @"
192    <xs:schema id='foo' targetNamespace='foo' 
193      elementFormDefault='qualified' 
194      xmlns='foo'     
195      xmlns:xs='http://www.w3.org/2001/XMLSchema'>
196
197  <xs:group name='LayoutElementTypes'>
198   <xs:choice>   
199    <xs:element name='Rows' type='Rows' />
200    <xs:element name='Conditional' type='Conditional' />   
201   </xs:choice>
202  </xs:group>
203
204  <xs:complexType name='Element' abstract='true'>
205   <xs:attribute name='id' type='xs:ID' use='optional'/>
206  </xs:complexType>
207
208  <xs:complexType name='SingleChildElement' abstract='true'>
209   <xs:complexContent>
210    <xs:extension base='Element'>
211     <xs:group ref='LayoutElementTypes' minOccurs='1' maxOccurs='1' />
212    </xs:extension>
213   </xs:complexContent>
214  </xs:complexType>
215
216  <xs:complexType name='Rows'>
217   <xs:complexContent>
218    <xs:extension base='Element'>
219     <xs:sequence minOccurs='1' maxOccurs='unbounded'>
220      <xs:element name='Row' type='Row' />
221     </xs:sequence>    
222          </xs:extension>
223   </xs:complexContent>
224  </xs:complexType> 
225
226    <xs:complexType name='Row'>
227   <xs:complexContent>
228    <xs:extension base='SingleChildElement'>    
229    </xs:extension>    
230   </xs:complexContent>
231  </xs:complexType>
232
233  <xs:complexType name='Conditional'>
234   <xs:complexContent>
235    <xs:extension base='Element'>    
236    </xs:extension>
237   </xs:complexContent>
238  </xs:complexType>
239
240  <xs:complexType name='Layout'>
241   <xs:complexContent>
242    <xs:extension base='SingleChildElement'>
243    </xs:extension>
244   </xs:complexContent>
245  </xs:complexType>
246
247  <xs:element name='Layout' type='Layout' />
248 </xs:schema>";
249
250                         XmlDocument doc = new XmlDocument ();
251                         doc.LoadXml (@"<Layout xmlns='foo'>
252   <Rows>
253     <Row><Conditional/></Row>     
254   </Rows>
255 </Layout>");
256
257                         XmlSchema schema = XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null);
258
259                         doc.Schemas.Add (schema);
260                         doc.Validate (null);
261                 }
262
263                 [Test]
264                 public void Bug557452 ()
265                 {
266                         string xsd = @"
267                         <xs:schema id='Settings'
268                                 targetNamespace='foo'
269                                 xmlns='foo'
270                                 xmlns:xs='http://www.w3.org/2001/XMLSchema'>
271
272                                 <xs:element name='Settings' type='Settings'/>
273
274                                 <xs:complexType name='Settings'>
275                                         <xs:attribute name='port' type='PortNumber' use='required'/>
276                                 </xs:complexType>
277
278                                 <xs:simpleType name='PortNumber'>
279                                         <xs:restriction base='xs:decimal'>
280                                                 <xs:minInclusive value='1'/>
281                                                 <xs:maxInclusive value='65535'/>
282                                         </xs:restriction>
283                                 </xs:simpleType>
284                         </xs:schema>";
285
286                         string xml = @"<Settings port='1337' xmlns='foo'/>";
287
288                         XmlDocument doc = new XmlDocument ();
289                         doc.LoadXml (xml);
290                         doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
291                         doc.Validate (null);
292                 }
293
294                 [Test]
295                 public void Bug584664 ()
296                 {
297                         Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664a.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664a.xsd"));
298                         Validate (File.ReadAllText ("Test/XmlFiles/xsd/584664b.xml"), File.ReadAllText ("Test/XmlFiles/xsd/584664b.xsd"));
299                 }
300
301                 [Test]
302                 public void MultipleMissingIds ()
303                 {
304                         var schema = XmlSchema.Read (new StringReader (@"<?xml version=""1.0"" encoding=""utf-8""?>
305 <xs:schema targetNamespace=""urn:multiple-ids"" elementFormDefault=""qualified"" xmlns=""urn:multiple-ids"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
306         <xs:element name=""root"">
307                 <xs:complexType>
308                         <xs:sequence minOccurs=""0"" maxOccurs=""unbounded"">
309                                 <xs:element name=""item"">
310                                         <xs:complexType>
311                                                 <xs:attribute name=""id"" type=""xs:ID"" />
312                                                 <xs:attribute name=""parent"" type=""xs:IDREF"" />
313                                         </xs:complexType>
314                                 </xs:element>
315                         </xs:sequence>
316                 </xs:complexType>
317         </xs:element>
318 </xs:schema>"), null);
319                         var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
320 <root xmlns=""urn:multiple-ids"">
321         <item id=""id2"" parent=""id1"" />
322         <item id=""id3"" parent=""id1"" />
323         <item id=""id1"" parent=""id1"" />
324 </root>";
325                         var document = new XmlDocument ();
326                         document.LoadXml (xml);
327                         document.Schemas = new XmlSchemaSet ();
328                         document.Schemas.Add (schema);
329                         document.Validate (null);
330                 }
331         }
332 }
333
334 #endif