Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / InversionOfControl / Castle.Windsor.Tests / MultiResolveTests.cs
blobd0d74e64202b383b3f80f36dd3e38a419e4fa3ae
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.Windsor.Tests
17 using System;
18 using Castle.MicroKernel.Handlers;
19 using NUnit.Framework;
21 [TestFixture]
22 public class MultiResolveTests
24 [Test]
25 public void CanResolveMoreThanSingleComponentForService()
27 IWindsorContainer container = new WindsorContainer()
28 .AddComponent<IClock, IsraelClock>()
29 .AddComponent<IClock, WorldClock>();
31 IClock[] clocks = container.ResolveAll<IClock>();
33 Assert.AreEqual(2, clocks.Length);
36 [Test]
37 public void MultiResolveWillResolveInRegistrationOrder()
39 IWindsorContainer container = new WindsorContainer()
40 .AddComponent<IClock, IsraelClock>()
41 .AddComponent<IClock, WorldClock>();
43 IClock[] clocks = container.ResolveAll<IClock>();
45 Assert.AreEqual(typeof(IsraelClock), clocks[0].GetType());
46 Assert.AreEqual(typeof(WorldClock), clocks[1].GetType());
48 //reversing order
49 container = new WindsorContainer()
50 .AddComponent<IClock, WorldClock>()
51 .AddComponent<IClock, IsraelClock>();
53 clocks = container.ResolveAll<IClock>();
55 Assert.AreEqual(typeof(WorldClock), clocks[0].GetType());
56 Assert.AreEqual(typeof(IsraelClock), clocks[1].GetType());
59 [Test]
60 public void CanUseMutliResolveWithGenericSpecialization()
62 IWindsorContainer container = new WindsorContainer()
63 .AddComponent("demo", typeof (IRepository<>), typeof (DemoRepository<>))
64 .AddComponent("trans", typeof(IRepository<>), typeof(TransientRepository<>));
66 IRepository<IClock> resolve = container.Resolve<IRepository<IClock>>();
67 Assert.IsNotNull(resolve);
69 IRepository<IsraelClock>[] repositories = container.ResolveAll<IRepository<IsraelClock>>();
70 Assert.AreEqual(2, repositories.Length);
73 [Test]
74 [ExpectedException(typeof(HandlerException))]
75 public void WillThrowIfOneOfTheComponentsIsNotValid()
77 IWindsorContainer container = new WindsorContainer()
78 .AddComponent<IClock, IsraelClock>()
79 .AddComponent<IClock, WorldClock>()
80 .AddComponent<IClock, DependantClock>();
82 container.ResolveAll<IClock>();
86 public interface IClock{}
87 public class IsraelClock : IClock{}
88 public class WorldClock : IClock{}
89 public class DependantClock : IClock
91 private IDisposable disposable;
93 public DependantClock(IDisposable disposable)
95 this.disposable = disposable;