Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new methods ProxyGenerator.CreateDelegateProxy[WithTarget] #403

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void InterfaceProxyWithTarget_MethodInvocationTarget_should_be_methodOnTa
}

[Test]
public void InterfaceProxyWithTarget_MethodInvocationTarget_should_be_null()
public void InterfaceProxyWithoutTarget_MethodInvocationTarget_should_be_null()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed a typo here; otherwise unrelated to this PR.

{
var interceptor = new KeepDataInterceptor();
var proxy = generator.CreateInterfaceProxyWithoutTarget<IService>(interceptor);
Expand Down Expand Up @@ -128,5 +128,29 @@ public void ClassProxyForGeneric_MethodInvocationTarget_should_be_proxyMethod()
Assert.IsTrue(proxy.IsChanged);
Assert.IsNotNull(interceptor.Invocation.MethodInvocationTarget);
}

[Test]
public void DelegateProxy_MethodInvocationTarget_should_be_null()
{
var interceptor = new KeepDataInterceptor();

var proxy = generator.CreateDelegateProxy<Action>(interceptor);
proxy();

Assert.Null(interceptor.Invocation.MethodInvocationTarget);
}

[Test]
public void DelegateProxyWithTarget_MethodInvocationTarget_should_be_Invoke_method_on_target_delegate_type()
{
var interceptor = new KeepDataInterceptor();
Action target = delegate { };

var proxy = generator.CreateDelegateProxyWithTarget<Action>(target, interceptor);
proxy();

var methodOnTarget = target.GetType().GetMethod("Invoke");
Assert.AreSame(methodOnTarget, interceptor.Invocation.MethodInvocationTarget);
}
}
}
118 changes: 118 additions & 0 deletions src/Castle.Core.Tests/DynamicProxy.Tests/InvocationMethodTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2004-2018 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests
{
using System;
using System.Reflection;

using Castle.DynamicProxy.Tests.Classes;
using Castle.DynamicProxy.Tests.GenClasses;
using Castle.DynamicProxy.Tests.Interceptors;
using Castle.DynamicProxy.Tests.InterClasses;
using Castle.DynamicProxy.Tests.Interfaces;

using NUnit.Framework;

[TestFixture]
public class InvocationMethodTestCase : BasePEVerifyTestCase
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole file is more or less a copy of InvocationMethodInvocationTargetTestCase.cs adjusted to .Method (instead of .MethodInvocationTarget). I removed a few non-delegate proxy tests where I'm simply not sufficiently knowledgeable to know what the expected behavior should be. We can always add these back at a later time.

{
[Test]
public void ClassProxy_Method_should_be_base_Method()
{
var interceptor = new KeepDataInterceptor();
var proxy = generator.CreateClassProxy<ServiceClass>(interceptor);
proxy.Sum(2, 2);
var methodOnClass = typeof(ServiceClass).GetMethod("Sum", new[] { typeof(int), typeof(int) });
Assert.AreSame(methodOnClass, interceptor.Invocation.Method);
}

[Test]
public void ClassProxy_Method_should_be_interface_method_for_interface_methods_implemented_non_virtually()
{
var interceptor = new KeepDataInterceptor();
var proxy = generator.CreateClassProxy(typeof(One), new[] { typeof(IOne) }, interceptor) as IOne;
proxy.OneMethod();
var methodOnInterface = typeof(IOne).GetMethod("OneMethod", Type.EmptyTypes);
Assert.AreSame(methodOnInterface, interceptor.Invocation.Method);
}

[Test]
public void ClassProxy_Method_should_be_base_Method_for_interface_methods_implemented_virtually()
{
var interceptor = new KeepDataInterceptor();
var proxy = generator.CreateClassProxy(typeof(ClassWithVirtualInterface), new[] { typeof(ISimpleInterface) }, interceptor) as ISimpleInterface;
proxy.Do();
var methodOnClass = typeof(ClassWithVirtualInterface).GetMethod("Do", Type.EmptyTypes);
Assert.AreSame(methodOnClass, interceptor.Invocation.Method);
}

[Test]
public void InterfaceProxyWithTarget_Method_should_be_interface_method()
{
var interceptor = new KeepDataInterceptor();
var target = new ServiceImpl();
var proxy = generator.CreateInterfaceProxyWithTarget<IService>(target, interceptor);
proxy.Sum(2, 2);
MethodInfo methodOnInterface = typeof(IService).GetMethod("Sum", new[] { typeof(int), typeof(int) });
Assert.AreSame(methodOnInterface, interceptor.Invocation.Method);
}

[Test]
public void InterfaceProxyWithoutTarget_Method_should_be_interface_method()
{
var interceptor = new KeepDataInterceptor();
var proxy = generator.CreateInterfaceProxyWithoutTarget<IService>(interceptor);
proxy.Sum(2, 2);
MethodInfo methodOnInterface = typeof(IService).GetMethod("Sum", new[] { typeof(int), typeof(int) });
Assert.AreSame(methodOnInterface, interceptor.Invocation.Method);
}

[Test]
public void InterfaceProxyWithTargetInterface_Method_should_be_interface_method()
{
var interceptor = new KeepDataInterceptor();
var target = new ServiceImpl();
var proxy = generator.CreateInterfaceProxyWithTargetInterface(typeof(IService), target, interceptor) as IService;
proxy.Sum(2, 2);
MethodInfo methodOnInterface = typeof(IService).GetMethod("Sum", new[] { typeof(int), typeof(int) });
Assert.AreSame(methodOnInterface, interceptor.Invocation.Method);
}

[Test]
public void DelegateProxy_Method_should_be_Invoke_method_on_delegate_type()
{
var interceptor = new KeepDataInterceptor();

var proxy = generator.CreateDelegateProxy<Action>(interceptor);
proxy();

var methodOnDelegateType = typeof(Action).GetMethod("Invoke");
Assert.AreSame(methodOnDelegateType, interceptor.Invocation.Method);
}

[Test]
public void DelegateProxyWithTarget_Method_should_be_Invoke_method_on_delegate_type()
{
var interceptor = new KeepDataInterceptor();
Action target = delegate { };

var proxy = generator.CreateDelegateProxyWithTarget<Action>(target, interceptor);
proxy();

var methodOnDelegateType = typeof(Action).GetMethod("Invoke");
Assert.AreSame(methodOnDelegateType, interceptor.Invocation.Method);
}
}
}