Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / BestConstructorTestCase.cs
blob9e6a812d73076d67d63e48e167e7808bc569caa8
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 System;
18 using Castle.Core.Configuration;
19 using Castle.MicroKernel.SubSystems.Configuration;
20 using NUnit.Framework;
22 public class ServiceUser
24 private A _a;
25 private B _b;
26 private C _c;
28 public ServiceUser(A a)
30 if (a == null) throw new ArgumentNullException();
31 _a = a;
34 public ServiceUser(A a, B b) : this(a)
36 if (b == null) throw new ArgumentNullException();
37 _b = b;
40 public ServiceUser(A a, B b, C c) : this(a, b)
42 if (c == null) throw new ArgumentNullException();
43 _c = c;
46 public A AComponent
48 get { return _a; }
51 public B BComponent
53 get { return _b; }
56 public C CComponent
58 get { return _c; }
62 public class ServiceUser2 : ServiceUser
64 private string _name;
65 private int _port;
66 private int _scheduleinterval;
68 public ServiceUser2(A a, String name, int port) : base(a)
70 _name = name;
71 _port = port;
74 public ServiceUser2(A a, String name, int port, int scheduleinterval) : this(a, name, port)
76 _scheduleinterval = scheduleinterval;
79 public String Name
81 get { return _name; }
84 public int Port
86 get { return _port; }
89 public int ScheduleInterval
91 get { return _scheduleinterval; }
95 /// <summary>
96 /// Summary description for BestConstructorTestCase.
97 /// </summary>
98 [TestFixture]
99 public class BestConstructorTestCase
101 private IKernel kernel;
103 [SetUp]
104 public void Init()
106 kernel = new DefaultKernel();
109 [TearDown]
110 public void Dispose()
112 kernel.Dispose();
115 [Test]
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);
131 [Test]
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);
146 [Test]
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);
160 [Test]
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);
188 [Test]
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);