Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / RhinoMocksTestCase.cs
blob9bf840bf5d4e31492c1adca526f80b6f8d48f5d3
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 using System.Runtime.CompilerServices;
17 [assembly :
18 InternalsVisibleTo(
19 "DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7"
22 namespace Castle.DynamicProxy.Tests
24 using System;
25 using System.Collections.Generic;
26 using System.Data;
27 using System.Runtime.InteropServices;
28 using Castle.Core.Interceptor;
29 using Castle.DynamicProxy.Tests.Interceptors;
30 using NUnit.Framework;
32 #if !MONO
34 [TestFixture]
35 public class RhinoMocksTestCase : BasePEVerifyTestCase
37 [Test]
38 public void GenericClassWithGenericMethod()
40 LogInvocationInterceptor logger = new LogInvocationInterceptor();
41 IDoubleGeneric<int> proxy =
42 (IDoubleGeneric<int>) generator.CreateInterfaceProxyWithTarget(typeof(IDoubleGeneric<int>),
43 new DoubleGenericImpl<int>(), logger);
44 proxy.Call<string>(1, "");
45 Assert.AreEqual("Call", logger.Invocations[0]);
48 [Test]
49 public void GenericClassWithGenericMethodWitoutTarget()
51 BasicInterfaceProxyWithoutTargetTestCase.ReturnThreeInterceptor interceptor =
52 new BasicInterfaceProxyWithoutTargetTestCase.ReturnThreeInterceptor();
53 IDoubleGeneric<int> proxy =
54 (IDoubleGeneric<int>) generator.CreateInterfaceProxyWithoutTarget(typeof(IDoubleGeneric<int>),
55 interceptor);
56 object o = proxy.Call<string>(1, "");
57 Assert.AreEqual(3, o);
60 [Test]
61 public void UsingEvents_Interface()
63 LogInvocationInterceptor logger = new LogInvocationInterceptor();
65 IWithEvents proxy = (IWithEvents) generator.CreateInterfaceProxyWithTarget(typeof(IWithEvents),
66 new FakeWithEvents(),
67 logger);
69 Assert.IsNotNull(proxy);
71 proxy.Foo += null;
72 proxy.Foo -= null;
74 Assert.AreEqual(2, logger.Invocations.Count);
77 [Test]
78 public void UsingEvents_Class()
80 LogInvocationInterceptor logger = new LogInvocationInterceptor();
81 FakeWithEvents proxy = (FakeWithEvents) generator.CreateClassProxy(
82 typeof(FakeWithEvents),
83 ProxyGenerationOptions.Default,
84 logger);
86 Assert.IsNotNull(proxy);
88 proxy.Foo += null;
89 proxy.Foo -= null;
91 Assert.AreEqual(2, logger.Invocations.Count);
94 [Test, Ignore("I dont think the effort to fix this edge case is worthwhile")]
95 public void NeedingToCreateNewMethodTableSlot()
97 generator.CreateClassProxy(typeof(MultiClass), new Type[] {typeof(ISpecialMulti)});
100 [Test]
101 public void ProxyingInterfaceWithGuid()
103 object o = generator.CreateInterfaceProxyWithoutTarget(typeof(IWithGuid), new StandardInterceptor());
104 Assert.IsNotNull(o);
107 [Test]
108 public void ProxyingInternalInterface()
110 object o = generator.CreateInterfaceProxyWithoutTarget(typeof(IFoo), new StandardInterceptor());
111 Assert.IsNotNull(o);
114 [Test]
115 public void CanProxyDataSet()
117 generator.CreateClassProxy(typeof(DataSet), new Type[0], new StandardInterceptor());
120 [Test]
121 public void ProxyingComInteraces()
123 object o = generator
124 .CreateInterfaceProxyWithoutTarget(typeof(IComServiceProvider), new StandardInterceptor());
125 Assert.IsNotNull(o);
128 [Test]
129 public void ProxyingGenericClassWithGenericClassConstraint()
131 object o = generator
132 .CreateInterfaceProxyWithoutTarget(typeof(IFactory2), new StandardInterceptor());
133 Assert.IsNotNull(o);
137 [Test]
138 public void CanGetCorrectValuesFromIntPtr()
140 IFooWithIntPtr o = (IFooWithIntPtr)generator
141 .CreateInterfaceProxyWithoutTarget(typeof(IFooWithIntPtr), new IntPtrInterceptor());
142 IntPtr buffer = o.Buffer(15);
143 Assert.AreEqual(IntPtr.Zero, buffer);
146 [Test]
147 public void ProxyInternalMethod()
149 LogInvocationInterceptor logging = new LogInvocationInterceptor();
150 WithInternalMethod o = (WithInternalMethod) generator.CreateClassProxy(typeof(WithInternalMethod),
151 new Type[0], logging);
152 o.Foo();
153 Assert.AreEqual("Foo ", logging.LogContents);
156 [Test]
157 public void ProxyingProtectedInternalAbstractMethod()
159 LogInvocationInterceptor logging = new LogInvocationInterceptor();
160 SomeClassWithProtectedInternalAbstractClass o =
161 (SomeClassWithProtectedInternalAbstractClass)
162 generator.CreateClassProxy(typeof(SomeClassWithProtectedInternalAbstractClass),
163 new Type[0], logging);
164 Assert.IsNotNull(o);
167 [Test]
168 public void VirtualMethodCallsFromTheConstructor()
170 LogInvocationInterceptor logging = new LogInvocationInterceptor();
171 MakeVirtualCallFromCtor o = (MakeVirtualCallFromCtor) generator.CreateClassProxy(typeof(MakeVirtualCallFromCtor),
172 new Type[0], logging);
173 Assert.AreEqual(1, logging.Invocations.Count);
174 Assert.IsNotNull(o);
177 [Test]
178 public void InternalClassWithInternalMethodAndProperty()
180 LogInvocationInterceptor logging = new LogInvocationInterceptor();
181 InternalClassWithInternalMembers o =
182 (InternalClassWithInternalMembers) generator.CreateClassProxy(typeof(InternalClassWithInternalMembers),
183 new Type[0], logging);
184 Assert.IsNotNull(o);
185 o.TestMethod();
186 Assert.AreEqual(1, logging.Invocations.Count);
187 string t = o.TestProperty;
188 Assert.AreEqual(2, logging.Invocations.Count);
191 [Test]
192 public void CanProxyMethodWithOutIntPtrParamter()
194 IFooWithOutIntPtr o =
195 (IFooWithOutIntPtr)generator.CreateInterfaceProxyWithoutTarget(typeof(IFooWithOutIntPtr),
196 new Type[0], new SkipCallingMethodInterceptorWithOutputParams());
197 Assert.IsNotNull(o);
198 IntPtr i;
199 o.Bar(out i);
203 public class SkipCallingMethodInterceptorWithOutputParams : IInterceptor
205 public void Intercept(IInvocation invocation)
207 invocation.Arguments[0] = IntPtr.Zero;
208 invocation.ReturnValue = 5;
212 public interface IWithEvents
214 event EventHandler Foo;
217 public class FakeWithEvents : IWithEvents
219 public virtual event EventHandler Foo;
221 public void MethodToGetAroundFooNotUsedError()
223 Foo(this, EventArgs.Empty);
227 public interface IMulti
229 void OriginalMethod1();
230 void OriginalMethod2();
233 public class MultiClass : IMulti
235 // NON-virtual method
236 public void OriginalMethod1()
240 // VIRTUAL method
241 public virtual void OriginalMethod2()
246 public interface ISpecialMulti : IMulti
248 void ExtraMethod();
251 [Guid("AFCF8240-61AF-4089-BD98-3CD4D719EDBA")]
252 public interface IWithGuid
257 public class WithInternalMethod
259 internal virtual void Foo()
264 public interface IDoubleGeneric<One>
266 object Call<T>(One one, T two);
269 internal interface IFoo
271 int Bar();
274 public class DoubleGenericImpl<One> : IDoubleGeneric<One>
276 public object Call<T>(One one, T two)
278 return two;
282 [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),
283 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
284 public interface IComServiceProvider
286 [return : MarshalAs(UnmanagedType.IUnknown)]
287 object QueryService([In] ref Guid guidService, [In] ref Guid riid);
290 public interface IFactory2
292 T Create<T>() where T : List<T>;
295 public interface IFooWithIntPtr
297 IntPtr Buffer(UInt32 index);
300 public abstract class
301 SomeClassWithProtectedInternalAbstractClass
303 protected internal abstract void Quack();
306 internal class InternalClassWithInternalMembers
308 internal virtual string TestMethod()
310 return "TestMethod";
313 internal virtual string TestProperty
315 get { return "TestProperty"; }
319 public class MakeVirtualCallFromCtor
321 private string name;
323 public MakeVirtualCallFromCtor()
325 Name = "Blah";
328 public virtual string Name
330 get { return name; }
331 set { name = value; }
335 public class IntPtrInterceptor : IInterceptor
337 public void Intercept(IInvocation invocation)
339 invocation.ReturnValue = IntPtr.Zero;
343 public interface IFooWithOutIntPtr
345 int Bar(out IntPtr i);
348 public class Foo : IFooWithOutIntPtr
350 public int Bar(out IntPtr i)
352 i = (IntPtr)Test();
353 return 5;
356 private object Test()
358 return new IntPtr(3);
361 #endif