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