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
;
18 using Castle
.Core
.Configuration
;
19 using Castle
.MicroKernel
.Registration
;
20 using Castle
.MicroKernel
.Tests
.Lifestyle
.Components
;
21 using ClassComponents
;
23 using NUnit
.Framework
;
26 public class ComponentRegistrationTestCase
28 private IKernel kernel
;
33 kernel
= new DefaultKernel();
37 public void AddComponent_WithServiceOnly_RegisteredWithServiceTypeName()
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
);
55 public void AddComponent_WithServiceAndName_RegisteredNamed()
58 Component
.For
<CustomerImpl
>()
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
);
72 [ExpectedException(typeof(ComponentRegistrationException
),
73 "This component has already been assigned name 'customer'")]
74 public void AddComponent_NamedAlreadyAssigned_ThrowsException()
77 Component
.For
<CustomerImpl
>()
84 public void AddComponent_WithServiceAndClass_RegisteredWithClassTypeName()
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
);
98 [ExpectedException(typeof(ComponentRegistrationException
),
99 "This component has already been assigned implementation Castle.MicroKernel.Tests.ClassComponents.CustomerImpl")]
100 public void AddComponent_WithImplementationAlreadyAssigned_ThrowsException()
103 Component
.For
<ICustomer
>()
104 .ImplementedBy
<CustomerImpl
>()
105 .ImplementedBy
<CustomerImpl2
>()
110 public void AddComponent_Instance_UsesInstance()
112 CustomerImpl customer
= new CustomerImpl();
115 Component
.For
<ICustomer
>()
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
);
129 public void AddComponent_WithTransientLifestyle_WorksFine()
132 Component
.For
<ICustomer
>()
134 .ImplementedBy
<CustomerImpl
>()
138 IHandler handler
= kernel
.GetHandler("customer");
139 Assert
.AreEqual(LifestyleType
.Transient
, handler
.ComponentModel
.LifestyleType
);
143 public void AddComponent_WithSingletonLifestyle_WorksFine()
146 Component
.For
<ICustomer
>()
148 .ImplementedBy
<CustomerImpl
>()
152 IHandler handler
= kernel
.GetHandler("customer");
153 Assert
.AreEqual(LifestyleType
.Singleton
, handler
.ComponentModel
.LifestyleType
);
157 public void AddComponent_WithCustomLifestyle_WorksFine()
160 Component
.For
<ICustomer
>()
162 .ImplementedBy
<CustomerImpl
>()
163 .LifeStyle
.Custom
<MyLifestyleHandler
>()
166 IHandler handler
= kernel
.GetHandler("customer");
167 Assert
.AreEqual(LifestyleType
.Custom
, handler
.ComponentModel
.LifestyleType
);
171 public void AddComponent_WithThreadLifestyle_WorksFine()
174 Component
.For
<ICustomer
>()
176 .ImplementedBy
<CustomerImpl
>()
180 IHandler handler
= kernel
.GetHandler("customer");
181 Assert
.AreEqual(LifestyleType
.Thread
, handler
.ComponentModel
.LifestyleType
);
185 public void AddComponent_WithPerWebRequestLifestyle_WorksFine()
188 Component
.For
<ICustomer
>()
190 .ImplementedBy
<CustomerImpl
>()
191 .LifeStyle
.PerWebRequest
194 IHandler handler
= kernel
.GetHandler("customer");
195 Assert
.AreEqual(LifestyleType
.PerWebRequest
, handler
.ComponentModel
.LifestyleType
);
199 public void AddComponent_WithPooledLifestyle_WorksFine()
202 Component
.For
<ICustomer
>()
204 .ImplementedBy
<CustomerImpl
>()
208 IHandler handler
= kernel
.GetHandler("customer");
209 Assert
.AreEqual(LifestyleType
.Pooled
, handler
.ComponentModel
.LifestyleType
);
213 public void AddComponent_WithPooledWithSizeLifestyle_WorksFine()
216 Component
.For
<ICustomer
>()
218 .ImplementedBy
<CustomerImpl
>()
219 .LifeStyle
.PooledWithSize(5, 10)
222 IHandler handler
= kernel
.GetHandler("customer");
223 Assert
.AreEqual(LifestyleType
.Pooled
, handler
.ComponentModel
.LifestyleType
);
227 public void AddComponent_Activator_WorksFine()
230 Component
.For
<ICustomer
>()
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
);
244 public void AddComponent_ExtendedProperties_WorksFine()
247 Component
.For
<ICustomer
>()
248 .ImplementedBy
<CustomerImpl
>()
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"]);
263 public void AddComponent_ExtendedProperties_UsingAnonymousType()
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"]);
278 public void AddComponent_CustomDependencies_WorksFine()
281 Component
.For
<ICustomer
>()
282 .ImplementedBy
<CustomerImpl
>()
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);
299 public void AddComponent_CustomDependencies_UsingAnonymousType()
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);
314 public void AddComponent_CustomDependenciesDictionary_WorksFine()
316 Hashtable customDependencies
= new Hashtable();
317 customDependencies
["Name"] = "Caption Hook";
318 customDependencies
["Address"] = "Fairyland";
319 customDependencies
["Age"] = 45;
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);
334 public void AddComponent_ServiceOverrides_WorksFine()
337 Component
.For
<ICustomer
>()
339 .ImplementedBy
<CustomerImpl
>()
341 Property
.ForKey("Name").Eq("Caption Hook"),
342 Property
.ForKey("Address").Eq("Fairyland"),
343 Property
.ForKey("Age").Eq(45)
345 Component
.For
<CustomerChain1
>()
348 Property
.ForKey("Name").Eq("Bigfoot"),
349 Property
.ForKey("Address").Eq("Forest"),
350 Property
.ForKey("Age").Eq(100)
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);
365 public void AddComponent_ArrayServiceOverrides_WorksFine()
368 Component
.For
<ICommon
>()
370 .ImplementedBy
<CommonImpl1
>(),
371 Component
.For
<ICommon
>()
373 .ImplementedBy
<CommonImpl2
>(),
374 Component
.For
<ClassWithArrayConstructor
>()
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]);
391 public void AddComponent_GenericListServiceOverrides_WorksFine()
394 Component
.For
<ICommon
>()
396 .ImplementedBy
<CommonImpl1
>(),
397 Component
.For
<ICommon
>()
399 .ImplementedBy
<CommonImpl2
>(),
400 Component
.For
<ClassWithListConstructor
>()
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]);
417 public void AddComponent_ServiceOverrides_UsingAnonymousType()
420 Component
.For
<ICustomer
>()
422 .ImplementedBy
<CustomerImpl
>()
424 Property
.ForKey("Name").Eq("Caption Hook"),
425 Property
.ForKey("Address").Eq("Fairyland"),
426 Property
.ForKey("Age").Eq(45)
428 Component
.For
<CustomerChain1
>()
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);
448 public void AddComponent_ServiceOverridesDictionary_WorksFine()
450 Hashtable serviceOverrides
= new Hashtable();
451 serviceOverrides
["customer"] = "customer1";
454 Component
.For
<ICustomer
>()
456 .ImplementedBy
<CustomerImpl
>()
458 Property
.ForKey("Name").Eq("Caption Hook"),
459 Property
.ForKey("Address").Eq("Fairyland"),
460 Property
.ForKey("Age").Eq(45)
462 Component
.For
<CustomerChain1
>()
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);
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}"));
488 Component
.For
<ICommon
>()
490 .ImplementedBy
<CommonImpl1
>(),
491 Component
.For
<ICommon
>()
493 .ImplementedBy
<CommonImpl2
>(),
494 Component
.For
<ClassWithArrayConstructor
>()
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]);
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}"));
519 Component
.For
<ICommon
>()
521 .ImplementedBy
<CommonImpl1
>(),
522 Component
.For
<ICommon
>()
524 .ImplementedBy
<CommonImpl2
>(),
525 Component
.For
<ClassWithListConstructor
>()
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]);