using System;
using System.Reflection;
using Castle.Core.Resource;
using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using NUnit.Framework;
namespace WindsorInitConfig {
///
/// http://stackoverflow.com/questions/4322989/castle-windsor-interceptor-selectors-and-hooks
///
[TestFixture]
public class ProxyGenerationHookTests {
public class LoggingProxyGenerationHook : IProxyGenerationHook {
public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo) {
//throw new NotImplementedException();
}
public void MethodsInspected() {
//throw new Exception("The method or operation is not implemented.");
}
public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo) {
return methodInfo.Name.StartsWith("Save", StringComparison.Ordinal);
}
}
public class SomeInterceptor: IInterceptor {
public static bool Intercepted;
public void Intercept(IInvocation invocation) {
Intercepted = true;
}
}
public class TestClass {
public virtual void SaveSomething() {}
public virtual void DoSomethingElse() { }
}
[Test]
public void test() {
var container = new WindsorContainer(new XmlInterpreter(new StaticContentResource(@"
${LoggingAspect}
"
.Replace("{0}", typeof(SomeInterceptor).AssemblyQualifiedName)
.Replace("{1}", typeof(LoggingProxyGenerationHook).AssemblyQualifiedName)
.Replace("{2}", typeof(TestClass).AssemblyQualifiedName)
)));
var c = container.Resolve();
c.SaveSomething();
Assert.IsTrue(SomeInterceptor.Intercepted);
SomeInterceptor.Intercepted = false;
c.DoSomethingElse();
Assert.IsFalse(SomeInterceptor.Intercepted);
}
}
}