2 // System.Xml.XmlSchemaTests.cs
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 // (C) 2002 Atsushi Enomoto
13 using System.Xml.Schema;
14 using System.Xml.Serialization;
15 using NUnit.Framework;
17 namespace MonoTests.System.Xml
20 public class XmlSchemaTests : XmlSchemaAssertion
23 public void TestRead ()
25 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
26 AssertEquals (6, schema.Items.Count);
28 bool fooValidated = false;
29 bool barValidated = false;
30 string ns = "urn:bar";
32 foreach (XmlSchemaObject obj in schema.Items) {
33 XmlSchemaElement element = obj as XmlSchemaElement;
36 if (element.Name == "Foo") {
37 AssertElement (element, "Foo",
38 XmlQualifiedName.Empty, null,
39 QName ("string", XmlSchema.Namespace), null);
42 if (element.Name == "Bar") {
43 AssertElement (element, "Bar",
44 XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
48 Assert (fooValidated);
49 Assert (barValidated);
53 public void TestReadFlags ()
55 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
56 schema.Compile (null);
57 XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
59 AssertEquals (XmlSchemaDerivationMethod.Extension, el.Block);
61 el = schema.Items [1] as XmlSchemaElement;
63 AssertEquals (XmlSchemaDerivationMethod.Extension |
64 XmlSchemaDerivationMethod.Restriction, el.Block);
68 public void TestWriteFlags ()
70 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
71 StringWriter sw = new StringWriter ();
72 XmlTextWriter xtw = new XmlTextWriter (sw);
77 public void TestCompile ()
79 XmlQualifiedName qname;
80 XmlSchemaComplexContentExtension xccx;
81 XmlSchemaComplexType cType;
82 XmlSchemaSequence seq;
84 XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
85 // Assert (!schema.IsCompiled);
86 schema.Compile (null);
87 Assert (schema.IsCompiled);
88 string ns = "urn:bar";
90 XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
92 XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
93 AssertNotNull (stringDatatype);
96 qname = QName ("HogeType", ns);
97 cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
98 AssertNotNull (cType);
99 AssertNull (cType.ContentModel);
100 AssertCompiledComplexType (cType, qname, 0, 0,
101 false, null, true, XmlSchemaContentType.ElementOnly);
102 seq = cType.ContentTypeParticle as XmlSchemaSequence;
104 AssertEquals (2, seq.Items.Count);
105 XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
106 AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
109 qname = QName ("FugaType", ns);
110 cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
111 AssertNotNull (cType);
112 xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
113 AssertCompiledComplexContentExtension (
114 xccx, 0, false, QName ("HogeType", ns));
116 AssertCompiledComplexType (cType, qname, 0, 0,
117 false, typeof (XmlSchemaComplexContent),
118 true, XmlSchemaContentType.ElementOnly);
119 AssertNotNull (cType.BaseSchemaType);
121 seq = xccx.Particle as XmlSchemaSequence;
123 AssertEquals (1, seq.Items.Count);
124 XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
125 AssertNotNull (refBaz);
126 AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
128 qname = QName ("Bar", ns);
129 XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
130 AssertCompiledElement (element, qname, cType);
134 [ExpectedException (typeof (XmlSchemaException))]
135 public void TestCompileNonSchema ()
137 XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
138 XmlSchema schema = XmlSchema.Read (xtr, null);
143 public void TestSimpleImport ()
145 XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
146 AssertEquals ("urn:foo", schema.TargetNamespace);
147 XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
148 AssertNotNull (import);
150 schema.Compile (null);
151 AssertEquals (4, schema.Elements.Count);
152 AssertNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
153 AssertNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
154 AssertNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
155 AssertNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
160 public void TestQualification ()
162 XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
163 schema.Compile (null);
164 XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
166 XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
167 XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
168 XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
169 AssertEquals (QName ("Bar", ""), elp.QualifiedName);
171 schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
172 schema.Compile (null);
173 el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
175 ct = el.ElementType as XmlSchemaComplexType;
176 seq = ct.ContentTypeParticle as XmlSchemaSequence;
177 elp = seq.Items [0] as XmlSchemaElement;
178 AssertEquals (QName ("Bar", "urn:bar"), elp.QualifiedName);
182 public void TestWriteNamespaces ()
184 XmlDocument doc = new XmlDocument ();
190 xs = new XmlSchema ();
191 sw = new StringWriter ();
192 xw = new XmlTextWriter (sw);
194 doc.LoadXml (sw.ToString ());
195 AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
198 xs = new XmlSchema ();
199 sw = new StringWriter ();
200 xw = new XmlTextWriter (sw);
201 xs.TargetNamespace = "urn:foo";
203 Console.WriteLine ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
205 // XmlSerializerNamespaces
206 xs = new XmlSchema ();
207 sw = new StringWriter ();
208 xw = new XmlTextWriter (sw);
209 xs.Namespaces.Add ("hoge", "urn:hoge");
211 doc.LoadXml (sw.ToString ());
212 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
213 // AssertEquals ("#3", "<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
215 // TargetNamespace + XmlSerializerNamespaces
216 xs = new XmlSchema ();
217 sw = new StringWriter ();
218 xw = new XmlTextWriter (sw);
219 xs.TargetNamespace = "urn:foo";
220 xs.Namespaces.Add ("hoge", "urn:hoge");
222 doc.LoadXml (sw.ToString ());
223 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
224 // AssertEquals ("#4", "<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
226 // Add XmlSchema.Namespace to XmlSerializerNamespaces
227 xs = new XmlSchema ();
228 sw = new StringWriter ();
229 xw = new XmlTextWriter (sw);
230 xs.Namespaces.Add ("a", XmlSchema.Namespace);
232 doc.LoadXml (sw.ToString ());
233 AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
235 // UnhandledAttributes + XmlSerializerNamespaces
236 xs = new XmlSchema ();
237 sw = new StringWriter ();
238 xw = new XmlTextWriter (sw);
239 XmlAttribute attr = doc.CreateAttribute ("hoge");
240 xs.UnhandledAttributes = new XmlAttribute [] {attr};
241 xs.Namespaces.Add ("hoge", "urn:hoge");
243 doc.LoadXml (sw.ToString ());
244 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
245 // AssertEquals ("#6", "<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
247 // Adding xmlns to UnhandledAttributes -> no output
248 xs = new XmlSchema ();
249 sw = new StringWriter ();
250 xw = new XmlTextWriter (sw);
251 attr = doc.CreateAttribute ("xmlns");
252 attr.Value = "urn:foo";
253 xs.UnhandledAttributes = new XmlAttribute [] {attr};
255 doc.LoadXml (sw.ToString ());
256 AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
259 [Category ("NotWorking")]
261 public void TestWriteNamespaces2 ()
263 string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
264 XmlSchema xs = new XmlSchema ();
265 XmlSerializerNamespaces nss =
266 new XmlSerializerNamespaces ();
268 sw = new StringWriter ();
269 xs.Write (new XmlTextWriter (sw));
270 AssertEquals ("#1", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
273 sw = new StringWriter ();
274 xs.Write (new XmlTextWriter (sw));
275 AssertEquals ("#2", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
277 nss.Add ("foo", "urn:foo");
278 sw = new StringWriter ();
279 xs.Write (new XmlTextWriter (sw));
280 // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
281 // AssertEquals ("#3", xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
283 nss.Add ("", "urn:foo");
284 sw = new StringWriter ();
285 xs.Write (new XmlTextWriter (sw));
286 // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
287 // AssertEquals ("#4", xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
289 nss.Add ("q1", "urn:q1");
290 sw = new StringWriter ();
291 xs.Write (new XmlTextWriter (sw));
292 //Not sure if testing for exact order of these name spaces is
293 // relevent, so using less strict test that passes on MS.NET
294 //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 ());
295 Assert("q1", sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1);
299 public void ReaderPositionAfterRead ()
301 string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
302 XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
304 XmlSchema xs = XmlSchema.Read (xtr, null);
305 AssertEquals (XmlNodeType.EndElement, xtr.NodeType);