1 // Copyright 2004-2008 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
17 using Castle
.MicroKernel
.Handlers
;
18 using NUnit
.Framework
;
40 public CycleA(CycleB b
)
47 public CycleB(CycleA a
)
53 public class DependencyGraph
55 private IKernel kernel
;
60 kernel
= new DefaultKernel();
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"]);
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
);
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
);
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"]);