31fda435418aad5ed53ca565b7d74233548eae4f
[mono.git] / mcs / class / System.XML / Test / XmlElementTests.cs
1 //
2 // XmlElementTests
3 //
4 // Author:
5 //   Jason Diamond (jason@injektilo.org)
6 //
7 // (C) 2002 Jason Diamond  http://injektilo.org/
8 //
9
10 using System;
11 using System.Xml;
12 using System.IO;
13 using System.Text;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Xml
18 {
19         public class XmlElementTests : TestCase
20         {
21                 public XmlElementTests () : base ("MonoTests.System.Xml.XmlElementTests testsuite") { }
22                 public XmlElementTests (string name) : base (name) { }
23
24                 private XmlDocument document;
25
26                 protected override void SetUp()
27                 {
28                         document = new XmlDocument ();
29                 }
30
31                 private void AssertElement (XmlElement element, string prefix,
32                                             string localName, string namespaceURI,
33                                             int attributesCount)
34                 {
35                         AssertEquals (prefix != String.Empty ? prefix + ":" + localName : localName, element.Name);
36                         AssertEquals (prefix, element.Prefix);
37                         AssertEquals (localName, element.LocalName);
38                         AssertEquals (namespaceURI, element.NamespaceURI);
39                         //AssertEquals (attributesCount, element.Attributes.Count);
40                 }
41
42                 public void TestCloneNode ()
43                 {
44                         XmlElement element = document.CreateElement ("foo");
45                         XmlElement child = document.CreateElement ("bar");
46                         XmlElement grandson = document.CreateElement ("baz");
47
48                         element.SetAttribute ("attr1", "val1");
49                         element.SetAttribute ("attr2", "val2");
50                         element.AppendChild (child);
51                         child.SetAttribute ("attr3", "val3");
52                         child.AppendChild (grandson);
53                         
54                         document.AppendChild (element);
55                         XmlNode deep = element.CloneNode (true);
56                         // AssertEquals ("These should be the same", deep.OuterXml, element.OuterXml); 
57                         AssertNull ("This is not null", deep.ParentNode);
58                         Assert ("Copies, not pointers", !Object.ReferenceEquals (element,deep));
59
60                         XmlNode shallow = element.CloneNode (false);
61                         AssertNull ("This is not null", shallow.ParentNode);
62                         Assert ("Copies, not pointers", !Object.ReferenceEquals (element,shallow));
63                         AssertEquals ("Shallow clones shalt have no children!", false, shallow.HasChildNodes);
64                 }
65
66                 public void TestCreateElement1 ()
67                 {
68                         XmlElement element = document.CreateElement ("name");
69                         AssertElement (element, String.Empty, "name", String.Empty, 0);
70                 }
71
72                 public void TestCreateElement1WithPrefix ()
73                 {
74                         XmlElement element = document.CreateElement ("prefix:localName");
75                         AssertElement (element, "prefix", "localName", String.Empty, 0);
76                 }
77
78                 public void TestCreateElement2 ()
79                 {
80                         XmlElement element = document.CreateElement ("qualifiedName", "namespaceURI");
81                         AssertElement (element, String.Empty, "qualifiedName",
82                                        "namespaceURI", 0);
83                 }
84
85                 public void TestCreateElement2WithPrefix ()
86                 {
87                         XmlElement element = document.CreateElement ("prefix:localName", "namespaceURI");
88                         AssertElement (element, "prefix", "localName", "namespaceURI", 0);
89                 }
90
91                 public void TestCreateElement3 ()
92                 {
93                         XmlElement element = document.CreateElement ("prefix", "localName", "namespaceURI");
94                         AssertElement (element, "prefix", "localName", "namespaceURI", 0);
95                 }
96
97                 public void TestCreateElement3WithNullNamespace ()
98                 {
99                         // bug #26855, NamespaceURI should NEVER be null.
100                         XmlElement element = document.CreateElement (null, "localName", null);
101                         AssertElement (element, String.Empty, "localName", String.Empty, 0);
102                 }
103
104                 public void TestInnerAndOuterXml ()
105                 {
106                         XmlElement element;
107                         XmlText text;
108                         XmlComment comment;
109                         
110                         element = document.CreateElement ("foo");
111                         AssertEquals (String.Empty, element.InnerXml);
112                         AssertEquals ("<foo />", element.OuterXml);
113
114                         text = document.CreateTextNode ("bar");
115                         element.AppendChild (text);
116                         AssertEquals ("bar", element.InnerXml);
117                         AssertEquals ("<foo>bar</foo>", element.OuterXml);
118
119                         element.SetAttribute ("baz", "quux");
120                         AssertEquals ("bar", element.InnerXml);
121                         AssertEquals ("<foo baz=\"quux\">bar</foo>", element.OuterXml);
122
123                         comment = document.CreateComment ("squonk");
124                         element.AppendChild (comment);
125                         AssertEquals ("bar<!--squonk-->", element.InnerXml);
126                         AssertEquals ("<foo baz=\"quux\">bar<!--squonk--></foo>", element.OuterXml);
127
128
129                 }
130
131                 public void TestSetGetAttribute ()
132                 {
133                         XmlElement element = document.CreateElement ("foo");
134                         element.SetAttribute ("attr1", "val1");
135                         element.SetAttribute ("attr2", "val2");
136                         AssertEquals ("val1", element.GetAttribute ("attr1"));
137                         AssertEquals ("val2", element.GetAttribute ("attr2"));
138                 }
139
140                 public void TestGetElementsByTagNameNoNameSpace ()
141                 {
142                         string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
143                                 <price>34.95</price></book><book><title>Bear and the Dragon</title>
144                                 <author>Tom Clancy</author><price>6.95</price></book><book>
145                                 <title>Bourne Identity</title><author>Robert Ludlum</author>
146                                 <price>9.95</price></book><Fluffer><Nutter><book>
147                                 <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
148                                 <price>9.95</price></book></Nutter></Fluffer></library>";
149
150                         MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
151                         document = new XmlDocument ();
152                         document.Load (memoryStream);
153                         XmlNodeList bookList = document.GetElementsByTagName ("book");
154                         AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
155                 }
156
157                 public void TestOuterXmlWithNamespace ()
158                 {
159                         XmlElement element = document.CreateElement ("foo", "bar", "#foo");
160                         AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
161                 }               
162         }
163 }