Can now scan most location paths (without predicates).
[mono.git] / mcs / class / System.XML / Test / XmlNamespaceManagerTests.cs
1 //
2 // XmlNamespaceManagerTests.cs
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
13 using NUnit.Framework;
14
15 namespace Ximian.Mono.Tests
16 {
17         public class XmlNamespaceManagerTests : TestCase
18         {
19                 public XmlNamespaceManagerTests () : base ("Ximian.Mono.Tests.XmlNameSpaceManagerTests testsuite") { }
20                 public XmlNamespaceManagerTests (string name) : base (name) { }
21
22                 private XmlNameTable nameTable;
23                 private XmlNamespaceManager namespaceManager;
24
25                 protected override void SetUp ()
26                 {
27                         nameTable = new NameTable ();
28                         namespaceManager = new XmlNamespaceManager (nameTable);
29                 }
30
31                 public void TestNewNamespaceManager ()
32                 {
33                         // make sure that you can call PopScope when there aren't any to pop.
34                         Assert (!namespaceManager.PopScope ());
35
36                         // the following strings should have been added to the name table by the
37                         // namespace manager.
38                         string xmlnsPrefix = nameTable.Get ("xmlns");
39                         string xmlPrefix = nameTable.Get ("xml");
40                         string stringEmpty = nameTable.Get (String.Empty);
41                         string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
42                         string xmlNamespace = "http://www.w3.org/XML/1998/namespace";
43
44                         // none of them should be null.
45                         AssertNotNull (xmlnsPrefix);
46                         AssertNotNull (xmlPrefix);
47                         AssertNotNull (stringEmpty);
48                         AssertNotNull (xmlnsNamespace);
49                         AssertNotNull (xmlNamespace);
50
51                         // Microsoft's XmlNamespaceManager reports that these three
52                         // namespaces aren't declared for some reason.
53                         Assert (!namespaceManager.HasNamespace ("xmlns"));
54                         Assert (!namespaceManager.HasNamespace ("xml"));
55                         Assert (!namespaceManager.HasNamespace (String.Empty));
56
57                         // these three namespaces are declared by default.
58                         AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
59                         AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
60                         AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));
61
62                         // the namespaces should be the same references found in the name table.
63                         AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
64                         AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
65                         AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
66
67                         // looking up undeclared namespaces should return null.
68                         AssertNull (namespaceManager.LookupNamespace ("foo"));
69                 }
70
71                 public void TestAddNamespace ()
72                 {
73                         // add a new namespace.
74                         namespaceManager.AddNamespace ("foo", "http://foo/");
75                         // make sure the new namespace is there.
76                         Assert (namespaceManager.HasNamespace ("foo"));
77                         AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
78                 }
79
80                 public void TestAddNamespaceWithNameTable ()
81                 {
82                         // add a known reference to the name table.
83                         string fooNamespace = "http://foo/";
84                         nameTable.Add(fooNamespace);
85
86                         // create a new string with the same value but different address.
87                         string fooNamespace2 = "http://";
88                         fooNamespace2 += "foo/";
89
90                         // the references must be different in order for this test to prove anything.
91                         Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
92
93                         // add the namespace with the reference that's not in the name table.
94                         namespaceManager.AddNamespace ("foo", fooNamespace2);
95
96                         // the returned reference should be the same one that's in the name table.
97                         AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
98                 }
99
100                 public void TestPushScope ()
101                 {
102                         // add a new namespace.
103                         namespaceManager.AddNamespace ("foo", "http://foo/");
104                         // make sure the new namespace is there.
105                         Assert (namespaceManager.HasNamespace ("foo"));
106                         AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
107                         // push a new scope.
108                         namespaceManager.PushScope ();
109                         // add a new namespace.
110                         namespaceManager.AddNamespace ("bar", "http://bar/");
111                         // make sure the old namespace is not in this new scope.
112                         Assert (!namespaceManager.HasNamespace ("foo"));
113                         // but we're still supposed to be able to lookup the old namespace.
114                         AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
115                         // make sure the new namespace is there.
116                         Assert (namespaceManager.HasNamespace ("bar"));
117                         AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
118                 }
119
120                 public void TestPopScope ()
121                 {
122                         // add some namespaces and a scope.
123                         TestPushScope ();
124                         // pop the scope.
125                         Assert (namespaceManager.PopScope ());
126                         // make sure the first namespace is still there.
127                         Assert (namespaceManager.HasNamespace ("foo"));
128                         AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
129                         // make sure the second namespace is no longer there.
130                         Assert (!namespaceManager.HasNamespace ("bar"));
131                         AssertNull (namespaceManager.LookupNamespace ("bar"));
132                         // make sure there are no more scopes to pop.
133                         Assert (!namespaceManager.PopScope ());
134                         // make sure that popping again doesn't cause an exception.
135                         Assert (!namespaceManager.PopScope ());
136                 }
137         }
138 }