1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
;
19 "DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7"
22 namespace Castle
.DynamicProxy
.Tests
25 using System
.Collections
.Generic
;
27 using System
.Runtime
.InteropServices
;
28 using Castle
.Core
.Interceptor
;
29 using Castle
.DynamicProxy
.Tests
.Interceptors
;
30 using NUnit
.Framework
;
35 public class RhinoMocksTestCase
: BasePEVerifyTestCase
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]);
49 public void GenericClassWithGenericMethodWitoutTarget()
51 BasicInterfaceProxyWithoutTargetTestCase
.ReturnThreeInterceptor interceptor
=
52 new BasicInterfaceProxyWithoutTargetTestCase
.ReturnThreeInterceptor();
53 IDoubleGeneric
<int> proxy
=
54 (IDoubleGeneric
<int>) generator
.CreateInterfaceProxyWithoutTarget(typeof(IDoubleGeneric
<int>),
56 object o
= proxy
.Call
<string>(1, "");
57 Assert
.AreEqual(3, o
);
61 public void UsingEvents_Interface()
63 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
65 IWithEvents proxy
= (IWithEvents
) generator
.CreateInterfaceProxyWithTarget(typeof(IWithEvents
),
69 Assert
.IsNotNull(proxy
);
74 Assert
.AreEqual(2, logger
.Invocations
.Count
);
78 public void UsingEvents_Class()
80 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
81 FakeWithEvents proxy
= (FakeWithEvents
) generator
.CreateClassProxy(
82 typeof(FakeWithEvents
),
83 ProxyGenerationOptions
.Default
,
86 Assert
.IsNotNull(proxy
);
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)}
);
101 public void ProxyingInterfaceWithGuid()
103 object o
= generator
.CreateInterfaceProxyWithoutTarget(typeof(IWithGuid
), new StandardInterceptor());
108 public void ProxyingInternalInterface()
110 object o
= generator
.CreateInterfaceProxyWithoutTarget(typeof(IFoo
), new StandardInterceptor());
115 public void CanProxyDataSet()
117 generator
.CreateClassProxy(typeof(DataSet
), new Type
[0], new StandardInterceptor());
121 public void ProxyingComInteraces()
124 .CreateInterfaceProxyWithoutTarget(typeof(IComServiceProvider
), new StandardInterceptor());
129 public void ProxyingGenericClassWithGenericClassConstraint()
132 .CreateInterfaceProxyWithoutTarget(typeof(IFactory2
), new StandardInterceptor());
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
);
147 public void ProxyInternalMethod()
149 LogInvocationInterceptor logging
= new LogInvocationInterceptor();
150 WithInternalMethod o
= (WithInternalMethod
) generator
.CreateClassProxy(typeof(WithInternalMethod
),
151 new Type
[0], logging
);
153 Assert
.AreEqual("Foo ", logging
.LogContents
);
157 public void ProxyingProtectedInternalAbstractMethod()
159 LogInvocationInterceptor logging
= new LogInvocationInterceptor();
160 SomeClassWithProtectedInternalAbstractClass o
=
161 (SomeClassWithProtectedInternalAbstractClass
)
162 generator
.CreateClassProxy(typeof(SomeClassWithProtectedInternalAbstractClass
),
163 new Type
[0], logging
);
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
);
178 public void InternalClassWithInternalMethodAndProperty()
180 LogInvocationInterceptor logging
= new LogInvocationInterceptor();
181 InternalClassWithInternalMembers o
=
182 (InternalClassWithInternalMembers
) generator
.CreateClassProxy(typeof(InternalClassWithInternalMembers
),
183 new Type
[0], logging
);
186 Assert
.AreEqual(1, logging
.Invocations
.Count
);
187 string t
= o
.TestProperty
;
188 Assert
.AreEqual(2, logging
.Invocations
.Count
);
192 public void CanProxyMethodWithOutIntPtrParamter()
194 IFooWithOutIntPtr o
=
195 (IFooWithOutIntPtr
)generator
.CreateInterfaceProxyWithoutTarget(typeof(IFooWithOutIntPtr
),
196 new Type
[0], new SkipCallingMethodInterceptorWithOutputParams());
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()
241 public virtual void OriginalMethod2()
246 public interface ISpecialMulti
: IMulti
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
274 public class DoubleGenericImpl
<One
> : IDoubleGeneric
<One
>
276 public object Call
<T
>(One one
, T 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()
313 internal virtual string TestProperty
315 get { return "TestProperty"; }
319 public class MakeVirtualCallFromCtor
323 public MakeVirtualCallFromCtor()
328 public virtual string 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
)
356 private object Test()
358 return new IntPtr(3);