User: xtoff
Date: 2009/12/24 05:34 PM
Added:
/InversionOfControl/trunk/src/Castle.Windsor.Tests/
Interceptors.config, InterceptorsInvalid.config, InterceptorsMultiple.config
Modified:
/InversionOfControl/trunk/src/Castle.MicroKernel/ModelBuilder/Inspectors/
ConfigurationParametersInspector.cs, InterceptorInspector.cs
/InversionOfControl/trunk/src/Castle.Windsor.Tests/
Castle.Windsor.Tests-vs2008.csproj, InterceptorsTestCase.cs
Log:
- added tests for reading interceptor information from config file (why there
weren't any?)
- minor modification - when interceptor is registerer with key, it's
registered as service override.
File Changes:
Directory: /InversionOfControl/trunk/src/Castle.Windsor.Tests/
==============================================================
File [modified]: Castle.Windsor.Tests-vs2008.csproj
Delta lines: +19 -0
===================================================================
--- InversionOfControl/trunk/src/Castle.Windsor.Tests/Interceptors.config
(rev 0)
+++ InversionOfControl/trunk/src/Castle.Windsor.Tests/Interceptors.config
2009-12-25 00:34:46 UTC (rev 6533)
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+ <components>
+ <component id="myInterceptor"
type="Castle.Windsor.Tests.ResultModifierInterceptor, Castle.Windsor.Tests"/>
+ <component
+ id="ValidComponent"
+ type="Castle.Windsor.Tests.Components.CalculatorService,
Castle.Windsor.Tests">
+ <interceptors>
+ <interceptor>${myInterceptor}</interceptor>
+ </interceptors>
+ </component>
+ <component
+ id="ComponentWithNonExistingInterceptor"
+ type="Castle.Windsor.Tests.Components.CalculatorService,
Castle.Windsor.Tests">
+ <interceptors>
+ <interceptor>${NonExistingInterceptor}</interceptor>
+ </interceptors>
+ </component>
+ </components>
File [added]: Interceptors.config
Delta lines: +12 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.Windsor.Tests/InterceptorsInvalid.config
(rev 0)
+++
InversionOfControl/trunk/src/Castle.Windsor.Tests/InterceptorsInvalid.config
2009-12-25 00:34:46 UTC (rev 6533)
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+ <components>
+ <component
+ id="ComponentWithInvalidInterceptor"
+ type="Castle.Windsor.Tests.Components.CalculatorService,
Castle.Windsor.Tests">
+ <interceptors>
+ <interceptor>InvalidInterceptor</interceptor>
+ </interceptors>
+ </component>
+ </components>
+</configuration>
File [added]: InterceptorsInvalid.config
Delta lines: +21 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.Windsor.Tests/InterceptorsMultiple.config
(rev 0)
+++
InversionOfControl/trunk/src/Castle.Windsor.Tests/InterceptorsMultiple.config
2009-12-25 00:34:46 UTC (rev 6533)
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+ <components>
+ <component id="returns5"
type="Castle.Windsor.Tests.ResultModifierInterceptor, Castle.Windsor.Tests">
+ <parameters>
+ <returnValue>5</returnValue>
+ </parameters>
+ </component>
+ <component id="returns10"
type="Castle.Windsor.Tests.ResultModifierInterceptor, Castle.Windsor.Tests">
+ <parameters>
+ <returnValue>10</returnValue>
+ </parameters>
+ </component>
+ <component
+ id="component"
+ type="Castle.Windsor.Tests.Components.CalculatorService,
Castle.Windsor.Tests">
+ <interceptors>
+ <interceptor>${returns10}</interceptor>
+ </interceptors>
+ </component>
+ </components>
File [added]: InterceptorsMultiple.config
Delta lines: +121 -65
===================================================================
--- InversionOfControl/trunk/src/Castle.Windsor.Tests/InterceptorsTestCase.cs
2009-12-24 22:36:59 UTC (rev 6532)
+++ InversionOfControl/trunk/src/Castle.Windsor.Tests/InterceptorsTestCase.cs
2009-12-25 00:34:46 UTC (rev 6533)
@@ -15,73 +15,74 @@
namespace Castle.Windsor.Tests
{
- using Castle.MicroKernel.Registration;
using System;
using System.Threading;
+
using Castle.Core;
using Castle.Core.Interceptor;
using Castle.MicroKernel;
+ using Castle.MicroKernel.Handlers;
+ using Castle.MicroKernel.Registration;
using Castle.Windsor.Tests.Components;
+
using NUnit.Framework;
[TestFixture]
public class InterceptorsTestCase
{
- private IWindsorContainer _container;
- private ManualResetEvent _startEvent = new
ManualResetEvent(false);
- private ManualResetEvent _stopEvent = new
ManualResetEvent(false);
- private CalculatorService _service;
+ private IWindsorContainer container;
+ private readonly ManualResetEvent startEvent = new
ManualResetEvent(false);
+ private readonly ManualResetEvent stopEvent = new
ManualResetEvent(false);
+ private CalculatorService service;
[SetUp]
public void Init()
{
- _container = new WindsorContainer();
+ container = new WindsorContainer();
- _container.AddFacility("1", new
MyInterceptorGreedyFacility());
- _container.AddFacility("2", new
MyInterceptorGreedyFacility());
- _container.AddFacility("3", new
MyInterceptorGreedyFacility());
+ container.AddFacility("1", new
MyInterceptorGreedyFacility());
+ container.AddFacility("2", new
MyInterceptorGreedyFacility());
+ container.AddFacility("3", new
MyInterceptorGreedyFacility());
}
[TearDown]
public void Terminate()
{
- _container.Dispose();
+ container.Dispose();
}
[Test]
public void InterfaceProxy()
{
- _container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
- _container.AddComponent("key",
- typeof(ICalcService),
typeof(CalculatorService));
+ container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
+ container.AddComponent("key",typeof(ICalcService),
typeof(CalculatorService));
- ICalcService service = (ICalcService)
_container.Resolve("key");
+ var service = container.Resolve<ICalcService>("key");
Assert.IsNotNull(service);
Assert.AreEqual(7, service.Sum(2, 2));
}
- [Test]
- public void Interface_that_depends_on_service_it_is_intercepting()
- {
- _container.AddComponent("interceptor",
typeof(InterceptorThatCauseStackOverflow));
- _container.Register(
- Component.For<ICameraService>().ImplementedBy<CameraService>()
- .Interceptors(new[]{new
InterceptorReference(typeof(InterceptorThatCauseStackOverflow)), }).First,
- //because it has no interceptors, it is okay to resolve it...
-
Component.For<ICameraService>().ImplementedBy<CameraService>().Named("okay to
resolve")
- );
- _container.Resolve<ICameraService>();
- }
+ [Test]
+ public void
Interface_that_depends_on_service_it_is_intercepting()
+ {
+ container.AddComponent("interceptor",
typeof(InterceptorThatCauseStackOverflow));
+ container.Register(
+
Component.For<ICameraService>().ImplementedBy<CameraService>()
+ .Interceptors(new[] { new
InterceptorReference(typeof(InterceptorThatCauseStackOverflow)), }).First,
+ //because it has no interceptors, it is okay to
resolve it...
+
Component.For<ICameraService>().ImplementedBy<CameraService>().Named("okay to
resolve")
+ );
+ container.Resolve<ICameraService>();
+ }
[Test]
public void InterfaceProxyWithLifecycle()
{
- _container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
- _container.AddComponent("key",
- typeof(ICalcService),
typeof(CalculatorServiceWithLifecycle));
+ container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
+ container.AddComponent("key", typeof(ICalcService),
typeof(CalculatorServiceWithLifecycle));
- ICalcService service = (ICalcService)
_container.Resolve("key");
+ ICalcService service = (ICalcService)
container.Resolve("key");
Assert.IsNotNull(service);
Assert.IsTrue(service.Initialized);
@@ -89,7 +90,7 @@
Assert.IsFalse(service.Disposed);
- _container.Release(service);
+ container.Release(service);
Assert.IsTrue(service.Disposed);
}
@@ -97,23 +98,62 @@
[Test]
public void ClassProxy()
{
- _container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
- _container.AddComponent("key",
typeof(CalculatorService));
+ container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
+ container.AddComponent("key",
typeof(CalculatorService));
- CalculatorService service = (CalculatorService)
_container.Resolve("key");
+ service = container.Resolve<CalculatorService>("key");
Assert.IsNotNull(service);
Assert.AreEqual(7, service.Sum(2, 2));
}
+#if (!SILVERLIGH) //no xml in Silverlight
+
[Test]
+ public void Xml_validComponent_resolves_correctly()
+ {
+
container.Install(Windsor.Installer.Configuration.FromXmlFile(ConfigHelper.ResolveConfigPath("Interceptors.config")));
+ service =
container.Resolve<CalculatorService>("ValidComponent");
+
+ Assert.IsNotNull(service);
+ Assert.AreEqual(5, service.Sum(2, 2));
+ }
+
+ [Test]
+ public void Xml_multiple_interceptors_resolves_correctly()
+ {
+
container.Install(Windsor.Installer.Configuration.FromXmlFile(ConfigHelper.ResolveConfigPath("InterceptorsMultiple.config")));
+ service =
container.Resolve<CalculatorService>("component");
+
+ Assert.IsNotNull(service);
+ Assert.AreEqual(10, service.Sum(2, 2));
+ }
+
+ [Test]
+ public void Xml_Component_With_Non_Existing_Interceptor_throws()
+ {
+
container.Install(Windsor.Installer.Configuration.FromXmlFile(ConfigHelper.ResolveConfigPath("Interceptors.config")));
+ Assert.Throws(typeof(HandlerException), () =>
+
container.Resolve<CalculatorService>("ComponentWithNonExistingInterceptor"));
+ }
+
+ [Test]
+ public void Xml_Component_With_Non_invalid_Interceptor_throws()
+ {
+ Assert.Throws(typeof(Exception), () =>
+ container.Install(
+
Windsor.Installer.Configuration.FromXmlFile(
+
ConfigHelper.ResolveConfigPath("InterceptorsInvalid.config"))));
+ }
+#endif
+ [Test]
public void OnBehalfOfTest()
{
- _container.AddComponent("interceptor",
typeof(InterceptorWithOnBehalf));
- _container.AddComponent("key",
typeof(CalculatorService));
+ container.AddComponent("interceptor",
typeof(InterceptorWithOnBehalf));
+ container.AddComponent("key",
typeof(CalculatorService));
CalculatorService service =
- (CalculatorService) _container.Resolve("key");
+ (CalculatorService) container.Resolve("key");
Assert.IsNotNull(service);
Assert.AreEqual(4, service.Sum(2, 2));
@@ -126,13 +166,13 @@
[Test]
public void ClassProxyWithAttributes()
{
- _container = new WindsorContainer(); // So we wont use
the facilities
+ container = new WindsorContainer(); // So we wont use
the facilities
- _container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
- _container.AddComponent("key",
typeof(CalculatorServiceWithAttributes));
+ container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
+ container.AddComponent("key",
typeof(CalculatorServiceWithAttributes));
CalculatorServiceWithAttributes service =
- (CalculatorServiceWithAttributes)
_container.Resolve("key");
+ (CalculatorServiceWithAttributes)
container.Resolve("key");
Assert.IsNotNull(service);
Assert.AreEqual(5, service.Sum(2, 2));
@@ -141,10 +181,10 @@
[Test]
public void Multithreaded()
{
- _container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
- _container.AddComponent("key",
typeof(CalculatorService));
+ container.AddComponent("interceptor",
typeof(ResultModifierInterceptor));
+ container.AddComponent("key",
typeof(CalculatorService));
- _service = (CalculatorService)
_container.Resolve("key");
+ service = (CalculatorService) container.Resolve("key");
const int threadCount = 10;
@@ -156,35 +196,35 @@
threads[i].Start();
}
- _startEvent.Set();
+ startEvent.Set();
Thread.CurrentThread.Join(1 * 2000);
- _stopEvent.Set();
+ stopEvent.Set();
}
[Test]
public void AutomaticallyOmitTarget()
{
- _container.Register(
+ container.Register(
Component.For<ICalcService>()
.Interceptors(InterceptorReference.ForType<ReturnDefaultInterceptor>()).Last,
Component.For<ReturnDefaultInterceptor>()
);
- ICalcService calcService =
_container.Resolve<ICalcService>();
+ ICalcService calcService =
container.Resolve<ICalcService>();
Assert.AreEqual(0, calcService.Sum(1, 2));
}
public void ExecuteMethodUntilSignal()
{
- _startEvent.WaitOne(int.MaxValue);
+ startEvent.WaitOne(int.MaxValue);
- while(!_stopEvent.WaitOne(1))
+ while(!stopEvent.WaitOne(1))
{
- Assert.AreEqual(7, _service.Sum(2, 2));
- Assert.AreEqual(8, _service.Sum(3, 2));
- Assert.AreEqual(10, _service.Sum(3, 4));
+ Assert.AreEqual(7, service.Sum(2, 2));
+ Assert.AreEqual(8, service.Sum(3, 2));
+ Assert.AreEqual(10, service.Sum(3, 4));
}
}
}
@@ -240,28 +280,44 @@
}
}
- public class InterceptorThatCauseStackOverflow : IInterceptor
- {
+ public class InterceptorThatCauseStackOverflow : IInterceptor
+ {
- public InterceptorThatCauseStackOverflow(ICameraService service)
- {
- }
+ public InterceptorThatCauseStackOverflow(ICameraService service)
+ {
+ }
- public void Intercept(IInvocation invocation)
- {
-
- }
- }
+ public void Intercept(IInvocation invocation)
+ {
- public class ResultModifierInterceptor : IInterceptor
+ }
+ }
+
+ public class ResultModifierInterceptor : IInterceptor
{
+ private readonly int? returnValue;
+
+ public ResultModifierInterceptor()
+ {
+ }
+
+ public ResultModifierInterceptor(int returnValue)
+ {
+ this.returnValue = returnValue;
+ }
+
public void Intercept(IInvocation invocation)
{
if (invocation.Method.Name.Equals("Sum"))
{
invocation.Proceed();
object result = invocation.ReturnValue;
- invocation.ReturnValue = ((int) result) + 1;
+ if (!returnValue.HasValue)
+ {
+ invocation.ReturnValue = ((int)result)
+ 1;
+ return;
+ }
+ invocation.ReturnValue = returnValue.Value;
return;
}
File [modified]: InterceptorsTestCase.cs
Delta lines: +0 -0
===================================================================
Directory:
/InversionOfControl/trunk/src/Castle.MicroKernel/ModelBuilder/Inspectors/
====================================================================================
File [modified]: ConfigurationParametersInspector.cs
Delta lines: +15 -18
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/ModelBuilder/Inspectors/InterceptorInspector.cs
2009-12-24 22:36:59 UTC (rev 6532)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/ModelBuilder/Inspectors/InterceptorInspector.cs
2009-12-25 00:34:46 UTC (rev 6533)
@@ -17,7 +17,6 @@
using System;
using Castle.MicroKernel.Util;
using Castle.Core;
- using Castle.Core.Configuration;
/// <summary>
/// Inspect the component for <c>InterceptorAttribute</c> and
@@ -38,29 +37,23 @@
{
if (model.Configuration == null) return;
- IConfiguration interceptors =
model.Configuration.Children["interceptors"];
-
+ var interceptors =
model.Configuration.Children["interceptors"];
if (interceptors == null) return;
- foreach(IConfiguration interceptor in
interceptors.Children)
+ foreach (var interceptor in interceptors.Children)
{
- String value = interceptor.Value;
+ var value = interceptor.Value;
if (!ReferenceExpressionUtil.IsReference(value))
{
- String message = String.Format(
- "The value for the interceptor
must be a reference " +
- "to a component (Currently
{0})",
- value);
-
- throw new Exception(message);
+ throw new Exception(
+ String.Format("The value for
the interceptor must be a reference to a component (Currently {0})", value));
}
- InterceptorReference interceptorRef =
- new InterceptorReference(
ReferenceExpressionUtil.ExtractComponentKey(value) );
-
- model.Interceptors.Add(interceptorRef);
- model.Dependencies.Add(
CreateDependencyModel(interceptorRef) );
+ var reference = new
InterceptorReference(ReferenceExpressionUtil.ExtractComponentKey(value));
+
+ model.Interceptors.Add(reference);
+
model.Dependencies.Add(CreateDependencyModel(reference));
}
}
@@ -91,8 +84,12 @@
protected DependencyModel
CreateDependencyModel(InterceptorReference interceptor)
{
- return new DependencyModel(DependencyType.Service,
interceptor.ComponentKey,
- interceptor.ServiceType, false);
+ if (string.IsNullOrEmpty(interceptor.ComponentKey))
+ {
+ return new
DependencyModel(DependencyType.Service, interceptor.ComponentKey,
interceptor.ServiceType, false);
+ }
+
+ return new
DependencyModel(DependencyType.ServiceOverride, interceptor.ComponentKey,
interceptor.ServiceType, false);
}
File [modified]: InterceptorInspector.cs
Delta lines: +3 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2008.csproj
2009-12-24 22:36:59 UTC (rev 6532)
+++
InversionOfControl/trunk/src/Castle.Windsor.Tests/Castle.Windsor.Tests-vs2008.csproj
2009-12-25 00:34:46 UTC (rev 6533)
@@ -431,6 +431,9 @@
<None Include="DotNet2Config\chainOfRespnsability.config" />
<None Include="DotNet2Config\chainOfRespnsability_smart.config" />
<None Include="Facilities\EventWiring\App.config" />
+ <EmbeddedResource Include="InterceptorsMultiple.config" />
+ <EmbeddedResource Include="InterceptorsInvalid.config" />
+ <EmbeddedResource Include="Interceptors.config" />
<Content Include="RemotingTcpConfig2.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
--
You received this message because you are subscribed to the Google Groups
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-commits?hl=en.