XmlSchemaTests.cs 8.3 KB

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