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