Reverted DictionaryComponentAttribute changes.
[castle.git] / InversionOfControl / Castle.Windsor.Tests / WindsorDotNet2Tests.cs
blob2369c65847fd1e046b6cb51f560d8d0f19efb82e
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 #if !MONO
17 namespace Castle.Windsor.Tests
19 using System;
20 using System.IO;
21 using Castle.Core.Interceptor;
22 using Castle.Windsor.Configuration.Interpreters;
23 using Components;
24 using Core;
25 using NUnit.Framework;
27 [TestFixture]
28 public class WindsorDotNet2Tests
30 [Test]
31 public void UsingResolveGenericMethodOverload()
33 IWindsorContainer container = new WindsorContainer(new XmlInterpreter(GetFilePath("GenericsConfig.xml")));
34 ICalcService svr = container.Resolve<ICalcService>();
35 Assert.IsTrue(typeof (CalculatorService).IsInstanceOfType(svr));
38 [Test]
39 public void ResolveGenericWithId()
41 IWindsorContainer container = new WindsorContainer(new XmlInterpreter(GetFilePath("GenericsConfig.xml")));
42 ICalcService svr = container.Resolve<ICalcService>("calc");
43 Assert.IsTrue(typeof (CalculatorService).IsInstanceOfType(svr));
46 [Test]
47 public void GetGenericService()
49 IWindsorContainer container = new WindsorContainer(new XmlInterpreter(GetFilePath("GenericsConfig.xml")));
50 IRepository<int> repos = container.Resolve<IRepository<int>>("int.repos.generic");
51 Assert.IsNotNull(repos);
52 Assert.IsTrue(typeof (DemoRepository<int>).IsInstanceOfType(repos));
55 [Test]
56 public void GetGenericServiceWithDecorator()
58 IWindsorContainer container = new WindsorContainer(new XmlInterpreter(GetFilePath("GenericsConfig.xml")));
59 IRepository<int> repos = container.Resolve<IRepository<int>>("int.repos");
60 Assert.IsNotNull(repos);
61 Assert.IsTrue(typeof (LoggingRepositoryDecorator<int>).IsInstanceOfType(repos));
62 Assert.IsTrue(typeof (DemoRepository<int>).IsInstanceOfType(((LoggingRepositoryDecorator<int>) repos).inner));
65 [Test]
66 public void WillUseDefaultCtorOnGenericComponentIfTryingToResolveOnSameComponent()
68 IWindsorContainer container =
69 new WindsorContainer(new XmlInterpreter(GetFilePath("RecursiveDecoratorConfig.xml")));
70 LoggingRepositoryDecorator<int> resolve =
71 (LoggingRepositoryDecorator<int>) container.Resolve<IRepository<int>>();
72 Assert.IsNull(resolve.inner);
75 [Test]
76 public void GetGenericServiceWithDecorator_GenericDecoratorOnTop()
78 IWindsorContainer container = new WindsorContainer(new XmlInterpreter(GetFilePath("DecoratorConfig.xml")));
79 IRepository<int> repos = container.Resolve<IRepository<int>>();
80 Assert.IsTrue(typeof (LoggingRepositoryDecorator<int>).IsInstanceOfType(repos));
82 Assert.IsTrue(typeof (LoggingRepositoryDecorator<int>).IsInstanceOfType(repos));
83 Assert.IsTrue(typeof (DemoRepository<int>).IsInstanceOfType(((LoggingRepositoryDecorator<int>) repos).inner));
85 DemoRepository<int> inner = ((LoggingRepositoryDecorator<int>) repos).inner as DemoRepository<int>;
87 Assert.AreEqual("second", inner.Name);
90 [Test]
91 public void InferGenericArgumentForComponentFromPassedType()
93 IWindsorContainer container =
94 new WindsorContainer(new XmlInterpreter(GetFilePath("GenericDecoratorConfig.xml")));
96 IRepository<string> repos = container.Resolve<IRepository<string>>();
97 Assert.IsTrue(typeof (LoggingRepositoryDecorator<string>).IsInstanceOfType(repos));
99 DemoRepository<string> inner = ((LoggingRepositoryDecorator<string>) repos).inner as DemoRepository<string>;
101 Assert.AreEqual("second", inner.Name);
104 [Test]
105 public void GetSameInstanceFromGenericType()
107 IWindsorContainer container =
108 new WindsorContainer(new XmlInterpreter(GetFilePath("GenericDecoratorConfig.xml")));
110 IRepository<string> repos1 = container.Resolve<IRepository<string>>();
111 IRepository<string> repos2 = container.Resolve<IRepository<string>>();
113 Assert.AreSame(repos1, repos2);
116 [Test]
117 public void GetSameInstanceOfGenericFromTwoDifferentGenericTypes()
119 IWindsorContainer container =
120 new WindsorContainer(new XmlInterpreter(GetFilePath("GenericDecoratorConfig.xml")));
122 IRepository<string> repos1 = container.Resolve<IRepository<string>>();
123 IRepository<string> repos2 = container.Resolve<IRepository<string>>();
125 Assert.AreSame(repos1, repos2);
127 IRepository<int> repos3 = container.Resolve<IRepository<int>>();
128 IRepository<int> repos4 = container.Resolve<IRepository<int>>();
130 Assert.AreSame(repos3, repos4);
133 [Test]
134 public void ComplexGenericConfiguration_GetGenericRepostiory()
136 IWindsorContainer container =
137 new WindsorContainer(new XmlInterpreter(GetFilePath("ComplexGenericConfig.xml")));
138 IRepository<IEmployee> empRepost = container[typeof (IRepository<IEmployee>)] as IRepository<IEmployee>;
139 Assert.IsNotNull(empRepost);
140 Assert.IsTrue(typeof (LoggingRepositoryDecorator<IEmployee>).IsInstanceOfType(empRepost));
141 LoggingRepositoryDecorator<IEmployee> log = empRepost as LoggingRepositoryDecorator<IEmployee>;
142 IRepository<IEmployee> inner = log.inner;
143 Assert.IsNotNull(inner);
144 Assert.IsTrue(typeof (DemoRepository<IEmployee>).IsInstanceOfType(inner));
145 DemoRepository<IEmployee> demoEmpRepost = inner as DemoRepository<IEmployee>;
146 Assert.AreEqual("Generic Repostiory", demoEmpRepost.Name);
147 Assert.IsNotNull(demoEmpRepost.Cache);
148 Assert.IsTrue(typeof (HashTableCache<IEmployee>).IsInstanceOfType(demoEmpRepost.Cache));
151 [Test]
152 public void ComplexGenericConfiguration_GetReviewRepository_BoundAtConfiguration()
154 IWindsorContainer container =
155 new WindsorContainer(new XmlInterpreter(GetFilePath("ComplexGenericConfig.xml")));
156 IRepository<IReviewer> rev = container.Resolve<IRepository<IReviewer>>();
158 Assert.IsTrue(typeof (ReviewerRepository).IsInstanceOfType(rev));
159 ReviewerRepository repos = rev as ReviewerRepository;
160 Assert.AreEqual("Reviewer Repository", repos.Name);
161 Assert.IsNotNull(repos.Cache);
162 Assert.IsTrue(typeof (HashTableCache<IReviewer>).IsInstanceOfType(repos.Cache));
165 [Test]
166 public void
167 ComplexGenericConfiguration_GetReviewableRepostiory_ShouldResolveToDemoRepostiroyInsideLoggingRepositoryWithNoCaching
170 IWindsorContainer container =
171 new WindsorContainer(new XmlInterpreter(GetFilePath("ComplexGenericConfig.xml")));
172 IRepository<IReviewableEmployee> empRepost = container.Resolve<IRepository<IReviewableEmployee>>();
173 Assert.IsNotNull(empRepost);
174 Assert.IsTrue(typeof (LoggingRepositoryDecorator<IReviewableEmployee>).IsInstanceOfType(empRepost));
175 LoggingRepositoryDecorator<IReviewableEmployee> log =
176 empRepost as LoggingRepositoryDecorator<IReviewableEmployee>;
177 IRepository<IReviewableEmployee> inner = log.inner;
178 Assert.IsNotNull(inner);
179 Assert.IsTrue(typeof (DemoRepository<IReviewableEmployee>).IsInstanceOfType(inner));
180 DemoRepository<IReviewableEmployee> demoEmpRepost = inner as DemoRepository<IReviewableEmployee>;
181 Assert.AreEqual("Generic Repostiory With No Cache", demoEmpRepost.Name);
182 Assert.IsNotNull(demoEmpRepost.Cache);
183 Assert.IsTrue(typeof (NullCache<IReviewableEmployee>).IsInstanceOfType(demoEmpRepost.Cache));
186 [Test]
187 public void TestGenericSpecialization()
189 IWindsorContainer container =
190 new WindsorContainer(new XmlInterpreter(GetFilePath("ComplexGenericConfig.xml")));
191 IRepository<IReviewer> repository = container.Resolve<IRepository<IReviewer>>();
192 Assert.IsTrue(typeof (ReviewerRepository).IsInstanceOfType(repository), "Not ReviewerRepository!");
195 [Test]
196 public void ComplexGenericConfiguration_GetRepositoryByIdAndType()
198 IWindsorContainer container =
199 new WindsorContainer(new XmlInterpreter(GetFilePath("ComplexGenericConfig.xml")));
200 IRepository<IReviewer> repository = container.Resolve<IRepository<IReviewer>>("generic.repository");
201 Assert.IsTrue(typeof (DemoRepository<IReviewer>).IsInstanceOfType(repository), "Not DemoRepository!");
204 [Test]
205 public void TestComponentLifestylePerGenericType()
207 IWindsorContainer container = new WindsorContainer();
209 container.AddComponent("comp", typeof (IRepository<>), typeof (TransientRepository<>));
211 object o1 = container.Resolve<IRepository<Employee>>();
212 object o2 = container.Resolve<IRepository<Employee>>();
213 object o3 = container.Resolve<IRepository<Reviewer>>();
214 object o4 = container.Resolve<IRepository<Reviewer>>();
216 Assert.IsFalse(ReferenceEquals(o1, o2));
217 Assert.IsFalse(ReferenceEquals(o1, o3));
218 Assert.IsFalse(ReferenceEquals(o1, o4));
221 [Test]
222 public void CanCreateANormalTypeWithCtorDependencyOnGenericType()
224 IWindsorContainer container = new WindsorContainer();
226 container.AddComponent("comp", typeof (NeedsGenericType));
227 container.AddComponent("cache", typeof (ICache<>), typeof (NullCache<>));
229 NeedsGenericType needsGenericType = container.Resolve<NeedsGenericType>();
231 Assert.IsNotNull(needsGenericType);
234 [Test]
235 public void InterceptGeneric1()
237 WindsorContainer container = new WindsorContainer();
239 container.AddFacility("interceptor-facility", new MyInterceptorGreedyFacility());
240 container.AddComponent("interceptor", typeof (StandardInterceptor));
241 container.AddComponent("key", typeof (IRepository<Employee>), typeof (DemoRepository<Employee>));
243 IRepository<Employee> store = container.Resolve<IRepository<Employee>>();
245 Assert.IsFalse(typeof (DemoRepository<Employee>).IsInstanceOfType(store), "This should have been a proxy");
248 [Test]
249 public void InterceptGeneric2()
251 WindsorContainer container = new WindsorContainer();
253 container.AddFacility("interceptor-facility", new MyInterceptorGreedyFacility2());
254 container.AddComponent("interceptor", typeof (StandardInterceptor));
255 container.AddComponent("key", typeof (IRepository<>), typeof (DemoRepository<>));
257 IRepository<Employee> store = container.Resolve<IRepository<Employee>>();
259 Assert.IsFalse(typeof (DemoRepository<Employee>).IsInstanceOfType(store), "This should have been a proxy");
262 [Test]
263 public void ChainOfResponsability()
265 IWindsorContainer container =
266 new WindsorContainer(new XmlInterpreter(GetFilePath("chainOfRespnsability.config")));
267 IResultFinder<int> resolve = container.Resolve<IResultFinder<int>>();
268 Assert.IsTrue(resolve.Finder is DatabaseResultFinder<int>);
269 Assert.IsTrue(resolve.Finder.Finder is WebServiceResultFinder<int>);
270 Assert.IsTrue(resolve.Finder.Finder.Finder is FailedResultFinder<int>);
271 Assert.IsTrue(resolve.Finder.Finder.Finder.Finder == null);
274 [Test]
275 public void ChainOfResponsability_Smart()
277 IWindsorContainer container =
278 new WindsorContainer(new XmlInterpreter(GetFilePath("chainOfRespnsability_smart.config")));
279 IResultFinder<int> resolve = container.Resolve<IResultFinder<int>>();
280 Assert.IsTrue(resolve is CacheResultFinder<int>);
281 Assert.IsTrue(resolve.Finder is DatabaseResultFinder<int>);
282 Assert.IsTrue(resolve.Finder.Finder is WebServiceResultFinder<int>);
283 Assert.IsNull(resolve.Finder.Finder.Finder);
285 IResultFinder<String> resolve2 = container.Resolve<IResultFinder<String>>();
286 Assert.IsTrue(resolve2 is ResultFinderStringDecorator);
287 Assert.IsNotNull(resolve2.Finder);
290 [Test]
291 public void LifestyleIsInheritsFromGenericType()
293 WindsorContainer container = new WindsorContainer();
295 container.AddFacility("interceptor-facility", new MyInterceptorGreedyFacility2());
296 container.AddComponent("interceptor", typeof (StandardInterceptor));
297 container.AddComponentWithLifestyle("key", typeof (IRepository<>), typeof (DemoRepository<>), LifestyleType.Transient);
299 IRepository<Employee> store = container.Resolve<IRepository<Employee>>();
300 IRepository<Employee> anotherStore = container.Resolve<IRepository<Employee>>();
302 Assert.IsFalse(typeof (DemoRepository<Employee>).IsInstanceOfType(store), "This should have been a proxy");
303 Assert.AreNotSame(store, anotherStore, "This should be two different instances");
306 [Test]
307 public void InterceptorInheritFromGenericType()
309 WindsorContainer container = new WindsorContainer();
311 container.AddComponent("interceptor", typeof (MyInterceptor));
312 container.AddComponent("key", typeof (IRepository<>), typeof (DemoRepository<>));
313 container.Kernel.GetHandler(typeof (IRepository<>)).ComponentModel.Interceptors.Add(
314 new InterceptorReference(typeof (MyInterceptor)));
316 IRepository<object> demoRepository = container.Resolve<IRepository<object>>();
317 demoRepository.Get(12);
319 Assert.IsTrue(MyInterceptor.InterceptedId == 12, "invocation should have been intercepted by MyInterceptor");
322 public string GetFilePath(string fileName)
324 return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
325 ConfigHelper.ResolveConfigPath("DotNet2Config/" + fileName));
329 public class MyInterceptor : IInterceptor
331 public static int InterceptedId = 0;
333 public void Intercept(IInvocation invocation)
335 invocation.Proceed();
336 InterceptedId = (int) invocation.Arguments[0];
340 public interface IResultFinder<T>
342 T Process(ISpecification specification);
343 IResultFinder<T> Finder { get; }
346 public class CacheResultFinder<T> : IResultFinder<T>
348 private IResultFinder<T> finder;
350 public IResultFinder<T> Finder
352 get { return finder; }
355 public CacheResultFinder()
359 public CacheResultFinder(IResultFinder<T> finder)
361 this.finder = finder;
364 public T Process(ISpecification specification)
366 return default(T);
370 public class DatabaseResultFinder<T> : IResultFinder<T>
372 private IResultFinder<T> finder;
374 public IResultFinder<T> Finder
376 get { return finder; }
379 public DatabaseResultFinder()
383 public DatabaseResultFinder(IResultFinder<T> finder)
385 this.finder = finder;
388 public T Process(ISpecification specification)
390 return default(T);
394 public class WebServiceResultFinder<T> : IResultFinder<T>
396 private IResultFinder<T> finder;
398 public IResultFinder<T> Finder
400 get { return finder; }
403 public WebServiceResultFinder()
407 public WebServiceResultFinder(IResultFinder<T> finder)
409 this.finder = finder;
412 public T Process(ISpecification specification)
414 return default(T);
418 public class FailedResultFinder<T> : IResultFinder<T>
420 public IResultFinder<T> Finder
422 get { return null; }
425 public T Process(ISpecification specification)
427 return default(T);
431 public class ResultFinderStringDecorator : IResultFinder<string>
433 private IResultFinder<string> finder;
435 public ResultFinderStringDecorator(IResultFinder<string> finder)
437 this.finder = finder;
440 public IResultFinder<string> Finder
442 get { return finder; }
445 public String Process(ISpecification specification)
447 return String.Empty;
451 public interface ISpecification
456 #endif