// Copyright 2004-2008 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.Collections; using Castle.DynamicProxy.Tests.GenClasses; using Castle.DynamicProxy.Tests.Interceptors; using NUnit.Framework; [TestFixture] public class GenericClassProxyTestCase : BasePEVerifyTestCase { private LogInvocationInterceptor logger; public override void Init() { base.Init(); logger = new LogInvocationInterceptor(); } [Test] public void ProxyWithGenericArgument() { ClassWithGenArgs proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); proxy.DoSomething(); Assert.IsTrue(proxy.Invoked); proxy.AProperty = true; Assert.IsTrue(proxy.AProperty); Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents); } [Test] public void ProxyWithGenericArguments() { ClassWithGenArgs proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); proxy.DoSomething(); Assert.IsTrue(proxy.Invoked); proxy.AProperty = true; Assert.IsTrue(proxy.AProperty); Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents); } [Test] public void ProxyWithGenericArgumentsWithBaseGenericClass() { SubClassWithGenArgs proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); proxy.DoSomething(); Assert.IsTrue(proxy.Invoked); proxy.AProperty = true; Assert.IsTrue(proxy.AProperty); Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents); } [Test] public void ProxyWithGenericArgumentsAndArgumentConstraints() { GenClassWithConstraints proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); proxy.DoSomething(); Assert.IsTrue(proxy.Invoked); Assert.AreEqual("DoSomething ", logger.LogContents); } [Test] public void GenericProxyWithIndexer() { object proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); ClassWithIndexer type = (ClassWithIndexer) proxy; type["name"] = 10; Assert.AreEqual(10, type["name"]); Assert.AreEqual("set_Item get_Item ", logger.LogContents); } #if !MONO [Test] public void ProxyWithGenericArgumentsAndMethodGenericArguments() { GenClassWithGenMethods proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); proxy.DoSomething("z param"); Assert.IsTrue(proxy.Invoked); Assert.AreEqual("z param", proxy.SavedParam); Assert.AreEqual("DoSomething ", logger.LogContents); } [Test] public void ProxyWithGenericArgumentsAndMethodGenericArgumentsWithConstraints() { GenClassWithGenMethodsConstrained proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); proxy.DoSomething("z param"); Assert.IsTrue(proxy.Invoked); Assert.AreEqual("z param", proxy.SavedParam); Assert.AreEqual("DoSomething ", logger.LogContents); } [Test] public void ProxyWithGenericArgumentsAndMethodGenericArgumentsWithOneNotDefinedOnType() { GenClassWithGenMethods proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); int value1 = 10; proxy.DoSomethingElse(delegate(int param1) { return param1.ToString(); }, value1); Assert.IsTrue(proxy.Invoked); Assert.AreEqual("10", proxy.SavedParam); Assert.AreEqual("DoSomethingElse ", logger.LogContents); } [Test] public void ProxyWithGenericArgumentsAndMethodGenericReturn() { GenClassWithGenReturn proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); object ret1 = proxy.DoSomethingT(); object ret2 = proxy.DoSomethingZ(); Assert.IsInstanceOfType(typeof (ArrayList), ret1); Assert.IsInstanceOfType(typeof (Hashtable), ret2); Assert.AreEqual("DoSomethingT DoSomethingZ ", logger.LogContents); } [Test] public void GenericMethodArgumentsAndTypeGenericArgumentsWithSameName() { GenClassNameClash proxy = generator.CreateClassProxy>(logger); Assert.IsNotNull(proxy); proxy.DoSomethingT(1); proxy.DoSomethingZ(1L); proxy.DoSomethingTX(1, "a"); proxy.DoSomethingZX(1L, "b"); Assert.AreEqual("DoSomethingT DoSomethingZ DoSomethingTX DoSomethingZX ", logger.LogContents); } [Test] public void ClassWithGenMethodOnly() { OnlyGenMethodsClass proxy = generator.CreateClassProxy(logger); Assert.IsNotNull(proxy); proxy.DoSomething(new ArrayList()); Assert.IsTrue(proxy.Invoked); Assert.AreEqual("DoSomething ", logger.LogContents); } [Test] public void MethodInfoClosedInGenTypeGenMethodRefType() { KeepDataInterceptor interceptor = new KeepDataInterceptor(); GenClassWithGenMethods proxy = generator.CreateClassProxy>(interceptor); proxy.DoSomething(1); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (ArrayList), typeof (int)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); proxy.DoSomething(new Hashtable()); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (ArrayList), typeof (Hashtable)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); } [Test] public void MethodInfoClosedInGenTypeGenMethodValueType() { KeepDataInterceptor interceptor = new KeepDataInterceptor(); GenClassWithGenMethods proxy = generator.CreateClassProxy>(interceptor); proxy.DoSomething(1); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int), typeof (int)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); proxy.DoSomething(new Hashtable()); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int), typeof (Hashtable)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); } [Test] public void MethodInfoClosedInGenTypeNongenMethodRefTypeRefType() { KeepDataInterceptor interceptor = new KeepDataInterceptor(); GenClassWithGenReturn proxy = generator.CreateClassProxy>(interceptor); proxy.DoSomethingT(); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (ArrayList)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); proxy.DoSomethingZ(); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (ArrayList)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); } [Test] public void MethodInfoClosedInGenTypeNongenMethodValueTypeValueType() { KeepDataInterceptor interceptor = new KeepDataInterceptor(); GenClassWithGenReturn proxy = generator.CreateClassProxy>(interceptor); proxy.DoSomethingT(); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); proxy.DoSomethingZ(); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); } [Test] public void MethodInfoClosedInGenTypeNongenMethodValueTypeRefType() { KeepDataInterceptor interceptor = new KeepDataInterceptor(); GenClassWithGenReturn proxy = generator.CreateClassProxy>(interceptor); proxy.DoSomethingT(); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); proxy.DoSomethingZ(); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (ArrayList)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); } [Test] public void MethodInfoClosedInNongenTypeGenMethod() { KeepDataInterceptor interceptor = new KeepDataInterceptor(); OnlyGenMethodsClass proxy = generator.CreateClassProxy(interceptor); proxy.DoSomething(1); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int), typeof (int)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); proxy.DoSomething(new Hashtable()); GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (Hashtable), typeof (Hashtable)); Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(), interceptor.Invocation.GetConcreteMethodInvocationTarget()); } [Test] [ExpectedException(typeof (ArgumentException))] public void ThrowsWhenProxyingGenericTypeDefNoTarget() { KeepDataInterceptor interceptor = new KeepDataInterceptor(); object o = generator.CreateClassProxy(typeof (GenClassWithGenReturn<,>), interceptor); } #endif } }