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
.Windsor
.Tests
.Adapters
.ComponentModel
18 using System
.ComponentModel
;
19 using System
.ComponentModel
.Design
;
20 using Castle
.MicroKernel
;
21 using Castle
.Windsor
.Adapters
.ComponentModel
;
22 using Castle
.Windsor
.Tests
.Components
;
23 using NUnit
.Framework
;
26 public class ContainerAdapterTestCase
28 private bool disposed
;
29 private int calledCount
;
30 private IContainerAdapter container
;
37 container
= new ContainerAdapter();
47 public void GetIntrinsicServices()
49 Assert
.IsNotNull(container
.GetService(typeof(IContainer
)));
50 Assert
.IsNotNull(container
.GetService(typeof(IServiceContainer
)));
51 Assert
.IsNotNull(container
.GetService(typeof(IWindsorContainer
)));
52 Assert
.IsNotNull(container
.GetService(typeof(IKernel
)));
56 [ExpectedException(typeof(ArgumentException
),
57 "A service for type 'Castle.Windsor.IWindsorContainer' already exists")]
58 public void AddIntrinsicService()
60 container
.AddService(typeof(IWindsorContainer
), new WindsorContainer());
64 [ExpectedException(typeof(ArgumentException
), "Cannot remove an instrinsic service")]
65 public void RemoveInstrinsicService()
67 container
.RemoveService(typeof(IWindsorContainer
));
71 public void GetExistingServiceFromKernel()
73 WindsorContainer windsor
= new WindsorContainer();
75 windsor
.AddComponent("calculator", typeof(ICalcService
), typeof(CalculatorService
));
77 IContainerAdapter adapter
= new ContainerAdapter(windsor
);
79 ICalcService service
= (ICalcService
) adapter
.GetService(typeof(ICalcService
));
81 Assert
.IsNotNull(service
);
85 public void AddUnamedComponent()
87 IComponent component
= new Component();
89 container
.Add(component
);
90 ISite site
= component
.Site
;
91 Assert
.IsNotNull(site
);
92 Assert
.AreSame(container
, site
.Container
);
93 Assert
.AreEqual(1, container
.Components
.Count
);
94 Assert
.IsNotNull(site
.GetService(typeof(IHandler
)));
96 IComponent component2
= container
.Components
[0];
97 Assert
.AreSame(component
, component2
);
98 Assert
.AreSame(container
, component2
.Site
.Container
);
102 public void AddNamedComponent()
104 IComponent component
= new Component();
106 container
.Add(component
, "myComponent");
107 ISite site
= component
.Site
;
108 Assert
.IsNotNull(site
);
109 Assert
.AreSame(container
, site
.Container
);
110 Assert
.AreEqual(1, container
.Components
.Count
);
111 Assert
.IsNotNull(container
.Components
["myComponent"]);
112 Assert
.IsTrue(container
.Container
.Kernel
.HasComponent("myComponent"));
113 Assert
.IsNotNull(site
.GetService(typeof(IHandler
)));
115 IComponent component2
= container
.Components
["myComponent"];
116 Assert
.AreSame(component2
, component
);
117 Assert
.AreSame(component2
, container
.Container
["myComponent"]);
118 Assert
.AreSame(container
, component2
.Site
.Container
);
122 [ExpectedException(typeof(ArgumentException
),
123 "There is a component already registered for the given key myComponent")]
124 public void AddDuplicateComponent()
126 container
.Add(new Component(), "myComponent");
127 container
.Add(new Component(), "myComponent");
131 public void RemoveUnnamedComponent()
133 IComponent component
= new Component();
135 container
.Add(component
);
136 IContainerAdapterSite site
= component
.Site
as IContainerAdapterSite
;
137 Assert
.IsNotNull(site
);
139 container
.Remove(component
);
140 Assert
.IsNull(component
.Site
);
141 Assert
.AreEqual(0, container
.Components
.Count
);
142 Assert
.IsFalse(container
.Container
.Kernel
.HasComponent(site
.EffectiveName
));
146 public void RemoveNamedComponent()
148 IComponent component
= new Component();
150 container
.Add(component
, "myComponent");
151 ISite site
= component
.Site
as ISite
;
152 Assert
.IsNotNull(site
);
154 container
.Remove(component
);
155 Assert
.IsNull(component
.Site
);
156 Assert
.AreEqual(0, container
.Components
.Count
);
157 Assert
.IsFalse(container
.Container
.Kernel
.HasComponent("myComponent"));
161 public void RemoveComponentFromWindsor()
163 IComponent component
= new Component();
165 container
.Add(component
, "myComponent");
166 IContainerAdapterSite site
= component
.Site
as IContainerAdapterSite
;
168 container
.Container
.Kernel
.RemoveComponent(site
.EffectiveName
);
169 Assert
.AreEqual(0, container
.Components
.Count
);
173 public void GetComponentHandlers()
175 IComponent component
= new Component();
177 container
.Add(component
);
179 IHandler
[] handlers
= container
.Container
.Kernel
.GetHandlers(typeof(IComponent
));
180 Assert
.AreEqual(1, handlers
.Length
);
181 Assert
.AreEqual(handlers
[0].Resolve(CreationContext
.Empty
), component
);
185 public void AddServiceInstance()
187 ICalcService service
= new CalculatorService();
189 container
.AddService(typeof(ICalcService
), service
);
191 Assert
.AreSame(container
.GetService(typeof(ICalcService
)), service
);
192 Assert
.AreSame(container
.Container
[typeof(ICalcService
)], service
);
196 public void AddPromotedServiceInstance()
198 ContainerAdapter child
= new ContainerAdapter();
199 container
.Add(child
);
201 ICalcService service
= new CalculatorService();
203 child
.AddService(typeof(ICalcService
), service
, true);
205 Assert
.AreSame(child
.GetService(typeof(ICalcService
)), service
);
206 Assert
.AreSame(container
.GetService(typeof(ICalcService
)), service
);
208 container
.Remove(child
);
209 Assert
.IsNull(child
.GetService(typeof(ICalcService
)));
210 Assert
.AreSame(container
.GetService(typeof(ICalcService
)), service
);
214 [ExpectedException(typeof(ArgumentException
),
215 "A service for type 'Castle.Windsor.Tests.Components.ICalcService' already exists")]
216 public void AddExistingServiceInstance()
218 container
.AddService(typeof(ICalcService
), new CalculatorService());
219 container
.AddService(typeof(ICalcService
), new CalculatorService());
223 public void AddServiceCreatorCallback()
225 ServiceCreatorCallback callback
= new ServiceCreatorCallback(CreateCalculatorService
);
227 container
.AddService(typeof(ICalcService
), callback
);
229 ICalcService service
= (ICalcService
) container
.GetService(typeof(ICalcService
));
231 Assert
.IsNotNull(service
);
232 Assert
.AreSame(service
, container
.Container
[typeof(ICalcService
)]);
234 service
= (ICalcService
) container
.GetService(typeof(ICalcService
));
235 Assert
.AreEqual(1, calledCount
);
239 public void AddPromotedServiceCreatorCallback()
241 ContainerAdapter child
= new ContainerAdapter();
242 container
.Add(child
);
244 ServiceCreatorCallback callback
= new ServiceCreatorCallback(CreateCalculatorService
);
246 child
.AddService(typeof(ICalcService
), callback
, true);
248 ICalcService service
= (ICalcService
) child
.GetService(typeof(ICalcService
));
249 Assert
.IsNotNull(service
);
251 ICalcService promotedService
= (ICalcService
) container
.GetService(typeof(ICalcService
));
252 Assert
.IsNotNull(service
);
254 Assert
.AreSame(service
, promotedService
);
256 container
.Remove(child
);
257 Assert
.IsNull(child
.GetService(typeof(ICalcService
)));
258 Assert
.AreSame(container
.GetService(typeof(ICalcService
)), service
);
262 public void RemoveServiceInstance()
264 ICalcService service
= new CalculatorService();
266 container
.AddService(typeof(ICalcService
), service
);
267 container
.RemoveService(typeof(ICalcService
));
268 Assert
.IsNull(container
.GetService(typeof(ICalcService
)));
269 Assert
.IsFalse(container
.Container
.Kernel
.HasComponent(typeof(ICalcService
)));
273 public void RemovePromotedServiceInstance()
275 ContainerAdapter child
= new ContainerAdapter();
276 container
.Add(child
);
278 ICalcService service
= new CalculatorService();
280 child
.AddService(typeof(ICalcService
), service
, true);
281 Assert
.IsNotNull(child
.GetService(typeof(ICalcService
)));
283 child
.RemoveService(typeof(ICalcService
), true);
284 Assert
.IsNull(child
.GetService(typeof(ICalcService
)));
285 Assert
.IsNull(container
.GetService(typeof(ICalcService
)));
288 public void ChainContainers()
290 ICalcService service
= new CalculatorService();
291 container
.AddService(typeof(ICalcService
), service
);
293 IContainerAdapter adapter
= new ContainerAdapter(container
);
295 Assert
.AreSame(service
, container
.GetService(typeof(ICalcService
)));
299 public void ComponentLifecyle()
301 TestComponent component
= new TestComponent();
302 Assert
.IsFalse(component
.IsSited
);
303 Assert
.IsFalse(component
.IsDisposed
);
305 container
.Add(component
);
306 Assert
.IsTrue(component
.IsSited
);
309 Assert
.IsTrue(component
.IsDisposed
);
313 public void ContainerLifecyle()
315 container
.Disposed
+= new EventHandler(Container_Disposed
);
316 Assert
.IsFalse(disposed
);
319 Assert
.IsTrue(disposed
);
323 public void DisposeWindsorContainer()
325 container
.Disposed
+= new EventHandler(Container_Disposed
);
326 Assert
.IsFalse(disposed
);
328 container
.Container
.Dispose();
329 Assert
.IsTrue(disposed
);
332 private object CreateCalculatorService(IServiceContainer container
, Type serviceType
)
335 return new CalculatorService();
338 private void Container_Disposed(object source
, EventArgs args
)