Added testcase for generic registrations and a workaround for the CLR GetInterfaces...
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Registration / ComponentRegistrationTestCase.cs
blob7617fbfa0041e8068492b4c6877f064e4c53963b
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.Configuration;
19 using Castle.MicroKernel.Registration;
20 using Castle.MicroKernel.Tests.Lifestyle.Components;
21 using ClassComponents;
22 using Core;
23 using NUnit.Framework;
25 [TestFixture]
26 public class ComponentRegistrationTestCase
28 private IKernel kernel;
30 [SetUp]
31 public void Init()
33 kernel = new DefaultKernel();
36 [Test]
37 public void AddComponent_WithServiceOnly_RegisteredWithServiceTypeName()
39 kernel.Register(
40 Component.For<CustomerImpl>());
42 IHandler handler = kernel.GetHandler(typeof(CustomerImpl));
43 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Service);
44 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Implementation);
46 CustomerImpl customer = kernel.Resolve<CustomerImpl>();
47 Assert.IsNotNull(customer);
49 object customer1 = kernel[typeof(CustomerImpl).FullName];
50 Assert.IsNotNull(customer1);
51 Assert.AreSame(customer, customer1);
54 [Test]
55 public void AddComponent_WithServiceAndName_RegisteredNamed()
57 kernel.Register(
58 Component.For<CustomerImpl>()
59 .Named("customer")
62 IHandler handler = kernel.GetHandler("customer");
63 Assert.AreEqual("customer", handler.ComponentModel.Name);
64 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Service);
65 Assert.AreEqual(typeof(CustomerImpl), handler.ComponentModel.Implementation);
67 CustomerImpl customer = (CustomerImpl) kernel["customer"];
68 Assert.IsNotNull(customer);
71 [Test]
72 [ExpectedException(typeof(ComponentRegistrationException),
73 "This component has already been assigned name 'customer'")]
74 public void AddComponent_NamedAlreadyAssigned_ThrowsException()
76 kernel.Register(
77 Component.For<CustomerImpl>()
78 .Named("customer")
79 .Named("customer1")
83 [Test]
84 public void AddComponent_WithServiceAndClass_RegisteredWithClassTypeName()
86 kernel.Register(
87 Component.For<ICustomer>()
88 .ImplementedBy<CustomerImpl>());
90 ICustomer customer = kernel.Resolve<ICustomer>();
91 Assert.IsNotNull(customer);
93 object customer1 = kernel[typeof(CustomerImpl).FullName];
94 Assert.IsNotNull(customer1);
97 [Test]
98 [ExpectedException(typeof(ComponentRegistrationException),
99 "This component has already been assigned implementation Castle.MicroKernel.Tests.ClassComponents.CustomerImpl")]
100 public void AddComponent_WithImplementationAlreadyAssigned_ThrowsException()
102 kernel.Register(
103 Component.For<ICustomer>()
104 .ImplementedBy<CustomerImpl>()
105 .ImplementedBy<CustomerImpl2>()
109 [Test]
110 public void AddComponent_Instance_UsesInstance()
112 CustomerImpl customer = new CustomerImpl();
114 kernel.Register(
115 Component.For<ICustomer>()
116 .Named("key")
117 .Instance(customer)
119 Assert.IsTrue(kernel.HasComponent("key"));
121 CustomerImpl customer2 = kernel["key"] as CustomerImpl;
122 Assert.AreSame(customer, customer2);
124 customer2 = kernel[typeof(ICustomer)] as CustomerImpl;
125 Assert.AreSame(customer, customer2);
128 [Test]
129 public void AddComponent_WithTransientLifestyle_WorksFine()
131 kernel.Register(
132 Component.For<ICustomer>()
133 .Named("customer")
134 .ImplementedBy<CustomerImpl>()
135 .LifeStyle.Transient
138 IHandler handler = kernel.GetHandler("customer");
139 Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
142 [Test]
143 public void AddComponent_WithSingletonLifestyle_WorksFine()
145 kernel.Register(
146 Component.For<ICustomer>()
147 .Named("customer")
148 .ImplementedBy<CustomerImpl>()
149 .LifeStyle.Singleton
152 IHandler handler = kernel.GetHandler("customer");
153 Assert.AreEqual(LifestyleType.Singleton, handler.ComponentModel.LifestyleType);
156 [Test]
157 public void AddComponent_WithCustomLifestyle_WorksFine()
159 kernel.Register(
160 Component.For<ICustomer>()
161 .Named("customer")
162 .ImplementedBy<CustomerImpl>()
163 .LifeStyle.Custom<MyLifestyleHandler>()
166 IHandler handler = kernel.GetHandler("customer");
167 Assert.AreEqual(LifestyleType.Custom, handler.ComponentModel.LifestyleType);
170 [Test]
171 public void AddComponent_WithThreadLifestyle_WorksFine()
173 kernel.Register(
174 Component.For<ICustomer>()
175 .Named("customer")
176 .ImplementedBy<CustomerImpl>()
177 .LifeStyle.PerThread
180 IHandler handler = kernel.GetHandler("customer");
181 Assert.AreEqual(LifestyleType.Thread, handler.ComponentModel.LifestyleType);
184 [Test]
185 public void AddComponent_WithPerWebRequestLifestyle_WorksFine()
187 kernel.Register(
188 Component.For<ICustomer>()
189 .Named("customer")
190 .ImplementedBy<CustomerImpl>()
191 .LifeStyle.PerWebRequest
194 IHandler handler = kernel.GetHandler("customer");
195 Assert.AreEqual(LifestyleType.PerWebRequest, handler.ComponentModel.LifestyleType);
198 [Test]
199 public void AddComponent_WithPooledLifestyle_WorksFine()
201 kernel.Register(
202 Component.For<ICustomer>()
203 .Named("customer")
204 .ImplementedBy<CustomerImpl>()
205 .LifeStyle.Pooled
208 IHandler handler = kernel.GetHandler("customer");
209 Assert.AreEqual(LifestyleType.Pooled, handler.ComponentModel.LifestyleType);
212 [Test]
213 public void AddComponent_WithPooledWithSizeLifestyle_WorksFine()
215 kernel.Register(
216 Component.For<ICustomer>()
217 .Named("customer")
218 .ImplementedBy<CustomerImpl>()
219 .LifeStyle.PooledWithSize(5, 10)
222 IHandler handler = kernel.GetHandler("customer");
223 Assert.AreEqual(LifestyleType.Pooled, handler.ComponentModel.LifestyleType);
226 [Test]
227 public void AddComponent_Activator_WorksFine()
229 kernel.Register(
230 Component.For<ICustomer>()
231 .Named("customer")
232 .ImplementedBy<CustomerImpl>()
233 .Activator<MyCustomerActivator>()
236 IHandler handler = kernel.GetHandler("customer");
237 Assert.AreEqual(typeof(MyCustomerActivator), handler.ComponentModel.CustomComponentActivator);
239 ICustomer customer = kernel.Resolve<ICustomer>();
240 Assert.AreEqual("James Bond", customer.Name);
243 [Test]
244 public void AddComponent_ExtendedProperties_WorksFine()
246 kernel.Register(
247 Component.For<ICustomer>()
248 .ImplementedBy<CustomerImpl>()
249 .ExtendedProperties(
250 Property.ForKey("key1").Eq("value1"),
251 Property.ForKey("key2").Eq("value2")
255 IHandler handler = kernel.GetHandler(typeof(ICustomer));
256 Assert.AreEqual("value1", handler.ComponentModel.ExtendedProperties["key1"]);
257 Assert.AreEqual("value2", handler.ComponentModel.ExtendedProperties["key2"]);
260 #if DOTNET35
262 [Test]
263 public void AddComponent_ExtendedProperties_UsingAnonymousType()
265 kernel.Register(
266 Component.For<ICustomer>()
267 .ImplementedBy<CustomerImpl>()
268 .ExtendedProperties(new { key1 = "value1", key2 = "value2" }));
270 IHandler handler = kernel.GetHandler(typeof(ICustomer));
271 Assert.AreEqual("value1", handler.ComponentModel.ExtendedProperties["key1"]);
272 Assert.AreEqual("value2", handler.ComponentModel.ExtendedProperties["key2"]);
275 #endif
277 [Test]
278 public void AddComponent_CustomDependencies_WorksFine()
280 kernel.Register(
281 Component.For<ICustomer>()
282 .ImplementedBy<CustomerImpl>()
283 .CustomDependencies(
284 Property.ForKey("Name").Eq("Caption Hook"),
285 Property.ForKey("Address").Eq("Fairyland"),
286 Property.ForKey("Age").Eq(45)
290 ICustomer customer = kernel.Resolve<ICustomer>();
291 Assert.AreEqual(customer.Name, "Caption Hook");
292 Assert.AreEqual(customer.Address, "Fairyland");
293 Assert.AreEqual(customer.Age, 45);
296 #if DOTNET35
298 [Test]
299 public void AddComponent_CustomDependencies_UsingAnonymousType()
301 kernel.Register(
302 Component.For<ICustomer>()
303 .ImplementedBy<CustomerImpl>()
304 .CustomDependencies(new { Name = "Caption Hook", Address = "Fairyland", Age = 45 }));
306 ICustomer customer = kernel.Resolve<ICustomer>();
307 Assert.AreEqual(customer.Name, "Caption Hook");
308 Assert.AreEqual(customer.Address, "Fairyland");
309 Assert.AreEqual(customer.Age, 45);
311 #endif
313 [Test]
314 public void AddComponent_CustomDependenciesDictionary_WorksFine()
316 Hashtable customDependencies = new Hashtable();
317 customDependencies["Name"] = "Caption Hook";
318 customDependencies["Address"] = "Fairyland";
319 customDependencies["Age"] = 45;
321 kernel.Register(
322 Component.For<ICustomer>()
323 .ImplementedBy<CustomerImpl>()
324 .CustomDependencies(customDependencies)
327 ICustomer customer = kernel.Resolve<ICustomer>();
328 Assert.AreEqual(customer.Name, "Caption Hook");
329 Assert.AreEqual(customer.Address, "Fairyland");
330 Assert.AreEqual(customer.Age, 45);
333 [Test]
334 public void AddComponent_ServiceOverrides_WorksFine()
336 kernel.Register(
337 Component.For<ICustomer>()
338 .Named("customer1")
339 .ImplementedBy<CustomerImpl>()
340 .CustomDependencies(
341 Property.ForKey("Name").Eq("Caption Hook"),
342 Property.ForKey("Address").Eq("Fairyland"),
343 Property.ForKey("Age").Eq(45)
345 Component.For<CustomerChain1>()
346 .Named("customer2")
347 .CustomDependencies(
348 Property.ForKey("Name").Eq("Bigfoot"),
349 Property.ForKey("Address").Eq("Forest"),
350 Property.ForKey("Age").Eq(100)
352 .ServiceOverrides(
353 ServiceOverride.ForKey("customer").Eq("customer1")
357 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
358 Assert.IsNotNull(customer.CustomerBase);
359 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
360 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
361 Assert.AreEqual(customer.CustomerBase.Age, 45);
364 [Test]
365 public void AddComponent_ArrayServiceOverrides_WorksFine()
367 kernel.Register(
368 Component.For<ICommon>()
369 .Named("common1")
370 .ImplementedBy<CommonImpl1>(),
371 Component.For<ICommon>()
372 .Named("common2")
373 .ImplementedBy<CommonImpl2>(),
374 Component.For<ClassWithArrayConstructor>()
375 .ServiceOverrides(
376 ServiceOverride.ForKey("first").Eq("common2"),
377 ServiceOverride.ForKey("services").Eq("common1", "common2")
381 ICommon common1 = (ICommon) kernel["common1"];
382 ICommon common2 = (ICommon) kernel["common2"];
383 ClassWithArrayConstructor component = kernel.Resolve<ClassWithArrayConstructor>();
384 Assert.AreSame(common2, component.First);
385 Assert.AreEqual(2, component.Services.Length);
386 Assert.AreSame(common1, component.Services[0]);
387 Assert.AreSame(common2, component.Services[1]);
390 [Test]
391 public void AddComponent_GenericListServiceOverrides_WorksFine()
393 kernel.Register(
394 Component.For<ICommon>()
395 .Named("common1")
396 .ImplementedBy<CommonImpl1>(),
397 Component.For<ICommon>()
398 .Named("common2")
399 .ImplementedBy<CommonImpl2>(),
400 Component.For<ClassWithListConstructor>()
401 .ServiceOverrides(
402 ServiceOverride.ForKey("services").Eq<ICommon>("common1", "common2")
406 ICommon common1 = (ICommon)kernel["common1"];
407 ICommon common2 = (ICommon)kernel["common2"];
408 ClassWithListConstructor component = kernel.Resolve<ClassWithListConstructor>();
409 Assert.AreEqual(2, component.Services.Count);
410 Assert.AreSame(common1, component.Services[0]);
411 Assert.AreSame(common2, component.Services[1]);
414 #if DOTNET35
416 [Test]
417 public void AddComponent_ServiceOverrides_UsingAnonymousType()
419 kernel.Register(
420 Component.For<ICustomer>()
421 .Named("customer1")
422 .ImplementedBy<CustomerImpl>()
423 .CustomDependencies(
424 Property.ForKey("Name").Eq("Caption Hook"),
425 Property.ForKey("Address").Eq("Fairyland"),
426 Property.ForKey("Age").Eq(45)
428 Component.For<CustomerChain1>()
429 .Named("customer2")
430 .CustomDependencies(
431 Property.ForKey("Name").Eq("Bigfoot"),
432 Property.ForKey("Address").Eq("Forest"),
433 Property.ForKey("Age").Eq(100)
435 .ServiceOverrides(new { customer = "customer1" })
438 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
439 Assert.IsNotNull(customer.CustomerBase);
440 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
441 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
442 Assert.AreEqual(customer.CustomerBase.Age, 45);
445 #endif
447 [Test]
448 public void AddComponent_ServiceOverridesDictionary_WorksFine()
450 Hashtable serviceOverrides = new Hashtable();
451 serviceOverrides["customer"] = "customer1";
453 kernel.Register(
454 Component.For<ICustomer>()
455 .Named("customer1")
456 .ImplementedBy<CustomerImpl>()
457 .CustomDependencies(
458 Property.ForKey("Name").Eq("Caption Hook"),
459 Property.ForKey("Address").Eq("Fairyland"),
460 Property.ForKey("Age").Eq(45)
462 Component.For<CustomerChain1>()
463 .Named("customer2")
464 .CustomDependencies(
465 Property.ForKey("Name").Eq("Bigfoot"),
466 Property.ForKey("Address").Eq("Forest"),
467 Property.ForKey("Age").Eq(100)
469 .ServiceOverrides(serviceOverrides)
472 CustomerChain1 customer = (CustomerChain1) kernel["customer2"];
473 Assert.IsNotNull(customer.CustomerBase);
474 Assert.AreEqual(customer.CustomerBase.Name, "Caption Hook");
475 Assert.AreEqual(customer.CustomerBase.Address, "Fairyland");
476 Assert.AreEqual(customer.CustomerBase.Age, 45);
479 [Test]
480 public void AddComponent_ArrayConfigurationParameters_WorksFine()
482 MutableConfiguration list = new MutableConfiguration("list");
483 list.Attributes.Add("type", typeof(ICommon).AssemblyQualifiedName);
484 list.Children.Add(new MutableConfiguration("item", "${common1}"));
485 list.Children.Add(new MutableConfiguration("item", "${common2}"));
487 kernel.Register(
488 Component.For<ICommon>()
489 .Named("common1")
490 .ImplementedBy<CommonImpl1>(),
491 Component.For<ICommon>()
492 .Named("common2")
493 .ImplementedBy<CommonImpl2>(),
494 Component.For<ClassWithArrayConstructor>()
495 .Parameters(
496 Parameter.ForKey("first").Eq("${common2}"),
497 Parameter.ForKey("services").Eq(list)
501 ICommon common1 = (ICommon)kernel["common1"];
502 ICommon common2 = (ICommon)kernel["common2"];
503 ClassWithArrayConstructor component = kernel.Resolve<ClassWithArrayConstructor>();
504 Assert.AreSame(common2, component.First);
505 Assert.AreEqual(2, component.Services.Length);
506 Assert.AreSame(common1, component.Services[0]);
507 Assert.AreSame(common2, component.Services[1]);
510 [Test]
511 public void AddComponent_ListConfigurationParameters_WorksFine()
513 MutableConfiguration list = new MutableConfiguration("list");
514 list.Attributes.Add("type", typeof(ICommon).AssemblyQualifiedName);
515 list.Children.Add(new MutableConfiguration("item", "${common1}"));
516 list.Children.Add(new MutableConfiguration("item", "${common2}"));
518 kernel.Register(
519 Component.For<ICommon>()
520 .Named("common1")
521 .ImplementedBy<CommonImpl1>(),
522 Component.For<ICommon>()
523 .Named("common2")
524 .ImplementedBy<CommonImpl2>(),
525 Component.For<ClassWithListConstructor>()
526 .Parameters(
527 Parameter.ForKey("services").Eq(list)
531 ICommon common1 = (ICommon)kernel["common1"];
532 ICommon common2 = (ICommon)kernel["common2"];
533 ClassWithListConstructor component = kernel.Resolve<ClassWithListConstructor>();
534 Assert.AreEqual(2, component.Services.Count);
535 Assert.AreSame(common1, component.Services[0]);
536 Assert.AreSame(common2, component.Services[1]);