XmlSchemaTests.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // System.Xml.XmlSchemaTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2002 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Xml;
  12. using System.Xml.Schema;
  13. using System.Xml.Serialization;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Xml
  16. {
  17. [TestFixture]
  18. public class XmlSchemaTests : XmlSchemaAssertion
  19. {
  20. [Test]
  21. public void TestRead ()
  22. {
  23. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  24. AssertEquals (6, schema.Items.Count);
  25. bool fooValidated = false;
  26. bool barValidated = false;
  27. string ns = "urn:bar";
  28. foreach (XmlSchemaObject obj in schema.Items) {
  29. XmlSchemaElement element = obj as XmlSchemaElement;
  30. if (element == null)
  31. continue;
  32. if (element.Name == "Foo") {
  33. AssertElement (element, "Foo",
  34. XmlQualifiedName.Empty, null,
  35. QName ("string", XmlSchema.Namespace), null);
  36. fooValidated = true;
  37. }
  38. if (element.Name == "Bar") {
  39. AssertElement (element, "Bar",
  40. XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
  41. barValidated = true;
  42. }
  43. }
  44. Assert (fooValidated);
  45. Assert (barValidated);
  46. }
  47. [Test]
  48. public void TestReadFlags ()
  49. {
  50. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  51. schema.Compile (null);
  52. XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
  53. AssertNotNull (el);
  54. AssertEquals (XmlSchemaDerivationMethod.Extension, el.Block);
  55. el = schema.Items [1] as XmlSchemaElement;
  56. AssertNotNull (el);
  57. AssertEquals (XmlSchemaDerivationMethod.Extension |
  58. XmlSchemaDerivationMethod.Restriction, el.Block);
  59. }
  60. [Test]
  61. public void TestWriteFlags ()
  62. {
  63. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  64. StringWriter sw = new StringWriter ();
  65. XmlTextWriter xtw = new XmlTextWriter (sw);
  66. schema.Write (xtw);
  67. }
  68. [Test]
  69. public void TestCompile ()
  70. {
  71. XmlQualifiedName qname;
  72. XmlSchemaComplexContentExtension xccx;
  73. XmlSchemaComplexType cType;
  74. XmlSchemaSequence seq;
  75. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  76. // Assert (!schema.IsCompiled);
  77. schema.Compile (null);
  78. Assert (schema.IsCompiled);
  79. string ns = "urn:bar";
  80. XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
  81. AssertNotNull (foo);
  82. XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
  83. AssertNotNull (stringDatatype);
  84. // HogeType
  85. qname = QName ("HogeType", ns);
  86. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  87. AssertNotNull (cType);
  88. AssertNull (cType.ContentModel);
  89. AssertCompiledComplexType (cType, qname, 0, 0,
  90. false, null, true, XmlSchemaContentType.ElementOnly);
  91. seq = cType.ContentTypeParticle as XmlSchemaSequence;
  92. AssertNotNull (seq);
  93. AssertEquals (2, seq.Items.Count);
  94. XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
  95. AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
  96. // FugaType
  97. qname = QName ("FugaType", ns);
  98. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  99. AssertNotNull (cType);
  100. xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
  101. AssertCompiledComplexContentExtension (
  102. xccx, 0, false, QName ("HogeType", ns));
  103. AssertCompiledComplexType (cType, qname, 0, 0,
  104. false, typeof (XmlSchemaComplexContent),
  105. true, XmlSchemaContentType.ElementOnly);
  106. AssertNotNull (cType.BaseSchemaType);
  107. seq = xccx.Particle as XmlSchemaSequence;
  108. AssertNotNull (seq);
  109. AssertEquals (1, seq.Items.Count);
  110. XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
  111. AssertNotNull (refBaz);
  112. AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
  113. qname = QName ("Bar", ns);
  114. XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
  115. AssertCompiledElement (element, qname, cType);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (XmlSchemaException))]
  119. public void TestCompileNonSchema ()
  120. {
  121. XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
  122. XmlSchema schema = XmlSchema.Read (xtr, null);
  123. xtr.Close ();
  124. }
  125. [Test]
  126. public void TestSimpleImport ()
  127. {
  128. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
  129. AssertEquals ("urn:foo", schema.TargetNamespace);
  130. XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
  131. AssertNotNull (import);
  132. schema.Compile (null);
  133. AssertEquals (4, schema.Elements.Count);
  134. AssertNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
  135. AssertNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
  136. AssertNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
  137. AssertNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
  138. }
  139. [Test]
  140. public void TestQualification ()
  141. {
  142. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
  143. schema.Compile (null);
  144. XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  145. AssertNotNull (el);
  146. XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
  147. XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
  148. XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
  149. AssertEquals (QName ("Bar", ""), elp.QualifiedName);
  150. schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
  151. schema.Compile (null);
  152. el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  153. AssertNotNull (el);
  154. ct = el.ElementType as XmlSchemaComplexType;
  155. seq = ct.ContentTypeParticle as XmlSchemaSequence;
  156. elp = seq.Items [0] as XmlSchemaElement;
  157. AssertEquals (QName ("Bar", "urn:bar"), elp.QualifiedName);
  158. }
  159. [Test]
  160. public void TestWriteNamespaces ()
  161. {
  162. XmlDocument doc = new XmlDocument ();
  163. XmlSchema xs;
  164. StringWriter sw;
  165. XmlTextWriter xw;
  166. // empty
  167. xs = new XmlSchema ();
  168. sw = new StringWriter ();
  169. xw = new XmlTextWriter (sw);
  170. xs.Write (xw);
  171. doc.LoadXml (sw.ToString ());
  172. AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  173. // TargetNamespace
  174. xs = new XmlSchema ();
  175. sw = new StringWriter ();
  176. xw = new XmlTextWriter (sw);
  177. xs.TargetNamespace = "urn:foo";
  178. xs.Write (xw);
  179. Console.WriteLine ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  180. // XmlSerializerNamespaces
  181. xs = new XmlSchema ();
  182. sw = new StringWriter ();
  183. xw = new XmlTextWriter (sw);
  184. xs.Namespaces.Add ("hoge", "urn:hoge");
  185. xs.Write (xw);
  186. doc.LoadXml (sw.ToString ());
  187. AssertEquals ("#3", "<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  188. // TargetNamespace + XmlSerializerNamespaces
  189. xs = new XmlSchema ();
  190. sw = new StringWriter ();
  191. xw = new XmlTextWriter (sw);
  192. xs.TargetNamespace = "urn:foo";
  193. xs.Namespaces.Add ("hoge", "urn:hoge");
  194. xs.Write (xw);
  195. doc.LoadXml (sw.ToString ());
  196. AssertEquals ("#4", "<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  197. // Add XmlSchema.Namespace to XmlSerializerNamespaces
  198. xs = new XmlSchema ();
  199. sw = new StringWriter ();
  200. xw = new XmlTextWriter (sw);
  201. xs.Namespaces.Add ("a", XmlSchema.Namespace);
  202. xs.Write (xw);
  203. doc.LoadXml (sw.ToString ());
  204. AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  205. // UnhandledAttributes + XmlSerializerNamespaces
  206. xs = new XmlSchema ();
  207. sw = new StringWriter ();
  208. xw = new XmlTextWriter (sw);
  209. XmlAttribute attr = doc.CreateAttribute ("hoge");
  210. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  211. xs.Namespaces.Add ("hoge", "urn:hoge");
  212. xs.Write (xw);
  213. doc.LoadXml (sw.ToString ());
  214. AssertEquals ("#6", "<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  215. // Adding xmlns to UnhandledAttributes -> no output
  216. xs = new XmlSchema ();
  217. sw = new StringWriter ();
  218. xw = new XmlTextWriter (sw);
  219. attr = doc.CreateAttribute ("xmlns");
  220. attr.Value = "urn:foo";
  221. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  222. xs.Write (xw);
  223. doc.LoadXml (sw.ToString ());
  224. AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  225. }
  226. [Category ("NotWorking")]
  227. [Test]
  228. public void TestWriteNamespaces2 ()
  229. {
  230. string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  231. XmlSchema xs = new XmlSchema ();
  232. XmlSerializerNamespaces nss =
  233. new XmlSerializerNamespaces ();
  234. StringWriter sw;
  235. sw = new StringWriter ();
  236. xs.Write (new XmlTextWriter (sw));
  237. AssertEquals ("#1", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  238. xs.Namespaces = nss;
  239. sw = new StringWriter ();
  240. xs.Write (new XmlTextWriter (sw));
  241. AssertEquals ("#2", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  242. nss.Add ("foo", "urn:foo");
  243. sw = new StringWriter ();
  244. xs.Write (new XmlTextWriter (sw));
  245. AssertEquals ("#3", xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  246. nss.Add ("", "urn:foo");
  247. sw = new StringWriter ();
  248. xs.Write (new XmlTextWriter (sw));
  249. AssertEquals ("#4", xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  250. nss.Add ("q1", "urn:q1");
  251. sw = new StringWriter ();
  252. xs.Write (new XmlTextWriter (sw));
  253. //Not sure if testing for exact order of these name spaces is
  254. // relevent, so using less strict test that passes on MS.NET
  255. //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 ());
  256. Assert("q1", sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1);
  257. }
  258. }
  259. }