1 //**************************************************************************
4 // National Institute Of Standards and Technology
9 // Written by: Carmelo Montanez
11 // Ported to System.Xml by: Mizrahi Rafael rafim@mainsoft.com
12 // Mainsoft Corporation (c) 2003-2004
14 //**************************************************************************
18 using NUnit.Framework;
\r
20 namespace nist_dom.fundamental
\r
23 public class AttrTest : Assertion//, ITest
\r
25 public static int i = 1;
\r
27 public testResults[] RunTests()
\r
29 testResults[] tests = new testResults[] {core0001A(), core0002A(), core0003A(),core0004A(),
30 core0005A(), core0006A(), core0007A(), core0008A(),
31 core0009A(), core0010A(), core0011A(), core0012A(),
32 core0013A(), core0014A()};
\r
37 //------------------------ test case core-0001A ------------------------
39 // Testing feature - The parentNode attribute for an Attr object should
42 // Testing approach - Retrieve the attribute named "domestic" from the last
43 // child of of the first employee and examine its
44 // parentNode attribute. This test uses the
45 // "GetNamedItem(name)" method from the NamedNodeMap
48 // Semantic Requirements: 1
50 //----------------------------------------------------------------------------
53 public void core0001A()
55 object computedValue = null;
56 object expectedValue = null;
57 System.Xml.XmlNode testNode = null;
58 System.Xml.XmlAttribute domesticAttr = null;
60 testResults results = new testResults("Core0001A");
64 results.description = "The ParentNode attribute should be null for" +
67 // Retrieve targeted data and examine parentNode attribute.
69 testNode = util.nodeObject(util.FIRST, util.SIXTH);
70 domesticAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("domestic");
71 computedValue = domesticAttr.ParentNode;
76 catch(System.Exception ex)
\r
78 computedValue = "Exception " + ex.Message;
81 results.expected = (expectedValue == null).ToString();
82 results.actual = (computedValue == null).ToString();
83 AssertEquals (results.expected, results.actual);
88 //------------------------ End test case core-0001A --------------------------
90 //------------------------- test case core-0002A ----------------------------
93 // Written By: Carmelo Montanez
95 // Testing feature - The previousSibling attribute for an Attr object
98 // Testing approach - Retrieve the attribute named "domestic" from the last
99 // child of of the first employee and examine its
100 // previousSibling attribute. This test uses the
101 // "GetNamedItem(name)" method from the NamedNodeMap
104 // Semantic Requirements: 1
106 //----------------------------------------------------------------------------
109 public void core0002A()
111 object computedValue = null;
112 object expectedValue = null;
113 System.Xml.XmlAttribute domesticAttr = null;
114 System.Xml.XmlNode testNode = null;
116 testResults results = new testResults("Core0002A");
119 results.description = "The previousSibling attribute should be " +
120 "null for an Attr object.";
122 // Retrieve the targeted data and examine its previousSibling attribute.
124 testNode = util.nodeObject(util.FIRST,util.SIXTH);
125 domesticAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("domestic");
126 computedValue = domesticAttr.PreviousSibling;
128 catch(System.Exception ex)
\r
130 computedValue = "Exception " + ex.Message;
135 results.expected = (expectedValue == null).ToString();
136 results.actual = (computedValue == null).ToString();
138 AssertEquals (results.expected, results.actual);
142 //------------------------ End test case core-0002A --------------------------
144 //------------------------- test case core-0003A ----------------------------
145 // Written By: Carmelo Montanez
147 // Testing feature - The nextSibling attribute for an Attr object should
150 // Testing approach - Retrieve the attribute named "domestic" from the
151 // last child of of the first employee and examine
152 // its nextSibling attribute. This test uses the
153 // "GetNamedItem(name)" method from the NamedNodeMap
156 // Semantic Requirements: 1
158 //----------------------------------------------------------------------------
161 public void core0003A()
163 object computedValue = null;
164 object expectedValue = null;
165 System.Xml.XmlAttribute domesticAttr = null;
166 System.Xml.XmlNode testNode = null;
168 testResults results = new testResults("Core0003A");
171 results.description = "The nextSibling attribute should be null " +
172 "for an Attr object.";
174 // Retrieve the targeted data and examine its nextSibling attribute.
176 testNode = util.nodeObject(util.FIRST,util.SIXTH);
177 domesticAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("domestic");
178 computedValue = domesticAttr.NextSibling;
180 catch(System.Exception ex)
\r
182 computedValue = "Exception " + ex.Message;
187 results.expected = (expectedValue == null).ToString();
188 results.actual = (computedValue == null).ToString();
190 AssertEquals (results.expected, results.actual);
194 //------------------------ End test case core-0003A --------------------------
196 //------------------------- test case core-0004A ----------------------------
198 // Written By: Carmelo Montanez
200 // Testing feature - Attr objects may be associated with Element nodes
201 // contained within a DocumentFragment.
203 // Testing approach - Create a new DocumentFragment object and add a newly
204 // created Element node to it (with one attribute). Once
205 // the element is added, its attribute should be available
206 // as an attribute associated with an Element within a
209 // Semantic Requirements: 2
211 //----------------------------------------------------------------------------
214 public void core0004A()
216 string computedValue = "";
217 string expectedValue = "domestic";
218 System.Xml.XmlAttribute domesticAttr = null;
220 testResults results = new testResults("Core0004A");
223 results.description = "Attr objects may be associated with Element " +
224 "nodes contained within a DocumentFragment.";
226 System.Xml.XmlDocumentFragment docFragment = util.getDOMDocument().CreateDocumentFragment();
227 System.Xml.XmlElement newElement = (System.Xml.XmlElement)util.createNode(util.ELEMENT_NODE,"element1");
229 // The new DocumentFragment is empty upon creation. Set an attribute for
230 // a newly created element and add the element to the documentFragment.
232 newElement.SetAttribute("domestic","Yes");
233 docFragment.AppendChild(newElement);
235 // Access the attributes of the only child of the documentFragment
237 domesticAttr = (System.Xml.XmlAttribute)docFragment.FirstChild.Attributes.Item(0) ;
238 computedValue = domesticAttr.Name;
240 catch(System.Exception ex)
\r
242 computedValue = "Exception " + ex.Message;
247 results.expected = expectedValue;
248 results.actual = computedValue;
251 AssertEquals (results.expected, results.actual);
255 //------------------------ End test case core-0004A --------------------------
257 //-------------------------- test case core-0005A ----------------------------
259 // Testing feature - If an Attr is explicitly assigned any value, then that
260 // value is the attribute's effective value.
262 // Testing approach - Retrieve the attribute name "domestic" from the last
263 // child of of the first employee element and examine its
264 // assigned value. This test uses the
265 // "GetNamedItem(name)" method from the NamedNodeMap
268 // Semantic Requirements: 3
270 //----------------------------------------------------------------------------
273 public void core0005A()
275 string computedValue = "";
276 string expectedValue = "Yes";
277 System.Xml.XmlAttribute domesticAttr = null;
278 System.Xml.XmlNode testNode = null;
280 testResults results = new testResults("Core0005A");
283 results.description = "If an attribute is explicitly assigned any value, " +
284 "then that value is the attribute's effective value.";
286 // Retrieve the targeted data and examine its assigned value.
288 testNode = util.nodeObject(util.FIRST,util.SIXTH);
289 domesticAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("domestic");
290 computedValue = domesticAttr.Value;
292 catch(System.Exception ex)
\r
294 computedValue = "Exception " + ex.Message;
299 results.expected = expectedValue;
300 results.actual = computedValue;
302 AssertEquals (results.expected, results.actual);
306 //------------------------ End test case core-0005A --------------------------
308 //-------------------------- test case core-0006A ----------------------------
310 // Testing feature - If there is no explicit value assigned to an attribute
311 // and there is a declaration for this attribute and that
312 // declaration includes a default value, then that default
313 // value is the Attribute's default value.
315 // Testing approach - Retrieve the attribute named "street" from the
316 // last child of of the first employee and examine its
317 // value. That value should be the value given during
318 // the declaration of the attribute in the DTD file.
319 // This test uses the "GetNamedItem(name)" method from
320 // the NamedNodeMap interface.
322 // Semantic Requirements: 4
324 //----------------------------------------------------------------------------
327 public void core0006A()
329 string computedValue = "";
330 string expectedValue = "Yes";
331 System.Xml.XmlAttribute streetAttr = null;
332 System.Xml.XmlNode testNode = null;
334 testResults results = new testResults("Core0006A");
337 results.description = "If there is no explicit value assigned to an " +
338 "attribute and there is a declaration for this " +
339 "attribute and that declaration includes a default " +
340 "value, then that default value is the Attribute's " +
343 // Retrieve the targeted data and examine its default value.
345 testNode = util.nodeObject(util.FIRST,util.SIXTH);
346 streetAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("street");
347 computedValue = streetAttr.Value;
349 catch(System.Exception ex)
\r
351 computedValue = "Exception " + ex.Message;
356 results.expected = expectedValue;
357 results.actual = computedValue;
359 AssertEquals (results.expected, results.actual);
363 //------------------------ End test case core-0006A --------------------------
365 //-------------------------- test case core-0007A ---------------------------
367 // Testing feature - The "name" Attribute of an Attribute node.
369 // Testing approach - Retrieve the attribute named "street" from the
370 // last child of the second employee and examine its "name"
371 // attribute. This test uses the "GetNamedItem(name)"
372 // method from the NamedNodeMap interface.
374 // Semantic Requirements: 5
376 //----------------------------------------------------------------------------
379 public void core0007A()
381 string computedValue = "";
382 string expectedValue = "street";
383 System.Xml.XmlAttribute streetAttr = null;
384 System.Xml.XmlNode testNode = null;
386 testResults results = new testResults("Core0007A");
389 results.description = "The \"name\" attribute of an Attr object contains " +
390 "the name of that attribute.";
392 // Retrieve the targeted data and capture its assigned name.
394 testNode = util.nodeObject(util.SECOND,util.SIXTH);
395 streetAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("street");
396 computedValue = streetAttr.Name;
398 catch(System.Exception ex)
\r
400 computedValue = "Exception " + ex.Message;
405 results.expected = expectedValue;
406 results.actual = computedValue;
408 AssertEquals (results.expected, results.actual);
412 //------------------------ End test case core-0007A --------------------------
414 //-------------------------- test case core-0008A ---------------------------
416 // Testing feature - The "specified" attribute of an Attr node should be set
417 // to true if the attribute was explicitly given a value.
419 // Testing approach - Retrieve the attribute named "doestic" from the last
420 // child of the first employee and examine its "specified"
421 // attribute. It should be set to true. This test
422 // uses the "GetNamedItem(name)" method from the
423 // NamedNodeMap interface.
425 // Semantic Requirements: 6
427 //----------------------------------------------------------------------------
430 public void core0008A()
432 string computedValue = "";//0;
433 string expectedValue = "True";
434 System.Xml.XmlNode testNode = null;
435 System.Xml.XmlAttribute domesticAttr = null;
437 testResults results = new testResults("Core0008A");
440 results.description = "The \"specified\" attribute for an Attr object " +
441 "should be set to true if the attribute was " +
442 "explictly given a value.";
444 // Retrieve the targeted data and capture its "specified" value.
446 testNode = util.nodeObject(util.FIRST,util.SIXTH);
447 domesticAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("domestic");;
448 computedValue = domesticAttr.Specified.ToString();
450 catch(System.Exception ex)
\r
452 computedValue = "Exception " + ex.Message;
457 results.expected = expectedValue;
458 results.actual = computedValue;
460 AssertEquals (results.expected, results.actual);
464 //------------------------ End test case core-0008A --------------------------
466 //-------------------------- test case core-0009A ---------------------------
468 // Testing feature - The "specified" attribute for an Attr node should be
469 // set to false if the attribute was not explicitly given
472 // Testing approach - Retrieve the attribute named "street"(not explicity
473 // given a value) from the last child of the first employee
474 // and examine its "specified" attribute. It should be
475 // set to false. This test uses the
476 // "GetNamedItem(name)" method from the NamedNodeMap
479 // Semantic Requirements: 6
481 //----------------------------------------------------------------------------
484 public void core0009A()
486 string computedValue = "";//0;
487 string expectedValue = "False";
488 System.Xml.XmlAttribute streetAttr = null;
489 System.Xml.XmlNode testNode = null;
491 testResults results = new testResults("Core0009A");
494 results.description = "The \"specified\" attribute for an Attr node " +
495 "should be set to false if the attribute was " +
496 "not explictly given a value.";
498 // Retrieve the targeted data and capture its "specified" attribute.
500 testNode = util.nodeObject(util.FIRST,util.SIXTH);
501 streetAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("street");
502 computedValue = streetAttr.Specified.ToString();
504 catch(System.Exception ex)
\r
506 computedValue = "Exception " + ex.Message;
511 results.expected = expectedValue;
512 results.actual = computedValue;
514 AssertEquals (results.expected, results.actual);
518 //------------------------ End test case core-0009A --------------------------
520 //-------------------------- test case core-0010A ---------------------------
522 // Testing feature - The "specified" attribute for an Attr node should be
523 // automatically flipped to true if value of the attribute
524 // is changed (even its ends up having a default DTD value)
526 // Testing approach - Retrieve the attribute named "street" from the last
527 // child of the third employee and change its value to "Yes"
528 // (which is its default DTD value). This should cause the
529 // "specified" attribute to be flipped to true. This test
530 // makes use of the "setAttribute(name,value )" method from
531 // the Element interface and the "GetNamedItem(name)"
532 // method from the NamedNodeMap interface.
534 // Semantic Requirements: 7
536 //----------------------------------------------------------------------------
539 public void core0010A()
541 string computedValue = "";//"";
542 string expectedValue = "True";
543 System.Xml.XmlAttribute streetAttr = null;
544 System.Xml.XmlElement testNode = null;
546 testResults results = new testResults("Core0010A");
549 results.description = "The \"specified\" attribute for an Attr node " +
550 "should be flipped to true if the attribute value " +
551 "is changed (even it changed to its default value).";
553 // Retrieve the targeted data and set a new attribute for it, then
554 // capture its "specified" attribute.
556 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,util.FIFTH);
557 testNode.SetAttribute("street","Yes");//testNode.node.setAttribute("street","Yes");
558 streetAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("street");
559 computedValue = streetAttr.Specified.ToString();
561 catch(System.Exception ex)
\r
563 computedValue = "Exception " + ex.Message;
568 results.expected = expectedValue;
569 results.actual = computedValue;
571 AssertEquals (results.expected, results.actual);
575 //------------------------ End test case core-0010A --------------------------
577 //-------------------------- test case core-0011A ---------------------------
579 // Testing feature - To respecify the attribute to its default value from the
580 // DTD, the attribute must be deleted. The implementation
581 // will then make a new attribute available with the
582 // "specified" attribute set to false.
584 // Testing approach - Retrieve the attribute named "street" from the last
585 // child of the third employee and delete it. The
586 // implementation should then create a new attribute with
587 // its default value and "specified" set to false. This
588 // test uses the "removeAttribute(name)" from the Element
589 // interface and the "GetNamedItem(name)" method from the
590 // NamedNodeMap interface.
592 // Semantic Requirements: 8
594 //----------------------------------------------------------------------------
597 public void core0011A()
599 string computedValue = "";//"";
600 string expectedValue = "False";
601 System.Xml.XmlAttribute streetAttr = null;
602 System.Xml.XmlElement testNode = null;
604 testResults results = new testResults("Core0011A");
607 results.description = "Re-setting an attribute to its default value " +
608 "requires that the attribute be deleted. The " +
609 "implementation should create a new attribute " +
610 "with its \"specified\" attribute set to false.";
612 // Retrieve the targeted data, remove the "street" attribute and capture
613 // its specified attribute.
615 testNode = (System.Xml.XmlElement)util.nodeObject(util.THIRD,util.SIXTH);
616 testNode.RemoveAttribute("street");//testNode.node.removeAttribute("street");
617 streetAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("street");
618 computedValue = streetAttr.Specified.ToString();
620 catch(System.Exception ex)
\r
622 computedValue = "Exception " + ex.Message;
627 results.expected = expectedValue;
628 results.actual = computedValue;
630 AssertEquals (results.expected, results.actual);
634 //------------------------ End test case core-0011A --------------------------
636 //-------------------------- test case core-0012A ---------------------------
638 // Testing feature - Upon retrieval, the "value" of an attribute is returned
639 // as a string with characters and general entity references
640 // replaced with their values.
642 // Testing approach - Retrieve the attribute named "street" from the last
643 // child of the fourth employee and examine its value
644 // attribute. This value should be "Yes" after the
645 // EntityReference is replaced with its value. This
646 // test uses the "GetNamedItem(name)" method from
647 // the NamedNodeMap interface.
649 // Semantic Requirements: 9
651 //----------------------------------------------------------------------------
654 public void core0012A()
656 string computedValue = "";
657 string expectedValue = "Yes";
658 System.Xml.XmlAttribute streetAttr = null;
659 System.Xml.XmlNode testNode = null;
661 testResults results = new testResults("Core0012A");
664 results.description = "Upon retrieval the \"value\" attribute of an Attr" +
665 "object is returned as a string with any Entity " +
666 "References replaced with their values.";
668 // Retrieve the targeted data.
670 testNode = util.nodeObject(util.FOURTH,util.SIXTH);
671 streetAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("street");
672 computedValue = streetAttr.Value;
674 catch(System.Exception ex)
\r
676 computedValue = "Exception " + ex.Message;
681 results.expected = expectedValue;
682 results.actual = computedValue;
684 AssertEquals (results.expected, results.actual);
688 //------------------------ End test case core-0012A --------------------------
690 //-------------------------- test case core-0013A ---------------------------
692 // Testing feature - On setting, the "value" attribute of an Attr node
693 // creates a Text node with the unparsed content of
696 // Testing approach - Retrieve the attribute named "street" from the last
697 // child of the fourth employee and assign the "Y%ent1;"
698 // string to its value attribute. This value is not yet
699 // parsed and therefore should still be the same upon
700 // retrieval. This test uses the "GetNamedItem(name)"
701 // method from the NamedNodeMap interface.
703 // Semantic Requirements: 10
705 //----------------------------------------------------------------------------
708 public void core0013A()
710 string computedValue = "";
711 string expectedValue = "Y%ent1;";
712 System.Xml.XmlAttribute streetAttr = null;
713 System.Xml.XmlNode testNode = null;
715 testResults results = new testResults("Core0013A");
718 results.description = "On setting, the \"value\" attribute of an Attr " +
719 "object creates a Text node with the unparsed " +
720 "content of the string.";
722 // Retrieve the targeted data, assign a value to it and capture its
723 // "value" attribute.
725 testNode = util.nodeObject(util.FOURTH,util.SIXTH);
726 streetAttr = (System.Xml.XmlAttribute)testNode.Attributes.GetNamedItem("street");
727 streetAttr.Value = "Y%ent1;";
728 computedValue = streetAttr.Value;
730 catch(System.Exception ex)
\r
732 computedValue = "Exception " + ex.Message;
737 results.expected = expectedValue;
738 results.actual = computedValue;
740 AssertEquals (results.expected, results.actual);
744 //------------------------ End test case core-0013A --------------------------
746 //-------------------------- test case core-0014A ---------------------------
748 // Testing feature - Setting the "value" attribute raises a
749 // NO_MODIFICATION_ALLOWED_ERR Exception if the
752 // Testing approach - Retrieve the first attribute of the Entity node named
753 // "ent4" and attempt to change its value attribute.
754 // Since the descendants of Entity nodes are readonly, the
755 // desired exception should be raised.
757 // Semantic Requirements: 11
759 //----------------------------------------------------------------------------
762 public void core0014A()
764 string computedValue = "";
765 System.Xml.XmlNode testNode = null;
766 System.Xml.XmlAttribute readOnlyAttribute = null;
767 string expectedValue = "System.ArgumentException";//util.NO_MODIFICATION_ALLOWED_ERR;
769 testResults results = new testResults("Core0014A");
772 results.description = "Setting the \"value\" attribute raises a " +
773 "NO_MODIFICATION_ALLOWED_ERR Exception if " +
774 "the node is readonly.";
777 // Retrieve the targeted data.
779 testNode = util.getEntity("ent4");
780 readOnlyAttribute = (System.Xml.XmlAttribute)testNode.FirstChild.Attributes.Item(0);
782 // attempt to set a value on a readonly node should raise an exception.
786 readOnlyAttribute.Value = "ABCD";
788 catch (ArgumentException ex)
\r
790 computedValue = ex.GetType ().FullName;
793 catch(System.Exception ex)
\r
795 computedValue = "Exception " + ex.Message;
797 results.expected = expectedValue;
798 results.actual = computedValue;
802 AssertEquals (results.expected, results.actual);
805 //------------------------ End test case core-0014A --------------------------