Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / DependencyGraph.cs
blob0f5ad66a7f5da677dc8c50fd79d9e8fc00521092
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.MicroKernel.Tests
17 using Castle.MicroKernel.Handlers;
18 using NUnit.Framework;
20 public class A
24 public class B
26 public B(A a)
31 public class C
33 public C(B b)
38 public class CycleA
40 public CycleA(CycleB b)
45 public class CycleB
47 public CycleB(CycleA a)
52 [TestFixture]
53 public class DependencyGraph
55 private IKernel kernel;
57 [SetUp]
58 public void Init()
60 kernel = new DefaultKernel();
63 [TearDown]
64 public void Dispose()
66 kernel.Dispose();
69 [Test]
70 public void ValidSituation()
72 kernel.AddComponent("a", typeof(A));
73 kernel.AddComponent("b", typeof(B));
74 kernel.AddComponent("c", typeof(C));
76 Assert.IsNotNull(kernel["a"]);
77 Assert.IsNotNull(kernel["b"]);
78 Assert.IsNotNull(kernel["c"]);
81 [Test]
82 public void GraphInvalid()
84 kernel.AddComponent("b", typeof(B));
85 kernel.AddComponent("c", typeof(C));
87 IHandler handlerB = kernel.GetHandler(typeof(B));
88 IHandler handlerC = kernel.GetHandler(typeof(C));
90 Assert.AreEqual(HandlerState.WaitingDependency, handlerB.CurrentState);
91 Assert.AreEqual(HandlerState.WaitingDependency, handlerC.CurrentState);
94 [Test]
95 public void GraphInvalidAndLateValidation()
97 kernel.AddComponent("b", typeof(B));
98 kernel.AddComponent("c", typeof(C));
100 IHandler handlerB = kernel.GetHandler(typeof(B));
101 IHandler handlerC = kernel.GetHandler(typeof(C));
103 Assert.AreEqual(HandlerState.WaitingDependency, handlerB.CurrentState);
104 Assert.AreEqual(HandlerState.WaitingDependency, handlerC.CurrentState);
106 kernel.AddComponent("a", typeof(A));
108 Assert.AreEqual(HandlerState.Valid, handlerB.CurrentState);
109 Assert.AreEqual(HandlerState.Valid, handlerC.CurrentState);
112 [Test]
113 [ExpectedException(typeof(HandlerException),
114 "Can't create component 'a' as it has dependencies to be satisfied. \r\na is waiting for the following dependencies: \r\n\r\nServices: \r\n- Castle.MicroKernel.Tests.CycleB which was registered but is also waiting for dependencies. \r\n\r\nb is waiting for the following dependencies: \r\n\r\nServices: \r\n- Castle.MicroKernel.Tests.CycleA which was registered but is also waiting for dependencies. \r\n"
116 public void CycleComponentGraphs()
118 kernel.AddComponent("a", typeof(CycleA));
119 kernel.AddComponent("b", typeof(CycleB));
121 Assert.IsNotNull(kernel["a"]);
122 Assert.IsNotNull(kernel["b"]);