ExtensionsTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. [Category ("NotWorking")]
  114. public void XDocumentSuccessValidate ()
  115. {
  116. validationSucceded = true;
  117. ExtensionsClass.Validate (xmlDocument, schemaSet,
  118. new ValidationEventHandler (TestValidationHandler));
  119. Assert.AreEqual (true, validationSucceded, "XDocumentSuccessValidate");
  120. }
  121. // test failed validation
  122. [Test]
  123. [Category ("NotWorking")]
  124. public void XDocumentFailValidate ()
  125. {
  126. String elementName = "AlteringElementName";
  127. object elementValue = "AlteringElementValue";
  128. // alter XML document to invalidate
  129. XElement newElement = new XElement (elementName, elementValue);
  130. xmlDocument.Root.Add (newElement);
  131. validationSucceded = true;
  132. ExtensionsClass.Validate (xmlDocument, schemaSet,
  133. new ValidationEventHandler (TestValidationHandler));
  134. Assert.AreEqual (false, validationSucceded, "XDocumentFailValidate");
  135. }
  136. /*
  137. * test if exception is thrown when xml document does not
  138. * conform to the xml schema
  139. */
  140. [Test]
  141. [ExpectedException (typeof (XmlSchemaValidationException))]
  142. [Category ("NotWorking")]
  143. public void XDocumentThrowExceptionValidate ()
  144. {
  145. String elementName = "AlteringElementName";
  146. object elementValue = "AlteringElementValue";
  147. // alter XML document to invalidate
  148. XElement newElement = new XElement (elementName, elementValue);
  149. xmlDocument.Root.Add (newElement);
  150. ExtensionsClass.Validate (xmlDocument, schemaSet, null);
  151. }
  152. /*
  153. * test xml validation populating the XML tree with
  154. * the post-schema-validation infoset (PSVI)
  155. */
  156. [Test]
  157. [Category ("NotWorking")]
  158. public void XDocumentAddSchemaInfoValidate ()
  159. {
  160. // no. of elements before validation
  161. IEnumerable<XElement> elements = xmlDocument.Elements ();
  162. IEnumerator<XElement> elementsEnumerator = elements.GetEnumerator ();
  163. int beforeNoOfElements = 0;
  164. int beforeNoOfAttributes = 0;
  165. while (elementsEnumerator.MoveNext ()) {
  166. beforeNoOfElements++;
  167. if (!elementsEnumerator.Current.HasAttributes)
  168. continue;
  169. else {
  170. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  171. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  172. while (attributesEnumerator.MoveNext ())
  173. beforeNoOfAttributes++;
  174. }
  175. }
  176. // populate XML with PSVI
  177. validationSucceded = true;
  178. ExtensionsClass.Validate (xmlDocument, schemaSet,
  179. new ValidationEventHandler (TestValidationHandler), true);
  180. Assert.AreEqual (true, validationSucceded);
  181. // no. of elements after validation
  182. elements = xmlDocument.Elements ();
  183. elementsEnumerator = elements.GetEnumerator ();
  184. int afterNoOfElements = 0;
  185. int afterNoOfAttributes = 0;
  186. while (elementsEnumerator.MoveNext ()) {
  187. afterNoOfElements++;
  188. if (!elementsEnumerator.Current.HasAttributes)
  189. continue;
  190. else {
  191. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  192. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  193. while (attributesEnumerator.MoveNext ())
  194. afterNoOfAttributes++;
  195. }
  196. }
  197. Assert.GreaterOrEqual (afterNoOfAttributes, beforeNoOfAttributes, "newAttributes");
  198. Assert.GreaterOrEqual (afterNoOfElements, beforeNoOfElements, "newElements");
  199. }
  200. /*
  201. * test xml validation without populating the XML tree with
  202. * the post-schema-validation infoset (PSVI).
  203. */
  204. [Test]
  205. [Category ("NotWorking")]
  206. public void XDocumentNoSchemaInfoValidate ()
  207. {
  208. // no. of elements before validation
  209. IEnumerable<XElement> elements = xmlDocument.Elements ();
  210. IEnumerator<XElement> elementsEnumerator = elements.GetEnumerator ();
  211. int beforeNoOfElements = 0;
  212. int beforeNoOfAttributes = 0;
  213. while (elementsEnumerator.MoveNext ()) {
  214. beforeNoOfElements++;
  215. if (!elementsEnumerator.Current.HasAttributes)
  216. continue;
  217. else {
  218. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  219. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  220. while (attributesEnumerator.MoveNext ())
  221. beforeNoOfAttributes++;
  222. }
  223. }
  224. // don't populate XML with PSVI
  225. validationSucceded = true;
  226. ExtensionsClass.Validate (xmlDocument, schemaSet,
  227. new ValidationEventHandler (TestValidationHandler), false);
  228. Assert.AreEqual (true, validationSucceded);
  229. // no. of elements after validation
  230. elements = xmlDocument.Elements ();
  231. elementsEnumerator = elements.GetEnumerator ();
  232. int afterNoOfElements = 0;
  233. int afterNoOfAttributes = 0;
  234. while (elementsEnumerator.MoveNext ()) {
  235. afterNoOfElements++;
  236. if (!elementsEnumerator.Current.HasAttributes)
  237. continue;
  238. else {
  239. IEnumerable<XAttribute> attributes = elementsEnumerator.Current.Attributes ();
  240. IEnumerator<XAttribute> attributesEnumerator = attributes.GetEnumerator ();
  241. while (attributesEnumerator.MoveNext ())
  242. afterNoOfAttributes++;
  243. }
  244. }
  245. Assert.AreEqual (afterNoOfAttributes, beforeNoOfAttributes, "oldAttributes");
  246. Assert.AreEqual (afterNoOfElements, beforeNoOfElements, "oldElements");
  247. }
  248. // attribute validation succeeds after change
  249. [Test]
  250. [Category ("NotWorking")]
  251. public void XAttributeSuccessValidate ()
  252. {
  253. String elementName = "note";
  254. String attributeName = "date";
  255. object attributeValue = "2010-05-27";
  256. // validate the entire document
  257. validationSucceded = true;
  258. ExtensionsClass.Validate (xmlDocument, schemaSet,
  259. new ValidationEventHandler (TestValidationHandler), true);
  260. Assert.AreEqual (true, validationSucceded);
  261. // change and re-validate attribute value
  262. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  263. date.SetValue (attributeValue);
  264. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute,schemaSet,
  265. new ValidationEventHandler (TestValidationHandler));
  266. Assert.AreEqual (true, validationSucceded, "XAttributeSuccessValidate");
  267. }
  268. // attribute validation fails after change
  269. [Test]
  270. [Category ("NotWorking")]
  271. public void XAttributeFailValidate ()
  272. {
  273. String elementName = "note";
  274. String attributeName = "date";
  275. object attributeValue = "2010-12-32";
  276. // validate the entire document
  277. validationSucceded = true;
  278. ExtensionsClass.Validate (xmlDocument, schemaSet,
  279. new ValidationEventHandler (TestValidationHandler),true);
  280. Assert.AreEqual (true, validationSucceded);
  281. // change and re-validate attribute value
  282. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  283. date.SetValue (attributeValue);
  284. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute, schemaSet,
  285. new ValidationEventHandler (TestValidationHandler));
  286. Assert.AreEqual (false, validationSucceded, "XAttributeFailValidate");
  287. }
  288. /*
  289. * test if exception is thrown when xml document does not
  290. * conform to the xml schema after attribute value change
  291. */
  292. [Test]
  293. [ExpectedException (typeof (XmlSchemaValidationException))]
  294. [Category ("NotWorking")]
  295. public void XAttributeThrowExceptionValidate ()
  296. {
  297. String elementName = "note";
  298. String attributeName = "date";
  299. object attributeValue = "2010-12-32";
  300. // validate the entire document
  301. validationSucceded = true;
  302. ExtensionsClass.Validate (xmlDocument, schemaSet,
  303. new ValidationEventHandler ( TestValidationHandler),true);
  304. Assert.AreEqual (true, validationSucceded);
  305. // change and re-validate attribute value
  306. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  307. date.SetValue (attributeValue);
  308. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute, schemaSet, null);
  309. }
  310. // element validation succeeds after change
  311. [Test]
  312. [Category ("NotWorking")]
  313. public void XElementSuccessValidate ()
  314. {
  315. String parentElementName = "note";
  316. String childElementName = "body";
  317. object childElementValue = "Please call me!";
  318. // validate the entire document
  319. validationSucceded = true;
  320. ExtensionsClass.Validate (xmlDocument, schemaSet,
  321. new ValidationEventHandler (TestValidationHandler), true);
  322. Assert.AreEqual (true, validationSucceded);
  323. // alter element
  324. XElement root = xmlDocument.Element (parentElementName);
  325. root.SetElementValue (childElementName, childElementValue);
  326. ExtensionsClass.Validate (root, root.GetSchemaInfo ().SchemaElement, schemaSet,
  327. new ValidationEventHandler (TestValidationHandler));
  328. Assert.AreEqual (true, validationSucceded, "XElementSuccessValidate");
  329. }
  330. // element validation fails after change
  331. [Test]
  332. [Category ("NotWorking")]
  333. public void XElementFailValidate ()
  334. {
  335. String parentElementName = "note";
  336. String childElementName = "body";
  337. object childElementValue = "Don't forget to call me! Please!";
  338. // validate the entire document
  339. validationSucceded = true;
  340. ExtensionsClass.Validate (xmlDocument, schemaSet,
  341. new ValidationEventHandler (TestValidationHandler), true);
  342. Assert.AreEqual (true, validationSucceded);
  343. // alter element
  344. XElement root = xmlDocument.Element (parentElementName);
  345. root.SetElementValue (childElementName, childElementValue);
  346. ExtensionsClass.Validate (root, root.GetSchemaInfo ().SchemaElement, schemaSet,
  347. new ValidationEventHandler (TestValidationHandler));
  348. Assert.AreEqual (false, validationSucceded, "XElementFailValidate");
  349. }
  350. /*
  351. * test if exception is thrown when xml document does not
  352. * conform to the xml schema after element value change
  353. */
  354. [Test]
  355. [ExpectedException (typeof (XmlSchemaValidationException))]
  356. [Category ("NotWorking")]
  357. public void XElementThrowExceptionValidate ()
  358. {
  359. String parentElementName = "note" ;
  360. String childElementName = "body";
  361. object childElementValue = "Don't forget to call me! Please!";
  362. // validate the entire document
  363. validationSucceded = true;
  364. ExtensionsClass.Validate (xmlDocument, schemaSet,
  365. new ValidationEventHandler (TestValidationHandler), true);
  366. Assert.AreEqual (true, validationSucceded);
  367. // alter element
  368. XElement root = xmlDocument.Element (parentElementName);
  369. root.SetElementValue (childElementName, childElementValue);
  370. ExtensionsClass.Validate (root, root.GetSchemaInfo ().SchemaElement, schemaSet, null);
  371. }
  372. // test attribute schema info
  373. [Test]
  374. [Category ("NotWorking")]
  375. public void XAttributeGetSchemaInfo ()
  376. {
  377. String elementName = "note";
  378. String attributeName = "date";
  379. // validate the entire document
  380. validationSucceded = true;
  381. ExtensionsClass.Validate (xmlDocument, schemaSet,
  382. new ValidationEventHandler (TestValidationHandler), true);
  383. Assert.AreEqual (true, validationSucceded);
  384. // validate attribute
  385. XAttribute date = xmlDocument.Element (elementName).Attribute (attributeName);
  386. ExtensionsClass.Validate (date, date.GetSchemaInfo ().SchemaAttribute, schemaSet,
  387. new ValidationEventHandler (TestValidationHandler));
  388. Assert.AreEqual (true, validationSucceded);
  389. IXmlSchemaInfo schemaInfo = ExtensionsClass.GetSchemaInfo (date);
  390. Assert.IsNotNull (schemaInfo, "XAttributeGetSchemaInfo");
  391. }
  392. // test element schema info
  393. [Test]
  394. [Category ("NotWorking")]
  395. public void XElementGetSchemaInfo ()
  396. {
  397. String elementName = "body";
  398. // validate the entire document
  399. validationSucceded = true;
  400. ExtensionsClass.Validate (xmlDocument, schemaSet,
  401. new ValidationEventHandler (TestValidationHandler), true);
  402. Assert.AreEqual (true, validationSucceded);
  403. // validate element
  404. XElement body = xmlDocument.Root.Element (elementName);
  405. ExtensionsClass.Validate (body, body.GetSchemaInfo ().SchemaElement, schemaSet,
  406. new ValidationEventHandler (TestValidationHandler));
  407. Assert.AreEqual (true, validationSucceded);
  408. IXmlSchemaInfo schemaInfo = ExtensionsClass.GetSchemaInfo (body);
  409. Assert.IsNotNull (schemaInfo, "ElementGetSchemaInfo");
  410. }
  411. }
  412. }