Reverted DictionaryComponentAttribute changes.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / UnsatisfiedDependenciesTestCase.cs
blobe6e43899b0c99266c8791994fe1dcb083608c0aa
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 namespace Castle.MicroKernel.Tests
17 using Castle.Core.Configuration;
18 using Castle.MicroKernel.Handlers;
19 using Castle.MicroKernel.Resolvers;
20 using Castle.MicroKernel.Tests.ClassComponents;
21 using NUnit.Framework;
23 [TestFixture]
24 public class UnsatisfiedDependenciesTestCase
26 private IKernel kernel;
28 [SetUp]
29 public void Init()
31 kernel = new DefaultKernel();
34 [TearDown]
35 public void Dispose()
37 kernel.Dispose();
40 [Test]
41 [ExpectedException(typeof(HandlerException))]
42 public void UnsatisfiedService()
44 kernel.AddComponent("key", typeof(CommonServiceUser));
45 object instance = kernel["key"];
48 [Test]
49 [ExpectedException(typeof(HandlerException),
50 "Can't create component 'key' as it has dependencies to be satisfied. \r\nkey is waiting for the following dependencies: \r\n\r\n" +
51 "Keys (components with specific keys)\r\n- name which was not registered. \r\n- address which was not registered. \r\n" +
52 "- age which was not registered. \r\n")]
53 public void UnsatisfiedConfigValues()
55 MutableConfiguration config = new MutableConfiguration("component");
57 MutableConfiguration parameters = (MutableConfiguration)
58 config.Children.Add(new MutableConfiguration("parameters"));
60 parameters.Children.Add(new MutableConfiguration("name", "hammett"));
62 kernel.ConfigurationStore.AddComponentConfiguration("customer", config);
64 kernel.AddComponent("key", typeof(CustomerImpl2));
66 object instance = kernel["key"];
69 [Test]
70 [ExpectedException(typeof(HandlerException),
71 "Can't create component 'key' as it has dependencies to be satisfied. \r\nkey is waiting for the following dependencies: \r\n\r\nKeys (components with specific keys)\r\n- common2 which was not registered. \r\n"
73 public void UnsatisfiedOverride()
75 MutableConfiguration config = new MutableConfiguration("component");
77 MutableConfiguration parameters = (MutableConfiguration)
78 config.Children.Add(new MutableConfiguration("parameters"));
80 parameters.Children.Add(new MutableConfiguration("common", "${common2}"));
82 kernel.ConfigurationStore.AddComponentConfiguration("key", config);
84 kernel.AddComponent("common1", typeof(ICommon), typeof(CommonImpl1));
85 kernel.AddComponent("key", typeof(CommonServiceUser));
86 object instance = kernel["key"];
89 [Test]
90 [ExpectedException(typeof(HandlerException),
91 "Can't create component 'key' as it has dependencies to be satisfied. \r\nkey is waiting for the following dependencies: \r\n\r\nKeys (components with specific keys)\r\n- common2 which was not registered. \r\n"
93 public void OverrideIsForcedDependency()
95 MutableConfiguration config = new MutableConfiguration("component");
97 MutableConfiguration parameters = (MutableConfiguration)
98 config.Children.Add(new MutableConfiguration("parameters"));
100 parameters.Children.Add(new MutableConfiguration("common", "${common2}"));
102 kernel.ConfigurationStore.AddComponentConfiguration("key", config);
104 kernel.AddComponent("common1", typeof(ICommon), typeof(CommonImpl1));
105 kernel.AddComponent("key", typeof(CommonServiceUser3));
106 object instance = kernel["key"];
109 [Test]
110 public void SatisfiedOverride()
112 MutableConfiguration config = new MutableConfiguration("component");
114 MutableConfiguration parameters = (MutableConfiguration)
115 config.Children.Add(new MutableConfiguration("parameters"));
117 parameters.Children.Add(new MutableConfiguration("common", "${common2}"));
119 kernel.ConfigurationStore.AddComponentConfiguration("key", config);
121 kernel.AddComponent("common1", typeof(ICommon), typeof(CommonImpl1));
122 kernel.AddComponent("common2", typeof(ICommon), typeof(CommonImpl2));
123 kernel.AddComponent("key", typeof(CommonServiceUser));
124 CommonServiceUser instance = (CommonServiceUser) kernel["key"];
126 Assert.IsNotNull(instance);
127 Assert.IsNotNull(instance.CommonService);
128 Assert.AreEqual("CommonImpl2", instance.CommonService.GetType().Name);
131 [Test]
132 public void SatisfiedOverrideRecursive()
134 MutableConfiguration config1 = new MutableConfiguration("component");
135 MutableConfiguration parameters1 =
136 (MutableConfiguration) config1.Children.Add(new MutableConfiguration("parameters"));
137 parameters1.Children.Add(new MutableConfiguration("inner", "${repository2}"));
138 kernel.ConfigurationStore.AddComponentConfiguration("repository1", config1);
139 kernel.AddComponent("repository1", typeof(IRepository), typeof(Repository1));
141 MutableConfiguration config2 = new MutableConfiguration("component");
142 MutableConfiguration parameters2 =
143 (MutableConfiguration) config2.Children.Add(new MutableConfiguration("parameters"));
144 parameters2.Children.Add(new MutableConfiguration("inner", "${repository3}"));
145 kernel.ConfigurationStore.AddComponentConfiguration("repository2", config2);
146 kernel.AddComponent("repository2", typeof(IRepository), typeof(Repository2));
148 MutableConfiguration config3 = new MutableConfiguration("component");
149 MutableConfiguration parameters3 =
150 (MutableConfiguration) config3.Children.Add(new MutableConfiguration("parameters"));
151 parameters3.Children.Add(new MutableConfiguration("inner", "${decoratedRepository}"));
152 kernel.ConfigurationStore.AddComponentConfiguration("repository3", config3);
153 kernel.AddComponent("repository3", typeof(IRepository), typeof(Repository3));
155 kernel.AddComponent("decoratedRepository", typeof(IRepository), typeof(DecoratedRepository));
157 IRepository instance = (Repository1) kernel[typeof(IRepository)];
159 Assert.IsNotNull(instance);
160 Assert.IsInstanceOfType(typeof(Repository1), instance);
161 Assert.IsInstanceOfType(typeof(Repository2), ((Repository1) instance).InnerRepository);
162 Assert.IsInstanceOfType(typeof(Repository3),
163 ((Repository2) (((Repository1) instance).InnerRepository)).InnerRepository);
164 Assert.IsInstanceOfType(typeof(DecoratedRepository),
165 ((Repository3) (((Repository2) (((Repository1) instance).InnerRepository)).InnerRepository)).
166 InnerRepository);