Allow component registration specification of LifeStyle using LifeStyleType.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / MicroKernelTestCase.cs
blob82f4d22340cb7a73808297705a8517108184c416
1 // Copyright 2004-2008 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 AddComponentInstance_ByService()
85 CustomerImpl customer = new CustomerImpl();
87 kernel.AddComponentInstance <ICustomer>(customer);
88 Assert.AreSame(kernel[typeof(ICustomer)],customer);
91 [Test]
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);
106 [Test]
107 public void AddCommonComponent()
109 kernel.AddComponent("key", typeof(ICustomer), typeof(CustomerImpl));
110 Assert.IsTrue(kernel.HasComponent("key"));
113 [Test]
114 public void HandlerForClassComponent()
116 kernel.AddComponent("key", typeof(CustomerImpl));
117 IHandler handler = kernel.GetHandler("key");
118 Assert.IsNotNull(handler);
121 [Test]
122 public void HandlerForClassWithNoInterface()
124 kernel.AddComponent("key", typeof(DefaultCustomer));
125 IHandler handler = kernel.GetHandler("key");
126 Assert.IsNotNull(handler);
129 [Test]
130 [ExpectedException(typeof(ComponentRegistrationException))]
131 public void KeyCollision()
133 kernel.AddComponent("key", typeof(CustomerImpl));
134 kernel.AddComponent("key", typeof(CustomerImpl));
137 [Test]
138 [ExpectedException(typeof(ComponentNotFoundException))]
139 public void UnregisteredComponentByKey()
141 kernel.AddComponent("key1", typeof(CustomerImpl));
142 object component = kernel["key2"];
145 [Test]
146 [ExpectedException(typeof(ComponentNotFoundException))]
147 public void UnregisteredComponentByService()
149 kernel.AddComponent("key1", typeof(CustomerImpl));
150 object component = kernel[typeof(IDisposable)];
153 [Test]
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);
162 [Test]
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);
171 [Test]
172 public void ResolveAllWaitingOnDependencies()
174 kernel.AddComponent("test", typeof(ICommon), typeof(CommonImplWithDependancy));
175 ICommon[] services = kernel.ResolveAll<ICommon>();
176 Assert.AreEqual(0, services.Length);