Fixing previous commit, using correct case for CaptureFor and removing the component...
[castle.git] / InversionOfControl / Castle.Windsor.Tests / InterceptorsTestCase.cs
blobacc3eb956a2338e01c7d88707573f63450bc5b1f
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.Windsor.Tests
17 using System;
18 using System.Threading;
19 using Castle.Core;
20 using Castle.Core.Interceptor;
21 using Castle.MicroKernel;
22 using Castle.Windsor.Tests.Components;
23 using NUnit.Framework;
25 [TestFixture]
26 public class InterceptorsTestCase
28 private IWindsorContainer _container;
29 private ManualResetEvent _startEvent = new ManualResetEvent(false);
30 private ManualResetEvent _stopEvent = new ManualResetEvent(false);
31 private CalculatorService _service;
33 [SetUp]
34 public void Init()
36 _container = new WindsorContainer();
38 _container.AddFacility("1", new MyInterceptorGreedyFacility());
39 _container.AddFacility("2", new MyInterceptorGreedyFacility());
40 _container.AddFacility("3", new MyInterceptorGreedyFacility());
43 [TearDown]
44 public void Terminate()
46 _container.Dispose();
49 [Test]
50 public void InterfaceProxy()
52 _container.AddComponent("interceptor", typeof(ResultModifierInterceptor));
53 _container.AddComponent("key",
54 typeof(ICalcService), typeof(CalculatorService));
56 ICalcService service = (ICalcService) _container.Resolve("key");
58 Assert.IsNotNull(service);
59 Assert.AreEqual(7, service.Sum(2, 2));
62 [Test]
63 public void InterfaceProxyWithLifecycle()
65 _container.AddComponent("interceptor", typeof(ResultModifierInterceptor));
66 _container.AddComponent("key",
67 typeof(ICalcService), typeof(CalculatorServiceWithLifecycle));
69 ICalcService service = (ICalcService) _container.Resolve("key");
71 Assert.IsNotNull(service);
72 Assert.IsTrue(service.Initialized);
73 Assert.AreEqual(7, service.Sum(2, 2));
75 Assert.IsFalse(service.Disposed);
77 _container.Release(service);
79 Assert.IsTrue(service.Disposed);
82 [Test]
83 public void ClassProxy()
85 _container.AddComponent("interceptor", typeof(ResultModifierInterceptor));
86 _container.AddComponent("key", typeof(CalculatorService));
88 CalculatorService service = (CalculatorService) _container.Resolve("key");
90 Assert.IsNotNull(service);
91 Assert.AreEqual(7, service.Sum(2, 2));
94 [Test]
95 public void OnBehalfOfTest()
97 _container.AddComponent("interceptor", typeof(InterceptorWithOnBehalf));
98 _container.AddComponent("key", typeof(CalculatorService));
100 CalculatorService service =
101 (CalculatorService) _container.Resolve("key");
103 Assert.IsNotNull(service);
104 Assert.AreEqual(4, service.Sum(2, 2));
105 Assert.IsNotNull(InterceptorWithOnBehalf.Model);
106 Assert.AreEqual("key", InterceptorWithOnBehalf.Model.Name);
107 Assert.AreEqual(typeof(CalculatorService),
108 InterceptorWithOnBehalf.Model.Implementation);
111 [Test]
112 public void ClassProxyWithAttributes()
114 _container = new WindsorContainer(); // So we wont use the facilities
116 _container.AddComponent("interceptor", typeof(ResultModifierInterceptor));
117 _container.AddComponent("key", typeof(CalculatorServiceWithAttributes));
119 CalculatorServiceWithAttributes service =
120 (CalculatorServiceWithAttributes) _container.Resolve("key");
122 Assert.IsNotNull(service);
123 Assert.AreEqual(5, service.Sum(2, 2));
126 [Test]
127 public void Multithreaded()
129 _container.AddComponent("interceptor", typeof(ResultModifierInterceptor));
130 _container.AddComponent("key", typeof(CalculatorService));
132 _service = (CalculatorService) _container.Resolve("key");
134 const int threadCount = 10;
136 Thread[] threads = new Thread[threadCount];
138 for(int i = 0; i < threadCount; i++)
140 threads[i] = new Thread(new ThreadStart(ExecuteMethodUntilSignal));
141 threads[i].Start();
144 _startEvent.Set();
146 Thread.CurrentThread.Join(1 * 2000);
148 _stopEvent.Set();
151 public void ExecuteMethodUntilSignal()
153 _startEvent.WaitOne(int.MaxValue, false);
155 while(!_stopEvent.WaitOne(1, false))
157 Assert.AreEqual(7, _service.Sum(2, 2));
158 Assert.AreEqual(8, _service.Sum(3, 2));
159 Assert.AreEqual(10, _service.Sum(3, 4));
164 public class MyInterceptorGreedyFacility : IFacility
166 #region IFacility Members
168 public void Init(IKernel kernel, Core.Configuration.IConfiguration facilityConfig)
170 kernel.ComponentRegistered += new ComponentDataDelegate(OnComponentRegistered);
173 public void Terminate()
177 #endregion
179 private void OnComponentRegistered(String key, IHandler handler)
181 if (key == "key")
183 handler.ComponentModel.Interceptors.Add(
184 new InterceptorReference("interceptor"));
189 public class MyInterceptorGreedyFacility2 : IFacility
191 #region IFacility Members
193 public void Init(IKernel kernel, Core.Configuration.IConfiguration facilityConfig)
195 kernel.ComponentRegistered += new ComponentDataDelegate(OnComponentRegistered);
198 public void Terminate()
202 #endregion
204 private void OnComponentRegistered(String key, IHandler handler)
206 if (typeof(IInterceptor).IsAssignableFrom(handler.ComponentModel.Service))
208 return;
211 handler.ComponentModel.Interceptors.Add(new InterceptorReference("interceptor"));
215 public class ResultModifierInterceptor : IInterceptor
217 public void Intercept(IInvocation invocation)
219 if (invocation.Method.Name.Equals("Sum"))
221 invocation.Proceed();
222 object result = invocation.ReturnValue;
223 invocation.ReturnValue = ((int) result) + 1;
224 return;
227 invocation.Proceed();
231 public class InterceptorWithOnBehalf : IInterceptor, IOnBehalfAware
233 private static ComponentModel _model;
235 #region IMethodInterceptor Members
237 public void Intercept(IInvocation invocation)
239 invocation.Proceed();
242 #endregion
244 public void SetInterceptedComponentModel(ComponentModel target)
246 _model = target;
249 public static ComponentModel Model
251 get { return _model; }