Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlNamespaceManagerTests.cs « Test « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 176c76765cb135d5accd927152ed0f9089694f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// XmlNamespaceManagerTests.cs
//
// Author:
//   Jason Diamond (jason@injektilo.org)
//
// (C) 2002 Jason Diamond  http://injektilo.org/
//

using System;
using System.Xml;

using NUnit.Framework;

namespace MonoTests.System.Xml
{
	public class XmlNamespaceManagerTests : TestCase
	{
		public XmlNamespaceManagerTests () : base ("MonoTests.System.Xml.XmlNameSpaceManagerTests testsuite") { }
		public XmlNamespaceManagerTests (string name) : base (name) { }

		private XmlNameTable nameTable;
		private XmlNamespaceManager namespaceManager;

		protected override void SetUp ()
		{
			nameTable = new NameTable ();
			namespaceManager = new XmlNamespaceManager (nameTable);
		}

		public void TestNewNamespaceManager ()
		{
			// make sure that you can call PopScope when there aren't any to pop.
			Assert (!namespaceManager.PopScope ());

			// the following strings should have been added to the name table by the
			// namespace manager.
			string xmlnsPrefix = nameTable.Get ("xmlns");
			string xmlPrefix = nameTable.Get ("xml");
			string stringEmpty = nameTable.Get (String.Empty);
			string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
			string xmlNamespace = "http://www.w3.org/XML/1998/namespace";

			// none of them should be null.
			AssertNotNull (xmlnsPrefix);
			AssertNotNull (xmlPrefix);
			AssertNotNull (stringEmpty);
			AssertNotNull (xmlnsNamespace);
			AssertNotNull (xmlNamespace);

			// Microsoft's XmlNamespaceManager reports that these three
			// namespaces aren't declared for some reason.
			Assert (!namespaceManager.HasNamespace ("xmlns"));
			Assert (!namespaceManager.HasNamespace ("xml"));
			Assert (!namespaceManager.HasNamespace (String.Empty));

			// these three namespaces are declared by default.
			AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
			AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
			AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));

			// the namespaces should be the same references found in the name table.
			AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
			AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
			AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));

			// looking up undeclared namespaces should return null.
			AssertNull (namespaceManager.LookupNamespace ("foo"));
		}

		public void TestAddNamespace ()
		{
			// add a new namespace.
			namespaceManager.AddNamespace ("foo", "http://foo/");
			// make sure the new namespace is there.
			Assert (namespaceManager.HasNamespace ("foo"));
			AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
		}

		public void TestAddNamespaceWithNameTable ()
		{
			// add a known reference to the name table.
			string fooNamespace = "http://foo/";
			nameTable.Add(fooNamespace);

			// create a new string with the same value but different address.
			string fooNamespace2 = "http://";
			fooNamespace2 += "foo/";

			// the references must be different in order for this test to prove anything.
			Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));

			// add the namespace with the reference that's not in the name table.
			namespaceManager.AddNamespace ("foo", fooNamespace2);

			// the returned reference should be the same one that's in the name table.
			AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
		}

		public void TestPushScope ()
		{
			// add a new namespace.
			namespaceManager.AddNamespace ("foo", "http://foo/");
			// make sure the new namespace is there.
			Assert (namespaceManager.HasNamespace ("foo"));
			AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
			// push a new scope.
			namespaceManager.PushScope ();
			// add a new namespace.
			namespaceManager.AddNamespace ("bar", "http://bar/");
			// make sure the old namespace is not in this new scope.
			Assert (!namespaceManager.HasNamespace ("foo"));
			// but we're still supposed to be able to lookup the old namespace.
			AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
			// make sure the new namespace is there.
			Assert (namespaceManager.HasNamespace ("bar"));
			AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
		}

		public void TestPopScope ()
		{
			// add some namespaces and a scope.
			TestPushScope ();
			// pop the scope.
			Assert (namespaceManager.PopScope ());
			// make sure the first namespace is still there.
			Assert (namespaceManager.HasNamespace ("foo"));
			AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
			// make sure the second namespace is no longer there.
			Assert (!namespaceManager.HasNamespace ("bar"));
			AssertNull (namespaceManager.LookupNamespace ("bar"));
			// make sure there are no more scopes to pop.
			Assert (!namespaceManager.PopScope ());
			// make sure that popping again doesn't cause an exception.
			Assert (!namespaceManager.PopScope ());
		}
	}
}