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
.MicroKernel
.Registration
;
19 using Castle
.MicroKernel
.Tests
.Lifestyle
.Components
;
20 using ClassComponents
;
22 using NUnit
.Framework
;
25 public class ComponentRegistrationTestCase
27 private IKernel kernel
;
32 kernel
= new DefaultKernel();
36 public void AddComponent_WithServiceOnly_RegisteredWithServiceTypeName()
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
);
54 public void AddComponent_WithServiceAndName_RegisteredNamed()
57 Component
.ForService
<CustomerImpl
>()
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
);
71 [ExpectedException(typeof(ComponentRegistrationException
),
72 "This component has already been assigned name 'customer'")]
73 public void AddComponent_NamedAlreadyAssigned_ThrowsException()
76 Component
.ForService
<CustomerImpl
>()
83 public void AddComponent_WithServiceAndClass_RegisteredWithClassTypeName()
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
);
97 [ExpectedException(typeof(ComponentRegistrationException
),
98 "This component has already been assigned implementation Castle.MicroKernel.Tests.ClassComponents.CustomerImpl")]
99 public void AddComponent_WithImplementationAlreadyAssigned_ThrowsException()
102 Component
.ForService
<ICustomer
>()
103 .ImplementedBy
<CustomerImpl
>()
104 .ImplementedBy
<CustomerImpl2
>()
109 public void AddComponent_Instance_UsesInstance()
111 CustomerImpl customer
= new CustomerImpl();
114 Component
.ForService
<ICustomer
>()
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
);
128 public void AddComponent_WithTransientLifestyle_WorksFine()
131 Component
.ForService
<ICustomer
>()
133 .ImplementedBy
<CustomerImpl
>()
137 IHandler handler
= kernel
.GetHandler("customer");
138 Assert
.AreEqual(LifestyleType
.Transient
, handler
.ComponentModel
.LifestyleType
);
142 public void AddComponent_WithSingletonLifestyle_WorksFine()
145 Component
.ForService
<ICustomer
>()
147 .ImplementedBy
<CustomerImpl
>()
151 IHandler handler
= kernel
.GetHandler("customer");
152 Assert
.AreEqual(LifestyleType
.Singleton
, handler
.ComponentModel
.LifestyleType
);
156 public void AddComponent_WithCustomLifestyle_WorksFine()
159 Component
.ForService
<ICustomer
>()
161 .ImplementedBy
<CustomerImpl
>()
162 .LifeStyle
.Custom
<MyLifestyleHandler
>()
165 IHandler handler
= kernel
.GetHandler("customer");
166 Assert
.AreEqual(LifestyleType
.Custom
, handler
.ComponentModel
.LifestyleType
);
170 public void AddComponent_WithThreadLifestyle_WorksFine()
173 Component
.ForService
<ICustomer
>()
175 .ImplementedBy
<CustomerImpl
>()
179 IHandler handler
= kernel
.GetHandler("customer");
180 Assert
.AreEqual(LifestyleType
.Thread
, handler
.ComponentModel
.LifestyleType
);
184 public void AddComponent_WithPerWebRequestLifestyle_WorksFine()
187 Component
.ForService
<ICustomer
>()
189 .ImplementedBy
<CustomerImpl
>()
190 .LifeStyle
.PerWebRequest
193 IHandler handler
= kernel
.GetHandler("customer");
194 Assert
.AreEqual(LifestyleType
.PerWebRequest
, handler
.ComponentModel
.LifestyleType
);
198 public void AddComponent_WithPooledLifestyle_WorksFine()
201 Component
.ForService
<ICustomer
>()
203 .ImplementedBy
<CustomerImpl
>()
207 IHandler handler
= kernel
.GetHandler("customer");
208 Assert
.AreEqual(LifestyleType
.Pooled
, handler
.ComponentModel
.LifestyleType
);
212 public void AddComponent_WithPooledWithSizeLifestyle_WorksFine()
215 Component
.ForService
<ICustomer
>()
217 .ImplementedBy
<CustomerImpl
>()
218 .LifeStyle
.PooledWithSize(5, 10)
221 IHandler handler
= kernel
.GetHandler("customer");
222 Assert
.AreEqual(LifestyleType
.Pooled
, handler
.ComponentModel
.LifestyleType
);
226 public void AddComponent_Activator_WorksFine()
229 Component
.ForService
<ICustomer
>()
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
);
243 public void AddComponent_ExtendedProperties_WorksFine()
246 Component
.ForService
<ICustomer
>()
247 .ImplementedBy
<CustomerImpl
>()
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"]);
262 public void AddComponent_ExtendedProperties_UsingAnonymousType()
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"]);
277 public void AddComponent_CustomDependencies_WorksFine()
280 Component
.ForService
<ICustomer
>()
281 .ImplementedBy
<CustomerImpl
>()
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);
298 public void AddComponent_CustomDependencies_UsingAnonymousType()
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);
313 public void AddComponent_CustomDependenciesDictionary_WorksFine()
315 Hashtable customDependencies
= new Hashtable();
316 customDependencies
["Name"] = "Caption Hook";
317 customDependencies
["Address"] = "Fairyland";
318 customDependencies
["Age"] = 45;
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);
333 public void AddComponent_ServiceOverrides_WorksFine()
336 Component
.ForService
<ICustomer
>()
338 .ImplementedBy
<CustomerImpl
>()
340 Property
.ForKey("Name").Eq("Caption Hook"),
341 Property
.ForKey("Address").Eq("Fairyland"),
342 Property
.ForKey("Age").Eq(45)
344 Component
.ForService
<CustomerChain1
>()
347 Property
.ForKey("Name").Eq("Bigfoot"),
348 Property
.ForKey("Address").Eq("Forest"),
349 Property
.ForKey("Age").Eq(100)
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);
366 public void AddComponent_ServiceOverrides_UsingAnonymousType()
369 Component
.ForService
<ICustomer
>()
371 .ImplementedBy
<CustomerImpl
>()
373 Property
.ForKey("Name").Eq("Caption Hook"),
374 Property
.ForKey("Address").Eq("Fairyland"),
375 Property
.ForKey("Age").Eq(45)
377 Component
.ForService
<CustomerChain1
>()
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);
397 public void AddComponent_ServiceOverridesDictionary_WorksFine()
399 Hashtable serviceOverrides
= new Hashtable();
400 serviceOverrides
["customer"] = "customer1";
403 Component
.ForService
<ICustomer
>()
405 .ImplementedBy
<CustomerImpl
>()
407 Property
.ForKey("Name").Eq("Caption Hook"),
408 Property
.ForKey("Address").Eq("Fairyland"),
409 Property
.ForKey("Age").Eq(45)
411 Component
.ForService
<CustomerChain1
>()
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);