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 namespace Castle
.DynamicProxy
.Tests
18 using System
.Collections
;
19 using Castle
.DynamicProxy
.Tests
.GenClasses
;
20 using Castle
.DynamicProxy
.Tests
.Interceptors
;
21 using NUnit
.Framework
;
24 public class GenericClassProxyTestCase
: BasePEVerifyTestCase
26 private LogInvocationInterceptor logger
;
28 public override void Init()
31 logger
= new LogInvocationInterceptor();
35 public void ProxyWithGenericArgument()
37 ClassWithGenArgs
<int> proxy
= generator
.CreateClassProxy
<ClassWithGenArgs
<int>>(logger
);
39 Assert
.IsNotNull(proxy
);
43 Assert
.IsTrue(proxy
.Invoked
);
45 proxy
.AProperty
= true;
46 Assert
.IsTrue(proxy
.AProperty
);
48 Assert
.AreEqual("DoSomething set_AProperty get_AProperty ", logger
.LogContents
);
52 public void ProxyWithGenericArguments()
54 ClassWithGenArgs
<int, string> proxy
= generator
.CreateClassProxy
<ClassWithGenArgs
<int, string>>(logger
);
56 Assert
.IsNotNull(proxy
);
60 Assert
.IsTrue(proxy
.Invoked
);
62 proxy
.AProperty
= true;
63 Assert
.IsTrue(proxy
.AProperty
);
65 Assert
.AreEqual("DoSomething set_AProperty get_AProperty ", logger
.LogContents
);
69 public void ProxyWithGenericArgumentsWithBaseGenericClass()
71 SubClassWithGenArgs
<int, string, int> proxy
=
72 generator
.CreateClassProxy
<SubClassWithGenArgs
<int, string, int>>(logger
);
74 Assert
.IsNotNull(proxy
);
78 Assert
.IsTrue(proxy
.Invoked
);
80 proxy
.AProperty
= true;
81 Assert
.IsTrue(proxy
.AProperty
);
83 Assert
.AreEqual("DoSomething set_AProperty get_AProperty ", logger
.LogContents
);
87 public void ProxyWithGenericArgumentsAndArgumentConstraints()
89 GenClassWithConstraints
<int> proxy
= generator
.CreateClassProxy
<GenClassWithConstraints
<int>>(logger
);
91 Assert
.IsNotNull(proxy
);
95 Assert
.IsTrue(proxy
.Invoked
);
97 Assert
.AreEqual("DoSomething ", logger
.LogContents
);
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
;
110 Assert
.AreEqual(10, type
["name"]);
112 Assert
.AreEqual("set_Item get_Item ", logger
.LogContents
);
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
);
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
);
148 public void ProxyWithGenericArgumentsAndMethodGenericArgumentsWithOneNotDefinedOnType()
150 GenClassWithGenMethods
<ArrayList
> proxy
=
151 generator
.CreateClassProxy
<GenClassWithGenMethods
<ArrayList
>>(logger
);
153 Assert
.IsNotNull(proxy
);
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
);
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
);
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
);
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
);
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
),
224 Assert
.AreEqual(interceptor
.Invocation
.GetConcreteMethod(),
225 interceptor
.Invocation
.GetConcreteMethodInvocationTarget());
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());
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());
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());
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());
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
),
312 Assert
.AreEqual(interceptor
.Invocation
.GetConcreteMethod(),
313 interceptor
.Invocation
.GetConcreteMethodInvocationTarget());
317 [ExpectedException(typeof(ArgumentException
))]
318 public void ThrowsWhenProxyingGenericTypeDefNoTarget()
320 KeepDataInterceptor interceptor
= new KeepDataInterceptor();
321 object o
= generator
.CreateClassProxy(typeof(GenClassWithGenReturn
<,>), interceptor
);