Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / DependencyResolvingTestCases.cs
blob792479d82a31ba217d68541ee0b667f00a2bf15a
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.DependencyResolving
17 using System.Collections;
18 using Castle.Core;
19 using Castle.Core.Configuration;
20 using Castle.MicroKernel.Tests.ClassComponents;
21 using NUnit.Framework;
23 /// <summary>
24 /// This test case ensures that the resolving event
25 /// is fired properly.
26 /// </summary>
27 [TestFixture]
28 public class EventTests
30 private IKernel kernel;
32 private Castle.Core.ComponentModel expectedClient;
33 private IList expectedModels;
35 #region Setup / Teardown
37 [SetUp]
38 public void SetUp()
40 kernel = new DefaultKernel();
41 kernel.DependencyResolving += new DependencyDelegate(AssertEvent);
44 [TearDown]
45 public void TearDown()
47 kernel.Dispose();
50 #endregion
52 [Test]
53 public void ResolvingPrimitivesThroughProperties()
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"));
61 parameters.Children.Add(new MutableConfiguration("address", "something"));
62 parameters.Children.Add(new MutableConfiguration("age", "25"));
64 kernel.ConfigurationStore.AddComponentConfiguration("customer", config);
66 kernel.AddComponent("customer", typeof(ICustomer), typeof(CustomerImpl));
68 expectedClient = kernel.GetHandler("customer").ComponentModel;
69 expectedModels = new ArrayList();
70 foreach(PropertySet prop in kernel.GetHandler("customer").ComponentModel.Properties)
72 expectedModels.Add(prop.Dependency);
75 ICustomer customer = (ICustomer) kernel["customer"];
77 Assert.IsNotNull(customer);
80 [Test]
81 public void ResolvingConcreteClassThroughProperties()
83 kernel.AddComponent("spamservice", typeof(DefaultSpamService));
84 kernel.AddComponent("mailsender", typeof(DefaultMailSenderService));
85 kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine));
87 DefaultMailSenderService mailservice = (DefaultMailSenderService) kernel["mailsender"];
88 DefaultTemplateEngine templateengine = (DefaultTemplateEngine) kernel["templateengine"];
90 Assert.IsNotNull(mailservice);
91 Assert.IsNotNull(templateengine);
93 expectedClient = kernel.GetHandler("spamservice").ComponentModel;
94 expectedModels = new ArrayList();
95 foreach(PropertySet prop in kernel.GetHandler("spamservice").ComponentModel.Properties)
97 expectedModels.Add(prop.Dependency);
100 DefaultSpamService spamservice = (DefaultSpamService) kernel["spamservice"];
102 Assert.IsNotNull(spamservice);
105 [Test]
106 public void ResolvingConcreteClassThroughConstructor()
108 kernel.AddComponent("spamservice", typeof(DefaultSpamServiceWithConstructor));
109 kernel.AddComponent("mailsender", typeof(DefaultMailSenderService));
110 kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine));
112 DefaultMailSenderService mailservice = (DefaultMailSenderService) kernel["mailsender"];
113 DefaultTemplateEngine templateengine = (DefaultTemplateEngine) kernel["templateengine"];
115 Assert.IsNotNull(mailservice);
116 Assert.IsNotNull(templateengine);
118 expectedClient = kernel.GetHandler("spamservice").ComponentModel;
119 expectedModels =
120 new ArrayList(kernel.GetHandler("spamservice").ComponentModel.Constructors.FewerArgumentsCandidate.Dependencies);
122 DefaultSpamServiceWithConstructor spamservice =
123 (DefaultSpamServiceWithConstructor) kernel["spamservice"];
125 Assert.IsNotNull(spamservice);
128 private void AssertEvent(Castle.Core.ComponentModel client, Castle.Core.DependencyModel model, object dependency)
130 bool ok = false;
131 Assert.AreEqual(expectedClient, client);
132 foreach(DependencyModel expectedModel in expectedModels)
134 if (expectedModel.Equals(model))
136 ok = true;
137 break;
140 Assert.IsTrue(ok);
141 Assert.IsNotNull(dependency);