Merge pull request #823 from DavidKarlas/master
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / XmlSchemaSetTests.cs
1 //
2 // System.Xml.XmlSchemaSetTests.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2004 Novell Inc.
8 //
9 #if NET_2_0
10
11 using System;
12 using System.Collections;
13 using System.IO;
14 using System.Text;
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 XmlSchemaSetTests
23         {
24                 [Test]
25                 public void Add ()
26                 {
27                         XmlSchemaSet ss = new XmlSchemaSet ();
28                         XmlDocument doc = new XmlDocument ();
29                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
30                         ss.Add (null, new XmlNodeReader (doc)); // null targetNamespace
31                         ss.Compile ();
32
33                         // same document, different targetNamespace
34                         ss.Add ("ab", new XmlNodeReader (doc));
35
36                         // Add(null, xmlReader) -> targetNamespace in the schema
37                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo' />");
38                         ss.Add (null, new XmlNodeReader (doc));
39
40                         Assert.AreEqual (3, ss.Count);
41
42                         bool chameleon = false;
43                         bool ab = false;
44                         bool urnfoo = false;
45
46                         foreach (XmlSchema schema in ss.Schemas ()) {
47                                 if (schema.TargetNamespace == null)
48                                         chameleon = true;
49                                 else if (schema.TargetNamespace == "ab")
50                                         ab = true;
51                                 else if (schema.TargetNamespace == "urn:foo")
52                                         urnfoo = true;
53                         }
54                         Assert.IsTrue (chameleon, "chameleon schema missing");
55                         Assert.IsTrue (ab, "target-remapped schema missing");
56                         Assert.IsTrue (urnfoo, "target specified in the schema ignored");
57                 }
58
59                 [Test]
60                 [ExpectedException (typeof (XmlSchemaException))]
61                 public void AddWrongTargetNamespace ()
62                 {
63                         string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='el' type='xs:int' /></xs:schema>";
64                         string xml = "<el xmlns='urn:foo'>a</el>";
65                         XmlSchemaSet xss = new XmlSchemaSet ();
66                         // unlike null, "" is regarded as an explicit
67                         // empty namespace indication.
68                         xss.Add ("", new XmlTextReader (new StringReader (xsd)));
69                 }
70
71                 [Test]
72                 public void AddSchemaThenReader ()
73                 {
74                         XmlSchemaSet ss = new XmlSchemaSet ();
75                         XmlDocument doc = new XmlDocument ();
76                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
77                         XmlSchema xs = new XmlSchema ();
78                         xs.TargetNamespace = "ab";
79                         ss.Add (xs);
80                         ss.Add ("ab", new XmlNodeReader (doc));
81                 }
82
83                 [Test]
84                 [Category ("NotWorking")] // How can we differentiate this
85                 // case and the testcase above?
86                 [ExpectedException (typeof (ArgumentException))]
87                 public void AddReaderTwice ()
88                 {
89                         XmlSchemaSet ss = new XmlSchemaSet ();
90                         XmlDocument doc = new XmlDocument ();
91                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
92                         ss.Add ("ab", new XmlNodeReader (doc));
93                         ss.Add ("ab", new XmlNodeReader (doc));
94                 }
95
96                 [Test]
97                 public void AddSchemaTwice ()
98                 {
99                         XmlSchemaSet ss = new XmlSchemaSet ();
100                         XmlDocument doc = new XmlDocument ();
101                         doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:ab' />");
102                         ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
103                         ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
104                 }
105
106                 [Test]
107                 public void CompilationSettings ()
108                 {
109                         Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
110                         new XmlSchemaSet ().CompilationSettings = null;
111                 }
112
113                 [Test]
114                 public void DisableUpaCheck ()
115                 {
116                         string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
117   <xs:complexType name='Foo'>
118     <xs:sequence>
119       <xs:choice minOccurs='0'>
120         <xs:element name='el'/>
121       </xs:choice>
122       <xs:element name='el' />
123     </xs:sequence>
124   </xs:complexType>
125 </xs:schema>";
126                         XmlSchema xs = XmlSchema.Read (new XmlTextReader (
127                                 schema, XmlNodeType.Document, null), null);
128                         XmlSchemaSet xss = new XmlSchemaSet ();
129                         xss.Add (xs);
130                         xss.CompilationSettings.EnableUpaCheck = false;
131
132                         xss.Compile ();
133                 }
134
135                 [Test]
136                 public void AddRollbackIsCompiled ()
137                 {
138                         XmlSchemaSet ss = new XmlSchemaSet ();
139                         ss.Add (new XmlSchema ());
140                         ss.Compile ();
141                         Assert.IsTrue (ss.IsCompiled, "#1");
142                         XmlSchema sc = new XmlSchema (); // compiled one
143                         sc.Compile (null);
144                         ss.Add (sc);
145                         Assert.IsFalse (ss.IsCompiled, "#2");
146                         ss.Add (new XmlSchema ()); // not-compiled one
147                         Assert.IsFalse (ss.IsCompiled, "#3");
148                         XmlSchema s;
149
150                         s = new XmlSchema ();
151                         s.TargetNamespace = "urn:foo";
152                         XmlSchemaElement el;
153                         el = new XmlSchemaElement ();
154                         el.Name = "root";
155                         s.Items.Add (el);
156                         ss.Add (s);
157
158                         s = new XmlSchema ();
159                         s.TargetNamespace = "urn:foo";
160                         el = new XmlSchemaElement ();
161                         el.Name = "foo";
162                         s.Items.Add (el);
163                         ss.Add (s);
164                         ss.Compile ();
165                         Assert.IsTrue (ss.IsCompiled, "#4");
166                         ss.RemoveRecursive (s);
167                         Assert.IsTrue (ss.IsCompiled, "#5");
168                 }
169
170                 [Test] // bug #77489
171                 public void CrossSchemaReferences ()
172                 {
173                         string schema1 = @"<xsd:schema id=""Base.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
174         <xsd:complexType name=""itemBase"" abstract=""true""> 
175                 <xsd:attribute name=""id"" type=""xsd:string""
176 use=""required""/> 
177                 <xsd:attribute name=""type"" type=""xsd:string""
178 use=""required""/> 
179         </xsd:complexType> 
180 </xsd:schema>";
181
182                         string schema2 = @"<xsd:schema id=""Sub.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
183         <xsd:complexType name=""item""> 
184                 <xsd:complexContent> 
185                         <xsd:extension base=""itemBase""> 
186                                 <xsd:attribute name=""itemName""
187 type=""xsd:string"" use=""required""/> 
188                         </xsd:extension> 
189                 </xsd:complexContent> 
190         </xsd:complexType> 
191 </xsd:schema>";
192                         XmlSchemaSet schemas = new XmlSchemaSet ();
193                         schemas.Add (XmlSchema.Read (new StringReader (schema1), null));
194                         schemas.Add (XmlSchema.Read (new StringReader (schema2), null));
195                         schemas.Compile ();
196                 }
197
198                 [Test]
199                 public void ImportSubstitutionGroupDBR ()
200                 {
201                         // This bug happened when
202                         // 1) a schema imports another schema,
203                         // 2) there is a substitutionGroup which is involved in
204                         //    complexContent schema conformance check, and
205                         // 3) the included schema is already added to XmlSchemaSet.
206                         XmlSchemaSet xss = new XmlSchemaSet ();
207                         xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-base.xsd");
208                         xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-ext.xsd");
209                         // should not result in lack of substitutionGroup
210                         // (and conformance error as its result)
211                         xss.Compile ();
212                 }
213
214                 [Test]
215                 public void AddWithNullTargetNS () // bug #571650
216                 {
217                         var xsdraw = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='foo' /></xs:schema>";
218                         var schemas = new XmlSchemaSet ();
219                         var xsd = schemas.Add ("", XmlReader.Create (new StringReader (xsdraw)));
220                         Assert.IsNull (xsd.TargetNamespace, "#1");
221                 }
222
223                 [Test] // part of bug #670945
224                 public void TwoSchemasInSameDocumentUri ()
225                 {
226                         string xsd1 = @"
227     <xs:schema
228     targetNamespace='http://www.onvif.org/ver10/schema'
229     elementFormDefault='qualified'
230     xmlns:xs='http://www.w3.org/2001/XMLSchema'
231     xmlns:tt='http://www.onvif.org/ver10/schema'>
232
233       <xs:complexType name='SystemDateTime'>
234         <xs:sequence>
235           <xs:element name='foobar' type='xs:string' minOccurs='0' />
236           <xs:element name='Extension' type='tt:SystemDateTimeExtension' minOccurs='0'/>
237         </xs:sequence>
238         <!-- xs:anyAttribute processContents='lax'/ -->
239       </xs:complexType>
240
241       <xs:complexType name='SystemDateTimeExtension'>
242         <xs:sequence>
243           <xs:any namespace='##any' processContents='lax' minOccurs='0' maxOccurs='unbounded'/>
244         </xs:sequence>
245       </xs:complexType>
246
247     </xs:schema>";
248
249                         string xsd2 = @"
250     <xs:schema
251       targetNamespace='http://www.onvif.org/ver10/device/wsdl'
252       xmlns:xs='http://www.w3.org/2001/XMLSchema'
253       xmlns:tt='http://www.onvif.org/ver10/schema'
254       xmlns:tds='http://www.onvif.org/ver10/device/wsdl' 
255       elementFormDefault='qualified'>
256       <xs:element name='GetSystemDateAndTime'>
257         <xs:complexType>
258           <xs:sequence/>
259
260         </xs:complexType>
261       </xs:element>
262       <xs:element name='GetSystemDateAndTimeResponse'>
263         <xs:complexType>
264           <xs:sequence>
265             <xs:element name='SystemDateAndTime' type='tt:SystemDateTime' />
266           </xs:sequence>
267         </xs:complexType>
268       </xs:element>
269     </xs:schema>";
270
271                         var xss = new XmlSchemaSet ();
272                         var xs1 = XmlSchema.Read (new StringReader (xsd1), null);
273                         xs1.SourceUri = "http://localhost:8080/dummy.wsdl";
274                         xs1.LineNumber = 5;
275                         xss.Add (xs1);
276                         var xs2 = XmlSchema.Read (new StringReader (xsd2), null);
277                         xs2.SourceUri = "http://localhost:8080/dummy.wsdl";
278                         xs2.LineNumber = 50;
279                         xss.Add (xs2);
280                         xss.Compile ();
281                         Assert.IsNotNull (xss.GlobalElements [new XmlQualifiedName ("GetSystemDateAndTimeResponse", "http://www.onvif.org/ver10/device/wsdl")], "#1");
282                 }
283                 
284                 [Test] // bug #13716
285                 public void ResolveSchemaUriUsingXmlResolver ()
286                 {
287                         var resolver = new Bug13716XmlResolver ();
288                         string xml = "<people xmlns='testschema'><person name='Ian'><books><book>Clean Code</book></books></person></people>";
289                         string ns = "testschema";
290                         string xsdPath = "my.xsd";
291
292                         var readerSettings = new XmlReaderSettings ();
293
294                         //readerSettings.XmlResolver = resolver;
295                         readerSettings.Schemas.XmlResolver = resolver;
296                         readerSettings.Schemas.Add (ns, xsdPath);
297                         readerSettings.ValidationType = ValidationType.Schema;
298
299                         using (var xr = XmlReader.Create (new StringReader (xml), readerSettings))
300                         {
301                                 while (!xr.EOF)
302                                         xr.Read ();
303                         }
304                 }
305                 
306                 public class Bug13716XmlResolver : XmlUrlResolver
307                 {
308                         public override object GetEntity(Uri absoluteUri, string role, Type typeOfObjectToReturn)
309                         {
310                                 string xsd = @"
311                 <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='testschema'>
312                   <xs:element name='people' />
313                 </xs:schema>";
314                                 return new MemoryStream (Encoding.UTF8.GetBytes (xsd));
315                         }
316                 }
317         }
318 }
319 #endif