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
18 using Castle
.MicroKernel
.Exceptions
;
19 using Castle
.MicroKernel
.Handlers
;
20 using Castle
.Windsor
.Tests
.Components
;
21 using NUnit
.Framework
;
24 public class CircularDependencyTests
27 public void ThrowsACircularDependencyException()
29 IWindsorContainer container
= new WindsorContainer();
30 container
.AddComponent("controller", typeof(IController
), typeof(Controller
));
31 container
.AddComponent("view", typeof(IView
), typeof(View
));
34 container
.Resolve("controller");
37 @"A cycle was detected when trying to resolve a dependency. The dependency graph that resulted in a cycle is:
38 - Service dependency 'view' type 'Castle.Windsor.Tests.Components.IView' for Void .ctor(Castle.Windsor.Tests.Components.IView) in type Castle.Windsor.Tests.Components.Controller
39 - Service dependency 'Controller' type 'Castle.Windsor.Tests.Components.IController' for Castle.Windsor.Tests.Components.IController Controller in type Castle.Windsor.Tests.Components.View
40 + Service dependency 'view' type 'Castle.Windsor.Tests.Components.IView' for Void .ctor(Castle.Windsor.Tests.Components.IView) in Castle.Windsor.Tests.Components.Controller
44 catch(CircularDependencyException
)
51 ExpectedException(typeof(HandlerException
),
52 @"Can't create component 'compA' as it has dependencies to be satisfied.
53 compA is waiting for the following dependencies:
56 - Castle.Windsor.Tests.Components.CompB which was registered but is also waiting for dependencies.
58 compB is waiting for the following dependencies:
61 - Castle.Windsor.Tests.Components.CompC which was registered but is also waiting for dependencies.
63 compC is waiting for the following dependencies:
66 - Castle.Windsor.Tests.Components.CompD which was registered but is also waiting for dependencies.
68 compD is waiting for the following dependencies:
71 - Castle.Windsor.Tests.Components.CompA which was registered but is also waiting for dependencies.
74 public void ThrowsACircularDependencyException2()
76 IWindsorContainer container
= new WindsorContainer();
77 container
.AddComponent("compA", typeof(CompA
));
78 container
.AddComponent("compB", typeof(CompB
));
79 container
.AddComponent("compC", typeof(CompC
));
80 container
.AddComponent("compD", typeof(CompD
));
82 container
.Resolve("compA");
86 public void ShouldNotGetCircularDepencyExceptionWhenResolvingTypeOnItselfWithDifferentModels()
88 WindsorContainer container
= new WindsorContainer(ConfigHelper
.ResolveConfigPath("IOC-51.xml"));
89 object o
= container
["path.fileFinder"];
96 using System
.Reflection
;
98 public interface IPathProvider
103 public class AssemblyPath
: IPathProvider
109 Uri uriPath
= new Uri(Assembly
.GetExecutingAssembly().GetName(false).CodeBase
);
110 return uriPath
.LocalPath
;
115 public class RelativeFilePath
: IPathProvider
117 public RelativeFilePath(IPathProvider basePathProvider
, string extensionsPath
)
119 _path
= System
.IO
.Path
.Combine(basePathProvider
.Path
+ "\\", extensionsPath
);
124 get { return _path; }
127 private string _path
;