1 // Copyright 2004-2007 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
17 using Castle
.Core
.Configuration
;
18 using Castle
.MicroKernel
.Handlers
;
19 using Castle
.MicroKernel
.Tests
.ClassComponents
;
20 using NUnit
.Framework
;
23 /// Summary description for DependencyResolvers.
26 public class DependencyResolvers
28 private IKernel kernel
;
33 kernel
= new DefaultKernel();
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
);
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
);
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
);
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"];
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
);