Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / GenericClassProxyTestCase.cs
bloba125016b9580e9dd227fe7f214332842240c6549
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 namespace Castle.DynamicProxy.Tests
17 using System;
18 using System.Collections;
19 using Castle.DynamicProxy.Tests.GenClasses;
20 using Castle.DynamicProxy.Tests.Interceptors;
21 using NUnit.Framework;
23 [TestFixture]
24 public class GenericClassProxyTestCase : BasePEVerifyTestCase
26 private LogInvocationInterceptor logger;
28 public override void Init()
30 base.Init();
31 logger = new LogInvocationInterceptor();
34 [Test]
35 public void ProxyWithGenericArgument()
37 ClassWithGenArgs<int> proxy = generator.CreateClassProxy<ClassWithGenArgs<int>>(logger);
39 Assert.IsNotNull(proxy);
41 proxy.DoSomething();
43 Assert.IsTrue(proxy.Invoked);
45 proxy.AProperty = true;
46 Assert.IsTrue(proxy.AProperty);
48 Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents);
51 [Test]
52 public void ProxyWithGenericArguments()
54 ClassWithGenArgs<int, string> proxy = generator.CreateClassProxy<ClassWithGenArgs<int, string>>(logger);
56 Assert.IsNotNull(proxy);
58 proxy.DoSomething();
60 Assert.IsTrue(proxy.Invoked);
62 proxy.AProperty = true;
63 Assert.IsTrue(proxy.AProperty);
65 Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents);
68 [Test]
69 public void ProxyWithGenericArgumentsWithBaseGenericClass()
71 SubClassWithGenArgs<int, string, int> proxy =
72 generator.CreateClassProxy<SubClassWithGenArgs<int, string, int>>(logger);
74 Assert.IsNotNull(proxy);
76 proxy.DoSomething();
78 Assert.IsTrue(proxy.Invoked);
80 proxy.AProperty = true;
81 Assert.IsTrue(proxy.AProperty);
83 Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents);
86 [Test]
87 public void ProxyWithGenericArgumentsAndArgumentConstraints()
89 GenClassWithConstraints<int> proxy = generator.CreateClassProxy<GenClassWithConstraints<int>>(logger);
91 Assert.IsNotNull(proxy);
93 proxy.DoSomething();
95 Assert.IsTrue(proxy.Invoked);
97 Assert.AreEqual("DoSomething ", logger.LogContents);
100 [Test]
101 public void GenericProxyWithIndexer()
103 object proxy = generator.CreateClassProxy<ClassWithIndexer<string, int>>(logger);
105 Assert.IsNotNull(proxy);
107 ClassWithIndexer<string, int> type = (ClassWithIndexer<string, int>) proxy;
109 type["name"] = 10;
110 Assert.AreEqual(10, type["name"]);
112 Assert.AreEqual("set_Item get_Item ", logger.LogContents);
115 #if !MONO
117 [Test]
118 public void ProxyWithGenericArgumentsAndMethodGenericArguments()
120 GenClassWithGenMethods<ArrayList> proxy =
121 generator.CreateClassProxy<GenClassWithGenMethods<ArrayList>>(logger);
123 Assert.IsNotNull(proxy);
125 proxy.DoSomething("z param");
127 Assert.IsTrue(proxy.Invoked);
128 Assert.AreEqual("z param", proxy.SavedParam);
129 Assert.AreEqual("DoSomething ", logger.LogContents);
132 [Test]
133 public void ProxyWithGenericArgumentsAndMethodGenericArgumentsWithConstraints()
135 GenClassWithGenMethodsConstrained<ArrayList> proxy =
136 generator.CreateClassProxy<GenClassWithGenMethodsConstrained<ArrayList>>(logger);
138 Assert.IsNotNull(proxy);
140 proxy.DoSomething("z param");
142 Assert.IsTrue(proxy.Invoked);
143 Assert.AreEqual("z param", proxy.SavedParam);
144 Assert.AreEqual("DoSomething ", logger.LogContents);
147 [Test]
148 public void ProxyWithGenericArgumentsAndMethodGenericArgumentsWithOneNotDefinedOnType()
150 GenClassWithGenMethods<ArrayList> proxy =
151 generator.CreateClassProxy<GenClassWithGenMethods<ArrayList>>(logger);
153 Assert.IsNotNull(proxy);
155 int value1 = 10;
157 proxy.DoSomethingElse<string>(delegate(int param1) { return param1.ToString(); }, value1);
159 Assert.IsTrue(proxy.Invoked);
160 Assert.AreEqual("10", proxy.SavedParam);
161 Assert.AreEqual("DoSomethingElse ", logger.LogContents);
164 [Test]
165 public void ProxyWithGenericArgumentsAndMethodGenericReturn()
167 GenClassWithGenReturn<ArrayList, Hashtable> proxy =
168 generator.CreateClassProxy<GenClassWithGenReturn<ArrayList, Hashtable>>(logger);
170 Assert.IsNotNull(proxy);
172 object ret1 = proxy.DoSomethingT();
173 object ret2 = proxy.DoSomethingZ();
175 Assert.IsInstanceOfType(typeof(ArrayList), ret1);
176 Assert.IsInstanceOfType(typeof(Hashtable), ret2);
177 Assert.AreEqual("DoSomethingT DoSomethingZ ", logger.LogContents);
180 [Test]
181 public void GenericMethodArgumentsAndTypeGenericArgumentsWithSameName()
183 GenClassNameClash<ArrayList, Hashtable> proxy =
184 generator.CreateClassProxy<GenClassNameClash<ArrayList, Hashtable>>(logger);
186 Assert.IsNotNull(proxy);
188 proxy.DoSomethingT<int>(1);
189 proxy.DoSomethingZ<long>(1L);
190 proxy.DoSomethingTX<int, string>(1, "a");
191 proxy.DoSomethingZX<long, string>(1L, "b");
193 Assert.AreEqual("DoSomethingT DoSomethingZ DoSomethingTX DoSomethingZX ", logger.LogContents);
196 [Test]
197 public void ClassWithGenMethodOnly()
199 OnlyGenMethodsClass proxy =
200 generator.CreateClassProxy<OnlyGenMethodsClass>(logger);
202 Assert.IsNotNull(proxy);
204 proxy.DoSomething(new ArrayList());
206 Assert.IsTrue(proxy.Invoked);
207 Assert.AreEqual("DoSomething ", logger.LogContents);
210 [Test]
211 public void MethodInfoClosedInGenTypeGenMethodRefType()
213 KeepDataInterceptor interceptor = new KeepDataInterceptor();
214 GenClassWithGenMethods<ArrayList> proxy = generator.CreateClassProxy<GenClassWithGenMethods<ArrayList>>(interceptor);
216 proxy.DoSomething(1);
217 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(ArrayList), typeof(int));
218 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
219 interceptor.Invocation.GetConcreteMethodInvocationTarget());
221 proxy.DoSomething(new Hashtable());
222 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(ArrayList),
223 typeof(Hashtable));
224 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
225 interceptor.Invocation.GetConcreteMethodInvocationTarget());
228 [Test]
229 public void MethodInfoClosedInGenTypeGenMethodValueType()
231 KeepDataInterceptor interceptor = new KeepDataInterceptor();
232 GenClassWithGenMethods<int> proxy = generator.CreateClassProxy<GenClassWithGenMethods<int>>(interceptor);
234 proxy.DoSomething(1);
235 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(int), typeof(int));
236 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
237 interceptor.Invocation.GetConcreteMethodInvocationTarget());
239 proxy.DoSomething(new Hashtable());
240 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(int), typeof(Hashtable));
241 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
242 interceptor.Invocation.GetConcreteMethodInvocationTarget());
245 [Test]
246 public void MethodInfoClosedInGenTypeNongenMethodRefTypeRefType()
248 KeepDataInterceptor interceptor = new KeepDataInterceptor();
249 GenClassWithGenReturn<ArrayList, ArrayList> proxy =
250 generator.CreateClassProxy<GenClassWithGenReturn<ArrayList, ArrayList>>(interceptor);
252 proxy.DoSomethingT();
253 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(ArrayList));
254 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
255 interceptor.Invocation.GetConcreteMethodInvocationTarget());
257 proxy.DoSomethingZ();
258 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(ArrayList));
259 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
260 interceptor.Invocation.GetConcreteMethodInvocationTarget());
263 [Test]
264 public void MethodInfoClosedInGenTypeNongenMethodValueTypeValueType()
266 KeepDataInterceptor interceptor = new KeepDataInterceptor();
267 GenClassWithGenReturn<int, int> proxy = generator.CreateClassProxy<GenClassWithGenReturn<int, int>>(interceptor);
269 proxy.DoSomethingT();
270 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(int));
271 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
272 interceptor.Invocation.GetConcreteMethodInvocationTarget());
274 proxy.DoSomethingZ();
275 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(int));
276 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
277 interceptor.Invocation.GetConcreteMethodInvocationTarget());
280 [Test]
281 public void MethodInfoClosedInGenTypeNongenMethodValueTypeRefType()
283 KeepDataInterceptor interceptor = new KeepDataInterceptor();
284 GenClassWithGenReturn<int, ArrayList> proxy =
285 generator.CreateClassProxy<GenClassWithGenReturn<int, ArrayList>>(interceptor);
287 proxy.DoSomethingT();
288 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(int));
289 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
290 interceptor.Invocation.GetConcreteMethodInvocationTarget());
292 proxy.DoSomethingZ();
293 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(ArrayList));
294 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
295 interceptor.Invocation.GetConcreteMethodInvocationTarget());
298 [Test]
299 public void MethodInfoClosedInNongenTypeGenMethod()
301 KeepDataInterceptor interceptor = new KeepDataInterceptor();
302 OnlyGenMethodsClass proxy = generator.CreateClassProxy<OnlyGenMethodsClass>(interceptor);
304 proxy.DoSomething(1);
305 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(int), typeof(int));
306 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
307 interceptor.Invocation.GetConcreteMethodInvocationTarget());
309 proxy.DoSomething(new Hashtable());
310 GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(Hashtable),
311 typeof(Hashtable));
312 Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
313 interceptor.Invocation.GetConcreteMethodInvocationTarget());
316 [Test]
317 [ExpectedException(typeof(ArgumentException))]
318 public void ThrowsWhenProxyingGenericTypeDefNoTarget()
320 KeepDataInterceptor interceptor = new KeepDataInterceptor();
321 object o = generator.CreateClassProxy(typeof(GenClassWithGenReturn<,>), interceptor);
324 #endif