Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / RuntimeParametersTestCase.cs
blobbe4f97a4599da41e5c4c477926038462eb168fb2
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.Collections;
18 using Castle.MicroKernel.Handlers;
19 using Castle.MicroKernel.Tests.RuntimeParameters;
20 using NUnit.Framework;
22 [TestFixture]
23 public class RuntimeParametersTestCase
25 private IKernel kernel;
26 private Hashtable deps;
28 [SetUp]
29 public void Init()
31 kernel = new DefaultKernel();
32 kernel.AddComponent("compa", typeof(CompA));
33 kernel.AddComponent("compb", typeof(CompB));
35 deps = new Hashtable();
36 deps.Add("cc", new CompC(12));
37 deps.Add("myArgument", "ernst");
40 [TearDown]
41 public void Dispose()
43 kernel.Dispose();
46 [Test]
47 [ExpectedException(typeof(HandlerException), "Can't create component 'compb' as it has " +
48 "dependencies to be satisfied. \r\ncompb is waiting for the following dependencies: \r\n\r\n" +
49 "Services: \r\n- Castle.MicroKernel.Tests.RuntimeParameters.CompC which was not registered. \r\n"
51 public void WithoutParameters()
53 CompB compb = kernel[typeof(CompB)] as CompB;
56 [Test]
57 public void WillAlwaysResolveCustomParameterFromServiceComponent()
59 kernel.AddComponent("compc", typeof(CompC));
60 Hashtable c_dependencies = new Hashtable();
61 c_dependencies["test"] = 15;
62 kernel.RegisterCustomDependencies(typeof(CompC), c_dependencies);
63 Hashtable b_dependencies = new Hashtable();
64 b_dependencies["myArgument"] = "foo";
65 kernel.RegisterCustomDependencies(typeof(CompB), b_dependencies);
66 CompB b = kernel["compb"] as CompB;
67 Assert.IsNotNull(b);
68 Assert.AreEqual(15, b.Compc.test);
71 [Test]
72 public void ResolveUsingParameters()
74 CompB compb = kernel.Resolve(typeof(CompB), deps) as CompB;
76 AssertDependencies(compb);
79 [Test]
80 public void ResolveUsingParametersWithinTheHandler()
82 kernel.RegisterCustomDependencies("compb", deps);
83 CompB compb = kernel[typeof(CompB)] as CompB;
85 AssertDependencies(compb);
88 [Test]
89 public void ParametersPrecedence()
91 kernel.RegisterCustomDependencies("compb", deps);
93 CompB instance_with_model = (CompB) kernel[typeof(CompB)];
94 Assert.AreSame(deps["cc"], instance_with_model.Compc, "Model dependency should override kernel dependency");
96 Hashtable deps2 = new Hashtable();
97 deps2.Add("cc", new CompC(12));
98 deps2.Add("myArgument", "ayende");
100 CompB instance_with_args = (CompB) kernel.Resolve(typeof(CompB), deps2);
102 Assert.AreSame(deps2["cc"], instance_with_args.Compc, "Should get it from resolve params");
103 Assert.AreEqual("ayende", instance_with_args.MyArgument);
106 [Test]
107 public void AddingDependencyToServiceWithCustomDependency()
109 DefaultKernel k = new DefaultKernel();
110 k.AddComponent("NeedClassWithCustomerDependency",typeof(NeedClassWithCustomerDependency));
111 k.AddComponent("HasCustomDependency", typeof(HasCustomDependency));
113 Assert.AreEqual(HandlerState.WaitingDependency, k.GetHandler("HasCustomDependency").CurrentState);
115 Hashtable hash = new Hashtable();
116 hash["name"] = new CompA();
117 k.RegisterCustomDependencies("HasCustomDependency", hash);
118 Assert.AreEqual(HandlerState.Valid, k.GetHandler("HasCustomDependency").CurrentState);
120 Assert.IsNotNull(k.Resolve(typeof(NeedClassWithCustomerDependency)));
123 private void AssertDependencies(CompB compb)
125 Assert.IsNotNull(compb, "Component B should have been resolved");
127 Assert.IsNotNull(compb.Compc, "CompC property should not be null");
128 Assert.IsTrue(compb.MyArgument != string.Empty, "MyArgument property should not be empty");
130 Assert.AreSame(deps["cc"], compb.Compc, "CompC property should be the same instnace as in the hashtable argument");
131 Assert.IsTrue("ernst".Equals(compb.MyArgument),
132 string.Format("The MyArgument property of compb should be equal to ernst, found {0}", compb.MyArgument));