Merge pull request #1631 from alexanderkyte/stringbuilder-referencesource
[mono.git] / mcs / class / System.Xml.Linq / Test / System.Xml.Linq / XNamespaceTest.cs
1 //
2 // Authors:
3 //   Atsushi Enomoto
4 //
5 // Copyright 2007 Novell (http://www.novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 using System;
28 using System.IO;
29 using System.Xml;
30 using System.Xml.Linq;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Xml.Linq
35 {
36         [TestFixture]
37         public class XNamespaceTest
38         {
39                 [Test]
40                 [ExpectedException (typeof (ArgumentNullException))]
41                 public void GetNull ()
42                 {
43                         XNamespace.Get (null);
44                 }
45
46                 [Test]
47                 public void GetEmpty ()
48                 {
49                         XNamespace n = XNamespace.Get (String.Empty);
50                         Assert.AreEqual (String.Empty, n.NamespaceName);
51                 }
52
53                 [Test]
54                 //[ExpectedException (typeof (ArgumentException))]
55                 public void GetBrokenFormat ()
56                 {
57                         XNamespace n = XNamespace.Get ("{");
58                         Assert.AreEqual ("{", n.NamespaceName, "#1");
59                 }
60
61                 [Test]
62                 //[ExpectedException (typeof (XmlException))]
63                 public void GetBrokenFormat2 ()
64                 {
65                         XNamespace n = XNamespace.Get ("}");
66                         Assert.AreEqual ("}", n.NamespaceName, "#1");
67                 }
68
69                 [Test]
70                 //[ExpectedException (typeof (ArgumentException))]
71                 public void GetBrokenFormat3 ()
72                 {
73                         XNamespace n = XNamespace.Get ("{{}}x");
74                         Assert.AreEqual ("{{}}x", n.NamespaceName, "#1");
75                 }
76
77                 [Test]
78                 public void GetBrokenFormat4 ()
79                 {
80                         XNamespace n = XNamespace.Get ("{}x}");
81                         Assert.AreEqual ("{}x}", n.NamespaceName, "#1");
82                 }
83
84                 [Test]
85                 public void Get1 ()
86                 {
87                         XNamespace n = XNamespace.Get ("{x_x}");
88                         Assert.AreEqual ("{x_x}", n.NamespaceName, "#1");
89
90                         n = XNamespace.Get ("x_x"); // looks like this is the ordinal use.
91                         Assert.AreEqual ("x_x", n.NamespaceName, "#2");
92                 }
93
94                 [Test]
95                 public void Get2 ()
96                 {
97                         XNamespace n = XNamespace.Get (String.Empty);
98                         Assert.IsTrue (Object.ReferenceEquals (XNamespace.None, n), "#1");
99                         n = XNamespace.Get ("http://www.w3.org/2000/xmlns/");
100                         Assert.IsTrue (Object.ReferenceEquals (XNamespace.Xmlns, n), "#2");
101                         Assert.IsTrue (Object.ReferenceEquals (XNamespace.Get ("urn:foo"), XNamespace.Get ("urn:foo")), "#3");
102                 }
103
104                 [Test]
105                 public void GetName ()
106                 {
107                         XNamespace n = XNamespace.Get ("urn:foo");
108                         Assert.IsTrue (Object.ReferenceEquals (n.GetName ("foo"), n.GetName ("foo")), "#1");
109                         Assert.IsTrue (n.GetName ("foo") == n.GetName ("foo"), "#2");
110                         Assert.IsFalse (n.GetName ("foo") == n.GetName ("bar"), "#3");
111                 }
112
113                 [Test]
114                 public void Predefined ()
115                 {
116                         Assert.AreEqual ("http://www.w3.org/XML/1998/namespace", XNamespace.Xml.NamespaceName, "#1");
117                         Assert.AreEqual ("http://www.w3.org/2000/xmlns/", XNamespace.Xmlns.NamespaceName, "#2");
118                 }
119
120                 [Test]
121                 public void Addition ()
122                 {
123                         XNamespace ns = "http://www.novell.com";
124                         XName d = ns + "hello";
125
126                         Assert.AreEqual ("hello", d.LocalName, "localname");
127                         Assert.AreEqual (ns, d.Namespace, "namespace");
128                         Assert.AreEqual ("http://www.novell.com", d.NamespaceName, "nsname");
129                 }
130                         
131                 [Test]
132                 public void Equals ()
133                 {
134                         Assert.IsTrue (XNamespace.None.Equals (XNamespace.Get ("")), "#1");
135                         Assert.IsTrue (XNamespace.None == XNamespace.Get (""), "#2");
136                         Assert.IsFalse (XNamespace.None.Equals (XNamespace.Get (" ")), "#3");
137                         Assert.IsFalse (XNamespace.None == XNamespace.Get (" "), "#4");
138                 }
139
140                 [Test]
141                 public void TestXmlNoNs ()
142                 {
143                         var ns = XNamespace.Get ("urn:foo");
144                         var element = new XElement ("Demo", new XAttribute (ns + "nil", true));
145                         Assert.AreEqual ("<Demo p1:nil=\"true\" xmlns:p1=\"urn:foo\" />", element.ToString ());
146                 }
147
148                 [Test]
149                 public void TestXmlWithNs ()
150                 {
151                         var ns = XNamespace.Get ("urn:foo");
152                         var element = new XElement ("Demo", new XAttribute (ns + "nil", true), new XAttribute (XNamespace.Xmlns + "xsi", ns));
153                         Assert.AreEqual ("<Demo xsi:nil=\"true\" xmlns:xsi=\"urn:foo\" />", element.ToString ());
154                 }
155         }
156 }