Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / MicroKernelTestCase.cs
blob38009281d24d0bcbf290eb7ca70cdb9cefefcbd4
1 // Copyright 2004-2007 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
17 using System;
18 using System.Collections;
19 using Castle.MicroKernel.Tests.ClassComponents;
20 using NUnit.Framework;
22 [TestFixture]
23 public class MicroKernelTestCase
25 private IKernel kernel;
27 [SetUp]
28 public void Init()
30 kernel = new DefaultKernel();
33 [TearDown]
34 public void Dispose()
36 kernel.Dispose();
39 [Test]
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);
53 [Test]
54 public void AddClassComponentWithInterface()
56 kernel.AddComponent("key", typeof(CustomerImpl));
57 Assert.IsTrue(kernel.HasComponent("key"));
60 [Test]
61 public void AddClassComponentWithNoInterface()
63 kernel.AddComponent("key", typeof(DefaultCustomer));
64 Assert.IsTrue(kernel.HasComponent("key"));
67 [Test]
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);
82 [Test]
83 public void AddComponentInstance2()
85 CustomerImpl customer = new CustomerImpl();
87 kernel.AddComponentInstance("key", customer);
88 Assert.IsTrue(kernel.HasComponent("key"));
90 CustomerImpl customer2 = kernel["key"] as CustomerImpl;
91 Assert.AreSame(customer, customer2);
93 customer2 = kernel[typeof(CustomerImpl)] as CustomerImpl;
94 Assert.AreSame(customer, customer2);
97 [Test]
98 public void AddCommonComponent()
100 kernel.AddComponent("key", typeof(ICustomer), typeof(CustomerImpl));
101 Assert.IsTrue(kernel.HasComponent("key"));
104 [Test]
105 public void HandlerForClassComponent()
107 kernel.AddComponent("key", typeof(CustomerImpl));
108 IHandler handler = kernel.GetHandler("key");
109 Assert.IsNotNull(handler);
112 [Test]
113 public void HandlerForClassWithNoInterface()
115 kernel.AddComponent("key", typeof(DefaultCustomer));
116 IHandler handler = kernel.GetHandler("key");
117 Assert.IsNotNull(handler);
120 [Test]
121 [ExpectedException(typeof(ComponentRegistrationException))]
122 public void KeyCollision()
124 kernel.AddComponent("key", typeof(CustomerImpl));
125 kernel.AddComponent("key", typeof(CustomerImpl));
128 [Test]
129 [ExpectedException(typeof(ComponentNotFoundException))]
130 public void UnregisteredComponentByKey()
132 kernel.AddComponent("key1", typeof(CustomerImpl));
133 object component = kernel["key2"];
136 [Test]
137 [ExpectedException(typeof(ComponentNotFoundException))]
138 public void UnregisteredComponentByService()
140 kernel.AddComponent("key1", typeof(CustomerImpl));
141 object component = kernel[typeof(IDisposable)];
144 [Test]
145 public void AddClassThatHasTwoParametersOfSameTypeAndNoOverloads()
147 kernel.AddComponent("test", typeof(ClassWithTwoParametersWithSameType));
148 kernel.AddComponent("test2", typeof(ICommon), typeof(CommonImpl1));
149 object resolved = kernel.Resolve(typeof(ClassWithTwoParametersWithSameType), new Hashtable());
150 Assert.IsNotNull(resolved);
153 [Test]
154 public void ResolveServices()
156 kernel.AddComponent("test", typeof(ICommon), typeof(CommonImpl2));
157 kernel.AddComponent("test2", typeof(ICommon), typeof(CommonImpl1));
158 ICommon[] services = kernel.ResolveServices<ICommon>();
159 Assert.AreEqual(2, services.Length);
162 [Test]
163 public void ResolveServicesWaitingOnDependencies()
165 kernel.AddComponent("test", typeof(ICommon), typeof(CommonImplWithDependancy));
166 ICommon[] services = kernel.ResolveServices<ICommon>();
167 Assert.AreEqual(0, services.Length);