PageRenderTime 90ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 8ms

/src/UnitTests/AOP/ThirdPartyMethodCallInterceptionTests.cs

http://github.com/philiplaureano/LinFu
C# | 147 lines | 116 code | 30 blank | 1 comment | 9 complexity | 961efebe99a576160b485f4c47e11ef3 MD5 | raw file
  1using System;
  2using System.Linq;
  3using System.Reflection;
  4using LinFu.AOP.Cecil.Extensions;
  5using LinFu.AOP.Interfaces;
  6using LinFu.Reflection.Emit;
  7using Mono.Cecil;
  8using Xunit;
  9using SampleLibrary.AOP;
 10
 11namespace LinFu.UnitTests.AOP
 12{
 13    public class ThirdPartyMethodCallInterceptionTests : BaseTestFixture
 14    {
 15        protected override void Init()
 16        {
 17        }
 18
 19        protected override void Term()
 20        {
 21            AroundInvokeMethodCallRegistry.Clear();
 22        }
 23
 24        private Type GetModifiedTargetType()
 25        {
 26            return GetModifiedTargetType(Modify);
 27        }
 28
 29        private Type GetModifiedTargetType(Action<string, TypeDefinition> modify)
 30        {
 31            var assembly = AssemblyDefinition.ReadAssembly("SampleLibrary.dll");
 32            var module = assembly.MainModule;
 33
 34            // Intercept all calls to the System.Console.WriteLine method from the DoSomething method
 35            var typeName = "SampleClassWithThirdPartyMethodCall";
 36            var targetType = (from TypeDefinition t in module.Types
 37                where t.Name == typeName
 38                select t).First();
 39
 40            modify(typeName, targetType);
 41
 42            var modifiedAssembly = assembly.ToAssembly();
 43            return (from t in modifiedAssembly.GetTypes()
 44                where t.Name == typeName
 45                select t).First();
 46        }
 47
 48        private void Modify(string typeName, TypeDefinition targetType)
 49        {
 50            targetType.InterceptMethodCalls(t => t.Name.Contains(typeName),
 51                m => m.DeclaringType.Name.Contains(typeName) && m.Name == "DoSomething",
 52                methodCall =>
 53                    methodCall.DeclaringType.Name == "Console" && methodCall.Name == "WriteLine");
 54        }
 55
 56        [Fact]
 57        public void ShouldCallInstanceAroundInvokeProvider()
 58        {
 59            var modifiedTargetType = GetModifiedTargetType();
 60            var instance = Activator.CreateInstance(modifiedTargetType);
 61            var aroundInvoke = new SampleAroundInvoke();
 62            var provider = new SampleAroundInvokeProvider(aroundInvoke);
 63
 64            var modified = (IModifiableType) instance;
 65            modified.AroundMethodCallProvider = provider;
 66            var targetMethod = modifiedTargetType.GetMethod("DoSomething");
 67
 68            Assert.NotNull(targetMethod);
 69            targetMethod.Invoke(instance, null);
 70
 71            Assert.True(aroundInvoke.BeforeInvokeWasCalled);
 72            Assert.True(aroundInvoke.AfterInvokeWasCalled);
 73        }
 74
 75        [Fact]
 76        public void ShouldCallStaticAroundInvokeProvider()
 77        {
 78            var modifiedTargetType = GetModifiedTargetType();
 79            var instance = Activator.CreateInstance(modifiedTargetType);
 80            var aroundInvoke = new SampleAroundInvoke();
 81            var provider = new SampleAroundInvokeProvider(aroundInvoke);
 82
 83            AroundInvokeMethodCallRegistry.AddProvider(provider);
 84
 85            var targetMethod = modifiedTargetType.GetMethod("DoSomething");
 86            Assert.NotNull(targetMethod);
 87            
 88            targetMethod.Invoke(instance, null);
 89
 90            Assert.True(aroundInvoke.BeforeInvokeWasCalled);
 91            Assert.True(aroundInvoke.AfterInvokeWasCalled);
 92        }
 93
 94        [Fact]
 95        public void ShouldImplementIMethodReplacementHostOnTargetType()
 96        {
 97            var modifiedTargetType = GetModifiedTargetType();
 98
 99            var instance = Activator.CreateInstance(modifiedTargetType);
100            Assert.NotNull(instance);
101            Assert.True(instance is IMethodReplacementHost);
102
103            var host = (IMethodReplacementHost) instance;
104
105            var interceptor = new SampleMethodReplacement();
106
107            host.MethodCallReplacementProvider = new SampleMethodReplacementProvider(interceptor);
108
109            var targetMethod = modifiedTargetType.GetMethod("DoSomething");
110            Assert.NotNull(targetMethod);
111            try
112            {
113                targetMethod.Invoke(instance, null);
114            }
115            catch (TargetInvocationException ex)
116            {
117                var innerException = ex.InnerException;
118                Console.WriteLine(ex.ToString());
119
120                if (innerException != null)
121                    throw innerException;
122
123                throw;
124            }
125
126            Assert.True(interceptor.HasBeenCalled);
127        }
128
129        [Fact]
130        public void ShouldNotInterceptConstructorsWhenIntereptingAllMethodCalls()
131        {
132            var modifiedTargetType = GetModifiedTargetType((name, type) => type.InterceptAllMethodCalls());
133            var instance = Activator.CreateInstance(modifiedTargetType);
134            var aroundInvoke = new SampleAroundInvoke();
135            var provider = new SampleAroundInvokeProvider(aroundInvoke);
136
137            AroundInvokeMethodCallRegistry.AddProvider(provider);
138            var targetMethod = modifiedTargetType.GetMethod("DoSomething");
139            Assert.NotNull(targetMethod);
140
141            targetMethod.Invoke(instance, null);
142
143            Assert.True(aroundInvoke.BeforeInvokeWasCalled);
144            Assert.True(aroundInvoke.AfterInvokeWasCalled);
145        }
146    }
147}