2 // System.Xml.XmlSchemaTests.cs
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 // (C) 2002 Atsushi Enomoto
14 using System.Xml.Schema;
15 using System.Xml.Serialization;
16 using NUnit.Framework;
18 namespace MonoTests.System.Xml
21 public class XmlSchemaTests : XmlSchemaAssertion
23 static readonly bool StrictMsCompliant = Environment.GetEnvironmentVariable ("MONO_STRICT_MS_COMPLIANT") == "yes";
26 public void TestRead ()
28 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
29 Assert.AreEqual (6, schema.Items.Count);
31 bool fooValidated = false;
32 bool barValidated = false;
33 string ns = "urn:bar";
35 foreach (XmlSchemaObject obj in schema.Items) {
36 XmlSchemaElement element = obj as XmlSchemaElement;
39 if (element.Name == "Foo") {
40 AssertElement (element, "Foo",
41 XmlQualifiedName.Empty, null,
42 QName ("string", XmlSchema.Namespace), null);
45 if (element.Name == "Bar") {
46 AssertElement (element, "Bar",
47 XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
51 Assert.IsTrue (fooValidated);
52 Assert.IsTrue (barValidated);
56 public void TestReadFlags ()
58 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
59 schema.Compile (null);
60 XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
61 Assert.IsNotNull (el);
62 Assert.AreEqual (XmlSchemaDerivationMethod.Extension, el.Block);
64 el = schema.Items [1] as XmlSchemaElement;
65 Assert.IsNotNull (el);
66 Assert.AreEqual (XmlSchemaDerivationMethod.Extension |
67 XmlSchemaDerivationMethod.Restriction, el.Block);
71 public void TestWriteFlags ()
73 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
74 StringWriter sw = new StringWriter ();
75 XmlTextWriter xtw = new XmlTextWriter (sw);
80 public void TestCompile ()
82 XmlQualifiedName qname;
83 XmlSchemaComplexContentExtension xccx;
84 XmlSchemaComplexType cType;
85 XmlSchemaSequence seq;
87 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
88 // Assert.IsTrue (!schema.IsCompiled);
89 schema.Compile (null);
90 Assert.IsTrue (schema.IsCompiled);
91 string ns = "urn:bar";
93 XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
94 Assert.IsNotNull (foo);
95 XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
96 Assert.IsNotNull (stringDatatype);
99 qname = QName ("HogeType", ns);
100 cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
101 Assert.IsNotNull (cType);
102 Assert.IsNull (cType.ContentModel);
103 AssertCompiledComplexType (cType, qname, 0, 0,
104 false, null, true, XmlSchemaContentType.ElementOnly);
105 seq = cType.ContentTypeParticle as XmlSchemaSequence;
106 Assert.IsNotNull (seq);
107 Assert.AreEqual (2, seq.Items.Count);
108 XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
109 AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
112 qname = QName ("FugaType", ns);
113 cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
114 Assert.IsNotNull (cType);
115 xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
116 AssertCompiledComplexContentExtension (
117 xccx, 0, false, QName ("HogeType", ns));
119 AssertCompiledComplexType (cType, qname, 0, 0,
120 false, typeof (XmlSchemaComplexContent),
121 true, XmlSchemaContentType.ElementOnly);
122 Assert.IsNotNull (cType.BaseSchemaType);
124 seq = xccx.Particle as XmlSchemaSequence;
125 Assert.IsNotNull (seq);
126 Assert.AreEqual (1, seq.Items.Count);
127 XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
128 Assert.IsNotNull (refBaz);
129 AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
131 qname = QName ("Bar", ns);
132 XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
133 AssertCompiledElement (element, qname, cType);
137 [ExpectedException (typeof (XmlSchemaException))]
138 public void TestCompile_ZeroLength_TargetNamespace ()
140 XmlSchema schema = new XmlSchema ();
141 schema.TargetNamespace = string.Empty;
142 Assert.IsTrue (!schema.IsCompiled);
144 // MS.NET 1.x: The Namespace '' is an invalid URI.
145 // MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value.
146 schema.Compile (null);
150 [ExpectedException (typeof (XmlSchemaException))]
151 public void TestCompileNonSchema ()
153 XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
154 XmlSchema schema = XmlSchema.Read (xtr, null);
159 public void TestSimpleImport ()
161 XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
162 Assert.AreEqual ("urn:foo", schema.TargetNamespace);
163 XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
164 Assert.IsNotNull (import);
166 schema.Compile (null);
167 Assert.AreEqual (4, schema.Elements.Count);
168 Assert.IsNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
169 Assert.IsNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
170 Assert.IsNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
171 Assert.IsNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
176 [Category ("MobileNotWorking")]
177 public void TestSimpleMutualImport ()
179 XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
181 XmlSchema.Read (r, null).Compile (null);
188 public void TestQualification ()
190 XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
191 schema.Compile (null);
192 XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
193 Assert.IsNotNull (el);
194 XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
195 XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
196 XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
197 Assert.AreEqual (QName ("Bar", ""), elp.QualifiedName);
199 schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
200 schema.Compile (null);
201 el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
202 Assert.IsNotNull (el);
203 ct = el.ElementType as XmlSchemaComplexType;
204 seq = ct.ContentTypeParticle as XmlSchemaSequence;
205 elp = seq.Items [0] as XmlSchemaElement;
206 Assert.AreEqual (QName ("Bar", "urn:bar"), elp.QualifiedName);
210 public void TestWriteNamespaces ()
212 XmlDocument doc = new XmlDocument ();
218 xs = new XmlSchema ();
219 sw = new StringWriter ();
220 xw = new XmlTextWriter (sw);
222 doc.LoadXml (sw.ToString ());
223 Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#1");
226 xs = new XmlSchema ();
227 sw = new StringWriter ();
228 xw = new XmlTextWriter (sw);
229 xs.TargetNamespace = "urn:foo";
231 doc.LoadXml (sw.ToString ());
232 Assert.AreEqual ("<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2");
234 // Zero-length TargetNamespace
235 xs = new XmlSchema ();
236 sw = new StringWriter ();
237 xw = new XmlTextWriter (sw);
238 xs.TargetNamespace = string.Empty;
240 doc.LoadXml (sw.ToString ());
241 Assert.AreEqual ("<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2b");
243 // XmlSerializerNamespaces
244 xs = new XmlSchema ();
245 sw = new StringWriter ();
246 xw = new XmlTextWriter (sw);
247 xs.Namespaces.Add ("hoge", "urn:hoge");
249 doc.LoadXml (sw.ToString ());
250 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
251 // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#3");
253 // TargetNamespace + XmlSerializerNamespaces
254 xs = new XmlSchema ();
255 sw = new StringWriter ();
256 xw = new XmlTextWriter (sw);
257 xs.TargetNamespace = "urn:foo";
258 xs.Namespaces.Add ("hoge", "urn:hoge");
260 doc.LoadXml (sw.ToString ());
261 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
262 // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#4");
264 // Add XmlSchema.Namespace to XmlSerializerNamespaces
265 xs = new XmlSchema ();
266 sw = new StringWriter ();
267 xw = new XmlTextWriter (sw);
268 xs.Namespaces.Add ("a", XmlSchema.Namespace);
270 doc.LoadXml (sw.ToString ());
271 Assert.AreEqual ("<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#5");
273 // UnhandledAttributes + XmlSerializerNamespaces
274 xs = new XmlSchema ();
275 sw = new StringWriter ();
276 xw = new XmlTextWriter (sw);
277 XmlAttribute attr = doc.CreateAttribute ("hoge");
278 xs.UnhandledAttributes = new XmlAttribute [] {attr};
279 xs.Namespaces.Add ("hoge", "urn:hoge");
281 doc.LoadXml (sw.ToString ());
282 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
283 // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#6");
285 // Adding xmlns to UnhandledAttributes -> no output
286 xs = new XmlSchema ();
287 sw = new StringWriter ();
288 xw = new XmlTextWriter (sw);
289 attr = doc.CreateAttribute ("xmlns");
290 attr.Value = "urn:foo";
291 xs.UnhandledAttributes = new XmlAttribute [] {attr};
293 doc.LoadXml (sw.ToString ());
294 Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#7");
297 [Category ("NotWorking")]
299 public void TestWriteNamespaces2 ()
301 string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
302 XmlSchema xs = new XmlSchema ();
303 XmlSerializerNamespaces nss =
304 new XmlSerializerNamespaces ();
306 sw = new StringWriter ();
307 xs.Write (new XmlTextWriter (sw));
308 Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#1");
311 sw = new StringWriter ();
312 xs.Write (new XmlTextWriter (sw));
313 Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#2");
315 nss.Add ("foo", "urn:foo");
316 sw = new StringWriter ();
317 xs.Write (new XmlTextWriter (sw));
318 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
319 // Assert.AreEqual (xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#3");
321 nss.Add ("", "urn:foo");
322 sw = new StringWriter ();
323 xs.Write (new XmlTextWriter (sw));
324 // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
325 // Assert.AreEqual (xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#4");
327 nss.Add ("q1", "urn:q1");
328 sw = new StringWriter ();
329 xs.Write (new XmlTextWriter (sw));
330 //Not sure if testing for exact order of these name spaces is
331 // relevent, so using less strict test that passes on MS.NET
332 //Assert.AreEqual (xmldecl + "<q2:schema xmlns:foo=\"urn:foo\" xmlns:q1=\"urn:q1\" xmlns=\"urn:foo\" xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
333 Assert.IsTrue (sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1, "q1");
337 public void ReaderPositionAfterRead ()
339 string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
340 XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
342 XmlSchema xs = XmlSchema.Read (xtr, null);
343 Assert.AreEqual (XmlNodeType.EndElement, xtr.NodeType);
348 public void AmbiguityDetectionOnChameleonAnyOther ()
350 string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
351 <xs:complexType name='TestType'>
353 <xs:any namespace='##other' minOccurs='0' />
354 <xs:element name='Item' />
355 <xs:any namespace='##other' minOccurs='0' />
359 XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
364 public void ReadDoesNotIgnoreDocumentationEmptyElement ()
366 string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
367 <xs:element name='choice'>
368 <xs:annotation><xs:documentation /></xs:annotation>
371 XmlTextReader tr = new XmlTextReader (
372 schemaxml, XmlNodeType.Document, null);
373 XmlSchema schema = XmlSchema.Read (tr, null);
374 XmlSchemaElement element =
375 schema.Items [0] as XmlSchemaElement;
376 XmlSchemaAnnotation annotation = element.Annotation;
377 XmlSchemaDocumentation doc =
378 annotation.Items [0] as XmlSchemaDocumentation;
379 Assert.AreEqual (0, doc.Markup.Length);
385 public void CompileFillsSchemaPropertyInExternal ()
387 string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
388 XmlTextReader tr = new XmlTextReader (schemaFileName);
390 XmlSchema schema = XmlSchema.Read (tr, null);
391 XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
392 Assert.IsNull (inc.Schema);
393 schema.Compile (null);
395 Assert.IsNotNull (inc.Schema);
399 // bug #78985 (contains two identical field path "@key" in
400 // two different keys where one is in scope within another)
401 public void DuplicateKeyFieldAttributePath ()
403 string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
404 string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
405 XmlTextReader tr = new XmlTextReader (schemaFileName);
407 XmlValidatingReader vr = new XmlValidatingReader (
408 new XmlTextReader (xmlFileName));
409 vr.Schemas.Add (XmlSchema.Read (tr, null));
415 public void ThreeLevelNestedInclusion ()
417 XmlTextReader r = new XmlTextReader ("Test/XmlFiles/xsd/361818.xsd");
419 XmlSchema xs = XmlSchema.Read (r, null);
426 [Test] // bug #502115
427 public void ExtensionRedefineAttribute1 ()
429 const string xml = "<Bar xmlns='foo'/>";
431 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-1.xsd");
434 XmlSchemaSet xss = new XmlSchemaSet ();
436 if (StrictMsCompliant) {
442 } catch (XmlSchemaException) {
447 StringReader sr = new StringReader (xml);
449 XmlReaderSettings settings = new XmlReaderSettings ();
450 settings.ValidationType = ValidationType.Schema;
451 settings.Schemas = xss;
452 XmlReader vr = XmlReader.Create (sr, settings);
454 if (StrictMsCompliant) {
455 schema.Compile (null);
458 schema.Compile (null);
460 } catch (XmlSchemaException) {
465 XmlValidatingReader vr = new XmlValidatingReader (xml,
466 XmlNodeType.Document, null);
467 vr.Schemas.Add (schema);
468 vr.ValidationType = ValidationType.Schema;
474 } catch (XmlSchemaException) {
478 [Test] // bug #502115
479 public void ExtensionRedefineAttribute2 ()
481 const string xml = "<Bar xmlns='foo'/>";
483 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-2.xsd");
486 XmlSchemaSet xss = new XmlSchemaSet ();
490 StringReader sr = new StringReader (xml);
492 XmlReaderSettings settings = new XmlReaderSettings ();
493 settings.ValidationType = ValidationType.Schema;
494 settings.Schemas = xss;
495 XmlReader vr = XmlReader.Create (sr, settings);
497 schema.Compile (null);
499 XmlValidatingReader vr = new XmlValidatingReader (xml,
500 XmlNodeType.Document, null);
501 vr.Schemas.Add (schema);
502 vr.ValidationType = ValidationType.Schema;
508 [Test] // bug #502115
509 public void ExtensionRedefineAttribute3 ()
511 const string xml = "<Bar xmlns='foo'/>";
513 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-3.xsd");
516 XmlSchemaSet xss = new XmlSchemaSet ();
518 if (StrictMsCompliant) {
524 } catch (XmlSchemaException) {
529 StringReader sr = new StringReader ("<Bar xmlns='foo'/>");
531 XmlReaderSettings settings = new XmlReaderSettings ();
532 settings.ValidationType = ValidationType.Schema;
533 settings.Schemas = xss;
534 XmlReader vr = XmlReader.Create (sr, settings);
536 if (StrictMsCompliant) {
537 schema.Compile (null);
540 schema.Compile (null);
542 } catch (XmlSchemaException) {
547 XmlValidatingReader vr = new XmlValidatingReader (xml,
548 XmlNodeType.Document, null);
549 vr.Schemas.Add (schema);
550 vr.ValidationType = ValidationType.Schema;
558 internal class XmlTestResolver : XmlResolver
562 public override ICredentials Credentials
564 set { throw new NotSupportedException (); }
567 public override Uri ResolveUri (Uri baseUri, string relativeUri)
569 return new Uri (relativeUri);
572 public Uri ReceivedUri
574 get { return receivedUri; }
577 public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
579 receivedUri = absoluteUri;
586 public void TestResolveUri ()
588 XmlSchemaSet schemaSet = new XmlSchemaSet ();
589 FileStream stream = new FileStream ("Test/XmlFiles/xsd/resolveUriSchema.xsd", FileMode.Open, FileAccess.Read);
590 schemaSet.Add ("http://tempuri.org/resolveUriSchema.xsd", new XmlTextReader (stream));
592 XmlTestResolver resolver = new XmlTestResolver ();
594 XmlReaderSettings settings = new XmlReaderSettings ();
595 settings.Schemas.XmlResolver = resolver;
596 settings.Schemas.Add (schemaSet);
597 settings.ValidationType = ValidationType.Schema;
598 settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
599 XmlReader reader = XmlReader.Create (stream, settings);
607 // do nothing - we are expecting this exception because the test xmlresolver returns null from its
611 Assert.AreEqual ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
615 public void TestImportNoSchemaLocation()
617 XmlSchemaSet schemaSet = new XmlSchemaSet ();
618 schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
619 schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
621 XmlReaderSettings settings = new XmlReaderSettings ();
622 settings.Schemas.Add (schemaSet);
623 settings.ValidationType = ValidationType.Schema;
625 XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/xsdimporttest.xml", settings);
628 while (reader.Read()) {}
633 public void TestImportSchemaThatIncludesAnother ()
635 XmlSchema xs = GetSchema ("Test/XmlFiles/xsd/importNamespaceTest2.xsd");