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 AssertEquals (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 (fooValidated);
52 Assert (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;
62 AssertEquals (XmlSchemaDerivationMethod.Extension, el.Block);
64 el = schema.Items [1] as XmlSchemaElement;
66 AssertEquals (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 (!schema.IsCompiled);
89 schema.Compile (null);
90 Assert (schema.IsCompiled);
91 string ns = "urn:bar";
93 XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
95 XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
96 AssertNotNull (stringDatatype);
99 qname = QName ("HogeType", ns);
100 cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
101 AssertNotNull (cType);
102 AssertNull (cType.ContentModel);
103 AssertCompiledComplexType (cType, qname, 0, 0,
104 false, null, true, XmlSchemaContentType.ElementOnly);
105 seq = cType.ContentTypeParticle as XmlSchemaSequence;
107 AssertEquals (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 AssertNotNull (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 AssertNotNull (cType.BaseSchemaType);
124 seq = xccx.Particle as XmlSchemaSequence;
126 AssertEquals (1, seq.Items.Count);
127 XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
128 AssertNotNull (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 (!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 AssertEquals ("urn:foo", schema.TargetNamespace);
163 XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
164 AssertNotNull (import);
166 schema.Compile (null);
167 AssertEquals (4, schema.Elements.Count);
168 AssertNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
169 AssertNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
170 AssertNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
171 AssertNotNull (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;
193 XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
194 XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
195 XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
196 AssertEquals (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;
202 ct = el.ElementType as XmlSchemaComplexType;
203 seq = ct.ContentTypeParticle as XmlSchemaSequence;
204 elp = seq.Items [0] as XmlSchemaElement;
205 AssertEquals (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 AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
225 xs = new XmlSchema ();
226 sw = new StringWriter ();
227 xw = new XmlTextWriter (sw);
228 xs.TargetNamespace = "urn:foo";
230 doc.LoadXml (sw.ToString ());
231 AssertEquals ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
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 AssertEquals ("#2b", "<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
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 // AssertEquals ("#3", "<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
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 // AssertEquals ("#4", "<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
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 AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
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 // AssertEquals ("#6", "<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
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 AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
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 AssertEquals ("#1", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
310 sw = new StringWriter ();
311 xs.Write (new XmlTextWriter (sw));
312 AssertEquals ("#2", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
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 // AssertEquals ("#3", xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
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 // AssertEquals ("#4", xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
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 //AssertEquals (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("q1", sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1);
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 AssertEquals (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 AssertEquals (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 AssertNull (inc.Schema);
392 schema.Compile (null);
394 AssertNotNull (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) {
463 XmlValidatingReader vr = new XmlValidatingReader (xml,
464 XmlNodeType.Document, null);
465 vr.Schemas.Add (schema);
466 vr.ValidationType = ValidationType.Schema;
472 } catch (XmlSchemaException) {
476 [Test] // bug #502115
477 public void ExtensionRedefineAttribute2 ()
479 const string xml = "<Bar xmlns='foo'/>";
481 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-2.xsd");
484 XmlSchemaSet xss = new XmlSchemaSet ();
488 StringReader sr = new StringReader (xml);
490 XmlReaderSettings settings = new XmlReaderSettings ();
491 settings.ValidationType = ValidationType.Schema;
492 settings.Schemas = xss;
493 XmlReader vr = XmlReader.Create (sr, settings);
495 schema.Compile (null);
497 XmlValidatingReader vr = new XmlValidatingReader (xml,
498 XmlNodeType.Document, null);
499 vr.Schemas.Add (schema);
500 vr.ValidationType = ValidationType.Schema;
506 [Test] // bug #502115
507 public void ExtensionRedefineAttribute3 ()
509 const string xml = "<Bar xmlns='foo'/>";
511 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-3.xsd");
514 XmlSchemaSet xss = new XmlSchemaSet ();
516 if (StrictMsCompliant) {
522 } catch (XmlSchemaException) {
527 StringReader sr = new StringReader ("<Bar xmlns='foo'/>");
529 XmlReaderSettings settings = new XmlReaderSettings ();
530 settings.ValidationType = ValidationType.Schema;
531 settings.Schemas = xss;
532 XmlReader vr = XmlReader.Create (sr, settings);
534 if (StrictMsCompliant) {
535 schema.Compile (null);
538 schema.Compile (null);
540 } catch (XmlSchemaException) {
544 XmlValidatingReader vr = new XmlValidatingReader (xml,
545 XmlNodeType.Document, null);
546 vr.Schemas.Add (schema);
547 vr.ValidationType = ValidationType.Schema;
555 internal class XmlTestResolver : XmlResolver
559 public override ICredentials Credentials
561 set { throw new NotSupportedException (); }
564 public override Uri ResolveUri (Uri baseUri, string relativeUri)
566 return new Uri (relativeUri);
569 public Uri ReceivedUri
571 get { return receivedUri; }
574 public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
576 receivedUri = absoluteUri;
583 public void TestResolveUri ()
585 XmlSchemaSet schemaSet = new XmlSchemaSet ();
586 FileStream stream = new FileStream ("Test/XmlFiles/xsd/resolveUriSchema.xsd", FileMode.Open);
587 schemaSet.Add ("http://tempuri.org/resolveUriSchema.xsd", new XmlTextReader (stream));
589 XmlTestResolver resolver = new XmlTestResolver ();
591 XmlReaderSettings settings = new XmlReaderSettings ();
592 settings.Schemas.XmlResolver = resolver;
593 settings.Schemas.Add (schemaSet);
594 settings.ValidationType = ValidationType.Schema;
595 settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
596 XmlReader reader = XmlReader.Create (stream, settings);
604 // do nothing - we are expecting this exception because the test xmlresolver returns null from its
608 AssertEquals ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
612 public void TestImportNoSchemaLocation()
614 XmlSchemaSet schemaSet = new XmlSchemaSet ();
615 schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
616 schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
618 XmlReaderSettings settings = new XmlReaderSettings ();
619 settings.Schemas.Add (schemaSet);
620 settings.ValidationType = ValidationType.Schema;
622 XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/xsdimporttest.xml", settings);
625 while (reader.Read()) {}