1 // Copyright 2004-2007 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
.Lifestyle
18 using System
.Threading
;
20 using Castle
.Core
.Configuration
;
21 using Castle
.MicroKernel
.Tests
.Lifestyle
.Components
;
22 using NUnit
.Framework
;
25 /// Summary description for LifestyleManagerTestCase.
28 public class LifestyleManagerTestCase
30 private IKernel kernel
;
32 private IComponent instance3
;
35 public void CreateContainer()
37 kernel
= new DefaultKernel();
41 public void DisposeContainer()
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
);
67 [ExpectedException(typeof(ArgumentException
))]
68 public void BadLifestyleSetProgromatically()
70 kernel
.AddComponent("a", typeof(IComponent
), typeof(NoInfoComponent
), LifestyleType
.Undefined
);
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
,
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
;
116 Assert
.AreSame(one
, two
);
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
);
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
);
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
);
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
);
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
);
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
);
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
));
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
;