1 // Copyright 2004-2007 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
;
20 "DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7"
24 namespace Castle
.DynamicProxy
.Tests
28 using System
.Runtime
.InteropServices
;
29 using Castle
.Core
.Interceptor
;
30 using Castle
.DynamicProxy
.Tests
.Interceptors
;
31 using NUnit
.Framework
;
32 using System
.Collections
.Generic
;
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());
137 public void ProxyInternalMethod()
139 LogInvocationInterceptor logging
= new LogInvocationInterceptor();
140 WithInternalMethod o
= (WithInternalMethod
)generator
.CreateClassProxy(typeof(WithInternalMethod
),
141 new Type
[0], logging
);
143 Assert
.AreEqual("Foo ", logging
.LogContents
);
147 public void ProxyingProtectedInternalAbstractMethod()
149 LogInvocationInterceptor logging
= new LogInvocationInterceptor();
150 SomeClassWithProtectedInternalAbstractClass o
= (SomeClassWithProtectedInternalAbstractClass
)generator
.CreateClassProxy(typeof(SomeClassWithProtectedInternalAbstractClass
),
151 new Type
[0], logging
);
156 public void VirtualMethodCallsFromTheConstructor()
158 LogInvocationInterceptor logging
= new LogInvocationInterceptor();
159 MakeVirtualCallFromCtor o
= (MakeVirtualCallFromCtor
)generator
.CreateClassProxy(typeof(MakeVirtualCallFromCtor
),
160 new Type
[0], logging
);
161 Assert
.AreEqual(1, logging
.Invocations
.Count
);
166 public void InternalClassWithInternalMethodAndProperty()
168 LogInvocationInterceptor logging
= new LogInvocationInterceptor();
169 InternalClassWithInternalMembers o
= (InternalClassWithInternalMembers
)generator
.CreateClassProxy(typeof(InternalClassWithInternalMembers
),
170 new Type
[0], logging
);
173 Assert
.AreEqual(1, logging
.Invocations
.Count
);
174 string t
= o
.TestProperty
;
175 Assert
.AreEqual(2, logging
.Invocations
.Count
);
178 public interface IWithEvents
180 event EventHandler Foo
;
183 public class FakeWithEvents
: IWithEvents
185 public virtual event EventHandler Foo
;
187 public void MethodToGetAroundFooNotUsedError()
189 Foo(this, EventArgs
.Empty
);
193 public interface IMulti
195 void OriginalMethod1();
196 void OriginalMethod2();
199 public class MultiClass
: IMulti
201 // NON-virtual method
202 public void OriginalMethod1()
207 public virtual void OriginalMethod2()
212 public interface ISpecialMulti
: IMulti
217 [Guid("AFCF8240-61AF-4089-BD98-3CD4D719EDBA")]
218 public interface IWithGuid
223 public class WithInternalMethod
225 internal virtual void Foo()
230 public interface IDoubleGeneric
<One
>
232 object Call
<T
>(One one
, T two
);
235 internal interface IFoo
240 public class DoubleGenericImpl
<One
> : IDoubleGeneric
<One
>
242 public object Call
<T
>(One one
, T two
)
248 [ComImport
, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),
249 InterfaceType(ComInterfaceType
.InterfaceIsIUnknown
)]
250 public interface IComServiceProvider
252 [return: MarshalAs(UnmanagedType
.IUnknown
)]
253 object QueryService([In
] ref Guid guidService
, [In
] ref Guid riid
);
256 public interface IFactory2
258 T Create
<T
>() where T
: List
<T
>;
261 public abstract class
262 SomeClassWithProtectedInternalAbstractClass
264 protected internal abstract void Quack();
267 internal class InternalClassWithInternalMembers
269 internal virtual string TestMethod()
274 internal virtual string TestProperty
276 get { return "TestProperty"; }
280 public class MakeVirtualCallFromCtor
284 public MakeVirtualCallFromCtor()
289 public virtual string Name
292 set { name = value; }