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
18 using System
.Collections
;
19 using Castle
.MicroKernel
.Tests
.ClassComponents
;
20 using NUnit
.Framework
;
23 public class MicroKernelTestCase
25 private IKernel kernel
;
30 kernel
= new DefaultKernel();
40 public void IOC_50_AddTwoComponentWithSameService_RequestFirstByKey_RemoveFirst_RequestByService_ShouldReturnSecond()
42 kernel
.AddComponent("key", typeof(ICustomer
), typeof(CustomerImpl
));
43 kernel
.AddComponent("key2", typeof(ICustomer
), typeof(CustomerImpl
));
44 object result
= kernel
["key"];
45 Assert
.IsNotNull(result
);
47 kernel
.RemoveComponent("key");
49 result
= kernel
[typeof(ICustomer
)];
50 Assert
.IsNotNull(result
);
54 public void AddClassComponentWithInterface()
56 kernel
.AddComponent("key", typeof(CustomerImpl
));
57 Assert
.IsTrue(kernel
.HasComponent("key"));
61 public void AddClassComponentWithNoInterface()
63 kernel
.AddComponent("key", typeof(DefaultCustomer
));
64 Assert
.IsTrue(kernel
.HasComponent("key"));
68 public void AddComponentInstance()
70 CustomerImpl customer
= new CustomerImpl();
72 kernel
.AddComponentInstance("key", typeof(ICustomer
), customer
);
73 Assert
.IsTrue(kernel
.HasComponent("key"));
75 CustomerImpl customer2
= kernel
["key"] as CustomerImpl
;
76 Assert
.AreSame(customer
, customer2
);
78 customer2
= kernel
[typeof(ICustomer
)] as CustomerImpl
;
79 Assert
.AreSame(customer
, customer2
);
83 public void AddComponentInstance_ByService()
85 CustomerImpl customer
= new CustomerImpl();
87 kernel
.AddComponentInstance
<ICustomer
>(customer
);
88 Assert
.AreSame(kernel
[typeof(ICustomer
)],customer
);
92 public void AddComponentInstance2()
94 CustomerImpl customer
= new CustomerImpl();
96 kernel
.AddComponentInstance("key", customer
);
97 Assert
.IsTrue(kernel
.HasComponent("key"));
99 CustomerImpl customer2
= kernel
["key"] as CustomerImpl
;
100 Assert
.AreSame(customer
, customer2
);
102 customer2
= kernel
[typeof(CustomerImpl
)] as CustomerImpl
;
103 Assert
.AreSame(customer
, customer2
);
107 public void AddCommonComponent()
109 kernel
.AddComponent("key", typeof(ICustomer
), typeof(CustomerImpl
));
110 Assert
.IsTrue(kernel
.HasComponent("key"));
114 public void HandlerForClassComponent()
116 kernel
.AddComponent("key", typeof(CustomerImpl
));
117 IHandler handler
= kernel
.GetHandler("key");
118 Assert
.IsNotNull(handler
);
122 public void HandlerForClassWithNoInterface()
124 kernel
.AddComponent("key", typeof(DefaultCustomer
));
125 IHandler handler
= kernel
.GetHandler("key");
126 Assert
.IsNotNull(handler
);
130 [ExpectedException(typeof(ComponentRegistrationException
))]
131 public void KeyCollision()
133 kernel
.AddComponent("key", typeof(CustomerImpl
));
134 kernel
.AddComponent("key", typeof(CustomerImpl
));
138 [ExpectedException(typeof(ComponentNotFoundException
))]
139 public void UnregisteredComponentByKey()
141 kernel
.AddComponent("key1", typeof(CustomerImpl
));
142 object component
= kernel
["key2"];
146 [ExpectedException(typeof(ComponentNotFoundException
))]
147 public void UnregisteredComponentByService()
149 kernel
.AddComponent("key1", typeof(CustomerImpl
));
150 object component
= kernel
[typeof(IDisposable
)];
154 public void AddClassThatHasTwoParametersOfSameTypeAndNoOverloads()
156 kernel
.AddComponent("test", typeof(ClassWithTwoParametersWithSameType
));
157 kernel
.AddComponent("test2", typeof(ICommon
), typeof(CommonImpl1
));
158 object resolved
= kernel
.Resolve(typeof(ClassWithTwoParametersWithSameType
), new Hashtable());
159 Assert
.IsNotNull(resolved
);
163 public void ResolveAll()
165 kernel
.AddComponent("test", typeof(ICommon
), typeof(CommonImpl2
));
166 kernel
.AddComponent("test2", typeof(ICommon
), typeof(CommonImpl1
));
167 ICommon
[] services
= kernel
.ResolveAll
<ICommon
>();
168 Assert
.AreEqual(2, services
.Length
);
172 public void ResolveAllWaitingOnDependencies()
174 kernel
.AddComponent("test", typeof(ICommon
), typeof(CommonImplWithDependancy
));
175 ICommon
[] services
= kernel
.ResolveAll
<ICommon
>();
176 Assert
.AreEqual(0, services
.Length
);