ExtensionsTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //
  2. // ExtensionsTest.cs - NUnit Test Cases for Extensions.cs class under
  3. // System.Xml.Schema namespace found in System.Xml.Linq assembly
  4. // (System.Xml.Linq.dll)
  5. //
  6. // Author:
  7. // Stefan Prutianu ([email protected])
  8. //
  9. // (C) Stefan Prutianu
  10. //
  11. using NUnit.Framework;
  12. using System;
  13. using System.Xml;
  14. using System.IO;
  15. using Network = System.Net;
  16. using System.Xml.Linq;
  17. using System.Xml.Schema;
  18. using System.Collections.Generic;
  19. using ExtensionsClass = System.Xml.Schema.Extensions;
  20. namespace MonoTests.System.Xml.Schema
  21. {
  22. [TestFixture]
  23. public class ExtensionsTest {
  24. public static String xsdString;
  25. public static XmlSchemaSet schemaSet;
  26. public static String xmlString;
  27. public static XDocument xmlDocument;
  28. public static bool validationSucceded;
  29. // initialize values for tests
  30. [SetUp]
  31. public void Init ()
  32. {
  33. xsdString = @"<?xml version='1.0'?>
  34. <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  35. <xs:element name='note'>
  36. <xs:complexType>
  37. <xs:sequence>
  38. <xs:element name='to' type='xs:string'/>
  39. <xs:element name='from' type='xs:string'/>
  40. <xs:element name='heading' type='xs:string'/>
  41. <xs:element name='ps' type='xs:string'
  42. maxOccurs='1' minOccurs = '0' default='Bye!'/>
  43. <xs:element name='body'>
  44. <xs:simpleType>
  45. <xs:restriction base='xs:string'>
  46. <xs:minLength value='5'/>
  47. <xs:maxLength value='30'/>
  48. </xs:restriction>
  49. </xs:simpleType>
  50. </xs:element>
  51. </xs:sequence>
  52. <xs:attribute name='subject' type='xs:string'
  53. default='mono'/>
  54. <xs:attribute name='date' type='xs:date'
  55. use='required'/>
  56. </xs:complexType>
  57. </xs:element>
  58. </xs:schema>";
  59. schemaSet = new XmlSchemaSet ();
  60. schemaSet.Add ("", XmlReader.Create (new StringReader (xsdString)));
  61. xmlString = @"<?xml version='1.0'?>
  62. <note date ='2010-05-26'>
  63. <to>Tove</to>
  64. <from>Jani</from>
  65. <heading>Reminder</heading>
  66. <body>Don't forget to call me!</body>
  67. </note>";
  68. xmlDocument = XDocument.Load (new StringReader (xmlString));
  69. validationSucceded = false;
  70. /*
  71. * Use this method to load the above data from disk
  72. * Comment the above code when using this method.
  73. * Also the arguments for the folowing tests: XAttributeSuccessValidate
  74. * XAttributeFailValidate, XAttributeThrowExceptionValidate, XElementSuccessValidate
  75. * XElementFailValidate,XElementThrowExceptionValidate, XAttributeGetSchemaInfo
  76. * XElementGetSchemaInfo may need to be changed.
  77. */
  78. //LoadOutsideDocuments ("c:\\note.xsd", "c:\\note.xml");
  79. }
  80. // Use this method to load data from disk
  81. public static void LoadOutsideDocuments (String xsdDocumentPath, String xmlDocumentPath)
  82. {
  83. // Create a resolver with default credentials.
  84. XmlUrlResolver resolver = new XmlUrlResolver ();
  85. resolver.Credentials = Network.CredentialCache.DefaultCredentials;
  86. // Set the reader settings object to use the resolver.
  87. XmlReaderSettings settings = new XmlReaderSettings ();
  88. settings.XmlResolver = resolver;
  89. // Create the XmlReader object.
  90. XmlReader reader = XmlReader.Create (xsdDocumentPath, settings);
  91. schemaSet = new XmlSchemaSet ();
  92. schemaSet.Add ("", reader);
  93. reader = XmlReader.Create (xmlDocumentPath, settings);
  94. xmlDocument = XDocument.Load (reader);
  95. validationSucceded = false;
  96. }
  97. // this gets called when a validation error occurs
  98. public void TestValidationHandler (object sender, ValidationEventArgs e)
  99. {
  100. validationSucceded = false;
  101. }
  102. [TearDown]
  103. public void Cleanup ()
  104. {
  105. xsdString = null;
  106. schemaSet = null;
  107. xmlString = null;
  108. xmlDocument = null;
  109. validationSucceded = false;
  110. }
  111. // test succesfull validation
  112. [Test]
  113. public void XDocumentSuccessValidate ()
  114. {
  115. validationSucceded = true;
  116. ExtensionsClass.Validate (xmlDocument, schemaSet,
  117. new ValidationEventHandler (TestValidationHandler));
  118. Assert.AreEqual (true, validationSucceded, "XDocumentSuccessValidate");
  119. }
  120. // test failed validation
  121. [Test]
  122. public void XDocumentFailValidate ()
  123. {
  124. String elementName = "AlteringElementName";
  125. object elementValue = "AlteringElementValue";
  126. // alter XML document to invalidate
  127. XElement newElement = new XElement (elementName, elementValue);
  128. xmlDocument.Root.Add (newElement);
  129. validationSucceded = true;
  130. ExtensionsClass.Validate (xmlDocument, schemaSet,
  131. new ValidationEventHandler (TestValidationHandler));
  132. Assert.AreEqual (false, validationSucceded, "XDocumentFailValidate");
  133. }
  134. /*
  135. * test if exception is thrown when xml document does not
  136. * conform to the xml schema
  137. */
  138. [Test]
  139. [ExpectedException (typeof (XmlSchemaValidationException))]
  140. public void XDocumentThrowExceptionValidate ()
  141. {
  142. String elementName = "AlteringElementName";
  143. object elementValue = "AlteringElementValue";
  144. // alter XML document to invalidate
  145. XElement newElement = new XElement (elementName, elementValue);
  146. xmlDocument.Root.Add (newElement);
  147. ExtensionsClass.Validate (xmlDocument, schemaSet, null);
  148. }
  149. /*
  150. * test xml validation populating the XML tree with
  151. * the post-schema-validation infoset (PSVI)
  152. */
  153. [Test]
  154. public void XDocumentAddSchemaInfoValidate ()
  155. {
  156. // no. of elements before validation
  157. IEnumerable<XElement> elements = xmlDocument.Elements ();
  158. IEnumerator<XElement> elementsEnumerator = elements.GetEnumerator ();
  159. int beforeNoOfElements = 0;
  160. int beforeNoOfAttributes = 0;
  161. while (elementsEnumerator.MoveNext ()) {
  162. beforeNoOfElements++;
  163. if (!elementsEnumerator.Current.HasAttributes)
  164. continue;
  165. else {
  166. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  167. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  168. while (attributesEnumerator.MoveNext ())
  169. beforeNoOfAttributes++;
  170. }
  171. }
  172. // populate XML with PSVI
  173. validationSucceded = true;
  174. ExtensionsClass.Validate (xmlDocument, schemaSet,
  175. new ValidationEventHandler (TestValidationHandler), true);
  176. Assert.AreEqual (true, validationSucceded);
  177. // no. of elements after validation
  178. elements = xmlDocument.Elements ();
  179. elementsEnumerator = elements.GetEnumerator ();
  180. int afterNoOfElements = 0;
  181. int afterNoOfAttributes = 0;
  182. while (elementsEnumerator.MoveNext ()) {
  183. afterNoOfElements++;
  184. if (!elementsEnumerator.Current.HasAttributes)
  185. continue;
  186. else {
  187. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  188. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  189. while (attributesEnumerator.MoveNext ())
  190. afterNoOfAttributes++;
  191. }
  192. }
  193. Assert.GreaterOrEqual (afterNoOfAttributes, beforeNoOfAttributes, "newAttributes");
  194. Assert.GreaterOrEqual (afterNoOfElements, beforeNoOfElements, "newElements");
  195. }
  196. /*
  197. * test xml validation without populating the XML tree with
  198. * the post-schema-validation infoset (PSVI).
  199. */
  200. [Test]
  201. public void XDocumentNoSchemaInfoValidate ()
  202. {
  203. // no. of elements before validation
  204. IEnumerable<XElement> elements = xmlDocument.Elements ();
  205. IEnumerator<XElement> elementsEnumerator = elements.GetEnumerator ();
  206. int beforeNoOfElements = 0;
  207. int beforeNoOfAttributes = 0;
  208. while (elementsEnumerator.MoveNext ()) {
  209. beforeNoOfElements++;
  210. if (!elementsEnumerator.Current.HasAttributes)
  211. continue;
  212. else {
  213. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  214. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  215. while (attributesEnumerator.MoveNext ())
  216. beforeNoOfAttributes++;
  217. }
  218. }
  219. // don't populate XML with PSVI
  220. validationSucceded = true;
  221. ExtensionsClass.Validate (xmlDocument, schemaSet,
  222. new ValidationEventHandler (TestValidationHandler), false);
  223. Assert.AreEqual (true, validationSucceded);
  224. // no. of elements after validation
  225. elements = xmlDocument.Elements ();
  226. elementsEnumerator = elements.GetEnumerator ();
  227. int afterNoOfElements = 0;
  228. int afterNoOfAttributes = 0;
  229. while (elementsEnumerator.MoveNext ()) {
  230. afterNoOfElements++;
  231. if (!elementsEnumerator.Current.HasAttributes)
  232. continue;
  233. else {
  234. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  235. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  236. while (attributesEnumerator.MoveNext ())
  237. afterNoOfAttributes++;
  238. }
  239. }
  240. Assert.AreEqual (afterNoOfAttributes, beforeNoOfAttributes, "oldAttributes");
  241. Assert.AreEqual (afterNoOfElements, beforeNoOfElements, "oldElements");
  242. }
  243. // attribute validation succeeds after change
  244. [Test]
  245. public void XAttributeSuccessValidate ()
  246. {
  247. String elementName = "note";
  248. String attributeName = "date";
  249. object attributeValue = "2010-05-27";
  250. // validate the entire document
  251. validationSucceded = true;
  252. ExtensionsClass.Validate (xmlDocument, schemaSet,
  253. new ValidationEventHandler (TestValidationHandler), true);
  254. Assert.AreEqual (true, validationSucceded);
  255. // change and re-validate attribute value
  256. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  257. date.SetValue (attributeValue);
  258. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute,schemaSet,
  259. new ValidationEventHandler (TestValidationHandler));
  260. Assert.AreEqual (true, validationSucceded, "XAttributeSuccessValidate");
  261. }
  262. // attribute validation fails after change
  263. [Test]
  264. public void XAttributeFailValidate ()
  265. {
  266. String elementName = "note";
  267. String attributeName = "date";
  268. object attributeValue = "2010-12-32";
  269. // validate the entire document
  270. validationSucceded = true;
  271. ExtensionsClass.Validate (xmlDocument, schemaSet,
  272. new ValidationEventHandler (TestValidationHandler),true);
  273. Assert.AreEqual (true, validationSucceded);
  274. // change and re-validate attribute value
  275. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  276. date.SetValue (attributeValue);
  277. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute, schemaSet,
  278. new ValidationEventHandler (TestValidationHandler));
  279. Assert.AreEqual (false, validationSucceded, "XAttributeFailValidate");
  280. }
  281. /*
  282. * test if exception is thrown when xml document does not
  283. * conform to the xml schema after attribute value change
  284. */
  285. [Test]
  286. [ExpectedException (typeof (XmlSchemaValidationException))]
  287. public void XAttributeThrowExceptionValidate ()
  288. {
  289. String elementName = "note";
  290. String attributeName = "date";
  291. object attributeValue = "2010-12-32";
  292. // validate the entire document
  293. validationSucceded = true;
  294. ExtensionsClass.Validate (xmlDocument, schemaSet,
  295. new ValidationEventHandler ( TestValidationHandler),true);
  296. Assert.AreEqual (true, validationSucceded);
  297. // change and re-validate attribute value
  298. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  299. date.SetValue (attributeValue);
  300. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute, schemaSet, null);
  301. }
  302. // element validation succeeds after change
  303. [Test]
  304. [Category ("NotWorking")]
  305. public void XElementSuccessValidate ()
  306. {
  307. String parentElementName = "note";
  308. String childElementName = "body";
  309. object childElementValue = "Please call me!";
  310. // validate the entire document
  311. validationSucceded = true;
  312. ExtensionsClass.Validate (xmlDocument, schemaSet,
  313. new ValidationEventHandler (TestValidationHandler), true);
  314. Assert.AreEqual (true, validationSucceded);
  315. // alter element
  316. XElement root = xmlDocument.Element (parentElementName);
  317. root.SetElementValue (childElementName, childElementValue);
  318. ExtensionsClass.Validate (root, root.GetSchemaInfo ().SchemaElement, schemaSet,
  319. new ValidationEventHandler (TestValidationHandler));
  320. Assert.AreEqual (true, validationSucceded, "XElementSuccessValidate");
  321. }
  322. // element validation fails after change
  323. [Test]
  324. [Category ("NotWorking")]
  325. public void XElementFailValidate ()
  326. {
  327. String parentElementName = "note";
  328. String childElementName = "body";
  329. object childElementValue = "Don't forget to call me! Please!";
  330. // validate the entire document
  331. validationSucceded = true;
  332. ExtensionsClass.Validate (xmlDocument, schemaSet,
  333. new ValidationEventHandler (TestValidationHandler), true);
  334. Assert.AreEqual (true, validationSucceded);
  335. // alter element
  336. XElement root = xmlDocument.Element (parentElementName);
  337. root.SetElementValue (childElementName, childElementValue);
  338. ExtensionsClass.Validate (root, root.GetSchemaInfo ().SchemaElement, schemaSet,
  339. new ValidationEventHandler (TestValidationHandler));
  340. Assert.AreEqual (false, validationSucceded, "XElementFailValidate");
  341. }
  342. /*
  343. * test if exception is thrown when xml document does not
  344. * conform to the xml schema after element value change
  345. */
  346. [Test]
  347. [ExpectedException (typeof (XmlSchemaValidationException))]
  348. [Category ("NotWorking")]
  349. public void XElementThrowExceptionValidate ()
  350. {
  351. String parentElementName = "note" ;
  352. String childElementName = "body";
  353. object childElementValue = "Don't forget to call me! Please!";
  354. // validate the entire document
  355. validationSucceded = true;
  356. ExtensionsClass.Validate (xmlDocument, schemaSet,
  357. new ValidationEventHandler (TestValidationHandler), true);
  358. Assert.AreEqual (true, validationSucceded);
  359. // alter element
  360. XElement root = xmlDocument.Element (parentElementName);
  361. root.SetElementValue (childElementName, childElementValue);
  362. ExtensionsClass.Validate (root, root.GetSchemaInfo ().SchemaElement, schemaSet, null);
  363. }
  364. // test attribute schema info
  365. [Test]
  366. [Category ("NotWorking")]
  367. public void XAttributeGetSchemaInfo ()
  368. {
  369. String elementName = "note";
  370. String attributeName = "date";
  371. // validate the entire document
  372. validationSucceded = true;
  373. ExtensionsClass.Validate (xmlDocument, schemaSet,
  374. new ValidationEventHandler (TestValidationHandler), true);
  375. Assert.AreEqual (true, validationSucceded);
  376. // validate attribute
  377. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  378. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute, schemaSet,
  379. new ValidationEventHandler (TestValidationHandler));
  380. Assert.AreEqual (true, validationSucceded);
  381. IXmlSchemaInfo schemaInfo = ExtensionsClass.GetSchemaInfo (date);
  382. Assert.IsNotNull (schemaInfo, "XAttributeGetSchemaInfo");
  383. }
  384. // test element schema info
  385. [Test]
  386. [Category ("NotWorking")]
  387. public void XElementGetSchemaInfo ()
  388. {
  389. String elementName = "body";
  390. // validate the entire document
  391. validationSucceded = true;
  392. ExtensionsClass.Validate (xmlDocument, schemaSet,
  393. new ValidationEventHandler (TestValidationHandler), true);
  394. Assert.AreEqual (true, validationSucceded);
  395. // validate element
  396. XElement body = xmlDocument.Root.Element (elementName);
  397. ExtensionsClass.Validate (body, body.GetSchemaInfo ().SchemaElement, schemaSet,
  398. new ValidationEventHandler (TestValidationHandler));
  399. Assert.AreEqual (true, validationSucceded);
  400. IXmlSchemaInfo schemaInfo = ExtensionsClass.GetSchemaInfo (body);
  401. Assert.IsNotNull (schemaInfo, "ElementGetSchemaInfo");
  402. }
  403. }
  404. }