2 // System.Xml.XmlSchemaSetTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2004 Novell Inc.
11 using System.Collections;
15 using System.Xml.Schema;
16 using NUnit.Framework;
18 namespace MonoTests.System.Xml
21 public class XmlSchemaSetTests
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
32 // same document, different targetNamespace
33 ss.Add ("ab", new XmlNodeReader (doc));
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));
39 Assert.AreEqual (3, ss.Count);
41 bool chameleon = false;
45 foreach (XmlSchema schema in ss.Schemas ()) {
46 if (schema.TargetNamespace == null)
48 else if (schema.TargetNamespace == "ab")
50 else if (schema.TargetNamespace == "urn:foo")
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");
59 [ExpectedException (typeof (XmlSchemaException))]
60 public void AddWrongTargetNamespace ()
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)));
71 public void AddSchemaThenReader ()
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";
79 ss.Add ("ab", new XmlNodeReader (doc));
83 [Category ("NotWorking")] // How can we differentiate this
84 // case and the testcase above?
85 [ExpectedException (typeof (ArgumentException))]
86 public void AddReaderTwice ()
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));
96 public void AddSchemaTwice ()
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));
106 public void CompilationSettings ()
108 Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
109 new XmlSchemaSet ().CompilationSettings = null;
113 public void DisableUpaCheck ()
115 string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
116 <xs:complexType name='Foo'>
118 <xs:choice minOccurs='0'>
119 <xs:element name='el'/>
121 <xs:element name='el' />
125 XmlSchema xs = XmlSchema.Read (new XmlTextReader (
126 schema, XmlNodeType.Document, null), null);
127 XmlSchemaSet xss = new XmlSchemaSet ();
129 xss.CompilationSettings.EnableUpaCheck = false;
135 public void AddRollbackIsCompiled ()
137 XmlSchemaSet ss = new XmlSchemaSet ();
138 ss.Add (new XmlSchema ());
140 Assert.IsTrue (ss.IsCompiled, "#1");
141 XmlSchema sc = new XmlSchema (); // compiled one
144 Assert.IsFalse (ss.IsCompiled, "#2");
145 ss.Add (new XmlSchema ()); // not-compiled one
146 Assert.IsFalse (ss.IsCompiled, "#3");
149 s = new XmlSchema ();
150 s.TargetNamespace = "urn:foo";
152 el = new XmlSchemaElement ();
157 s = new XmlSchema ();
158 s.TargetNamespace = "urn:foo";
159 el = new XmlSchemaElement ();
164 Assert.IsTrue (ss.IsCompiled, "#4");
165 ss.RemoveRecursive (s);
166 Assert.IsTrue (ss.IsCompiled, "#5");
170 public void CrossSchemaReferences ()
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""
176 <xsd:attribute name=""type"" type=""xsd:string""
181 string schema2 = @"<xsd:schema id=""Sub.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
182 <xsd:complexType name=""item"">
184 <xsd:extension base=""itemBase"">
185 <xsd:attribute name=""itemName""
186 type=""xsd:string"" use=""required""/>
188 </xsd:complexContent>
191 XmlSchemaSet schemas = new XmlSchemaSet ();
192 schemas.Add (XmlSchema.Read (new StringReader (schema1), null));
193 schemas.Add (XmlSchema.Read (new StringReader (schema2), null));
198 public void ImportSubstitutionGroupDBR ()
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)
214 public void AddWithNullTargetNS () // bug #571650
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");
222 [Test] // part of bug #670945
223 public void TwoSchemasInSameDocumentUri ()
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'>
232 <xs:complexType name='SystemDateTime'>
234 <xs:element name='foobar' type='xs:string' minOccurs='0' />
235 <xs:element name='Extension' type='tt:SystemDateTimeExtension' minOccurs='0'/>
237 <!-- xs:anyAttribute processContents='lax'/ -->
240 <xs:complexType name='SystemDateTimeExtension'>
242 <xs:any namespace='##any' processContents='lax' minOccurs='0' maxOccurs='unbounded'/>
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'>
261 <xs:element name='GetSystemDateAndTimeResponse'>
264 <xs:element name='SystemDateAndTime' type='tt:SystemDateTime' />
270 var xss = new XmlSchemaSet ();
271 var xs1 = XmlSchema.Read (new StringReader (xsd1), null);
272 xs1.SourceUri = "http://localhost:8080/dummy.wsdl";
275 var xs2 = XmlSchema.Read (new StringReader (xsd2), null);
276 xs2.SourceUri = "http://localhost:8080/dummy.wsdl";
280 Assert.IsNotNull (xss.GlobalElements [new XmlQualifiedName ("GetSystemDateAndTimeResponse", "http://www.onvif.org/ver10/device/wsdl")], "#1");
284 public void ResolveSchemaUriUsingXmlResolver ()
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";
291 var readerSettings = new XmlReaderSettings ();
293 //readerSettings.XmlResolver = resolver;
294 readerSettings.Schemas.XmlResolver = resolver;
295 readerSettings.Schemas.Add (ns, xsdPath);
296 readerSettings.ValidationType = ValidationType.Schema;
298 using (var xr = XmlReader.Create (new StringReader (xml), readerSettings))
305 public class Bug13716XmlResolver : XmlUrlResolver
307 public override object GetEntity(Uri absoluteUri, string role, Type typeOfObjectToReturn)
310 <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='testschema'>
311 <xs:element name='people' />
313 return new MemoryStream (Encoding.UTF8.GetBytes (xsd));