PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Castle.Core.Tests/GenericClassProxyTestCase.cs

https://github.com/dtchepak/Castle.Core
C# | 328 lines | 236 code | 79 blank | 13 comment | 0 complexity | 1ad1f433e7d20faa778b78598427e9cb MD5 | raw file
  1. // Copyright 2004-2010 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.
  14. namespace Castle.DynamicProxy.Tests
  15. {
  16. using System;
  17. using System.Collections.Generic;
  18. using Castle.DynamicProxy.Tests.GenClasses;
  19. using Castle.DynamicProxy.Tests.Interceptors;
  20. using NUnit.Framework;
  21. [TestFixture]
  22. public class GenericClassProxyTestCase : BasePEVerifyTestCase
  23. {
  24. private LogInvocationInterceptor logger;
  25. public override void Init()
  26. {
  27. base.Init();
  28. logger = new LogInvocationInterceptor();
  29. }
  30. [Test]
  31. public void ProxyWithGenericArgument()
  32. {
  33. ClassWithGenArgs<int> proxy = generator.CreateClassProxy<ClassWithGenArgs<int>>(logger);
  34. Assert.IsNotNull(proxy);
  35. proxy.DoSomething();
  36. Assert.IsTrue(proxy.Invoked);
  37. proxy.AProperty = true;
  38. Assert.IsTrue(proxy.AProperty);
  39. Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents);
  40. }
  41. [Test]
  42. public void ProxyWithGenericArguments()
  43. {
  44. ClassWithGenArgs<int, string> proxy = generator.CreateClassProxy<ClassWithGenArgs<int, string>>(logger);
  45. Assert.IsNotNull(proxy);
  46. proxy.DoSomething();
  47. Assert.IsTrue(proxy.Invoked);
  48. proxy.AProperty = true;
  49. Assert.IsTrue(proxy.AProperty);
  50. Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents);
  51. }
  52. [Test]
  53. public void ProxyWithGenericArgumentsWithBaseGenericClass()
  54. {
  55. SubClassWithGenArgs<int, string, int> proxy =
  56. generator.CreateClassProxy<SubClassWithGenArgs<int, string, int>>(logger);
  57. Assert.IsNotNull(proxy);
  58. proxy.DoSomething();
  59. Assert.IsTrue(proxy.Invoked);
  60. proxy.AProperty = true;
  61. Assert.IsTrue(proxy.AProperty);
  62. Assert.AreEqual("DoSomething set_AProperty get_AProperty ", logger.LogContents);
  63. }
  64. [Test]
  65. public void ProxyWithGenericArgumentsAndArgumentConstraints()
  66. {
  67. GenClassWithConstraints<int> proxy = generator.CreateClassProxy<GenClassWithConstraints<int>>(logger);
  68. Assert.IsNotNull(proxy);
  69. proxy.DoSomething();
  70. Assert.IsTrue(proxy.Invoked);
  71. Assert.AreEqual("DoSomething ", logger.LogContents);
  72. }
  73. [Test]
  74. public void GenericProxyWithIndexer()
  75. {
  76. object proxy = generator.CreateClassProxy<ClassWithIndexer<string, int>>(logger);
  77. Assert.IsNotNull(proxy);
  78. ClassWithIndexer<string, int> type = (ClassWithIndexer<string, int>) proxy;
  79. type["name"] = 10;
  80. Assert.AreEqual(10, type["name"]);
  81. Assert.AreEqual("set_Item get_Item ", logger.LogContents);
  82. }
  83. [Test]
  84. public void ProxyWithMethodReturningGenericOfGenericOfT()
  85. {
  86. var proxy = generator.CreateClassProxy<ClassWithMethodWithReturnArrayOfListOfT>();
  87. proxy.GenericMethodReturnsListArray<string>();
  88. proxy.GenericMethodReturnsGenericOfGenericType<int>();
  89. }
  90. #if !MONO
  91. [Test]
  92. public void ProxyWithGenericArgumentsAndMethodGenericArguments()
  93. {
  94. GenClassWithGenMethods<List<object>> proxy =
  95. generator.CreateClassProxy<GenClassWithGenMethods<List<object>>>(logger);
  96. Assert.IsNotNull(proxy);
  97. proxy.DoSomething("z param");
  98. Assert.IsTrue(proxy.Invoked);
  99. Assert.AreEqual("z param", proxy.SavedParam);
  100. Assert.AreEqual("DoSomething ", logger.LogContents);
  101. }
  102. [Test]
  103. public void ProxyWithGenericArgumentsAndMethodGenericArgumentsWithConstraints()
  104. {
  105. GenClassWithGenMethodsConstrained<List<object>> proxy =
  106. generator.CreateClassProxy<GenClassWithGenMethodsConstrained<List<object>>>(logger);
  107. Assert.IsNotNull(proxy);
  108. proxy.DoSomething("z param");
  109. Assert.IsTrue(proxy.Invoked);
  110. Assert.AreEqual("z param", proxy.SavedParam);
  111. Assert.AreEqual("DoSomething ", logger.LogContents);
  112. }
  113. [Test]
  114. public void ProxyWithGenericArgumentsAndMethodGenericArgumentsWithOneNotDefinedOnType()
  115. {
  116. GenClassWithGenMethods<List<object>> proxy =
  117. generator.CreateClassProxy<GenClassWithGenMethods<List<object>>>(logger);
  118. Assert.IsNotNull(proxy);
  119. int value1 = 10;
  120. proxy.DoSomethingElse<string>(delegate(int param1) { return param1.ToString(); }, value1);
  121. Assert.IsTrue(proxy.Invoked);
  122. Assert.AreEqual("10", proxy.SavedParam);
  123. Assert.AreEqual("DoSomethingElse ", logger.LogContents);
  124. }
  125. [Test]
  126. public void ProxyWithGenericArgumentsAndMethodGenericReturn()
  127. {
  128. GenClassWithGenReturn<List<object>, List<object>> proxy =
  129. generator.CreateClassProxy<GenClassWithGenReturn<List<object>, List<object>>>(logger);
  130. Assert.IsNotNull(proxy);
  131. object ret1 = proxy.DoSomethingT();
  132. object ret2 = proxy.DoSomethingZ();
  133. Assert.IsInstanceOf(typeof (List<object>), ret1);
  134. Assert.IsInstanceOf(typeof (List<object>), ret2);
  135. Assert.AreEqual("DoSomethingT DoSomethingZ ", logger.LogContents);
  136. }
  137. [Test]
  138. public void GenericMethodArgumentsAndTypeGenericArgumentsWithSameName()
  139. {
  140. GenClassNameClash<List<object>, List<object>> proxy =
  141. generator.CreateClassProxy<GenClassNameClash<List<object>, List<object>>>(logger);
  142. Assert.IsNotNull(proxy);
  143. proxy.DoSomethingT<int>(1);
  144. proxy.DoSomethingZ<long>(1L);
  145. proxy.DoSomethingTX<int, string>(1, "a");
  146. proxy.DoSomethingZX<long, string>(1L, "b");
  147. Assert.AreEqual("DoSomethingT DoSomethingZ DoSomethingTX DoSomethingZX ", logger.LogContents);
  148. }
  149. [Test]
  150. public void ClassWithGenMethodOnly()
  151. {
  152. OnlyGenMethodsClass proxy =
  153. generator.CreateClassProxy<OnlyGenMethodsClass>(logger);
  154. Assert.IsNotNull(proxy);
  155. proxy.DoSomething(new List<object>());
  156. Assert.IsTrue(proxy.Invoked);
  157. Assert.AreEqual("DoSomething ", logger.LogContents);
  158. }
  159. [Test]
  160. public void MethodInfoClosedInGenTypeGenMethodRefType()
  161. {
  162. KeepDataInterceptor interceptor = new KeepDataInterceptor();
  163. GenClassWithGenMethods<List<object>> proxy = generator.CreateClassProxy<GenClassWithGenMethods<List<object>>>(interceptor);
  164. proxy.DoSomething(1);
  165. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (List<object>),
  166. typeof (int));
  167. proxy.DoSomething(new List<object>());
  168. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (List<object>),
  169. typeof (List<object>));
  170. }
  171. [Test]
  172. public void MethodInfoClosedInGenTypeGenMethodValueType()
  173. {
  174. KeepDataInterceptor interceptor = new KeepDataInterceptor();
  175. GenClassWithGenMethods<int> proxy = generator.CreateClassProxy<GenClassWithGenMethods<int>>(interceptor);
  176. proxy.DoSomething(1);
  177. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int), typeof (int));
  178. proxy.DoSomething(new List<object>());
  179. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int),
  180. typeof (List<object>));
  181. }
  182. [Test]
  183. public void MethodInfoClosedInGenTypeNongenMethodRefTypeRefType()
  184. {
  185. KeepDataInterceptor interceptor = new KeepDataInterceptor();
  186. GenClassWithGenReturn<List<object>, List<object>> proxy =
  187. generator.CreateClassProxy<GenClassWithGenReturn<List<object>, List<object>>>(interceptor);
  188. proxy.DoSomethingT();
  189. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(List<object>));
  190. proxy.DoSomethingZ();
  191. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof(List<object>));
  192. }
  193. [Test]
  194. public void MethodInfoClosedInGenTypeNongenMethodValueTypeValueType()
  195. {
  196. KeepDataInterceptor interceptor = new KeepDataInterceptor();
  197. GenClassWithGenReturn<int, int> proxy = generator.CreateClassProxy<GenClassWithGenReturn<int, int>>(interceptor);
  198. proxy.DoSomethingT();
  199. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int));
  200. Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
  201. interceptor.Invocation.GetConcreteMethodInvocationTarget().GetBaseDefinition());
  202. proxy.DoSomethingZ();
  203. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int));
  204. Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
  205. interceptor.Invocation.GetConcreteMethodInvocationTarget().GetBaseDefinition());
  206. }
  207. [Test]
  208. public void MethodInfoClosedInGenTypeNongenMethodValueTypeRefType()
  209. {
  210. KeepDataInterceptor interceptor = new KeepDataInterceptor();
  211. GenClassWithGenReturn<int, List<object>> proxy =
  212. generator.CreateClassProxy<GenClassWithGenReturn<int, List<object>>>(interceptor);
  213. proxy.DoSomethingT();
  214. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int));
  215. Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
  216. interceptor.Invocation.GetConcreteMethodInvocationTarget().GetBaseDefinition());
  217. proxy.DoSomethingZ();
  218. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (List<object>));
  219. Assert.AreEqual(interceptor.Invocation.GetConcreteMethod(),
  220. interceptor.Invocation.GetConcreteMethodInvocationTarget().GetBaseDefinition());
  221. }
  222. [Test]
  223. public void MethodInfoClosedInNongenTypeGenMethod()
  224. {
  225. KeepDataInterceptor interceptor = new KeepDataInterceptor();
  226. OnlyGenMethodsClass proxy = generator.CreateClassProxy<OnlyGenMethodsClass>(interceptor);
  227. proxy.DoSomething(1);
  228. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (int), typeof (int));
  229. proxy.DoSomething(new List<object>());
  230. GenericTestUtility.CheckMethodInfoIsClosed(interceptor.Invocation.GetConcreteMethod(), typeof (List<object>),
  231. typeof (List<object>));
  232. }
  233. [Test]
  234. public void TypeWithGenericMethodHavingArgumentBeingGenericArrayOfT()
  235. {
  236. var proxy = generator.CreateClassProxy<MethodWithArgumentBeingArrayOfGenericTypeOfT>();
  237. Assert.IsNotNull(proxy);
  238. proxy.Method(new Action<string>[0]);
  239. }
  240. [Test]
  241. [ExpectedException(typeof (ArgumentException))]
  242. public void ThrowsWhenProxyingGenericTypeDefNoTarget()
  243. {
  244. KeepDataInterceptor interceptor = new KeepDataInterceptor();
  245. object o = generator.CreateClassProxy(typeof (GenClassWithGenReturn<,>), interceptor);
  246. }
  247. #endif
  248. }
  249. }