DP-51 - applying Henry Concei??o patch
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / GenericInterfaceProxyTestCase.cs
blob81545e5e0f9b9eaf95a3bda6d1ab377361a1e37f
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 namespace Castle.DynamicProxy.Tests
17 #if DOTNET2
18 using System;
19 using System.Collections.Generic;
20 using Castle.DynamicProxy.Generators;
21 using Castle.DynamicProxy.Tests.GenInterfaces;
22 using Castle.DynamicProxy.Tests.Interceptors;
23 using NUnit.Framework;
25 [TestFixture]
26 public class GenericInterfaceProxyTestCase : BasePEVerifyTestCase
28 private ProxyGenerator generator;
29 private LogInvocationInterceptor logger;
31 [SetUp]
32 public void Init()
34 generator = new ProxyGenerator();
35 logger = new LogInvocationInterceptor();
38 [Test]
39 public void ProxyWithGenericArgument()
41 GenInterface<int> proxy =
42 generator.CreateInterfaceProxyWithTarget<GenInterface<int>>(
43 new GenInterfaceImpl<int>(), logger);
45 Assert.IsNotNull(proxy);
47 Assert.AreEqual(1, proxy.DoSomething(1));
49 Assert.AreEqual("DoSomething ", logger.LogContents);
52 [Test]
53 public void ProxyWithGenericArgumentAndGenericMethod()
55 GenInterfaceWithGenMethods<int> proxy =
56 generator.CreateInterfaceProxyWithTarget<GenInterfaceWithGenMethods<int>>(
57 new GenInterfaceWithGenMethodsImpl<int>(), logger);
59 Assert.IsNotNull(proxy);
61 proxy.DoSomething<long>(10L, 1);
63 Assert.AreEqual("DoSomething ", logger.LogContents);
66 [Test]
67 public void ProxyWithGenericArgumentAndGenericMethodAndGenericReturn()
69 GenInterfaceWithGenMethodsAndGenReturn<int> proxy =
70 generator.CreateInterfaceProxyWithTarget<GenInterfaceWithGenMethodsAndGenReturn<int>>(
71 new GenInterfaceWithGenMethodsAndGenReturnImpl<int>(), logger);
73 Assert.IsNotNull(proxy);
75 Assert.AreEqual(10L, proxy.DoSomething<long>(10L, 1));
77 Assert.AreEqual("DoSomething ", logger.LogContents);
80 // [Test]
81 // public void ProxyWithGenInterfaceWithGenericTypes()
82 // {
83 // GenInterfaceWithGenericTypes proxy =
84 // generator.CreateInterfaceProxyWithTarget<GenInterfaceWithGenericTypes>(
85 // new GenInterfaceWithGenericTypesImpl(), logger);
87 // Assert.IsNotNull(proxy);
89 // Assert.IsNotNull(proxy.Find(""));
90 // Assert.IsNotNull(proxy.Find<String>(""));
91 //
92 // proxy.Populate<String>(new List<String>());
94 // Assert.AreEqual("Find Find Populate ", logger.LogContents);
96 // }
98 [Test]
99 public void ProxyWithGenInterfaceWithGenericArrays()
101 IGenInterfaceWithGenArray<int> proxy =
102 generator.CreateInterfaceProxyWithTarget<IGenInterfaceWithGenArray<int>>(
103 new GenInterfaceWithGenArray<int>(), logger);
105 Assert.IsNotNull(proxy);
107 int[] items = new int[] { 1,2,3 };
108 proxy.CopyTo(items);
109 items = proxy.CreateItems();
110 Assert.IsNotNull(items);
111 Assert.AreEqual(3, items.Length);
113 Assert.AreEqual("CopyTo CreateItems ", logger.LogContents);
116 [Test]
117 public void ProxyWithGenInterfaceWithBase()
119 IGenInterfaceHierarchySpecialization<int> proxy =
120 generator.CreateInterfaceProxyWithTarget<IGenInterfaceHierarchySpecialization<int>>(
121 new GenInterfaceHierarchy<int>(), logger);
123 Assert.IsNotNull(proxy);
125 proxy.Add();
126 proxy.Add(1);
127 Assert.IsNotNull(proxy.FetchAll());
129 Assert.AreEqual("Add Add FetchAll ", logger.LogContents);
132 [Test]
133 [ExpectedException(typeof(GeneratorException), "DynamicProxy cannot create an interface (with target) proxy for 'InterfaceWithExplicitImpl`1' as the target 'GenExplicitImplementation`1' has an explicit implementation of one of the methods exposed by the interface. The runtime prevents use from invoking the private method on the target. Method Castle.DynamicProxy.Tests.GenInterfaces.InterfaceWithExplicitImpl<T>.GetEnum1")]
134 public void ProxyWithGenExplicitImplementation()
136 generator.CreateInterfaceProxyWithTarget<InterfaceWithExplicitImpl<int>>(
137 new GenExplicitImplementation<int>(), logger);
140 [Test]
141 public void TwoGenericsInterfaceWithoutTarget()
143 generator.CreateInterfaceProxyWithoutTarget(typeof(GenInterface<object>),
144 new Type[] { typeof(InterfaceWithExplicitImpl<int>) }, new LogInvocationInterceptor());
147 [Test]
148 public void NonGenInterfaceWithParentGenClassImplementingGenInterface()
150 generator.CreateInterfaceProxyWithoutTarget(typeof(IUserRepository),
151 new Type[] { typeof(InterfaceWithExplicitImpl<int>) }, new LogInvocationInterceptor());
154 [Test]
155 public void WithoutTarget()
157 generator.CreateInterfaceProxyWithoutTarget(typeof(InterfaceWithExplicitImpl<int>), new LogInvocationInterceptor());
160 #endif