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 System
.Collections
;
18 using Castle
.MicroKernel
.Handlers
;
19 using Castle
.MicroKernel
.Tests
.RuntimeParameters
;
20 using NUnit
.Framework
;
23 public class RuntimeParametersTestCase
25 private IKernel kernel
;
26 private Hashtable deps
;
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");
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\r\n" +
50 "Keys (components with specific keys)\r\n- myArgument which was not registered. \r\n"
52 public void WithoutParameters()
54 CompB compb
= kernel
[typeof(CompB
)] as CompB
;
58 public void WillAlwaysResolveCustomParameterFromServiceComponent()
60 kernel
.AddComponent("compc", typeof(CompC
));
61 Hashtable c_dependencies
= new Hashtable();
62 c_dependencies
["test"] = 15;
63 kernel
.RegisterCustomDependencies(typeof(CompC
), c_dependencies
);
64 Hashtable b_dependencies
= new Hashtable();
65 b_dependencies
["myArgument"] = "foo";
66 kernel
.RegisterCustomDependencies(typeof(CompB
), b_dependencies
);
67 CompB b
= kernel
["compb"] as CompB
;
69 Assert
.AreEqual(15, b
.Compc
.test
);
73 public void ResolveUsingParameters()
75 CompB compb
= kernel
.Resolve(typeof(CompB
), deps
) as CompB
;
77 AssertDependencies(compb
);
81 public void ResolveUsingParametersWithinTheHandler()
83 kernel
.RegisterCustomDependencies("compb", deps
);
84 CompB compb
= kernel
[typeof(CompB
)] as CompB
;
86 AssertDependencies(compb
);
90 public void ParametersPrecedence()
92 kernel
.RegisterCustomDependencies("compb", deps
);
94 CompB instance_with_model
= (CompB
) kernel
[typeof(CompB
)];
95 Assert
.AreSame(deps
["cc"], instance_with_model
.Compc
, "Model dependency should override kernel dependency");
97 Hashtable deps2
= new Hashtable();
98 deps2
.Add("cc", new CompC(12));
99 deps2
.Add("myArgument", "ayende");
101 CompB instance_with_args
= (CompB
) kernel
.Resolve(typeof(CompB
), deps2
);
103 Assert
.AreSame(deps2
["cc"], instance_with_args
.Compc
, "Should get it from resolve params");
104 Assert
.AreEqual("ayende", instance_with_args
.MyArgument
);
108 public void AddingDependencyToServiceWithCustomDependency()
110 DefaultKernel k
= new DefaultKernel();
111 k
.AddComponent("NeedClassWithCustomerDependency",typeof(NeedClassWithCustomerDependency
));
112 k
.AddComponent("HasCustomDependency", typeof(HasCustomDependency
));
114 Assert
.AreEqual(HandlerState
.WaitingDependency
, k
.GetHandler("HasCustomDependency").CurrentState
);
116 Hashtable hash
= new Hashtable();
117 hash
["name"] = new CompA();
118 k
.RegisterCustomDependencies("HasCustomDependency", hash
);
119 Assert
.AreEqual(HandlerState
.Valid
, k
.GetHandler("HasCustomDependency").CurrentState
);
121 Assert
.IsNotNull(k
.Resolve(typeof(NeedClassWithCustomerDependency
)));
124 private void AssertDependencies(CompB compb
)
126 Assert
.IsNotNull(compb
, "Component B should have been resolved");
128 Assert
.IsNotNull(compb
.Compc
, "CompC property should not be null");
129 Assert
.IsTrue(compb
.MyArgument
!= string.Empty
, "MyArgument property should not be empty");
131 Assert
.AreSame(deps
["cc"], compb
.Compc
, "CompC property should be the same instnace as in the hashtable argument");
132 Assert
.IsTrue("ernst".Equals(compb
.MyArgument
),
133 string.Format("The MyArgument property of compb should be equal to ernst, found {0}", compb
.MyArgument
));