More working tests.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Registration / ComponentRegistrationTestCase.cs
blobbf293d57faa0b2c80a9ee0ca722768bf8d13e014
1 // Copyright 2004-2008 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.Registration
17 using System.Collections;
18 using Castle.MicroKernel.Registration;
19 using Castle.MicroKernel.Tests.Lifestyle.Components;
20 using ClassComponents;
21 using Core;
22 using NUnit.Framework;
24 [TestFixture]
25 public class ComponentRegistrationTestCase
27 private IKernel kernel;
29 [SetUp]
30 public void Init()
32 kernel = new DefaultKernel();
35 [Test]
36 public void AddComponent_WithServiceOnly_RegisteredWithServiceTypeName()
38 kernel.Register(
39 Component.ForService<CustomerImpl>());
41 IHandler handler = kernel.GetHandler(typeof(CustomerImpl));
42 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Service);
43 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Implementation);
45 CustomerImpl customer = kernel.Resolve<CustomerImpl>();
46 Assert.IsNotNull(customer);
48 object customer1 = kernel[typeof(CustomerImpl).FullName];
49 Assert.IsNotNull(customer1);
50 Assert.AreSame(customer, customer1);
53 [Test]
54 public void AddComponent_WithServiceAndName_RegisteredNamed()
56 kernel.Register(
57 Component.ForService<CustomerImpl>()
58 .Named("customer")
61 IHandler handler = kernel.GetHandler("customer");
62 Assert.AreEqual("customer", handler.ComponentModel.Name);
63 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Service);
64 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Implementation);
66 CustomerImpl customer = (CustomerImpl) kernel["customer"];
67 Assert.IsNotNull(customer);
70 [Test]
71 [ExpectedException(typeof(ComponentRegistrationException),
72 "This component has already been assigned name 'customer'")]
73 public void AddComponent_NamedAlreadyAssigned_ThrowsException()
75 kernel.Register(
76 Component.ForService<CustomerImpl>()
77 .Named("customer")
78 .Named("customer1")
82 [Test]
83 public void AddComponent_WithServiceAndClass_RegisteredWithClassTypeName()
85 kernel.Register(
86 Component.ForService<ICustomer>()
87 .ImplementedBy<CustomerImpl>());
89 ICustomer customer = kernel.Resolve<ICustomer>();
90 Assert.IsNotNull(customer);
92 object customer1 = kernel[typeof(CustomerImpl).FullName];
93 Assert.IsNotNull(customer1);
96 [Test]
97 [ExpectedException(typeof(ComponentRegistrationException),
98 "This component has already been assigned implementation Castle.MicroKernel.Tests.ClassComponents.CustomerImpl")]
99 public void AddComponent_WithImplementationAlreadyAssigned_ThrowsException()
101 kernel.Register(
102 Component.ForService<ICustomer>()
103 .ImplementedBy<CustomerImpl>()
104 .ImplementedBy<CustomerImpl2>()
108 [Test]
109 public void AddComponent_Instance_UsesInstance()
111 CustomerImpl customer = new CustomerImpl();
113 kernel.Register(
114 Component.ForService<ICustomer>()
115 .Named("key")
116 .Instance(customer)
118 Assert.IsTrue(kernel.HasComponent("key"));
120 CustomerImpl customer2 = kernel["key"] as CustomerImpl;
121 Assert.AreSame(customer, customer2);
123 customer2 = kernel[typeof(ICustomer)] as CustomerImpl;
124 Assert.AreSame(customer, customer2);
127 [Test]
128 public void AddComponent_WithTransientLifestyle_WorksFine()
130 kernel.Register(
131 Component.ForService<ICustomer>()
132 .Named("customer")
133 .ImplementedBy<CustomerImpl>()
134 .LifeStyle.Transient
137 IHandler handler = kernel.GetHandler("customer");
138 Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
141 [Test]
142 public void AddComponent_WithSingletonLifestyle_WorksFine()
144 kernel.Register(
145 Component.ForService<ICustomer>()
146 .Named("customer")
147 .ImplementedBy<CustomerImpl>()
148 .LifeStyle.Singleton
151 IHandler handler = kernel.GetHandler("customer");
152 Assert.AreEqual(LifestyleType.Singleton, handler.ComponentModel.LifestyleType);
155 [Test]
156 public void AddComponent_WithCustomLifestyle_WorksFine()
158 kernel.Register(
159 Component.ForService<ICustomer>()
160 .Named("customer")
161 .ImplementedBy<CustomerImpl>()
162 .LifeStyle.Custom<MyLifestyleHandler>()
165 IHandler handler = kernel.GetHandler("customer");
166 Assert.AreEqual(LifestyleType.Custom, handler.ComponentModel.LifestyleType);
169 [Test]
170 public void AddComponent_WithThreadLifestyle_WorksFine()
172 kernel.Register(
173 Component.ForService<ICustomer>()
174 .Named("customer")
175 .ImplementedBy<CustomerImpl>()
176 .LifeStyle.PerThread
179 IHandler handler = kernel.GetHandler("customer");
180 Assert.AreEqual(LifestyleType.Thread, handler.ComponentModel.LifestyleType);
183 [Test]
184 public void AddComponent_WithPerWebRequestLifestyle_WorksFine()
186 kernel.Register(
187 Component.ForService<ICustomer>()
188 .Named("customer")
189 .ImplementedBy<CustomerImpl>()
190 .LifeStyle.PerWebRequest
193 IHandler handler = kernel.GetHandler("customer");
194 Assert.AreEqual(LifestyleType.PerWebRequest, handler.ComponentModel.LifestyleType);
197 [Test]
198 public void AddComponent_WithPooledLifestyle_WorksFine()
200 kernel.Register(
201 Component.ForService<ICustomer>()
202 .Named("customer")
203 .ImplementedBy<CustomerImpl>()
204 .LifeStyle.Pooled
207 IHandler handler = kernel.GetHandler("customer");
208 Assert.AreEqual(LifestyleType.Pooled, handler.ComponentModel.LifestyleType);
211 [Test]
212 public void AddComponent_WithPooledWithSizeLifestyle_WorksFine()
214 kernel.Register(
215 Component.ForService<ICustomer>()
216 .Named("customer")
217 .ImplementedBy<CustomerImpl>()
218 .LifeStyle.PooledWithSize(5, 10)
221 IHandler handler = kernel.GetHandler("customer");
222 Assert.AreEqual(LifestyleType.Pooled, handler.ComponentModel.LifestyleType);
225 [Test]
226 public void AddComponent_Activator_WorksFine()
228 kernel.Register(
229 Component.ForService<ICustomer>()
230 .Named("customer")
231 .ImplementedBy<CustomerImpl>()
232 .Activator<MyCustomerActivator>()
235 IHandler handler = kernel.GetHandler("customer");
236 Assert.AreEqual(typeof(MyCustomerActivator), handler.ComponentModel.CustomComponentActivator);
238 ICustomer customer = kernel.Resolve<ICustomer>();
239 Assert.AreEqual("James Bond", customer.Name);
242 [Test]
243 public void AddComponent_ExtendedProperties_WorksFine()
245 kernel.Register(
246 Component.ForService<ICustomer>()
247 .ImplementedBy<CustomerImpl>()
248 .ExtendedProperties(
249 Property.ForKey("key1").Eq("value1"),
250 Property.ForKey("key2").Eq("value2")
254 IHandler handler = kernel.GetHandler(typeof(ICustomer));
255 Assert.AreEqual("value1", handler.ComponentModel.ExtendedProperties["key1"]);
256 Assert.AreEqual("value2", handler.ComponentModel.ExtendedProperties["key2"]);
259 #if DOTNET35
261 [Test]
262 public void AddComponent_ExtendedProperties_UsingAnonymousType()
264 kernel.Register(
265 Component.ForService<ICustomer>()
266 .ImplementedBy<CustomerImpl>()
267 .ExtendedProperties(new { key1 = "value1", key2 = "value2" }));
269 IHandler handler = kernel.GetHandler(typeof(ICustomer));
270 Assert.AreEqual("value1", handler.ComponentModel.ExtendedProperties["key1"]);
271 Assert.AreEqual("value2", handler.ComponentModel.ExtendedProperties["key2"]);
274 #endif
276 [Test]
277 public void AddComponent_CustomDependencies_WorksFine()
279 kernel.Register(
280 Component.ForService<ICustomer>()
281 .ImplementedBy<CustomerImpl>()
282 .CustomDependencies(
283 Property.ForKey("Name").Eq("Caption Hook"),
284 Property.ForKey("Address").Eq("Fairyland"),
285 Property.ForKey("Age").Eq(45)
289 ICustomer customer = kernel.Resolve<ICustomer>();
290 Assert.AreEqual(customer.Name, "Caption Hook");
291 Assert.AreEqual(customer.Address, "Fairyland");
292 Assert.AreEqual(customer.Age, 45);
295 #if DOTNET35
297 [Test]
298 public void AddComponent_CustomDependencies_UsingAnonymousType()
300 kernel.Register(
301 Component.ForService<ICustomer>()
302 .ImplementedBy<CustomerImpl>()
303 .CustomDependencies(new { Name = "Caption Hook", Address = "Fairyland", Age = 45 }));
305 ICustomer customer = kernel.Resolve<ICustomer>();
306 Assert.AreEqual(customer.Name, "Caption Hook");
307 Assert.AreEqual(customer.Address, "Fairyland");
308 Assert.AreEqual(customer.Age, 45);
310 #endif
312 [Test]
313 public void AddComponent_CustomDependenciesDictionary_WorksFine()
315 Hashtable customDependencies = new Hashtable();
316 customDependencies["Name"] = "Caption Hook";
317 customDependencies["Address"] = "Fairyland";
318 customDependencies["Age"] = 45;
320 kernel.Register(
321 Component.ForService<ICustomer>()
322 .ImplementedBy<CustomerImpl>()
323 .CustomDependencies(customDependencies)
326 ICustomer customer = kernel.Resolve<ICustomer>();
327 Assert.AreEqual(customer.Name, "Caption Hook");
328 Assert.AreEqual(customer.Address, "Fairyland");
329 Assert.AreEqual(customer.Age, 45);
332 [Test]
333 public void AddComponent_ServiceOverrides_WorksFine()
335 kernel.Register(
336 Component.ForService<ICustomer>()
337 .Named("customer1")
338 .ImplementedBy<CustomerImpl>()
339 .CustomDependencies(
340 Property.ForKey("Name").Eq("Caption Hook"),
341 Property.ForKey("Address").Eq("Fairyland"),
342 Property.ForKey("Age").Eq(45)
344 Component.ForService<CustomerChain1>()
345 .Named("customer2")
346 .CustomDependencies(
347 Property.ForKey("Name").Eq("Bigfoot"),
348 Property.ForKey("Address").Eq("Forest"),
349 Property.ForKey("Age").Eq(100)
351 .ServiceOverrides(
352 ServiceOverride.ForKey("customer").Eq("customer1")
356 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
357 Assert.IsNotNull(customer.CustomerBase);
358 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
359 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
360 Assert.AreEqual(customer.CustomerBase.Age, 45);
363 #if DOTNET35
365 [Test]
366 public void AddComponent_ServiceOverrides_UsingAnonymousType()
368 kernel.Register(
369 Component.ForService<ICustomer>()
370 .Named("customer1")
371 .ImplementedBy<CustomerImpl>()
372 .CustomDependencies(
373 Property.ForKey("Name").Eq("Caption Hook"),
374 Property.ForKey("Address").Eq("Fairyland"),
375 Property.ForKey("Age").Eq(45)
377 Component.ForService<CustomerChain1>()
378 .Named("customer2")
379 .CustomDependencies(
380 Property.ForKey("Name").Eq("Bigfoot"),
381 Property.ForKey("Address").Eq("Forest"),
382 Property.ForKey("Age").Eq(100)
384 .ServiceOverrides(new { customer = "customer1" })
387 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
388 Assert.IsNotNull(customer.CustomerBase);
389 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
390 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
391 Assert.AreEqual(customer.CustomerBase.Age, 45);
394 #endif
396 [Test]
397 public void AddComponent_ServiceOverridesDictionary_WorksFine()
399 Hashtable serviceOverrides = new Hashtable();
400 serviceOverrides["customer"] = "customer1";
402 kernel.Register(
403 Component.ForService<ICustomer>()
404 .Named("customer1")
405 .ImplementedBy<CustomerImpl>()
406 .CustomDependencies(
407 Property.ForKey("Name").Eq("Caption Hook"),
408 Property.ForKey("Address").Eq("Fairyland"),
409 Property.ForKey("Age").Eq(45)
411 Component.ForService<CustomerChain1>()
412 .Named("customer2")
413 .CustomDependencies(
414 Property.ForKey("Name").Eq("Bigfoot"),
415 Property.ForKey("Address").Eq("Forest"),
416 Property.ForKey("Age").Eq(100)
418 .ServiceOverrides(serviceOverrides)
421 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
422 Assert.IsNotNull(customer.CustomerBase);
423 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
424 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
425 Assert.AreEqual(customer.CustomerBase.Age, 45);