Reverted DictionaryComponentAttribute changes.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / DependencyResolvers.cs
blob9e5723f678d564ae5ab65b067904e4bc62924f64
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 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);