2004-03-31 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Test / System.Xml / nist_dom / fundamental / NodeList / NodeList.cs
1 //**************************************************************************\r
2 //\r
3 //\r
4 //                       National Institute Of Standards and Technology\r
5 //                                    DTS Version 1.0\r
6 //         \r
7 //                                   NodeList Interface\r
8 //\r
9 // Written by: Carmelo Montanez\r
10 // Modified by:  Mary Brady\r
11 //\r
12 // Ported to System.Xml by: Mizrahi Rafael rafim@mainsoft.com\r
13 // Mainsoft Corporation (c) 2003-2004\r
14 //**************************************************************************\r
15 using System;\r
16 using System.Xml;\r
17 \r
18 using nist_dom;\r
19 using NUnit.Framework;\r
20 \r
21 namespace nist_dom.fundamental\r
22 {\r
23     [TestFixture]\r
24     public class NodeListTest : Assertion\r
25     {\r
26         public static int i = 2;\r
27 /*\r
28         public testResults[] RunTests()\r
29         {\r
30             testResults[] tests = new testResults[] {core0001N(), core0002N(), core0003N(),core0004N(),\r
31                                                         core0005N(), core0006N(), core0007N(), core0008N(),\r
32                                                         core0009N()};\r
33   \r
34             return tests;\r
35         }\r
36 */\r
37         //------------------------ test case core-0001N ------------------------\r
38         //\r
39         // Testing feature - The items in the list are accessible via an integral\r
40         //                   index starting from zero. (index equal 0)\r
41         //\r
42         // Testing approach - Create a list of all the children elements of the\r
43         //                    third employee and access its first child by using\r
44         //                    an index of 0.  This should result in "employeeId"\r
45         //                    being selected.  Further we evaluate its content\r
46         //                    (by examining its "nodeName" attribute) to ensure \r
47         //                    the proper element was accessed.\r
48         //\r
49         // Semantic Requirements: 1\r
50         //\r
51         //----------------------------------------------------------------------------\r
52 \r
53         [Test]\r
54         public void core0001N()\r
55         {\r
56             string computedValue = "";\r
57             string expectedValue = "employeeId";\r
58             System.Xml.XmlNode employeeId = null;\r
59             System.Xml.XmlElement testNode = null;\r
60 \r
61             testResults results = new testResults("Core0001N");\r
62             try\r
63             {\r
64                 results.description = "The elements in the list are accessible via an "+\r
65                     "integral index starting from 0 (this test checks "+\r
66                     "for index equal to 0).";\r
67                 //\r
68                 // Retrieve targeted data.\r
69                 //\r
70                 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1); \r
71                 employeeId = util.getSubNodes(testNode).Item(util.FIRST); \r
72                 computedValue = employeeId.Name;\r
73             }\r
74             catch(System.Exception ex)\r
75             {\r
76                 computedValue = "Exception " + ex.Message;\r
77             }\r
78 \r
79             //\r
80             // Write out results \r
81             //\r
82             results.expected = expectedValue;\r
83             results.actual = computedValue;\r
84 \r
85             AssertEquals (results.expected, results.actual);\r
86         }\r
87 \r
88         //------------------------ End test case core-0001N --------------------------\r
89         //\r
90         //--------------------------- test case core-0002N ---------------------------\r
91         //\r
92         // Testing feature - The items in the list are accessible via an integral\r
93         //                   index starting from zero. (index not equal 0)\r
94         //\r
95         // Testing approach - Create a list of all the children elements of the\r
96         //                    third employee and access its fourth child by \r
97         //                    using an index of 3.  This should result in "salary"\r
98         //                    being selected.  Further we evaluate its "nodeName"\r
99         //                    attribute to ensure the proper element was accessed.\r
100         //\r
101         // Semantic Requirements: 1\r
102         //\r
103         //----------------------------------------------------------------------------\r
104 \r
105         [Test]\r
106         public void core0002N()\r
107         {\r
108             string computedValue = "";\r
109             string expectedValue = "salary";\r
110             System.Xml.XmlNode salary = null;\r
111             System.Xml.XmlElement testNode = null;\r
112 \r
113             testResults results = new testResults("Core0002N");\r
114             try\r
115             {\r
116                 results.description = "The elements in the list are accessible via an "+\r
117                     "integral index starting from 0 (this test checks "+\r
118                     "for index not equal 0).";\r
119                 //\r
120                 //  Retrieve targeted data.\r
121                 //\r
122                 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1); \r
123                 salary = util.getSubNodes(testNode).Item(util.FOURTH);\r
124                 computedValue = salary.Name;\r
125             }\r
126             catch(System.Exception ex)\r
127             {\r
128                 computedValue = "Exception " + ex.Message;\r
129             }\r
130             //\r
131             //    Write out results\r
132             //\r
133             results.expected = expectedValue;\r
134             results.actual = computedValue;\r
135 \r
136             AssertEquals (results.expected, results.actual);\r
137         }\r
138 \r
139         //------------------------ End test case core-0002N --------------------------\r
140         //\r
141         //--------------------------- test case core-0003N ---------------------------\r
142         //\r
143         // Testing feature - The "item(index)" method returns the indexth item \r
144         //                   in the collection.\r
145         //\r
146         // Testing approach - Create a list of all the Element children of the\r
147         //                    third employee and access its first child by invoking \r
148         //                    the "item(index)" method with index = 0.  This should \r
149         //                    cause the method to return the "employeeId" child.\r
150         //                    Further we evaluate the returned item's "nodeName" \r
151         //                    attribute to ensure the correct item was returned. \r
152         //          \r
153         // Semantic Requirements: 2 \r
154         //\r
155         //----------------------------------------------------------------------------\r
156 \r
157         [Test]\r
158         public void core0003N()\r
159         {\r
160             string computedValue = "";\r
161             string expectedValue = "employeeId";\r
162             System.Xml.XmlNode employeeId = null;\r
163             System.Xml.XmlElement testNode = null;\r
164 \r
165             testResults results = new testResults("Core0003N");\r
166             try\r
167             {\r
168                 results.description = "The \"item(index)\" method returns the indexth "+\r
169                     "item in the collection (return first item).";\r
170                 //\r
171                 // Retrieve targeted data.\r
172                 //\r
173                 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1);\r
174                 employeeId = util.getSubNodes(testNode).Item(util.FIRST);\r
175                 computedValue = employeeId.Name;\r
176             }\r
177             catch(System.Exception ex)\r
178             {\r
179                 computedValue = "Exception " + ex.Message;\r
180             }\r
181             //\r
182             // Write out results\r
183             //\r
184             results.expected = expectedValue;\r
185             results.actual = computedValue;\r
186 \r
187             AssertEquals (results.expected, results.actual);\r
188         }\r
189 \r
190         //------------------------ End test case core-0003N --------------------------\r
191         //\r
192         //--------------------------- test case core-0004N ---------------------------\r
193         //\r
194         // Testing feature - The "item(index)" method returns the indexth item \r
195         //                   in the collection.\r
196         //\r
197         // Testing approach - Create a list of all the Element children of the\r
198         //                    third employee and access its first child by invoking\r
199         //                    the "item(index)" method with index equals to the last \r
200         //                    item in the list.  This should cause the method to \r
201         //                    return the "address" child.  Further we evaluate the\r
202         //                    returned item's "nodeName" attribute to ensure the \r
203         //                    correct item was returned.\r
204         //\r
205         // Semantic Requirements: 2\r
206         //\r
207         //----------------------------------------------------------------------------\r
208 \r
209         [Test]\r
210         public void core0004N()\r
211         {\r
212             string computedValue = "";\r
213             string expectedValue = "address";\r
214             System.Xml.XmlNode address = null;\r
215             System.Xml.XmlElement testNode = null;\r
216 \r
217             testResults results = new testResults("Core0004N");\r
218             try\r
219             {\r
220                 results.description = "The \"item(index)\" method returns the indxth "+\r
221                     "item in the collection (return last item).";\r
222                 //\r
223                 // Retrieve targeted data.\r
224                 //\r
225                 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1);\r
226                 address = util.getSubNodes(testNode).Item(util.SIXTH);\r
227                 computedValue = address.Name;\r
228             }\r
229             catch(System.Exception ex)\r
230             {\r
231                 computedValue = "Exception " + ex.Message;\r
232             }\r
233             //\r
234             // Write out results\r
235             //\r
236             results.expected = expectedValue;\r
237             results.actual = computedValue;\r
238 \r
239             AssertEquals (results.expected, results.actual);\r
240         }\r
241 \r
242         //------------------------ End test case core-0004N --------------------------\r
243         //\r
244         //--------------------------- test case core-0005N ---------------------------\r
245         //\r
246         // Testing feature - If the index is greater than or equal to number of  \r
247         //                   nodes, the "item(index)" method returns null.\r
248         //\r
249         // Testing approach - Create a list of all the Element children of the third\r
250         //                    employee and then invoke its "item(index)" method with \r
251         //                    index equal to 6 (the number of nodes in the list).  This\r
252         //                    should cause the method to return null.\r
253         //\r
254         // Semantic Requirements: 3 \r
255         //\r
256         //----------------------------------------------------------------------------\r
257 \r
258         [Test]\r
259         public void core0005N()\r
260         {\r
261             object computedValue = null;\r
262             object expectedValue = null; \r
263             System.Xml.XmlElement testNode = null;\r
264 \r
265             testResults results = new testResults("Core0005N");\r
266             try\r
267             {\r
268                 results.description = "The \"item(index)\" method returns null if the "+\r
269                     "index is greater than or equal to the number of "+\r
270                     "nodes (index = number of nodes).";\r
271                 //\r
272                 // invoke the "item(index)" method with index equal to the number of nodes\r
273                 // in the list (6, count starts at zero).  It should return null.\r
274                 //\r
275                 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1);\r
276                 computedValue = util.getSubNodes(testNode).Item(util.SEVENTH);\r
277             }\r
278             catch(System.Exception ex)\r
279             {\r
280                 computedValue = "Exception " + ex.Message;\r
281             }\r
282             //\r
283             // Write out results\r
284             //\r
285             results.expected = (expectedValue == null).ToString();\r
286             results.actual = (computedValue == null).ToString();\r
287 \r
288             AssertEquals (results.expected, results.actual);\r
289         }\r
290 \r
291         //------------------------ End test case core-0005N --------------------------\r
292         //\r
293         //--------------------------- test case core-0006N ---------------------------\r
294         //\r
295         // Testing feature - If the index is greater than or equal to number of\r
296         //                   nodes, the "item(index)" method returns null.\r
297         //\r
298         // Testing approach - Create a list of all the Element children of the third\r
299         //                    employee and then invoke the "item(index)" with index\r
300         //                    equal to 7 (index is greater than number of nodes).  \r
301         //                    This should cause the method to return null.\r
302         //\r
303         // Semantic Requirements: 3\r
304         //\r
305         //----------------------------------------------------------------------------\r
306 \r
307         [Test]\r
308         public void core0006N()\r
309         {\r
310             object computedValue = null;\r
311             object expectedValue = null;\r
312             System.Xml.XmlElement testNode = null;\r
313 \r
314             testResults results = new testResults("Core0006N");\r
315             try\r
316             {\r
317                 results.description = "The \"item(index)\" method returns null if the "+ \r
318                     "index is greater than or equal to the number of "+\r
319                     "nodes (index > number of nodes).";\r
320                 //\r
321                 // Retrieve targeted data.  All counts start from zero\r
322                 //\r
323                 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1);\r
324                 computedValue = util.getSubNodes(testNode).Item(util.EIGHT);\r
325             }\r
326             catch(System.Exception ex)\r
327             {\r
328                 computedValue = "Exception " + ex.Message;\r
329             }\r
330             //\r
331             // Write out results\r
332             //\r
333             results.expected = (expectedValue == null).ToString();\r
334             results.actual = (computedValue == null).ToString();\r
335 \r
336             AssertEquals (results.expected, results.actual);\r
337         }\r
338 \r
339         //------------------------ End test case core-0006N --------------------------\r
340         //\r
341         //--------------------------- test case core-0007N ---------------------------\r
342         //\r
343         // Testing feature - The "length" attribute contains the number of items in\r
344         //                   the list.\r
345         //\r
346         // Testing approach - Create a list of all the Element children of the third\r
347         //                    employee and then access the "length" attribute.\r
348         //                    It should contain the value 6.\r
349         //\r
350         // Semantic Requirements: 4 \r
351         //\r
352         //----------------------------------------------------------------------------\r
353 \r
354         [Test]\r
355         public void core0007N()\r
356         {\r
357             string computedValue = "0";\r
358             string expectedValue = "6";\r
359             //System.Xml.XmlNodeList thirdEmployeeList = null;\r
360 \r
361             testResults results = new testResults("Core0007N");\r
362             try\r
363             {\r
364                 results.description = "The \"length\" attribute contains the number of "+\r
365                     "nodes in the list (non empty list).";\r
366                 //\r
367                 // retrieve the targeted data and access the "length" attribute. \r
368                 //\r
369                 System.Xml.XmlElement testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1);\r
370                 computedValue = util.getSubNodes(testNode).Count.ToString();\r
371             }\r
372             catch(System.Exception ex)\r
373             {\r
374                 computedValue = "Exception " + ex.Message;\r
375             }\r
376             //\r
377             // Write out results\r
378             //\r
379             results.expected = expectedValue;\r
380             results.actual = computedValue;\r
381 \r
382             AssertEquals (results.expected, results.actual);\r
383         }\r
384 \r
385         //------------------------ End test case core-0007N --------------------------\r
386         //\r
387         //--------------------------- test case core-0008N ---------------------------\r
388         //\r
389         // Testing feature - The "length" attribute contains the number of items in\r
390         //                   the list (test for empty list).\r
391         //\r
392         // Testing approach - Create a list of all the children of the Text node \r
393         //                    inside the first child o the third employee and\r
394         //                    then access its "length" attribute.  It should \r
395         //                    contain the value 0.\r
396         //\r
397         // Semantic Requirements: 4\r
398         //\r
399         //----------------------------------------------------------------------------\r
400 \r
401         [Test]\r
402         public void core0008N()\r
403         {\r
404             string computedValue = "0";\r
405             string expectedValue = "0";\r
406             System.Xml.XmlNode testNode = null;\r
407             System.Xml.XmlNode textNode = null;\r
408 \r
409             testResults results = new testResults("Core0008N");\r
410             try\r
411             {\r
412                 results.description = "The \"length\" attribute contains the number of "+\r
413                     "nodes in the list (test for empty list).";\r
414                 //\r
415                 // Access the targeted data and examine the "length" attribute of an\r
416                 // empty list.\r
417                 //\r
418                 testNode = util.nodeObject(util.THIRD,util.FIRST);\r
419                 textNode = testNode.FirstChild;\r
420                 computedValue = textNode.ChildNodes.Count.ToString();\r
421             }\r
422             catch(System.Exception ex)\r
423             {\r
424                 computedValue = "Exception " + ex.Message;\r
425             }\r
426             //\r
427             // Write out results\r
428             //\r
429             results.expected = expectedValue;\r
430             results.actual = computedValue;\r
431 \r
432             AssertEquals (results.expected, results.actual);\r
433         }\r
434 \r
435         //------------------------ End test case core-0008N --------------------------\r
436         //\r
437         //--------------------------- test case core-0009 ---------------------------\r
438         //\r
439         // Testing feature - The range of valid child nodes indices is 0 to length - 1.\r
440         // \r
441         // Testing approach - Create a list of all the Element children of the \r
442         //                    third employee and traverse the list from index\r
443         //                    0 to index length - 1.\r
444         //\r
445         // Semantic Requirements: 5 \r
446         //\r
447         //----------------------------------------------------------------------------\r
448 \r
449         [Test]\r
450         public void core0009N()\r
451         {\r
452             string computedValue = "";\r
453             string expectedValue = "employeeId name position salary gender address ";\r
454             int lastIndex = 0;\r
455             int listLength = 0;\r
456             System.Xml.XmlElement testNode = null;\r
457 \r
458             testResults results = new testResults("Core0009N");\r
459             try\r
460             {\r
461                 results.description = "The range of valid child nodes indices is 0 to "+\r
462                     "length - 1.";\r
463                 //\r
464                 // Retrieve the targeted data and determine the length of the list.\r
465                 //\r
466                 testNode =  (System.Xml.XmlElement)util.nodeObject(util.THIRD,-1);\r
467                 listLength = util.getSubNodes(testNode).Count; \r
468                 lastIndex = listLength - 1;\r
469                 //\r
470                 // Traverse the list from 0 to length - 1.  All indices should be valid.\r
471                 //\r
472                 for (int index = 0; index <= lastIndex; index++)\r
473                     computedValue += util.getSubNodes(testNode).Item(index).Name+" ";\r
474             }\r
475             catch(System.Exception ex)\r
476             {\r
477                 computedValue = "Exception " + ex.Message;\r
478             }\r
479             //\r
480             // Write out results. \r
481             //\r
482             results.expected = expectedValue;\r
483             results.actual = computedValue;\r
484 \r
485             AssertEquals (results.expected, results.actual);\r
486         }\r
487 \r
488         //------------------------ End test case core-0009N --------------------------\r
489     }\r
490 }