1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
;
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
;
27 public class ComponentRegistrationTestCase
29 private IKernel kernel
;
34 kernel
= new DefaultKernel();
38 public void AddComponent_WithServiceOnly_RegisteredWithServiceTypeName()
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
);
56 public void AddComponent_WithServiceAndName_RegisteredNamed()
59 Component
.For
<CustomerImpl
>()
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
);
73 [ExpectedException(typeof(ComponentRegistrationException
),
74 "This component has already been assigned name 'customer'")]
75 public void AddComponent_NamedAlreadyAssigned_ThrowsException()
78 Component
.For
<CustomerImpl
>()
85 public void AddComponent_WithServiceAndClass_RegisteredWithClassTypeName()
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
);
99 [ExpectedException(typeof(ComponentRegistrationException
),
100 "This component has already been assigned implementation Castle.MicroKernel.Tests.ClassComponents.CustomerImpl")]
101 public void AddComponent_WithImplementationAlreadyAssigned_ThrowsException()
104 Component
.For
<ICustomer
>()
105 .ImplementedBy
<CustomerImpl
>()
106 .ImplementedBy
<CustomerImpl2
>()
111 public void AddComponent_Instance_UsesInstance()
113 CustomerImpl customer
= new CustomerImpl();
116 Component
.For
<ICustomer
>()
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
);
132 public void AddComponent_WithTransientLifestyle_WorksFine()
135 Component
.For
<ICustomer
>()
137 .ImplementedBy
<CustomerImpl
>()
141 IHandler handler
= kernel
.GetHandler("customer");
142 Assert
.AreEqual(LifestyleType
.Transient
, handler
.ComponentModel
.LifestyleType
);
146 public void AddComponent_WithSingletonLifestyle_WorksFine()
149 Component
.For
<ICustomer
>()
151 .ImplementedBy
<CustomerImpl
>()
155 IHandler handler
= kernel
.GetHandler("customer");
156 Assert
.AreEqual(LifestyleType
.Singleton
, handler
.ComponentModel
.LifestyleType
);
160 public void AddComponent_WithCustomLifestyle_WorksFine()
163 Component
.For
<ICustomer
>()
165 .ImplementedBy
<CustomerImpl
>()
166 .LifeStyle
.Custom
<MyLifestyleHandler
>()
169 IHandler handler
= kernel
.GetHandler("customer");
170 Assert
.AreEqual(LifestyleType
.Custom
, handler
.ComponentModel
.LifestyleType
);
174 public void AddComponent_WithThreadLifestyle_WorksFine()
177 Component
.For
<ICustomer
>()
179 .ImplementedBy
<CustomerImpl
>()
183 IHandler handler
= kernel
.GetHandler("customer");
184 Assert
.AreEqual(LifestyleType
.Thread
, handler
.ComponentModel
.LifestyleType
);
188 public void AddComponent_WithPerWebRequestLifestyle_WorksFine()
191 Component
.For
<ICustomer
>()
193 .ImplementedBy
<CustomerImpl
>()
194 .LifeStyle
.PerWebRequest
197 IHandler handler
= kernel
.GetHandler("customer");
198 Assert
.AreEqual(LifestyleType
.PerWebRequest
, handler
.ComponentModel
.LifestyleType
);
202 public void AddComponent_WithPooledLifestyle_WorksFine()
205 Component
.For
<ICustomer
>()
207 .ImplementedBy
<CustomerImpl
>()
211 IHandler handler
= kernel
.GetHandler("customer");
212 Assert
.AreEqual(LifestyleType
.Pooled
, handler
.ComponentModel
.LifestyleType
);
216 public void AddComponent_WithPooledWithSizeLifestyle_WorksFine()
219 Component
.For
<ICustomer
>()
221 .ImplementedBy
<CustomerImpl
>()
222 .LifeStyle
.PooledWithSize(5, 10)
225 IHandler handler
= kernel
.GetHandler("customer");
226 Assert
.AreEqual(LifestyleType
.Pooled
, handler
.ComponentModel
.LifestyleType
);
230 public void AddComponent_Activator_WorksFine()
233 Component
.For
<ICustomer
>()
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
);
247 public void AddComponent_ExtendedProperties_WorksFine()
250 Component
.For
<ICustomer
>()
251 .ImplementedBy
<CustomerImpl
>()
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"]);
266 public void AddComponent_ExtendedProperties_UsingAnonymousType()
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"]);
281 public void AddComponent_CustomDependencies_WorksFine()
284 Component
.For
<ICustomer
>()
285 .ImplementedBy
<CustomerImpl
>()
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);
302 public void AddComponent_CustomDependencies_UsingAnonymousType()
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);
317 public void AddComponent_CustomDependenciesDictionary_WorksFine()
319 Hashtable customDependencies
= new Hashtable();
320 customDependencies
["Name"] = "Caption Hook";
321 customDependencies
["Address"] = "Fairyland";
322 customDependencies
["Age"] = 45;
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);
337 public void AddComponent_ServiceOverrides_WorksFine()
340 Component
.For
<ICustomer
>()
342 .ImplementedBy
<CustomerImpl
>()
344 Property
.ForKey("Name").Eq("Caption Hook"),
345 Property
.ForKey("Address").Eq("Fairyland"),
346 Property
.ForKey("Age").Eq(45)
348 Component
.For
<CustomerChain1
>()
351 Property
.ForKey("Name").Eq("Bigfoot"),
352 Property
.ForKey("Address").Eq("Forest"),
353 Property
.ForKey("Age").Eq(100)
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);
368 public void AddComponent_ArrayServiceOverrides_WorksFine()
371 Component
.For
<ICommon
>()
373 .ImplementedBy
<CommonImpl1
>(),
374 Component
.For
<ICommon
>()
376 .ImplementedBy
<CommonImpl2
>(),
377 Component
.For
<ClassWithArrayConstructor
>()
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]);
394 public void AddComponent_GenericListServiceOverrides_WorksFine()
397 Component
.For
<ICommon
>()
399 .ImplementedBy
<CommonImpl1
>(),
400 Component
.For
<ICommon
>()
402 .ImplementedBy
<CommonImpl2
>(),
403 Component
.For
<ClassWithListConstructor
>()
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]);
420 public void AddComponent_ServiceOverrides_UsingAnonymousType()
423 Component
.For
<ICustomer
>()
425 .ImplementedBy
<CustomerImpl
>()
427 Property
.ForKey("Name").Eq("Caption Hook"),
428 Property
.ForKey("Address").Eq("Fairyland"),
429 Property
.ForKey("Age").Eq(45)
431 Component
.For
<CustomerChain1
>()
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);
451 public void AddComponent_ServiceOverridesDictionary_WorksFine()
453 Hashtable serviceOverrides
= new Hashtable();
454 serviceOverrides
["customer"] = "customer1";
457 Component
.For
<ICustomer
>()
459 .ImplementedBy
<CustomerImpl
>()
461 Property
.ForKey("Name").Eq("Caption Hook"),
462 Property
.ForKey("Address").Eq("Fairyland"),
463 Property
.ForKey("Age").Eq(45)
465 Component
.For
<CustomerChain1
>()
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);
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}"));
491 Component
.For
<ICommon
>()
493 .ImplementedBy
<CommonImpl1
>(),
494 Component
.For
<ICommon
>()
496 .ImplementedBy
<CommonImpl2
>(),
497 Component
.For
<ClassWithArrayConstructor
>()
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]);
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}"));
522 Component
.For
<ICommon
>()
524 .ImplementedBy
<CommonImpl1
>(),
525 Component
.For
<ICommon
>()
527 .ImplementedBy
<CommonImpl2
>(),
528 Component
.For
<ClassWithListConstructor
>()
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]);
543 public void AddComponent_WithComplexConfiguration_WorksFine()
546 Component
.For
<ClassWithComplexParameter
>()
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
);
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
);