ExtensionsTest.cs 15 KB

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