Allow component registration specification of LifeStyle using LifeStyleType.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / DependencyResolvers.cs
blob072010fe333cabbb77ec67c630d62bb9163c9751
1 // Copyright 2004-2008 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.Tests.ClassComponents;
20 using NUnit.Framework;
22 /// <summary>
23 /// Summary description for DependencyResolvers.
24 /// </summary>
25 [TestFixture]
26 public class DependencyResolvers
28 private IKernel kernel;
30 [SetUp]
31 public void Init()
33 kernel = new DefaultKernel();
36 [TearDown]
37 public void Dispose()
39 kernel.Dispose();
42 [Test]
43 public void ResolvingPrimitivesThroughProperties()
45 MutableConfiguration config = new MutableConfiguration("component");
47 MutableConfiguration parameters = (MutableConfiguration)
48 config.Children.Add(new MutableConfiguration("parameters"));
50 parameters.Children.Add(new MutableConfiguration("name", "hammett"));
51 parameters.Children.Add(new MutableConfiguration("address", "something"));
52 parameters.Children.Add(new MutableConfiguration("age", "25"));
54 kernel.ConfigurationStore.AddComponentConfiguration("customer", config);
56 kernel.AddComponent("customer", typeof(ICustomer), typeof(CustomerImpl));
58 ICustomer customer = (ICustomer) kernel["customer"];
60 Assert.IsNotNull(customer);
61 Assert.AreEqual("hammett", customer.Name);
62 Assert.AreEqual("something", customer.Address);
63 Assert.AreEqual(25, customer.Age);
66 [Test]
67 public void ResolvingConcreteClassThroughProperties()
69 kernel.AddComponent("spamservice", typeof(DefaultSpamService));
70 kernel.AddComponent("mailsender", typeof(DefaultMailSenderService));
71 kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine));
73 DefaultSpamService spamservice = (DefaultSpamService) kernel["spamservice"];
75 Assert.IsNotNull(spamservice);
76 Assert.IsNotNull(spamservice.MailSender);
77 Assert.IsNotNull(spamservice.TemplateEngine);
80 [Test]
81 public void ResolvingConcreteClassThroughConstructor()
83 kernel.AddComponent("spamservice", typeof(DefaultSpamServiceWithConstructor));
84 kernel.AddComponent("mailsender", typeof(DefaultMailSenderService));
85 kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine));
87 DefaultSpamServiceWithConstructor spamservice =
88 (DefaultSpamServiceWithConstructor) kernel["spamservice"];
90 Assert.IsNotNull(spamservice);
91 Assert.IsNotNull(spamservice.MailSender);
92 Assert.IsNotNull(spamservice.TemplateEngine);
95 [Test]
96 [ExpectedException(typeof(HandlerException))]
97 public void UnresolvedDependencies()
99 kernel.AddComponent("spamservice", typeof(DefaultSpamServiceWithConstructor));
100 kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine));
102 DefaultSpamService spamservice = (DefaultSpamService) kernel["spamservice"];
105 [Test]
106 public void FactoryPattern()
108 kernel.AddComponent("spamservice", typeof(DefaultSpamServiceWithConstructor));
109 kernel.AddComponent("mailsender", typeof(DefaultMailSenderService));
110 kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine));
112 kernel.AddComponent("factory", typeof(ComponentFactory));
114 ComponentFactory factory = (ComponentFactory) kernel["factory"];
116 Assert.IsNotNull(factory);
118 DefaultSpamServiceWithConstructor spamservice =
119 (DefaultSpamServiceWithConstructor) factory.Create("spamservice");
121 Assert.IsNotNull(spamservice);
122 Assert.IsNotNull(spamservice.MailSender);
123 Assert.IsNotNull(spamservice.TemplateEngine);
126 [Test]
127 public void DependencyChain()
129 kernel.AddComponent("Customer9", typeof(ICustomer), typeof(CustomerChain9));
130 kernel.AddComponent("Customer8", typeof(ICustomer), typeof(CustomerChain8));
131 kernel.AddComponent("Customer7", typeof(ICustomer), typeof(CustomerChain7));
132 kernel.AddComponent("Customer6", typeof(ICustomer), typeof(CustomerChain6));
133 kernel.AddComponent("Customer5", typeof(ICustomer), typeof(CustomerChain5));
134 kernel.AddComponent("Customer4", typeof(ICustomer), typeof(CustomerChain4));
135 kernel.AddComponent("Customer3", typeof(ICustomer), typeof(CustomerChain3));
136 kernel.AddComponent("Customer2", typeof(ICustomer), typeof(CustomerChain2));
137 kernel.AddComponent("Customer1", typeof(ICustomer), typeof(CustomerChain1));
138 kernel.AddComponent("Customer", typeof(ICustomer), typeof(CustomerImpl));
140 CustomerChain1 customer = (CustomerChain1) kernel[typeof(ICustomer)];
141 Assert.IsInstanceOfType(typeof(CustomerChain9), customer);
142 customer = (CustomerChain1)customer.CustomerBase;
143 Assert.IsInstanceOfType(typeof(CustomerChain8), customer);
144 customer = (CustomerChain1)customer.CustomerBase;
145 Assert.IsInstanceOfType(typeof(CustomerChain7), customer);
146 customer = (CustomerChain1)customer.CustomerBase;
147 Assert.IsInstanceOfType(typeof(CustomerChain6), customer);
148 customer = (CustomerChain1)customer.CustomerBase;
149 Assert.IsInstanceOfType(typeof(CustomerChain5), customer);
150 customer = (CustomerChain1)customer.CustomerBase;
151 Assert.IsInstanceOfType(typeof(CustomerChain4), customer);
152 customer = (CustomerChain1)customer.CustomerBase;
153 Assert.IsInstanceOfType(typeof(CustomerChain3), customer);
154 customer = (CustomerChain1)customer.CustomerBase;
155 Assert.IsInstanceOfType(typeof(CustomerChain2), customer);
156 customer = (CustomerChain1)customer.CustomerBase;
157 Assert.IsInstanceOfType(typeof(CustomerChain1), customer);
158 ICustomer lastCustomer = customer.CustomerBase;
159 Assert.IsInstanceOfType(typeof(CustomerImpl), lastCustomer);