Reverted DictionaryComponentAttribute changes.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / Lifestyle / LifestyleManagerTestCase.cs
blob556a76e55d14d87fb5ceb3eb0dd67028cae0a5ef
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.Lifestyle
17 using System;
18 using System.Threading;
19 using Castle.Core;
20 using Castle.Core.Configuration;
21 using Castle.MicroKernel.Tests.Lifestyle.Components;
22 using NUnit.Framework;
24 /// <summary>
25 /// Summary description for LifestyleManagerTestCase.
26 /// </summary>
27 [TestFixture]
28 public class LifestyleManagerTestCase
30 private IKernel kernel;
32 private IComponent instance3;
34 [SetUp]
35 public void CreateContainer()
37 kernel = new DefaultKernel();
40 [TearDown]
41 public void DisposeContainer()
43 kernel.Dispose();
46 [Test]
47 public void TestTransient()
49 kernel.AddComponent("a", typeof(IComponent), typeof(TransientComponent));
51 IHandler handler = kernel.GetHandler("a");
53 IComponent instance1 = handler.Resolve(CreationContext.Empty) as IComponent;
54 IComponent instance2 = handler.Resolve(CreationContext.Empty) as IComponent;
56 Assert.IsNotNull(instance1);
57 Assert.IsNotNull(instance2);
59 Assert.IsTrue(!instance1.Equals(instance2));
60 Assert.IsTrue(instance1.ID != instance2.ID);
62 handler.Release(instance1);
63 handler.Release(instance2);
66 [Test]
67 [ExpectedException(typeof(ArgumentException))]
68 public void BadLifestyleSetProgromatically()
70 kernel.AddComponent("a", typeof(IComponent), typeof(NoInfoComponent), LifestyleType.Undefined);
73 [Test]
74 public void LifestyleSetProgromatically()
76 TestHandlersLifestyle(typeof(NoInfoComponent), LifestyleType.Transient, false);
77 TestHandlersLifestyle(typeof(NoInfoComponent), LifestyleType.Singleton, false);
78 TestHandlersLifestyle(typeof(NoInfoComponent), LifestyleType.Thread, false);
79 TestHandlersLifestyle(typeof(NoInfoComponent), LifestyleType.Transient, false);
80 TestHandlersLifestyle(typeof(NoInfoComponent), LifestyleType.PerWebRequest, false);
82 TestHandlersLifestyleWithService(typeof(NoInfoComponent), LifestyleType.Transient, false);
83 TestHandlersLifestyleWithService(typeof(NoInfoComponent), LifestyleType.Singleton, false);
84 TestHandlersLifestyleWithService(typeof(NoInfoComponent), LifestyleType.Thread, false);
85 TestHandlersLifestyleWithService(typeof(NoInfoComponent), LifestyleType.Transient, false);
86 TestHandlersLifestyleWithService(typeof(NoInfoComponent), LifestyleType.PerWebRequest, false);
88 TestLifestyleAndSameness(typeof(PerThreadComponent), LifestyleType.Transient, true, false);
89 TestLifestyleAndSameness(typeof(SingletonComponent), LifestyleType.Transient, true, false);
90 TestLifestyleAndSameness(typeof(TransientComponent), LifestyleType.Singleton, true, true);
92 TestLifestyleWithServiceAndSameness(typeof(PerThreadComponent), LifestyleType.Transient, true, false);
93 TestLifestyleWithServiceAndSameness(typeof(SingletonComponent), LifestyleType.Transient, true, false);
94 TestLifestyleWithServiceAndSameness(typeof(TransientComponent), LifestyleType.Singleton, true, true);
97 private void TestLifestyleAndSameness(Type componentType, LifestyleType lifestyle, bool overwrite, bool areSame)
99 string key = TestHandlersLifestyle(componentType, lifestyle, overwrite);
100 TestSameness(key, areSame);
103 private void TestLifestyleWithServiceAndSameness(Type componentType, LifestyleType lifestyle, bool overwrite,
104 bool areSame)
106 string key = TestHandlersLifestyleWithService(componentType, lifestyle, overwrite);
107 TestSameness(key, areSame);
110 private void TestSameness(string key, bool areSame)
112 IComponent one = kernel[key] as IComponent;
113 IComponent two = kernel[key] as IComponent;
114 if (areSame)
116 Assert.AreSame(one, two);
118 else
120 Assert.AreNotSame(one, two);
124 private string TestHandlersLifestyle(Type componentType, LifestyleType lifestyle, bool overwrite)
126 string key = Guid.NewGuid().ToString();
127 kernel.AddComponent(key, componentType, lifestyle, overwrite);
128 IHandler handler = kernel.GetHandler(key);
129 Assert.AreEqual(lifestyle, handler.ComponentModel.LifestyleType);
130 return key;
133 private string TestHandlersLifestyleWithService(Type componentType, LifestyleType lifestyle, bool overwrite)
135 string key = Guid.NewGuid().ToString();
136 kernel.AddComponent(key, typeof(IComponent), componentType, lifestyle, overwrite);
137 IHandler handler = kernel.GetHandler(key);
138 Assert.AreEqual(lifestyle, handler.ComponentModel.LifestyleType);
139 return key;
142 [Test]
143 public void LifestyleSetThroughAttribute()
145 kernel.AddComponent("a", typeof(TransientComponent));
146 IHandler handler = kernel.GetHandler("a");
147 Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
149 kernel.AddComponent("b", typeof(SingletonComponent));
150 handler = kernel.GetHandler("b");
151 Assert.AreEqual(LifestyleType.Singleton, handler.ComponentModel.LifestyleType);
153 kernel.AddComponent("c", typeof(CustomComponent));
154 handler = kernel.GetHandler("c");
155 Assert.AreEqual(LifestyleType.Custom, handler.ComponentModel.LifestyleType);
157 kernel.AddComponent("d", typeof(PerWebRequestComponent));
158 handler = kernel.GetHandler("d");
159 Assert.AreEqual(LifestyleType.PerWebRequest, handler.ComponentModel.LifestyleType);
162 [Test]
163 public void LifestyleSetThroughExternalConfig()
165 IConfiguration confignode = new MutableConfiguration("component");
166 confignode.Attributes.Add("lifestyle", "transient");
167 kernel.ConfigurationStore.AddComponentConfiguration("a", confignode);
168 kernel.AddComponent("a", typeof(NoInfoComponent));
169 IHandler handler = kernel.GetHandler("a");
170 Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
172 confignode = new MutableConfiguration("component");
173 confignode.Attributes.Add("lifestyle", "singleton");
174 kernel.ConfigurationStore.AddComponentConfiguration("b", confignode);
175 kernel.AddComponent("b", typeof(NoInfoComponent));
176 handler = kernel.GetHandler("b");
177 Assert.AreEqual(LifestyleType.Singleton, handler.ComponentModel.LifestyleType);
179 confignode = new MutableConfiguration("component");
180 confignode.Attributes.Add("lifestyle", "thread");
181 kernel.ConfigurationStore.AddComponentConfiguration("c", confignode);
182 kernel.AddComponent("c", typeof(NoInfoComponent));
183 handler = kernel.GetHandler("c");
184 Assert.AreEqual(LifestyleType.Thread, handler.ComponentModel.LifestyleType);
186 confignode = new MutableConfiguration("component");
187 confignode.Attributes.Add("lifestyle", "perWebRequest");
188 kernel.ConfigurationStore.AddComponentConfiguration("d", confignode);
189 kernel.AddComponent("d", typeof(NoInfoComponent));
190 handler = kernel.GetHandler("d");
191 Assert.AreEqual(LifestyleType.PerWebRequest, handler.ComponentModel.LifestyleType);
194 [Test]
195 public void TestSingleton()
197 kernel.AddComponent("a", typeof(IComponent), typeof(SingletonComponent));
199 IHandler handler = kernel.GetHandler("a");
201 IComponent instance1 = handler.Resolve(CreationContext.Empty) as IComponent;
202 IComponent instance2 = handler.Resolve(CreationContext.Empty) as IComponent;
204 Assert.IsNotNull(instance1);
205 Assert.IsNotNull(instance2);
207 Assert.IsTrue(instance1.Equals(instance2));
208 Assert.IsTrue(instance1.ID == instance2.ID);
210 handler.Release(instance1);
211 handler.Release(instance2);
214 [Test]
215 public void TestCustom()
217 kernel.AddComponent("a", typeof(IComponent), typeof(CustomComponent));
219 IHandler handler = kernel.GetHandler("a");
221 IComponent instance1 = handler.Resolve(CreationContext.Empty) as IComponent;
223 Assert.IsNotNull(instance1);
226 [Test]
227 public void TestPerThread()
229 kernel.AddComponent("a", typeof(IComponent), typeof(PerThreadComponent));
231 IHandler handler = kernel.GetHandler("a");
233 IComponent instance1 = handler.Resolve(CreationContext.Empty) as IComponent;
234 IComponent instance2 = handler.Resolve(CreationContext.Empty) as IComponent;
236 Assert.IsNotNull(instance1);
237 Assert.IsNotNull(instance2);
239 Assert.IsTrue(instance1.Equals(instance2));
240 Assert.IsTrue(instance1.ID == instance2.ID);
242 Thread thread = new Thread(new ThreadStart(OtherThread));
243 thread.Start();
244 thread.Join();
246 Assert.IsNotNull(instance3);
247 Assert.IsTrue(!instance1.Equals(instance3));
248 Assert.IsTrue(instance1.ID != instance3.ID);
250 handler.Release(instance1);
251 handler.Release(instance2);
254 private void OtherThread()
256 IHandler handler = kernel.GetHandler("a");
257 instance3 = handler.Resolve(CreationContext.Empty) as IComponent;