Test case or virtual method call from ctor.
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / RhinoMocksTestCase.cs
blob660fc84c56791c2e2affea48e1500a8738f586ba
1 // Copyright 2004-2007 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 #if DOTNET2
18 [assembly:
19 InternalsVisibleTo(
20 "DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7"
22 #endif
24 namespace Castle.DynamicProxy.Tests
26 using System;
27 using System.Data;
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;
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);
136 [Test]
137 public void ProxyInternalMethod()
139 LogInvocationInterceptor logging = new LogInvocationInterceptor();
140 WithInternalMethod o = (WithInternalMethod)generator.CreateClassProxy(typeof(WithInternalMethod),
141 new Type[0], logging);
142 o.Foo();
143 Assert.AreEqual("Foo ", logging.LogContents);
146 [Test]
147 public void ProxyingProtectedInternalAbstractMethod()
149 LogInvocationInterceptor logging = new LogInvocationInterceptor();
150 SomeClassWithProtectedInternalAbstractClass o = (SomeClassWithProtectedInternalAbstractClass)generator.CreateClassProxy(typeof(SomeClassWithProtectedInternalAbstractClass),
151 new Type[0], logging);
152 Assert.IsNotNull(o);
155 [Test]
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);
162 Assert.IsNotNull(o);
165 [Test]
166 public void InternalClassWithInternalMethodAndProperty()
168 LogInvocationInterceptor logging = new LogInvocationInterceptor();
169 InternalClassWithInternalMembers o = (InternalClassWithInternalMembers)generator.CreateClassProxy(typeof(InternalClassWithInternalMembers),
170 new Type[0], logging);
171 Assert.IsNotNull(o);
172 o.TestMethod();
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()
206 // VIRTUAL method
207 public virtual void OriginalMethod2()
212 public interface ISpecialMulti : IMulti
214 void ExtraMethod();
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
237 int Bar();
240 public class DoubleGenericImpl<One> : IDoubleGeneric<One>
242 public object Call<T>(One one, T two)
244 return 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()
271 return "TestMethod";
274 internal virtual string TestProperty
276 get { return "TestProperty"; }
280 public class MakeVirtualCallFromCtor
282 private string name;
284 public MakeVirtualCallFromCtor()
286 Name = "Blah";
289 public virtual string Name
291 get { return name; }
292 set { name = value; }