2005-10-23 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / XmlSchemaTests.cs
1 //
2 // System.Xml.XmlSchemaTests.cs
3 //
4 // Author:
5 //   Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 //
7 // (C) 2002 Atsushi Enomoto
8 //
9
10 using System;
11 using System.IO;
12 using System.Xml;
13 using System.Xml.Schema;
14 using System.Xml.Serialization;
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Xml
18 {
19         [TestFixture]
20         public class XmlSchemaTests : XmlSchemaAssertion
21         {
22                 [Test]
23                 public void TestRead ()
24                 {
25                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
26                         AssertEquals (6, schema.Items.Count);
27
28                         bool fooValidated = false;
29                         bool barValidated = false;
30                         string ns = "urn:bar";
31
32                         foreach (XmlSchemaObject obj in schema.Items) {
33                                 XmlSchemaElement element = obj as XmlSchemaElement;
34                                 if (element == null)
35                                         continue;
36                                 if (element.Name == "Foo") {
37                                         AssertElement (element, "Foo", 
38                                                 XmlQualifiedName.Empty, null,
39                                                 QName ("string", XmlSchema.Namespace), null);
40                                         fooValidated = true;
41                                 }
42                                 if (element.Name == "Bar") {
43                                         AssertElement (element, "Bar",
44                                                 XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
45                                         barValidated = true;
46                                 }
47                         }
48                         Assert (fooValidated);
49                         Assert (barValidated);
50                 }
51
52                 [Test]
53                 public void TestReadFlags ()
54                 {
55                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
56                         schema.Compile (null);
57                         XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
58                         AssertNotNull (el);
59                         AssertEquals (XmlSchemaDerivationMethod.Extension, el.Block);
60
61                         el = schema.Items [1] as XmlSchemaElement;
62                         AssertNotNull (el);
63                         AssertEquals (XmlSchemaDerivationMethod.Extension |
64                                 XmlSchemaDerivationMethod.Restriction, el.Block);
65                 }
66
67                 [Test]
68                 public void TestWriteFlags ()
69                 {
70                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
71                         StringWriter sw = new StringWriter ();
72                         XmlTextWriter xtw = new XmlTextWriter (sw);
73                         schema.Write (xtw);
74                 }
75
76                 [Test]
77                 public void TestCompile ()
78                 {
79                         XmlQualifiedName qname;
80                         XmlSchemaComplexContentExtension xccx;
81                         XmlSchemaComplexType cType;
82                         XmlSchemaSequence seq;
83
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";
89
90                         XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
91                         AssertNotNull (foo);
92                         XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
93                         AssertNotNull (stringDatatype);
94
95                         // HogeType
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;
103                         AssertNotNull (seq);
104                         AssertEquals (2, seq.Items.Count);
105                         XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
106                         AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
107
108                         // FugaType
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));
115
116                         AssertCompiledComplexType (cType, qname, 0, 0,
117                                 false, typeof (XmlSchemaComplexContent),
118                                 true, XmlSchemaContentType.ElementOnly);
119                         AssertNotNull (cType.BaseSchemaType);
120
121                         seq = xccx.Particle as XmlSchemaSequence;
122                         AssertNotNull (seq);
123                         AssertEquals (1, seq.Items.Count);
124                         XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
125                         AssertNotNull (refBaz);
126                         AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
127
128                         qname = QName ("Bar", ns);
129                         XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
130                         AssertCompiledElement (element, qname, cType);
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (XmlSchemaException))]
135                 public void TestCompileNonSchema ()
136                 {
137                         XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
138                         XmlSchema schema = XmlSchema.Read (xtr, null);
139                         xtr.Close ();\r
140                 }
141
142                 [Test]
143                 public void TestSimpleImport ()
144                 {
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);
149
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")]);
156                         
157                 }
158
159                 [Test]
160                 public void TestQualification ()
161                 {
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;
165                         AssertNotNull (el);
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);
170
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;
174                         AssertNotNull (el);
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);
179                 }
180
181                 [Test]
182                 public void TestWriteNamespaces ()
183                 {
184                         XmlDocument doc = new XmlDocument ();
185                         XmlSchema xs;
186                         StringWriter sw;
187                         XmlTextWriter xw;
188
189                         // empty
190                         xs = new XmlSchema ();
191                         sw = new StringWriter ();
192                         xw = new XmlTextWriter (sw);
193                         xs.Write (xw);
194                         doc.LoadXml (sw.ToString ());
195                         AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
196
197                         // TargetNamespace
198                         xs = new XmlSchema ();
199                         sw = new StringWriter ();
200                         xw = new XmlTextWriter (sw);
201                         xs.TargetNamespace = "urn:foo";
202                         xs.Write (xw);
203                         Console.WriteLine ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
204
205                         // XmlSerializerNamespaces
206                         xs = new XmlSchema ();
207                         sw = new StringWriter ();
208                         xw = new XmlTextWriter (sw);
209                         xs.Namespaces.Add ("hoge", "urn:hoge");
210                         xs.Write (xw);
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);
214
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");
221                         xs.Write (xw);
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);
225
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);
231                         xs.Write (xw);
232                         doc.LoadXml (sw.ToString ());
233                         AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
234
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");
242                         xs.Write (xw);
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);
246
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};
254                         xs.Write (xw);
255                         doc.LoadXml (sw.ToString ());
256                         AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
257                 }
258
259                 [Category ("NotWorking")]
260                 [Test]
261                 public void TestWriteNamespaces2 ()
262                 {
263                         string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
264                         XmlSchema xs = new XmlSchema ();
265                         XmlSerializerNamespaces nss =
266                                 new XmlSerializerNamespaces ();
267                         StringWriter sw;
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 ());
271
272                         xs.Namespaces = nss;
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 ());
276
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 ());
282
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 ());
288
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);
296                 }
297
298                 [Test]
299                 public void ReaderPositionAfterRead ()
300                 {
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);
303                         xtr.Read ();
304                         XmlSchema xs = XmlSchema.Read (xtr, null);
305                         AssertEquals (XmlNodeType.EndElement, xtr.NodeType);
306                 }
307         }
308 }