Fixed bug with ComponentRegistration.Instance in which the instance type was not...
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Registration / ComponentRegistrationTestCase.cs
blob5a5bf4e179e91533d168d13718387e0a67f4942a
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.Core;
19 using Castle.Core.Configuration;
20 using Castle.MicroKernel.Registration;
21 using Castle.MicroKernel.Tests.Lifestyle.Components;
22 using Castle.MicroKernel.Tests.Configuration.Components;
23 using Castle.MicroKernel.Tests.ClassComponents;
24 using NUnit.Framework;
26 [TestFixture]
27 public class ComponentRegistrationTestCase
29 private IKernel kernel;
31 [SetUp]
32 public void Init()
34 kernel = new DefaultKernel();
37 [Test]
38 public void AddComponent_WithServiceOnly_RegisteredWithServiceTypeName()
40 kernel.Register(
41 Component.For<CustomerImpl>());
43 IHandler handler = kernel.GetHandler(typeof(CustomerImpl));
44 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Service);
45 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Implementation);
47 CustomerImpl customer = kernel.Resolve<CustomerImpl>();
48 Assert.IsNotNull(customer);
50 object customer1 = kernel[typeof(CustomerImpl).FullName];
51 Assert.IsNotNull(customer1);
52 Assert.AreSame(customer, customer1);
55 [Test]
56 public void AddComponent_WithServiceAndName_RegisteredNamed()
58 kernel.Register(
59 Component.For<CustomerImpl>()
60 .Named("customer")
63 IHandler handler = kernel.GetHandler("customer");
64 Assert.AreEqual("customer", handler.ComponentModel.Name);
65 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Service);
66 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Implementation);
68 CustomerImpl customer = (CustomerImpl) kernel["customer"];
69 Assert.IsNotNull(customer);
72 [Test]
73 [ExpectedException(typeof(ComponentRegistrationException),
74 "This component has already been assigned name 'customer'")]
75 public void AddComponent_NamedAlreadyAssigned_ThrowsException()
77 kernel.Register(
78 Component.For<CustomerImpl>()
79 .Named("customer")
80 .Named("customer1")
84 [Test]
85 public void AddComponent_WithServiceAndClass_RegisteredWithClassTypeName()
87 kernel.Register(
88 Component.For<ICustomer>()
89 .ImplementedBy<CustomerImpl>());
91 ICustomer customer = kernel.Resolve<ICustomer>();
92 Assert.IsNotNull(customer);
94 object customer1 = kernel[typeof(CustomerImpl).FullName];
95 Assert.IsNotNull(customer1);
98 [Test]
99 [ExpectedException(typeof(ComponentRegistrationException),
100 "This component has already been assigned implementation Castle.MicroKernel.Tests.ClassComponents.CustomerImpl")]
101 public void AddComponent_WithImplementationAlreadyAssigned_ThrowsException()
103 kernel.Register(
104 Component.For<ICustomer>()
105 .ImplementedBy<CustomerImpl>()
106 .ImplementedBy<CustomerImpl2>()
110 [Test]
111 public void AddComponent_Instance_UsesInstance()
113 CustomerImpl customer = new CustomerImpl();
115 kernel.Register(
116 Component.For<ICustomer>()
117 .Named("key")
118 .Instance(customer)
120 Assert.IsTrue(kernel.HasComponent("key"));
121 IHandler handler = kernel.GetHandler("key");
122 Assert.AreEqual(customer.GetType(), handler.ComponentModel.Implementation);
124 CustomerImpl customer2 = kernel["key"] as CustomerImpl;
125 Assert.AreSame(customer, customer2);
127 customer2 = kernel[typeof(ICustomer)] as CustomerImpl;
128 Assert.AreSame(customer, customer2);
131 [Test]
132 public void AddComponent_WithTransientLifestyle_WorksFine()
134 kernel.Register(
135 Component.For<ICustomer>()
136 .Named("customer")
137 .ImplementedBy<CustomerImpl>()
138 .LifeStyle.Transient
141 IHandler handler = kernel.GetHandler("customer");
142 Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
145 [Test]
146 public void AddComponent_WithSingletonLifestyle_WorksFine()
148 kernel.Register(
149 Component.For<ICustomer>()
150 .Named("customer")
151 .ImplementedBy<CustomerImpl>()
152 .LifeStyle.Singleton
155 IHandler handler = kernel.GetHandler("customer");
156 Assert.AreEqual(LifestyleType.Singleton, handler.ComponentModel.LifestyleType);
159 [Test]
160 public void AddComponent_WithCustomLifestyle_WorksFine()
162 kernel.Register(
163 Component.For<ICustomer>()
164 .Named("customer")
165 .ImplementedBy<CustomerImpl>()
166 .LifeStyle.Custom<MyLifestyleHandler>()
169 IHandler handler = kernel.GetHandler("customer");
170 Assert.AreEqual(LifestyleType.Custom, handler.ComponentModel.LifestyleType);
173 [Test]
174 public void AddComponent_WithThreadLifestyle_WorksFine()
176 kernel.Register(
177 Component.For<ICustomer>()
178 .Named("customer")
179 .ImplementedBy<CustomerImpl>()
180 .LifeStyle.PerThread
183 IHandler handler = kernel.GetHandler("customer");
184 Assert.AreEqual(LifestyleType.Thread, handler.ComponentModel.LifestyleType);
187 [Test]
188 public void AddComponent_WithPerWebRequestLifestyle_WorksFine()
190 kernel.Register(
191 Component.For<ICustomer>()
192 .Named("customer")
193 .ImplementedBy<CustomerImpl>()
194 .LifeStyle.PerWebRequest
197 IHandler handler = kernel.GetHandler("customer");
198 Assert.AreEqual(LifestyleType.PerWebRequest, handler.ComponentModel.LifestyleType);
201 [Test]
202 public void AddComponent_WithPooledLifestyle_WorksFine()
204 kernel.Register(
205 Component.For<ICustomer>()
206 .Named("customer")
207 .ImplementedBy<CustomerImpl>()
208 .LifeStyle.Pooled
211 IHandler handler = kernel.GetHandler("customer");
212 Assert.AreEqual(LifestyleType.Pooled, handler.ComponentModel.LifestyleType);
215 [Test]
216 public void AddComponent_WithPooledWithSizeLifestyle_WorksFine()
218 kernel.Register(
219 Component.For<ICustomer>()
220 .Named("customer")
221 .ImplementedBy<CustomerImpl>()
222 .LifeStyle.PooledWithSize(5, 10)
225 IHandler handler = kernel.GetHandler("customer");
226 Assert.AreEqual(LifestyleType.Pooled, handler.ComponentModel.LifestyleType);
229 [Test]
230 public void AddComponent_Activator_WorksFine()
232 kernel.Register(
233 Component.For<ICustomer>()
234 .Named("customer")
235 .ImplementedBy<CustomerImpl>()
236 .Activator<MyCustomerActivator>()
239 IHandler handler = kernel.GetHandler("customer");
240 Assert.AreEqual(typeof(MyCustomerActivator), handler.ComponentModel.CustomComponentActivator);
242 ICustomer customer = kernel.Resolve<ICustomer>();
243 Assert.AreEqual("James Bond", customer.Name);
246 [Test]
247 public void AddComponent_ExtendedProperties_WorksFine()
249 kernel.Register(
250 Component.For<ICustomer>()
251 .ImplementedBy<CustomerImpl>()
252 .ExtendedProperties(
253 Property.ForKey("key1").Eq("value1"),
254 Property.ForKey("key2").Eq("value2")
258 IHandler handler = kernel.GetHandler(typeof(ICustomer));
259 Assert.AreEqual("value1", handler.ComponentModel.ExtendedProperties["key1"]);
260 Assert.AreEqual("value2", handler.ComponentModel.ExtendedProperties["key2"]);
263 #if DOTNET35
265 [Test]
266 public void AddComponent_ExtendedProperties_UsingAnonymousType()
268 kernel.Register(
269 Component.For<ICustomer>()
270 .ImplementedBy<CustomerImpl>()
271 .ExtendedProperties(new { key1 = "value1", key2 = "value2" }));
273 IHandler handler = kernel.GetHandler(typeof(ICustomer));
274 Assert.AreEqual("value1", handler.ComponentModel.ExtendedProperties["key1"]);
275 Assert.AreEqual("value2", handler.ComponentModel.ExtendedProperties["key2"]);
278 #endif
280 [Test]
281 public void AddComponent_CustomDependencies_WorksFine()
283 kernel.Register(
284 Component.For<ICustomer>()
285 .ImplementedBy<CustomerImpl>()
286 .CustomDependencies(
287 Property.ForKey("Name").Eq("Caption Hook"),
288 Property.ForKey("Address").Eq("Fairyland"),
289 Property.ForKey("Age").Eq(45)
293 ICustomer customer = kernel.Resolve<ICustomer>();
294 Assert.AreEqual(customer.Name, "Caption Hook");
295 Assert.AreEqual(customer.Address, "Fairyland");
296 Assert.AreEqual(customer.Age, 45);
299 #if DOTNET35
301 [Test]
302 public void AddComponent_CustomDependencies_UsingAnonymousType()
304 kernel.Register(
305 Component.For<ICustomer>()
306 .ImplementedBy<CustomerImpl>()
307 .CustomDependencies(new { Name = "Caption Hook", Address = "Fairyland", Age = 45 }));
309 ICustomer customer = kernel.Resolve<ICustomer>();
310 Assert.AreEqual(customer.Name, "Caption Hook");
311 Assert.AreEqual(customer.Address, "Fairyland");
312 Assert.AreEqual(customer.Age, 45);
314 #endif
316 [Test]
317 public void AddComponent_CustomDependenciesDictionary_WorksFine()
319 Hashtable customDependencies = new Hashtable();
320 customDependencies["Name"] = "Caption Hook";
321 customDependencies["Address"] = "Fairyland";
322 customDependencies["Age"] = 45;
324 kernel.Register(
325 Component.For<ICustomer>()
326 .ImplementedBy<CustomerImpl>()
327 .CustomDependencies(customDependencies)
330 ICustomer customer = kernel.Resolve<ICustomer>();
331 Assert.AreEqual(customer.Name, "Caption Hook");
332 Assert.AreEqual(customer.Address, "Fairyland");
333 Assert.AreEqual(customer.Age, 45);
336 [Test]
337 public void AddComponent_ServiceOverrides_WorksFine()
339 kernel.Register(
340 Component.For<ICustomer>()
341 .Named("customer1")
342 .ImplementedBy<CustomerImpl>()
343 .CustomDependencies(
344 Property.ForKey("Name").Eq("Caption Hook"),
345 Property.ForKey("Address").Eq("Fairyland"),
346 Property.ForKey("Age").Eq(45)
348 Component.For<CustomerChain1>()
349 .Named("customer2")
350 .CustomDependencies(
351 Property.ForKey("Name").Eq("Bigfoot"),
352 Property.ForKey("Address").Eq("Forest"),
353 Property.ForKey("Age").Eq(100)
355 .ServiceOverrides(
356 ServiceOverride.ForKey("customer").Eq("customer1")
360 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
361 Assert.IsNotNull(customer.CustomerBase);
362 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
363 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
364 Assert.AreEqual(customer.CustomerBase.Age, 45);
367 [Test]
368 public void AddComponent_ArrayServiceOverrides_WorksFine()
370 kernel.Register(
371 Component.For<ICommon>()
372 .Named("common1")
373 .ImplementedBy<CommonImpl1>(),
374 Component.For<ICommon>()
375 .Named("common2")
376 .ImplementedBy<CommonImpl2>(),
377 Component.For<ClassWithArrayConstructor>()
378 .ServiceOverrides(
379 ServiceOverride.ForKey("first").Eq("common2"),
380 ServiceOverride.ForKey("services").Eq("common1", "common2")
384 ICommon common1 = (ICommon) kernel["common1"];
385 ICommon common2 = (ICommon) kernel["common2"];
386 ClassWithArrayConstructor component = kernel.Resolve<ClassWithArrayConstructor>();
387 Assert.AreSame(common2, component.First);
388 Assert.AreEqual(2, component.Services.Length);
389 Assert.AreSame(common1, component.Services[0]);
390 Assert.AreSame(common2, component.Services[1]);
393 [Test]
394 public void AddComponent_GenericListServiceOverrides_WorksFine()
396 kernel.Register(
397 Component.For<ICommon>()
398 .Named("common1")
399 .ImplementedBy<CommonImpl1>(),
400 Component.For<ICommon>()
401 .Named("common2")
402 .ImplementedBy<CommonImpl2>(),
403 Component.For<ClassWithListConstructor>()
404 .ServiceOverrides(
405 ServiceOverride.ForKey("services").Eq<ICommon>("common1", "common2")
409 ICommon common1 = (ICommon)kernel["common1"];
410 ICommon common2 = (ICommon)kernel["common2"];
411 ClassWithListConstructor component = kernel.Resolve<ClassWithListConstructor>();
412 Assert.AreEqual(2, component.Services.Count);
413 Assert.AreSame(common1, component.Services[0]);
414 Assert.AreSame(common2, component.Services[1]);
417 #if DOTNET35
419 [Test]
420 public void AddComponent_ServiceOverrides_UsingAnonymousType()
422 kernel.Register(
423 Component.For<ICustomer>()
424 .Named("customer1")
425 .ImplementedBy<CustomerImpl>()
426 .CustomDependencies(
427 Property.ForKey("Name").Eq("Caption Hook"),
428 Property.ForKey("Address").Eq("Fairyland"),
429 Property.ForKey("Age").Eq(45)
431 Component.For<CustomerChain1>()
432 .Named("customer2")
433 .CustomDependencies(
434 Property.ForKey("Name").Eq("Bigfoot"),
435 Property.ForKey("Address").Eq("Forest"),
436 Property.ForKey("Age").Eq(100)
438 .ServiceOverrides(new { customer = "customer1" })
441 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
442 Assert.IsNotNull(customer.CustomerBase);
443 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
444 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
445 Assert.AreEqual(customer.CustomerBase.Age, 45);
448 #endif
450 [Test]
451 public void AddComponent_ServiceOverridesDictionary_WorksFine()
453 Hashtable serviceOverrides = new Hashtable();
454 serviceOverrides["customer"] = "customer1";
456 kernel.Register(
457 Component.For<ICustomer>()
458 .Named("customer1")
459 .ImplementedBy<CustomerImpl>()
460 .CustomDependencies(
461 Property.ForKey("Name").Eq("Caption Hook"),
462 Property.ForKey("Address").Eq("Fairyland"),
463 Property.ForKey("Age").Eq(45)
465 Component.For<CustomerChain1>()
466 .Named("customer2")
467 .CustomDependencies(
468 Property.ForKey("Name").Eq("Bigfoot"),
469 Property.ForKey("Address").Eq("Forest"),
470 Property.ForKey("Age").Eq(100)
472 .ServiceOverrides(serviceOverrides)
475 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
476 Assert.IsNotNull(customer.CustomerBase);
477 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
478 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
479 Assert.AreEqual(customer.CustomerBase.Age, 45);
482 [Test]
483 public void AddComponent_ArrayConfigurationParameters_WorksFine()
485 MutableConfiguration list = new MutableConfiguration("list");
486 list.Attributes.Add("type", typeof(ICommon).AssemblyQualifiedName);
487 list.Children.Add(new MutableConfiguration("item", "${common1}"));
488 list.Children.Add(new MutableConfiguration("item", "${common2}"));
490 kernel.Register(
491 Component.For<ICommon>()
492 .Named("common1")
493 .ImplementedBy<CommonImpl1>(),
494 Component.For<ICommon>()
495 .Named("common2")
496 .ImplementedBy<CommonImpl2>(),
497 Component.For<ClassWithArrayConstructor>()
498 .Parameters(
499 Parameter.ForKey("first").Eq("${common2}"),
500 Parameter.ForKey("services").Eq(list)
504 ICommon common1 = (ICommon)kernel["common1"];
505 ICommon common2 = (ICommon)kernel["common2"];
506 ClassWithArrayConstructor component = kernel.Resolve<ClassWithArrayConstructor>();
507 Assert.AreSame(common2, component.First);
508 Assert.AreEqual(2, component.Services.Length);
509 Assert.AreSame(common1, component.Services[0]);
510 Assert.AreSame(common2, component.Services[1]);
513 [Test]
514 public void AddComponent_ListConfigurationParameters_WorksFine()
516 MutableConfiguration list = new MutableConfiguration("list");
517 list.Attributes.Add("type", typeof(ICommon).AssemblyQualifiedName);
518 list.Children.Add(new MutableConfiguration("item", "${common1}"));
519 list.Children.Add(new MutableConfiguration("item", "${common2}"));
521 kernel.Register(
522 Component.For<ICommon>()
523 .Named("common1")
524 .ImplementedBy<CommonImpl1>(),
525 Component.For<ICommon>()
526 .Named("common2")
527 .ImplementedBy<CommonImpl2>(),
528 Component.For<ClassWithListConstructor>()
529 .Parameters(
530 Parameter.ForKey("services").Eq(list)
534 ICommon common1 = (ICommon)kernel["common1"];
535 ICommon common2 = (ICommon)kernel["common2"];
536 ClassWithListConstructor component = kernel.Resolve<ClassWithListConstructor>();
537 Assert.AreEqual(2, component.Services.Count);
538 Assert.AreSame(common1, component.Services[0]);
539 Assert.AreSame(common2, component.Services[1]);
542 [Test]
543 public void AddComponent_WithComplexConfiguration_WorksFine()
545 kernel.Register(
546 Component.For<ClassWithComplexParameter>()
547 .Configuration(
548 Child.ForName("parameters").Eq(
549 Attrib.ForName("notUsed").Eq(true),
550 Child.ForName("complexparam").Eq(
551 Child.ForName("complexparametertype").Eq(
552 Child.ForName("mandatoryvalue").Eq("value1"),
553 Child.ForName("optionalvalue").Eq("value2")
560 ClassWithComplexParameter component = kernel.Resolve<ClassWithComplexParameter>();
561 Assert.IsNotNull(component);
562 Assert.IsNotNull(component.ComplexParam);
563 Assert.AreEqual("value1", component.ComplexParam.MandatoryValue);
564 Assert.AreEqual("value2", component.ComplexParam.OptionalValue);
567 [Test]
568 public void CanUseExistingComponentModelWithComponentRegistration()
570 kernel.Register(Component.For<ICustomer>()
571 .ImplementedBy<CustomerImpl>()
574 IHandler handler = kernel.GetHandler(typeof(ICustomer));
575 ComponentRegistration component = Component.For(handler.ComponentModel);
577 Assert.AreEqual(typeof(ICustomer), component.ServiceType);
578 Assert.AreEqual(typeof(CustomerImpl), component.Implementation);