2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.XML / Test / System.Xml / nist_dom / fundamental / Text / Text.cs
1 //**************************************************************************
2 //
3 //
4 //                       National Institute Of Standards and Technology
5 //                                     DTS Version 1.0
6 //         
7 //                                      Text Interface
8 //
9 // Written by: Carmelo Montanez
10 // Modified by:  Mary Brady
11 //
12 // Ported to System.Xml by: Mizrahi Rafael rafim@mainsoft.com
13 // Mainsoft Corporation (c) 2003-2004
14 //**************************************************************************
15 using System;
16 using System.Xml;
17
18 using nist_dom;
19 using NUnit.Framework;
20
21 namespace nist_dom.fundamental
22 {
23     [TestFixture]
24     public class TextTest
25     {
26         public static int i = 2;
27 /*
28         public testResults[] RunTests()
29         {
30             testResults[] tests = new testResults[] {core0001T(), core0002T(), core0003T(),core0004T(),
31                                                         core0005T(), core0006T(), core0007T(), core0008T(),
32                                                         core0009T()};
33   
34             return tests;
35         }
36 */
37         //------------------------ test case core-0001T ------------------------
38         //
39         // Testing feature -  If there is no markup inside an Element or Attr node
40         //                    content, then the text is contained in a single object 
41         //                    implementing the Text interface that is the only child
42         //                    of the element.
43         //
44         // Testing approach - Retrieve the textual data from the second child of the
45         //                    third employee.  That Text node contains a block of 
46         //                    multiple text lines without markup, so they should be 
47         //                    treated as a single Text node.  The "nodeValue" attribute 
48         //                    should contain the combination of the two lines.
49         //                    
50         // Semantic Requirements: 1
51         //
52         //----------------------------------------------------------------------------
53
54         [Test]
55         public void core0001T()
56         {
57             string computedValue = "";
58             string expectedValue = "Roger\n Jones";
59             System.Xml.XmlNode testNode = null;
60             System.Xml.XmlCharacterData testNodeData = null;
61
62             testResults results = new testResults("Core0001T");
63             try
64             {
65                 results.description = "If there is no markup language in a block of text, " +
66                     "then the content of the text is contained into " +
67                     "an object implementing the Text interface that is " +
68                     "the only child of the element."; 
69                 //
70                 // Retrieve the second child of the second employee and access its
71                 // textual data. 
72                 //
73                 testNode = util.nodeObject(util.THIRD,util.SECOND); 
74                 testNode.Normalize();
75                 testNodeData = (System.Xml.XmlCharacterData)testNode.FirstChild;
76                 computedValue = testNodeData.Value;
77             }
78             catch(System.Exception ex)
79             {
80                 computedValue = "Exception " + ex.Message;
81             }
82
83             //
84             // Write out results 
85             //
86             results.expected = expectedValue;
87             results.actual = computedValue;
88
89             Assert.AreEqual (results.expected, results.actual);
90         }
91
92         //------------------------ End test case core-0001T --------------------------
93         //
94         //-------------------------- test case core-0002T ----------------------------
95         //
96         // Testing feature -  If there is markup inside the Text element content,
97         //                    then the text is parsed into a list of elements and text
98         //                    that forms the list of children of the element.
99         //
100         // Testing approach - Retrieve the textual data from the last child of the
101         //                    third employee.  That node is composed of two
102         //                    EntityReferences nodes and two Text nodes.  After the
103         //                    content of the node is parsed, the "address" Element
104         //                    should contain four children with each one of the
105         //                    EntityReferences containing one child in turn. 
106         //
107         // Semantic Requirements: 2 
108         //
109         //----------------------------------------------------------------------------
110
111         [Test]
112         public void core0002T()
113         {
114             string computedValue = "";
115             string expectedValue = "1900 Dallas Road Dallas, Texas\n 98554";
116             System.Xml.XmlNode testNode = null;
117             System.Xml.XmlNode textBlock1 = null;
118             System.Xml.XmlNode textBlock2 = null;
119             System.Xml.XmlNode textBlock3 = null;
120             System.Xml.XmlNode textBlock4 = null; 
121
122             testResults results = new testResults("Core0002T");
123             try
124             {
125                 results.description = "If there is markup language in the content of the " +
126                     "element then the content is parsed into a " +
127                     "list of elements and Text that are the children of " +
128                     "the element";
129                 //
130                 // This last child of the second employee should now have four children, 
131                 // two Text nodes and two EntityReference nodes.  Retrieve each one of them 
132                 // and in the case of EntityReferences retrieve their respective children. 
133                 //
134                 testNode = util.nodeObject(util.SECOND,util.SIXTH);
135                 textBlock1 = testNode.ChildNodes.Item(util.FIRST).FirstChild;
136                 textBlock2 = testNode.ChildNodes.Item(util.SECOND);
137                 textBlock3 = testNode.ChildNodes.Item(util.THIRD).FirstChild;
138                 textBlock4 = testNode.ChildNodes.Item(util.FOURTH);
139
140                 computedValue += textBlock1.Value;
141                 computedValue += textBlock2.Value;
142                 computedValue += textBlock3.Value;
143                 computedValue += textBlock4.Value;
144             }
145             catch(System.Exception ex)
146             {
147                 computedValue = "Exception " + ex.Message;
148             }
149
150             //
151             // Write out results
152             //
153             results.expected = expectedValue;
154             results.actual = computedValue;
155
156             Assert.AreEqual (results.expected, results.actual);
157         }
158
159         //------------------------ End test case core-0002 --------------------------
160         //
161         //-------------------------- test case core-0003T ---------------------------
162         //
163         // Testing feature -  The "splitText(offset)" method breaks the Text node
164         //                    into two Text nodes at the specified offset keeping  
165         //                    each node as siblings in the tree.
166         //
167         // Testing approach - Retrieve the textual data from the second child of the 
168         //                    third employee and invoke its "splitText(offset)" method.
169         //                    The method splits the Text node into two new sibling
170         //                    Text Nodes keeping both of them in the tree.  This test
171         //                    checks the "nextSibling" attribute of the original node
172         //                    to ensure that the two nodes are indeed siblings. 
173         //
174         // Semantic Requirements: 3 
175         //
176         //----------------------------------------------------------------------------
177
178         [Test]
179         public void core0003T()
180         {
181             string computedValue = "";
182             string expectedValue = "Jones";
183             System.Xml.XmlText oldTextNode = null;
184             System.Xml.XmlNode testNode = null;
185
186             testResults results = new testResults("Core0003T");
187             try
188             {
189                 results.description = "The \"splitText(offset)\" method breaks the Text node " +
190                     "into two Text nodes at the specified offset, keeping each " +
191                     "node in the tree as siblings.";
192                 //
193                 // Retrieve the targeted data.
194                 //
195                 testNode = util.nodeObject(util.THIRD,util.SECOND);
196                 oldTextNode = (System.Xml.XmlText)testNode.FirstChild;
197                 //
198                 // Split the two lines of text into two different Text nodes. 
199                 //
200                 oldTextNode.SplitText(util.EIGHT);
201                 computedValue = oldTextNode.NextSibling.Value;
202             }
203             catch(System.Exception ex)
204             {
205                 computedValue = "Exception " + ex.Message;
206             }
207
208             //
209             //  Write out results
210             //
211             results.expected = expectedValue;
212             results.actual = computedValue;
213
214             util.resetData();
215
216             Assert.AreEqual (results.expected, results.actual);
217         }
218
219         //------------------------ End test case core-0003T --------------------------
220         //
221         //-------------------------- test case core-0004T ---------------------------
222         //
223         // Testing feature -  After The "splitText(offset)" method breaks the Text node
224         //                    into two Text nodes, the original node contains all the
225         //                    content up to the offset point. 
226         //
227         // Testing approach - Retrieve the textual data from the second child
228         //                    of the third employee and invoke the "splitText(offset)"
229         //                    method.  The original Text node should contain all the
230         //                    content up to the offset point.  The "nodeValue" 
231         //                    attribute is invoke to check that indeed the original 
232         //                    node now contains the first five characters
233         //
234         // Semantic Requirements: 4 
235         //
236         //----------------------------------------------------------------------------
237
238         [Test]
239         public void core0004T()
240         {
241             string computedValue = "";
242             string expectedValue = "Roger";
243             System.Xml.XmlText oldTextNode = null;
244             System.Xml.XmlNode testNode = null;
245
246             testResults results = new testResults("Core0004T");
247             try
248             {
249                 results.description = "After the \"splitText(offset)\" method is invoked, the " +
250                     "original Text node contains all of the content up to the " +
251                     "offset point.";
252                 //
253                 // Retrieve targeted data.
254                 //
255                 testNode = util.nodeObject(util.THIRD,util.SECOND);
256                 oldTextNode = (System.Xml.XmlText)testNode.FirstChild;
257                 //
258                 // Split the two lines of text into two different Text nodes.
259                 //
260                 oldTextNode.SplitText(util.SIXTH);
261                 computedValue = oldTextNode.Value;
262             }
263             catch(System.Exception ex)
264             {
265                 computedValue = "Exception " + ex.Message;
266             }
267
268             //
269             //  Write out results
270             //
271             results.expected = expectedValue;
272             results.actual = computedValue;
273
274             util.resetData();
275
276             Assert.AreEqual (results.expected, results.actual);
277         }
278
279         //------------------------ End test case core-0004T --------------------------
280         //
281         //-------------------------- test case core-0005T ---------------------------
282         //
283         // Testing feature -  After The "splitText(offset)" method breaks the Text node
284         //                    into two Text nodes, the new Text node contains all the
285         //                    content at and after the offset point.
286         //
287         // Testing approach - Retrieve the textual data from the second child of the
288         //                    third employee and invoke the "splitText(offset)" method.
289         //                    The new Text node should contain all the content at 
290         //                    and after the offset point.  The "nodeValue" attribute 
291         //                    is invoked to check that indeed the new node now
292         //                    contains the first characters at and after position
293         //                    seven (starting from 0).
294         //
295         // Semantic Requirements: 5 
296         //
297         //----------------------------------------------------------------------------
298
299         [Test]
300         public void core0005T()
301         {
302             string computedValue = "";
303             string expectedValue = " Jones";
304             System.Xml.XmlText oldTextNode = null;
305             System.Xml.XmlText newTextNode = null;
306             System.Xml.XmlNode testNode = null;
307   
308             testResults results = new testResults("Core0005T");
309             try
310             {
311                 results.description = "After the \"splitText(offset)\" method is invoked, the " +
312                     "new Text node contains all of the content from the offset " +
313                     "point to the end of the text.";
314                 //
315                 // Retrieve the targeted data.
316                 //
317                 testNode = util.nodeObject(util.THIRD,util.SECOND);
318                 oldTextNode = (System.Xml.XmlText)testNode.FirstChild;
319                 //
320                 // Split the two lines of text into two different Text nodes.
321                 //
322                 newTextNode = oldTextNode.SplitText(util.SEVENTH);
323                 computedValue = newTextNode.Value;
324             }
325             catch(System.Exception ex)
326             {
327                 computedValue = "Exception " + ex.Message;
328             }
329
330             //
331             // Write out results
332             //
333             results.expected = expectedValue;
334             results.actual = computedValue;
335
336             util.resetData(); 
337
338             Assert.AreEqual (results.expected, results.actual);
339         }
340
341         //------------------------ End test case core-0005T --------------------------
342         //
343         //-------------------------- test case core-0006T ---------------------------
344         //
345         // Testing feature -  The "splitText(offset)" method returns the new Text 
346         //                    node.
347         //
348         // Testing approach - Retrieve the textual data from the last child of the
349         //                    first employee and invoke its "splitText(offset)" method.
350         //                    The method should return the new Text node.  The offset
351         //                    value used for this test is 30.  The "nodeValue" 
352         //                    attribute is invoked to check that indeed the new node 
353         //                    now contains the characters at and after postion 30 
354         //                    (counting from 0).
355         //
356         // Semantic Requirements: 6 
357         //
358         //----------------------------------------------------------------------------
359
360         [Test]
361         public void core0006T()
362         {
363             string computedValue = "";
364             string expectedValue = "98551";
365             System.Xml.XmlText oldTextNode = null;
366             System.Xml.XmlText newTextNode = null;
367             System.Xml.XmlNode testNode = null;
368
369             testResults results = new testResults("Core0006T");
370             try
371             {
372                 results.description = "The \"splitText(offset)\" method returns the " +
373                     "new Text node.";
374                 //
375                 // Retrieve the targeted data.
376                 //
377                 testNode = util.nodeObject(util.FIRST,util.SIXTH);
378                 oldTextNode = (System.Xml.XmlText)testNode.FirstChild;
379                 //
380                 // Split the two lines of text into two different Text nodes. 
381                 //
382                 newTextNode = oldTextNode.SplitText(30);
383                 computedValue = newTextNode.Value;
384             }
385             catch(System.Exception ex)
386             {
387                 computedValue = "Exception " + ex.Message;
388             }
389             //
390             // Write out results
391             //
392             results.expected = expectedValue;
393             results.actual = computedValue;
394     
395             util.resetData();
396
397             Assert.AreEqual (results.expected, results.actual);
398         }
399
400         //------------------------ End test case core-0006T --------------------------
401         //
402         //-------------------------- test case core-0007T ---------------------------
403         //
404         // Testing feature -  The "splitText(offset)" method raises an INDEX_SIZE_ERR
405         //                    Exception if the specified offset is negative. 
406         //
407         // Testing approach - Retrieve the textual data from the second child of 
408         //                    the third employee and invoke its "splitText(offset)" 
409         //                    method with "offset" equals to a negative number.  It 
410         //                    should raise the desired exception.
411         //
412         // Semantic Requirements: 7 
413         //
414         //----------------------------------------------------------------------------
415
416         [Test]
417         public void core0007T()
418         {
419             string computedValue = "";
420             System.Xml.XmlText oldTextNode = null;
421             System.Xml.XmlText newTextNode = null;
422             System.Xml.XmlNode testNode = null;
423             string expectedValue = "System.ArgumentOutOfRangeException";//util.INDEX_SIZE_ERR;
424
425             testResults results = new testResults("Core0007T");
426             try
427             {
428                 results.description = "The \"splitText(offset)\" method raises an " +
429                     "INDEX_SIZE_ERR Exception if the specified " +
430                     "offset is negative.";
431
432                 //
433                 // Retrieve the targeted data
434                 //
435                 testNode = util.nodeObject(util.THIRD,util.SECOND);
436                 oldTextNode = (System.Xml.XmlText)testNode.FirstChild;
437                 //
438                 // Call the "spitText(offset)" method with "offset" equal to a negative 
439                 // number should raise an exception.
440                 //
441                 try 
442                 {
443                     oldTextNode.SplitText(-69);
444                 }
445                 catch(System.Exception ex) 
446                 {
447                     computedValue = ex.GetType ().FullName; 
448                 }
449             }
450             catch(System.Exception ex)
451             {
452                 computedValue = "Exception " + ex.Message;
453             }
454
455             results.expected = expectedValue;
456             results.actual = computedValue;
457
458             util.resetData();
459
460             Assert.AreEqual (results.expected, results.actual);
461         }
462         //------------------------ End test case core-0007T --------------------------
463         //
464         //-------------------------- test case core-0008T ----------------------------
465         //
466         // Testing feature -  The "splitText(offset)" method raises an 
467                 //                    ArgumentOutOfRangeException if the specified offset is greater than the 
468         //                    number of 16-bit units in the Text node.
469         //
470         // Testing approach - Retrieve the textual data from the second child of
471         //                    third employee and invoke its "splitText(offset)" 
472         //                    method with "offset" greater than the number of
473         //                    characters in the Text node.  It should raise the 
474         //                    desired exception.
475         //
476         // Semantic Requirements: 7
477         //
478         //----------------------------------------------------------------------------
479
480             [Test]
481         public void core0008T()
482             {
483                 string computedValue = "";
484                 System.Xml.XmlText oldTextNode = null;
485                 System.Xml.XmlNode testNode = null;
486                 string expectedValue = "System.ArgumentOutOfRangeException";
487
488                 testResults results = new testResults("Core0008T");
489                 try
490                 {
491                     results.description = "The \"splitText(offset)\" method raises an " +
492                         "ArgumentOutOfRangeException if the specified " +
493                         "offset is greater than the number of 16-bit units " +
494                         "in the Text node.";
495                     //
496                     // Retrieve the targeted data.
497                     //
498                     testNode = util.nodeObject(util.THIRD,util.SECOND);
499                     oldTextNode = (System.Xml.XmlText)testNode.FirstChild;
500                     //
501                     // Call the "spitText(offset)" method with "offset" greater than the numbers 
502                     // of characters in the Text node, it should raise an exception.
503
504                     try 
505                     {
506                         oldTextNode.SplitText(300);
507                     }
508                     catch(System.Exception ex) 
509                     {
510                         computedValue = ex.GetType().ToString();
511                     }
512                 }
513                 catch(System.Exception ex)
514                 {
515                     computedValue = "Exception " + ex.Message;
516                 }
517
518                     results.expected = expectedValue;
519                     results.actual = computedValue;
520
521                     util.resetData();
522
523                     Assert.AreEqual (results.expected, results.actual);
524                 }
525         //------------------------ End test case core-0008T --------------------------
526         //
527         //-------------------------- test case core-0009T ----------------------------
528         //
529         // Testing feature -  The "splitText(offset)" method raises a
530         //                    NO_MODIFICATION_ALLOWED_ERR Exception if
531         //                    the node is readonly. 
532         //
533         // Testing approach - Retrieve the textual data from the first EntityReference 
534         //                    inside the last child of the second employee and invoke 
535         //                    its splitText(offset) method.  Descendants of 
536         //                    EntityReference nodes are readonly and therefore the 
537         //                    desired exception should be raised. 
538         //
539         // Semantic Requirements: 8 
540         //
541         //----------------------------------------------------------------------------
542
543         [Test]
544         [Category ("NotDotNet")] // MS DOM is buggy
545         public void core0009T()
546         {
547             string computedValue = "";
548             System.Xml.XmlNode testNode = null;
549             System.Xml.XmlText readOnlyText = null;
550             string expectedValue = "System.ArgumentException";//util.NO_MODIFICATION_ALLOWED_ERR;
551
552             testResults results = new testResults("Core0009T");
553             try
554             {
555                 results.description = "The \"splitText(offset)\" method raises a " +
556                     "NO_MODIFICATION_ALLOWED_ERR Exception if the " +
557                     "node is readonly.";
558                 //
559                 // Attempt to modify descendants of an EntityReference node should raise 
560                 // an exception. 
561                 //
562                 testNode = util.nodeObject(util.SECOND,util.SIXTH);
563                 readOnlyText = (System.Xml.XmlText)testNode.ChildNodes.Item(util.FIRST).FirstChild;
564
565                 try 
566                 {
567                     readOnlyText.SplitText(5);
568                 }
569                 catch(ArgumentException ex) 
570                 {
571                     computedValue = ex.GetType ().FullName; 
572                 }
573
574             }
575             catch(System.Exception ex)
576             {
577                 computedValue = "Exception " + ex.Message;
578             }
579
580             results.expected = expectedValue;
581             results.actual = computedValue;
582
583             util.resetData();
584
585             Assert.AreEqual (results.expected, results.actual);
586         }
587
588         //------------------------ End test case core-0009T --------------------------
589     }
590 }