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 public void TestSimpleMutualImport ()
178 XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
180 XmlSchema.Read (r, null).Compile (null);
187 public void TestQualification ()
189 XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
190 schema.Compile (null);
191 XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
192 Assert.IsNotNull (el);
193 XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
194 XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
195 XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
196 Assert.AreEqual (QName ("Bar", ""), elp.QualifiedName);
198 schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
199 schema.Compile (null);
200 el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
201 Assert.IsNotNull (el);
202 ct = el.ElementType as XmlSchemaComplexType;
203 seq = ct.ContentTypeParticle as XmlSchemaSequence;
204 elp = seq.Items [0] as XmlSchemaElement;
205 Assert.AreEqual (QName ("Bar", "urn:bar"), elp.QualifiedName);
209 public void TestWriteNamespaces ()
211 XmlDocument doc = new XmlDocument ();
217 xs = new XmlSchema ();
218 sw = new StringWriter ();
219 xw = new XmlTextWriter (sw);
221 doc.LoadXml (sw.ToString ());
222 Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#1");
225 xs = new XmlSchema ();
226 sw = new StringWriter ();
227 xw = new XmlTextWriter (sw);
228 xs.TargetNamespace = "urn:foo";
230 doc.LoadXml (sw.ToString ());
231 Assert.AreEqual ("<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2");
233 // Zero-length TargetNamespace
234 xs = new XmlSchema ();
235 sw = new StringWriter ();
236 xw = new XmlTextWriter (sw);
237 xs.TargetNamespace = string.Empty;
239 doc.LoadXml (sw.ToString ());
240 Assert.AreEqual ("<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2b");
242 // XmlSerializerNamespaces
243 xs = new XmlSchema ();
244 sw = new StringWriter ();
245 xw = new XmlTextWriter (sw);
246 xs.Namespaces.Add ("hoge", "urn:hoge");
248 doc.LoadXml (sw.ToString ());
249 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
250 // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#3");
252 // TargetNamespace + XmlSerializerNamespaces
253 xs = new XmlSchema ();
254 sw = new StringWriter ();
255 xw = new XmlTextWriter (sw);
256 xs.TargetNamespace = "urn:foo";
257 xs.Namespaces.Add ("hoge", "urn:hoge");
259 doc.LoadXml (sw.ToString ());
260 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
261 // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#4");
263 // Add XmlSchema.Namespace to XmlSerializerNamespaces
264 xs = new XmlSchema ();
265 sw = new StringWriter ();
266 xw = new XmlTextWriter (sw);
267 xs.Namespaces.Add ("a", XmlSchema.Namespace);
269 doc.LoadXml (sw.ToString ());
270 Assert.AreEqual ("<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#5");
272 // UnhandledAttributes + XmlSerializerNamespaces
273 xs = new XmlSchema ();
274 sw = new StringWriter ();
275 xw = new XmlTextWriter (sw);
276 XmlAttribute attr = doc.CreateAttribute ("hoge");
277 xs.UnhandledAttributes = new XmlAttribute [] {attr};
278 xs.Namespaces.Add ("hoge", "urn:hoge");
280 doc.LoadXml (sw.ToString ());
281 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
282 // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#6");
284 // Adding xmlns to UnhandledAttributes -> no output
285 xs = new XmlSchema ();
286 sw = new StringWriter ();
287 xw = new XmlTextWriter (sw);
288 attr = doc.CreateAttribute ("xmlns");
289 attr.Value = "urn:foo";
290 xs.UnhandledAttributes = new XmlAttribute [] {attr};
292 doc.LoadXml (sw.ToString ());
293 Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#7");
296 [Category ("NotWorking")]
298 public void TestWriteNamespaces2 ()
300 string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
301 XmlSchema xs = new XmlSchema ();
302 XmlSerializerNamespaces nss =
303 new XmlSerializerNamespaces ();
305 sw = new StringWriter ();
306 xs.Write (new XmlTextWriter (sw));
307 Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#1");
310 sw = new StringWriter ();
311 xs.Write (new XmlTextWriter (sw));
312 Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#2");
314 nss.Add ("foo", "urn:foo");
315 sw = new StringWriter ();
316 xs.Write (new XmlTextWriter (sw));
317 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
318 // Assert.AreEqual (xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#3");
320 nss.Add ("", "urn:foo");
321 sw = new StringWriter ();
322 xs.Write (new XmlTextWriter (sw));
323 // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
324 // Assert.AreEqual (xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#4");
326 nss.Add ("q1", "urn:q1");
327 sw = new StringWriter ();
328 xs.Write (new XmlTextWriter (sw));
329 //Not sure if testing for exact order of these name spaces is
330 // relevent, so using less strict test that passes on MS.NET
331 //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 ());
332 Assert.IsTrue (sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1, "q1");
336 public void ReaderPositionAfterRead ()
338 string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
339 XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
341 XmlSchema xs = XmlSchema.Read (xtr, null);
342 Assert.AreEqual (XmlNodeType.EndElement, xtr.NodeType);
347 public void AmbiguityDetectionOnChameleonAnyOther ()
349 string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
350 <xs:complexType name='TestType'>
352 <xs:any namespace='##other' minOccurs='0' />
353 <xs:element name='Item' />
354 <xs:any namespace='##other' minOccurs='0' />
358 XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
363 public void ReadDoesNotIgnoreDocumentationEmptyElement ()
365 string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
366 <xs:element name='choice'>
367 <xs:annotation><xs:documentation /></xs:annotation>
370 XmlTextReader tr = new XmlTextReader (
371 schemaxml, XmlNodeType.Document, null);
372 XmlSchema schema = XmlSchema.Read (tr, null);
373 XmlSchemaElement element =
374 schema.Items [0] as XmlSchemaElement;
375 XmlSchemaAnnotation annotation = element.Annotation;
376 XmlSchemaDocumentation doc =
377 annotation.Items [0] as XmlSchemaDocumentation;
378 Assert.AreEqual (0, doc.Markup.Length);
384 public void CompileFillsSchemaPropertyInExternal ()
386 string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
387 XmlTextReader tr = new XmlTextReader (schemaFileName);
389 XmlSchema schema = XmlSchema.Read (tr, null);
390 XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
391 Assert.IsNull (inc.Schema);
392 schema.Compile (null);
394 Assert.IsNotNull (inc.Schema);
398 // bug #78985 (contains two identical field path "@key" in
399 // two different keys where one is in scope within another)
400 public void DuplicateKeyFieldAttributePath ()
402 string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
403 string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
404 XmlTextReader tr = new XmlTextReader (schemaFileName);
406 XmlValidatingReader vr = new XmlValidatingReader (
407 new XmlTextReader (xmlFileName));
408 vr.Schemas.Add (XmlSchema.Read (tr, null));
414 public void ThreeLevelNestedInclusion ()
416 XmlTextReader r = new XmlTextReader ("Test/XmlFiles/xsd/361818.xsd");
418 XmlSchema xs = XmlSchema.Read (r, null);
425 [Test] // bug #502115
426 public void ExtensionRedefineAttribute1 ()
428 const string xml = "<Bar xmlns='foo'/>";
430 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-1.xsd");
433 XmlSchemaSet xss = new XmlSchemaSet ();
435 if (StrictMsCompliant) {
441 } catch (XmlSchemaException) {
446 StringReader sr = new StringReader (xml);
448 XmlReaderSettings settings = new XmlReaderSettings ();
449 settings.ValidationType = ValidationType.Schema;
450 settings.Schemas = xss;
451 XmlReader vr = XmlReader.Create (sr, settings);
453 if (StrictMsCompliant) {
454 schema.Compile (null);
457 schema.Compile (null);
459 } catch (XmlSchemaException) {
464 XmlValidatingReader vr = new XmlValidatingReader (xml,
465 XmlNodeType.Document, null);
466 vr.Schemas.Add (schema);
467 vr.ValidationType = ValidationType.Schema;
473 } catch (XmlSchemaException) {
477 [Test] // bug #502115
478 public void ExtensionRedefineAttribute2 ()
480 const string xml = "<Bar xmlns='foo'/>";
482 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-2.xsd");
485 XmlSchemaSet xss = new XmlSchemaSet ();
489 StringReader sr = new StringReader (xml);
491 XmlReaderSettings settings = new XmlReaderSettings ();
492 settings.ValidationType = ValidationType.Schema;
493 settings.Schemas = xss;
494 XmlReader vr = XmlReader.Create (sr, settings);
496 schema.Compile (null);
498 XmlValidatingReader vr = new XmlValidatingReader (xml,
499 XmlNodeType.Document, null);
500 vr.Schemas.Add (schema);
501 vr.ValidationType = ValidationType.Schema;
507 [Test] // bug #502115
508 public void ExtensionRedefineAttribute3 ()
510 const string xml = "<Bar xmlns='foo'/>";
512 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-3.xsd");
515 XmlSchemaSet xss = new XmlSchemaSet ();
517 if (StrictMsCompliant) {
523 } catch (XmlSchemaException) {
528 StringReader sr = new StringReader ("<Bar xmlns='foo'/>");
530 XmlReaderSettings settings = new XmlReaderSettings ();
531 settings.ValidationType = ValidationType.Schema;
532 settings.Schemas = xss;
533 XmlReader vr = XmlReader.Create (sr, settings);
535 if (StrictMsCompliant) {
536 schema.Compile (null);
539 schema.Compile (null);
541 } catch (XmlSchemaException) {
546 XmlValidatingReader vr = new XmlValidatingReader (xml,
547 XmlNodeType.Document, null);
548 vr.Schemas.Add (schema);
549 vr.ValidationType = ValidationType.Schema;
557 internal class XmlTestResolver : XmlResolver
561 public override ICredentials Credentials
563 set { throw new NotSupportedException (); }
566 public override Uri ResolveUri (Uri baseUri, string relativeUri)
568 return new Uri (relativeUri);
571 public Uri ReceivedUri
573 get { return receivedUri; }
576 public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
578 receivedUri = absoluteUri;
585 public void TestResolveUri ()
587 XmlSchemaSet schemaSet = new XmlSchemaSet ();
588 FileStream stream = new FileStream ("Test/XmlFiles/xsd/resolveUriSchema.xsd", FileMode.Open);
589 schemaSet.Add ("http://tempuri.org/resolveUriSchema.xsd", new XmlTextReader (stream));
591 XmlTestResolver resolver = new XmlTestResolver ();
593 XmlReaderSettings settings = new XmlReaderSettings ();
594 settings.Schemas.XmlResolver = resolver;
595 settings.Schemas.Add (schemaSet);
596 settings.ValidationType = ValidationType.Schema;
597 settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
598 XmlReader reader = XmlReader.Create (stream, settings);
606 // do nothing - we are expecting this exception because the test xmlresolver returns null from its
610 Assert.AreEqual ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
614 public void TestImportNoSchemaLocation()
616 XmlSchemaSet schemaSet = new XmlSchemaSet ();
617 schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
618 schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
620 XmlReaderSettings settings = new XmlReaderSettings ();
621 settings.Schemas.Add (schemaSet);
622 settings.ValidationType = ValidationType.Schema;
624 XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/xsdimporttest.xml", settings);
627 while (reader.Read()) {}