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
.MicroKernel
.Tests
.Configuration
17 using Castle
.Core
.Configuration
;
18 using Castle
.MicroKernel
.Resolvers
;
19 using Castle
.MicroKernel
.Tests
.ClassComponents
;
20 using Castle
.MicroKernel
.Tests
.Configuration
.Components
;
21 using NUnit
.Framework
;
24 public class ConfigurationTestCase
26 private IKernel kernel
;
31 kernel
= new DefaultKernel();
41 [ExpectedException(typeof(DependencyResolverException
))]
42 public void ConstructorWithUnsatisfiedParameters()
44 kernel
.AddComponent("key", typeof(ClassWithConstructors
));
45 object res
= kernel
["key"];
49 public void ConstructorWithStringParameters()
51 MutableConfiguration confignode
= new MutableConfiguration("key");
53 IConfiguration parameters
=
54 confignode
.Children
.Add(new MutableConfiguration("parameters"));
56 parameters
.Children
.Add(new MutableConfiguration("host", "castleproject.org"));
58 kernel
.ConfigurationStore
.AddComponentConfiguration("key", confignode
);
60 kernel
.AddComponent("key", typeof(ClassWithConstructors
));
62 ClassWithConstructors instance
= (ClassWithConstructors
) kernel
["key"];
63 Assert
.IsNotNull(instance
);
64 Assert
.IsNotNull(instance
.Host
);
65 Assert
.AreEqual("castleproject.org", instance
.Host
);
69 public void ServiceOverride()
71 MutableConfiguration confignode
= new MutableConfiguration("key");
73 IConfiguration parameters
=
74 confignode
.Children
.Add(new MutableConfiguration("parameters"));
76 parameters
.Children
.Add(new MutableConfiguration("common", "${commonservice2}"));
78 kernel
.ConfigurationStore
.AddComponentConfiguration("commonserviceuser", confignode
);
80 kernel
.AddComponent("commonservice1", typeof(ICommon
), typeof(CommonImpl1
));
81 kernel
.AddComponent("commonservice2", typeof(ICommon
), typeof(CommonImpl2
));
82 kernel
.AddComponent("commonserviceuser", typeof(CommonServiceUser
));
84 CommonServiceUser instance
= (CommonServiceUser
) kernel
["commonserviceuser"];
86 Assert
.IsNotNull(instance
);
87 Assert
.AreEqual(typeof(CommonImpl2
), instance
.CommonService
.GetType());
91 public void ServiceOverrideUsingProperties()
93 MutableConfiguration confignode
= new MutableConfiguration("key");
95 IConfiguration parameters
=
96 confignode
.Children
.Add(new MutableConfiguration("parameters"));
98 parameters
.Children
.Add(new MutableConfiguration("CommonService", "${commonservice2}"));
100 kernel
.ConfigurationStore
.AddComponentConfiguration("commonserviceuser", confignode
);
102 kernel
.AddComponent("commonservice1", typeof(ICommon
), typeof(CommonImpl1
));
103 kernel
.AddComponent("commonservice2", typeof(ICommon
), typeof(CommonImpl2
));
105 kernel
.AddComponent("commonserviceuser", typeof(CommonServiceUser2
));
107 CommonServiceUser2 instance
= (CommonServiceUser2
) kernel
["commonserviceuser"];
109 Assert
.IsNotNull(instance
);
110 Assert
.AreEqual(typeof(CommonImpl2
), instance
.CommonService
.GetType());
114 public void ConstructorWithArrayParameter()
116 MutableConfiguration confignode
= new MutableConfiguration("key");
118 IConfiguration parameters
=
119 confignode
.Children
.Add(new MutableConfiguration("parameters"));
121 IConfiguration hosts
= parameters
.Children
.Add(new MutableConfiguration("hosts"));
122 IConfiguration array
= hosts
.Children
.Add(new MutableConfiguration("array"));
123 array
.Children
.Add(new MutableConfiguration("item", "castle"));
124 array
.Children
.Add(new MutableConfiguration("item", "uol"));
125 array
.Children
.Add(new MutableConfiguration("item", "folha"));
127 kernel
.ConfigurationStore
.AddComponentConfiguration("key", confignode
);
129 kernel
.AddComponent("key", typeof(ClassWithConstructors
));
131 ClassWithConstructors instance
= (ClassWithConstructors
) kernel
["key"];
132 Assert
.IsNotNull(instance
);
133 Assert
.IsNull(instance
.Host
);
134 Assert
.AreEqual("castle", instance
.Hosts
[0]);
135 Assert
.AreEqual("uol", instance
.Hosts
[1]);
136 Assert
.AreEqual("folha", instance
.Hosts
[2]);
140 public void ConstructorWithListParameterAndCustomType()
142 MutableConfiguration confignode
= new MutableConfiguration("key");
144 IConfiguration parameters
=
145 confignode
.Children
.Add(new MutableConfiguration("parameters"));
147 IConfiguration services
= parameters
.Children
.Add(new MutableConfiguration("services"));
148 MutableConfiguration list
= (MutableConfiguration
)
149 services
.Children
.Add(new MutableConfiguration("list"));
150 list
.Attributes
.Add("type", "Castle.MicroKernel.Tests.ClassComponents.ICommon, Castle.MicroKernel.Tests");
152 list
.Children
.Add(new MutableConfiguration("item", "${commonservice1}"));
153 list
.Children
.Add(new MutableConfiguration("item", "${commonservice2}"));
155 kernel
.ConfigurationStore
.AddComponentConfiguration("key", confignode
);
156 kernel
.AddComponent("key", typeof(ClassWithListConstructor
));
158 kernel
.AddComponent("commonservice1", typeof(ICommon
), typeof(CommonImpl1
));
159 kernel
.AddComponent("commonservice2", typeof(ICommon
), typeof(CommonImpl2
));
161 ClassWithListConstructor instance
= (ClassWithListConstructor
) kernel
["key"];
162 Assert
.IsNotNull(instance
.Services
);
163 Assert
.AreEqual(2, instance
.Services
.Count
);
164 Assert
.AreEqual("CommonImpl1", instance
.Services
[0].GetType().Name
);
165 Assert
.AreEqual("CommonImpl2", instance
.Services
[1].GetType().Name
);
169 public void ConstructorWithArrayParameterAndCustomType()
171 MutableConfiguration confignode
= new MutableConfiguration("key");
173 IConfiguration parameters
=
174 confignode
.Children
.Add(new MutableConfiguration("parameters"));
176 IConfiguration services
= parameters
.Children
.Add(new MutableConfiguration("services"));
177 MutableConfiguration array
= (MutableConfiguration
)
178 services
.Children
.Add(new MutableConfiguration("array"));
179 //list.Attributes.Add("type", "Castle.MicroKernel.Tests.ClassComponents.ICommon, Castle.MicroKernel.Tests");
181 array
.Children
.Add(new MutableConfiguration("item", "${commonservice1}"));
182 array
.Children
.Add(new MutableConfiguration("item", "${commonservice2}"));
184 kernel
.ConfigurationStore
.AddComponentConfiguration("key", confignode
);
186 kernel
.AddComponent("key", typeof(ClassWithArrayConstructor
));
188 kernel
.AddComponent("commonservice1", typeof(ICommon
), typeof(CommonImpl1
));
189 kernel
.AddComponent("commonservice2", typeof(ICommon
), typeof(CommonImpl2
));
191 ClassWithArrayConstructor instance
= (ClassWithArrayConstructor
) kernel
["key"];
192 Assert
.IsNotNull(instance
.Services
);
193 Assert
.AreEqual(2, instance
.Services
.Length
);
194 Assert
.AreEqual("CommonImpl1", instance
.Services
[0].GetType().Name
);
195 Assert
.AreEqual("CommonImpl2", instance
.Services
[1].GetType().Name
);
199 public void CustomLifestyleManager()
203 MutableConfiguration confignode
= new MutableConfiguration(key
);
204 confignode
.Attributes
.Add("lifestyle", "custom");
206 confignode
.Attributes
.Add("customLifestyleType",
207 "Castle.MicroKernel.Tests.ClassComponents.CustomLifestyleManager, Castle.MicroKernel.Tests");
209 kernel
.ConfigurationStore
.AddComponentConfiguration(key
, confignode
);
210 kernel
.AddComponent(key
, typeof(ICommon
), typeof(CommonImpl1
));
212 ICommon instance
= (ICommon
) kernel
[key
];
213 IHandler handler
= kernel
.GetHandler(key
);
215 Assert
.IsNotNull(instance
);
216 Assert
.AreEqual(Core
.LifestyleType
.Custom
, handler
.ComponentModel
.LifestyleType
);
217 Assert
.AreEqual(typeof(CustomLifestyleManager
), handler
.ComponentModel
.CustomLifestyle
);
221 public void ComplexConfigurationParameter()
224 string value1
= "value1";
225 string value2
= "value2";
227 MutableConfiguration confignode
= new MutableConfiguration(key
);
229 IConfiguration parameters
=
230 confignode
.Children
.Add(new MutableConfiguration("parameters"));
232 IConfiguration complexParam
233 = parameters
.Children
.Add(new MutableConfiguration("complexparam"));
235 IConfiguration complexNode
236 = complexParam
.Children
.Add(new MutableConfiguration("complexparametertype"));
238 complexNode
.Children
.Add(new MutableConfiguration("mandatoryvalue", value1
));
239 complexNode
.Children
.Add(new MutableConfiguration("optionalvalue", value2
));
242 kernel
.ConfigurationStore
.AddComponentConfiguration(key
, confignode
);
243 kernel
.AddComponent(key
, typeof(ClassWithComplexParameter
));
245 ClassWithComplexParameter instance
= (ClassWithComplexParameter
) kernel
[key
];
247 Assert
.IsNotNull(instance
);
248 Assert
.IsNotNull(instance
.ComplexParam
);
249 Assert
.AreEqual(value1
, instance
.ComplexParam
.MandatoryValue
);
250 Assert
.AreEqual(value2
, instance
.ComplexParam
.OptionalValue
);