Reverted DictionaryComponentAttribute changes.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / BinaryTreeComponentNameTestCase.cs
blob151314723da444d4aa543affcd3762373db9f9c1
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MicroKernel.Tests
17 using Castle.Core;
18 using Castle.MicroKernel.Handlers;
19 using Castle.MicroKernel.SubSystems.Naming;
20 using NUnit.Framework;
22 [TestFixture]
23 public class BinaryTreeComponentNameTestCase
25 [Test]
26 public void Usage()
28 BinaryTreeComponentName tree = new BinaryTreeComponentName();
30 DefaultHandler handler1 = new DefaultHandler(
31 new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
32 DefaultHandler handler2 = new DefaultHandler(
33 new ComponentModel("B", typeof(DefaultHandler), typeof(DefaultHandler)));
34 DefaultHandler handler3 = new DefaultHandler(
35 new ComponentModel("C", typeof(DefaultHandler), typeof(DefaultHandler)));
36 DefaultHandler handler4 = new DefaultHandler(
37 new ComponentModel("D", typeof(DefaultHandler), typeof(DefaultHandler)));
38 DefaultHandler handler5 = new DefaultHandler(
39 new ComponentModel("E", typeof(DefaultHandler), typeof(DefaultHandler)));
40 DefaultHandler handler6 = new DefaultHandler(
41 new ComponentModel("F", typeof(DefaultHandler), typeof(DefaultHandler)));
43 tree.Add(new ComponentName("protocolhandler"), handler1);
44 tree.Add(new ComponentName("protocolhandler:key=1"), handler2);
45 tree.Add(new ComponentName("protocolhandler:key=2"), handler3);
46 tree.Add(new ComponentName("protocolhandler:key=2,secure=true"), handler4);
47 tree.Add(new ComponentName("modelmanager"), handler5);
48 tree.Add(new ComponentName("viewmanager"), handler6);
50 Assert.AreSame(handler1, tree.GetHandler(new ComponentName("protocolhandler")));
51 Assert.AreSame(handler2, tree.GetHandler(new ComponentName("protocolhandler:key=1")));
52 Assert.AreSame(handler3, tree.GetHandler(new ComponentName("protocolhandler:key=2")));
53 Assert.AreSame(handler4, tree.GetHandler(new ComponentName("protocolhandler:key=2,secure=true")));
54 Assert.AreSame(handler5, tree.GetHandler(new ComponentName("modelmanager")));
55 Assert.AreSame(handler6, tree.GetHandler(new ComponentName("viewmanager")));
57 IHandler[] handlers = tree.GetHandlers(new ComponentName("protocolhandler"));
59 Assert.AreEqual(4, handlers.Length);
60 Assert.AreSame(handler1, handlers[0]);
61 Assert.AreSame(handler2, handlers[1]);
62 Assert.AreSame(handler3, handlers[2]);
63 Assert.AreSame(handler4, handlers[3]);
65 handlers = tree.GetHandlers(new ComponentName("protocolhandler:*"));
67 Assert.AreEqual(4, handlers.Length);
68 Assert.AreSame(handler1, handlers[0]);
69 Assert.AreSame(handler2, handlers[1]);
70 Assert.AreSame(handler3, handlers[2]);
71 Assert.AreSame(handler4, handlers[3]);
73 handlers = tree.GetHandlers(new ComponentName("protocolhandler:secure=true"));
75 Assert.AreEqual(1, handlers.Length);
76 Assert.AreSame(handler4, handlers[0]);
78 handlers = tree.GetHandlers(new ComponentName("protocolhandler:key=2"));
80 Assert.AreEqual(2, handlers.Length);
81 Assert.AreSame(handler3, handlers[0]);
82 Assert.AreSame(handler4, handlers[1]);
86 [Test]
87 public void ComponentNameEmptyProperties()
89 BinaryTreeComponentName tree = new BinaryTreeComponentName();
90 DefaultHandler handler1 = new DefaultHandler(new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
91 DefaultHandler handler2 = new DefaultHandler(new ComponentModel("B", typeof(DefaultHandler), typeof(DefaultHandler)));
92 tree.Add(new ComponentName("protocolhandler:key=1"), handler2);
93 tree.Add(new ComponentName("protocolhandler"), handler1);
95 Assert.AreEqual(handler1, tree.GetHandler(new ComponentName("protocolhandler")));
98 [Test]
99 public void RemoveUnbalancedRoot()
101 BinaryTreeComponentName tree = new BinaryTreeComponentName();
102 DefaultHandler handler1 = new DefaultHandler(new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
103 tree.Add(new ComponentName("1000"), handler1);
104 tree.Add(new ComponentName("7500"), handler1);
105 tree.Add(new ComponentName("6000"), handler1);
106 tree.Add(new ComponentName("2000"), handler1);
108 tree.Remove(new ComponentName("1000"));
109 assertRemoved(tree, 3, new ComponentName("1000"), new ComponentName("6000"));
113 [Test]
114 public void RemoveBalancedRoot()
116 BinaryTreeComponentName tree = new BinaryTreeComponentName();
117 DefaultHandler handler1 = new DefaultHandler(new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
118 tree.Add(new ComponentName("1000"), handler1);
119 tree.Add(new ComponentName("0500"), handler1);
120 tree.Add(new ComponentName("6000"), handler1);
121 tree.Add(new ComponentName("2000"), handler1);
123 tree.Remove(new ComponentName("1000"));
124 assertRemoved(tree, 3, new ComponentName("1000"), new ComponentName("6000"));
127 [Test]
128 public void RemoveSibling()
130 BinaryTreeComponentName tree = new BinaryTreeComponentName();
131 DefaultHandler handler1 = new DefaultHandler(new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
133 tree.Add(new ComponentName("1000"), handler1);
134 tree.Add(new ComponentName("0500"), handler1);
135 tree.Add(new ComponentName("0500:P=1"), handler1);
136 tree.Add(new ComponentName("0500:p=2"), handler1);
138 tree.Remove(new ComponentName("0500:p=2"));
139 assertRemoved(tree, 3, new ComponentName("0500:P=2"), new ComponentName("0500:P=1"));
143 [Test]
144 public void RemoveLeaf()
146 BinaryTreeComponentName tree = new BinaryTreeComponentName();
147 DefaultHandler handler1 = new DefaultHandler(new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
149 tree.Add(new ComponentName("1000"), handler1);
150 tree.Add(new ComponentName("0500"), handler1);
152 tree.Remove(new ComponentName("0500"));
153 assertRemoved(tree, 1, new ComponentName("0500"), new ComponentName("1000"));
156 [Test]
157 public void RemoveNodeWithSiblings()
159 BinaryTreeComponentName tree = new BinaryTreeComponentName();
160 DefaultHandler handler1 = new DefaultHandler(new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
162 tree.Add(new ComponentName("1000"), handler1);
163 tree.Add(new ComponentName("0500"), handler1);
164 tree.Add(new ComponentName("0500:P=1"), handler1);
165 tree.Add(new ComponentName("0500:p=2"), handler1);
166 tree.Add(new ComponentName("0400"), handler1);
168 tree.Remove(new ComponentName("0500"));
169 Assert.AreEqual(4, tree.Count);
170 Assert.AreEqual(4, tree.Handlers.Length);
171 Assert.AreEqual(2, tree.GetHandlers(new ComponentName("0500")).Length);
172 Assert.IsNotNull(tree.GetHandler(new ComponentName("0500:p=2")));
175 [Test]
176 public void RemoveBranchNode()
178 BinaryTreeComponentName tree = new BinaryTreeComponentName();
179 DefaultHandler handler1 = new DefaultHandler(new ComponentModel("A", typeof(DefaultHandler), typeof(DefaultHandler)));
181 tree.Add(new ComponentName("1000"), handler1);
183 tree.Add(new ComponentName("0500"), handler1);
184 tree.Add(new ComponentName("0700"), handler1);
185 tree.Add(new ComponentName("0800"), handler1);
187 tree.Add(new ComponentName("0400"), handler1);
189 tree.Remove(new ComponentName("0500"));
190 assertRemoved(tree, 4, new ComponentName("0500"), new ComponentName("0800"));
194 private void assertRemoved(BinaryTreeComponentName tree, int expectedCount, ComponentName removed,
195 ComponentName exists)
197 Assert.AreEqual(expectedCount, tree.Count);
198 Assert.AreEqual(expectedCount, tree.Handlers.Length);
199 Assert.IsNull(tree.GetHandler(removed));
200 Assert.IsNotNull(tree.GetHandler(exists));
204 [Test]
205 public void AccessEmptyTree()
207 BinaryTreeComponentName tree = new BinaryTreeComponentName();
208 Assert.IsFalse(tree.Contains(new ComponentName("Test")));