2 // System.Xml.XmlSchemaSetTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2004 Novell Inc.
12 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)