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
.Windsor
.Tests
18 using System
.Threading
;
20 using Castle
.Core
.Interceptor
;
21 using Castle
.MicroKernel
;
22 using Castle
.Windsor
.Tests
.Components
;
23 using NUnit
.Framework
;
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
;
36 _container
= new WindsorContainer();
38 _container
.AddFacility("1", new MyInterceptorGreedyFacility());
39 _container
.AddFacility("2", new MyInterceptorGreedyFacility());
40 _container
.AddFacility("3", new MyInterceptorGreedyFacility());
44 public void Terminate()
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));
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
);
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));
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
);
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));
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
));
146 Thread
.CurrentThread
.Join(1 * 2000);
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()
179 private void OnComponentRegistered(String key
, IHandler handler
)
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()
204 private void OnComponentRegistered(String key
, IHandler handler
)
206 if (typeof(IInterceptor
).IsAssignableFrom(handler
.ComponentModel
.Service
))
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;
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();
244 public void SetInterceptedComponentModel(ComponentModel target
)
249 public static ComponentModel Model
251 get { return _model; }