2 // System.Xml.XmlSchemaSetTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2004 Novell Inc.
12 using System.Collections;
16 using System.Xml.Schema;
17 using NUnit.Framework;
19 namespace MonoTests.System.Xml
22 public class XmlSchemaSetTests
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
33 // same document, different targetNamespace
34 ss.Add ("ab", new XmlNodeReader (doc));
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));
40 Assert.AreEqual (3, ss.Count);
42 bool chameleon = false;
46 foreach (XmlSchema schema in ss.Schemas ()) {
47 if (schema.TargetNamespace == null)
49 else if (schema.TargetNamespace == "ab")
51 else if (schema.TargetNamespace == "urn:foo")
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");
60 [ExpectedException (typeof (XmlSchemaException))]
61 public void AddWrongTargetNamespace ()
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)));
72 public void AddSchemaThenReader ()
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";
80 ss.Add ("ab", new XmlNodeReader (doc));
84 [Category ("NotWorking")] // How can we differentiate this
85 // case and the testcase above?
86 [ExpectedException (typeof (ArgumentException))]
87 public void AddReaderTwice ()
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));
97 public void AddSchemaTwice ()
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));
107 public void CompilationSettings ()
109 Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
110 new XmlSchemaSet ().CompilationSettings = null;
114 public void DisableUpaCheck ()
116 string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
117 <xs:complexType name='Foo'>
119 <xs:choice minOccurs='0'>
120 <xs:element name='el'/>
122 <xs:element name='el' />
126 XmlSchema xs = XmlSchema.Read (new XmlTextReader (
127 schema, XmlNodeType.Document, null), null);
128 XmlSchemaSet xss = new XmlSchemaSet ();
130 xss.CompilationSettings.EnableUpaCheck = false;
136 public void AddRollbackIsCompiled ()
138 XmlSchemaSet ss = new XmlSchemaSet ();
139 ss.Add (new XmlSchema ());
141 Assert.IsTrue (ss.IsCompiled, "#1");
142 XmlSchema sc = new XmlSchema (); // compiled one
145 Assert.IsFalse (ss.IsCompiled, "#2");
146 ss.Add (new XmlSchema ()); // not-compiled one
147 Assert.IsFalse (ss.IsCompiled, "#3");
150 s = new XmlSchema ();
151 s.TargetNamespace = "urn:foo";
153 el = new XmlSchemaElement ();
158 s = new XmlSchema ();
159 s.TargetNamespace = "urn:foo";
160 el = new XmlSchemaElement ();
165 Assert.IsTrue (ss.IsCompiled, "#4");
166 ss.RemoveRecursive (s);
167 Assert.IsTrue (ss.IsCompiled, "#5");
171 public void CrossSchemaReferences ()
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""
177 <xsd:attribute name=""type"" type=""xsd:string""
182 string schema2 = @"<xsd:schema id=""Sub.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
183 <xsd:complexType name=""item"">
185 <xsd:extension base=""itemBase"">
186 <xsd:attribute name=""itemName""
187 type=""xsd:string"" use=""required""/>
189 </xsd:complexContent>
192 XmlSchemaSet schemas = new XmlSchemaSet ();
193 schemas.Add (XmlSchema.Read (new StringReader (schema1), null));
194 schemas.Add (XmlSchema.Read (new StringReader (schema2), null));
199 public void ImportSubstitutionGroupDBR ()
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)
215 public void AddWithNullTargetNS () // bug #571650
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");
223 [Test] // part of bug #670945
224 public void TwoSchemasInSameDocumentUri ()
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'>
233 <xs:complexType name='SystemDateTime'>
235 <xs:element name='foobar' type='xs:string' minOccurs='0' />
236 <xs:element name='Extension' type='tt:SystemDateTimeExtension' minOccurs='0'/>
238 <!-- xs:anyAttribute processContents='lax'/ -->
241 <xs:complexType name='SystemDateTimeExtension'>
243 <xs:any namespace='##any' processContents='lax' minOccurs='0' maxOccurs='unbounded'/>
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'>
262 <xs:element name='GetSystemDateAndTimeResponse'>
265 <xs:element name='SystemDateAndTime' type='tt:SystemDateTime' />
271 var xss = new XmlSchemaSet ();
272 var xs1 = XmlSchema.Read (new StringReader (xsd1), null);
273 xs1.SourceUri = "http://localhost:8080/dummy.wsdl";
276 var xs2 = XmlSchema.Read (new StringReader (xsd2), null);
277 xs2.SourceUri = "http://localhost:8080/dummy.wsdl";
281 Assert.IsNotNull (xss.GlobalElements [new XmlQualifiedName ("GetSystemDateAndTimeResponse", "http://www.onvif.org/ver10/device/wsdl")], "#1");
285 public void ResolveSchemaUriUsingXmlResolver ()
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";
292 var readerSettings = new XmlReaderSettings ();
294 //readerSettings.XmlResolver = resolver;
295 readerSettings.Schemas.XmlResolver = resolver;
296 readerSettings.Schemas.Add (ns, xsdPath);
297 readerSettings.ValidationType = ValidationType.Schema;
299 using (var xr = XmlReader.Create (new StringReader (xml), readerSettings))
306 public class Bug13716XmlResolver : XmlUrlResolver
308 public override object GetEntity(Uri absoluteUri, string role, Type typeOfObjectToReturn)
311 <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='testschema'>
312 <xs:element name='people' />
314 return new MemoryStream (Encoding.UTF8.GetBytes (xsd));