5 // Copyright 2007 Novell (http://www.novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
30 using System.Xml.Linq;
32 using NUnit.Framework;
34 namespace MonoTests.System.Xml.Linq
37 public class XNamespaceTest
40 [ExpectedException (typeof (ArgumentNullException))]
41 public void GetNull ()
43 XNamespace.Get (null);
47 public void GetEmpty ()
49 XNamespace n = XNamespace.Get (String.Empty);
50 Assert.AreEqual (String.Empty, n.NamespaceName);
54 //[ExpectedException (typeof (ArgumentException))]
55 public void GetBrokenFormat ()
57 XNamespace n = XNamespace.Get ("{");
58 Assert.AreEqual ("{", n.NamespaceName, "#1");
62 //[ExpectedException (typeof (XmlException))]
63 public void GetBrokenFormat2 ()
65 XNamespace n = XNamespace.Get ("}");
66 Assert.AreEqual ("}", n.NamespaceName, "#1");
70 //[ExpectedException (typeof (ArgumentException))]
71 public void GetBrokenFormat3 ()
73 XNamespace n = XNamespace.Get ("{{}}x");
74 Assert.AreEqual ("{{}}x", n.NamespaceName, "#1");
78 public void GetBrokenFormat4 ()
80 XNamespace n = XNamespace.Get ("{}x}");
81 Assert.AreEqual ("{}x}", n.NamespaceName, "#1");
87 XNamespace n = XNamespace.Get ("{x_x}");
88 Assert.AreEqual ("{x_x}", n.NamespaceName, "#1");
90 n = XNamespace.Get ("x_x"); // looks like this is the ordinal use.
91 Assert.AreEqual ("x_x", n.NamespaceName, "#2");
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");
105 public void GetName ()
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");
114 public void Predefined ()
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");
121 public void Addition ()
123 XNamespace ns = "http://www.novell.com";
124 XName d = ns + "hello";
126 Assert.AreEqual ("hello", d.LocalName, "localname");
127 Assert.AreEqual (ns, d.Namespace, "namespace");
128 Assert.AreEqual ("http://www.novell.com", d.NamespaceName, "nsname");
132 public void Equals ()
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");
141 public void TestXmlNoNs ()
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 ());
149 public void TestXmlWithNs ()
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 ());