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
18 using Castle
.Core
.Configuration
;
19 using Castle
.MicroKernel
.SubSystems
.Configuration
;
20 using NUnit
.Framework
;
22 public class ServiceUser
28 public ServiceUser(A a
)
30 if (a
== null) throw new ArgumentNullException();
34 public ServiceUser(A a
, B b
) : this(a
)
36 if (b
== null) throw new ArgumentNullException();
40 public ServiceUser(A a
, B b
, C c
) : this(a
, b
)
42 if (c
== null) throw new ArgumentNullException();
62 public class ServiceUser2
: ServiceUser
66 private int _scheduleinterval
;
68 public ServiceUser2(A a
, String name
, int port
) : base(a
)
74 public ServiceUser2(A a
, String name
, int port
, int scheduleinterval
) : this(a
, name
, port
)
76 _scheduleinterval
= scheduleinterval
;
89 public int ScheduleInterval
91 get { return _scheduleinterval; }
96 /// Summary description for BestConstructorTestCase.
99 public class BestConstructorTestCase
101 private IKernel kernel
;
106 kernel
= new DefaultKernel();
110 public void Dispose()
116 public void ConstructorWithMoreArguments()
118 kernel
.AddComponent("a", typeof(A
));
119 kernel
.AddComponent("b", typeof(B
));
120 kernel
.AddComponent("c", typeof(C
));
121 kernel
.AddComponent("service", typeof(ServiceUser
));
123 ServiceUser service
= (ServiceUser
) kernel
["service"];
125 Assert
.IsNotNull(service
);
126 Assert
.IsNotNull(service
.AComponent
);
127 Assert
.IsNotNull(service
.BComponent
);
128 Assert
.IsNotNull(service
.CComponent
);
132 public void ConstructorWithTwoArguments()
134 kernel
.AddComponent("a", typeof(A
));
135 kernel
.AddComponent("b", typeof(B
));
136 kernel
.AddComponent("service", typeof(ServiceUser
));
138 ServiceUser service
= (ServiceUser
) kernel
["service"];
140 Assert
.IsNotNull(service
);
141 Assert
.IsNotNull(service
.AComponent
);
142 Assert
.IsNotNull(service
.BComponent
);
143 Assert
.IsNull(service
.CComponent
);
147 public void ConstructorWithOneArgument()
149 kernel
.AddComponent("a", typeof(A
));
150 kernel
.AddComponent("service", typeof(ServiceUser
));
152 ServiceUser service
= (ServiceUser
) kernel
["service"];
154 Assert
.IsNotNull(service
);
155 Assert
.IsNotNull(service
.AComponent
);
156 Assert
.IsNull(service
.BComponent
);
157 Assert
.IsNull(service
.CComponent
);
161 public void ParametersAndServicesBestCase()
163 DefaultConfigurationStore store
= new DefaultConfigurationStore();
165 MutableConfiguration config
= new MutableConfiguration("component");
166 MutableConfiguration parameters
= (MutableConfiguration
)
167 config
.Children
.Add(new MutableConfiguration("parameters"));
168 parameters
.Children
.Add(new MutableConfiguration("name", "hammett"));
169 parameters
.Children
.Add(new MutableConfiguration("port", "120"));
171 store
.AddComponentConfiguration("service", config
);
173 kernel
.ConfigurationStore
= store
;
175 kernel
.AddComponent("a", typeof(A
));
176 kernel
.AddComponent("service", typeof(ServiceUser2
));
178 ServiceUser2 service
= (ServiceUser2
) kernel
["service"];
180 Assert
.IsNotNull(service
);
181 Assert
.IsNotNull(service
.AComponent
);
182 Assert
.IsNull(service
.BComponent
);
183 Assert
.IsNull(service
.CComponent
);
184 Assert
.AreEqual("hammett", service
.Name
);
185 Assert
.AreEqual(120, service
.Port
);
189 public void ParametersAndServicesBestCase2()
191 DefaultConfigurationStore store
= new DefaultConfigurationStore();
193 MutableConfiguration config
= new MutableConfiguration("component");
194 MutableConfiguration parameters
= (MutableConfiguration
)
195 config
.Children
.Add(new MutableConfiguration("parameters"));
196 parameters
.Children
.Add(new MutableConfiguration("name", "hammett"));
197 parameters
.Children
.Add(new MutableConfiguration("port", "120"));
198 parameters
.Children
.Add(new MutableConfiguration("Scheduleinterval", "22"));
200 store
.AddComponentConfiguration("service", config
);
202 kernel
.ConfigurationStore
= store
;
204 kernel
.AddComponent("a", typeof(A
));
205 kernel
.AddComponent("service", typeof(ServiceUser2
));
207 ServiceUser2 service
= (ServiceUser2
) kernel
["service"];
209 Assert
.IsNotNull(service
);
210 Assert
.IsNotNull(service
.AComponent
);
211 Assert
.IsNull(service
.BComponent
);
212 Assert
.IsNull(service
.CComponent
);
213 Assert
.AreEqual("hammett", service
.Name
);
214 Assert
.AreEqual(120, service
.Port
);
215 Assert
.AreEqual(22, service
.ScheduleInterval
);