1 // Copyright 2004-2007 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
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
;
26 public class GenericInterfaceProxyTestCase
: BasePEVerifyTestCase
28 private ProxyGenerator generator
;
29 private LogInvocationInterceptor logger
;
34 generator
= new ProxyGenerator();
35 logger
= new LogInvocationInterceptor();
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
);
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
);
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
);
81 // public void ProxyWithGenInterfaceWithGenericTypes()
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>(""));
92 // proxy.Populate<String>(new List<String>());
94 // Assert.AreEqual("Find Find Populate ", logger.LogContents);
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 }
;
109 items
= proxy
.CreateItems();
110 Assert
.IsNotNull(items
);
111 Assert
.AreEqual(3, items
.Length
);
113 Assert
.AreEqual("CopyTo CreateItems ", logger
.LogContents
);
117 public void ProxyWithGenInterfaceWithBase()
119 IGenInterfaceHierarchySpecialization
<int> proxy
=
120 generator
.CreateInterfaceProxyWithTarget
<IGenInterfaceHierarchySpecialization
<int>>(
121 new GenInterfaceHierarchy
<int>(), logger
);
123 Assert
.IsNotNull(proxy
);
127 Assert
.IsNotNull(proxy
.FetchAll());
129 Assert
.AreEqual("Add Add FetchAll ", logger
.LogContents
);
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
);
141 public void TwoGenericsInterfaceWithoutTarget()
143 generator
.CreateInterfaceProxyWithoutTarget(typeof(GenInterface
<object>),
144 new Type
[] { typeof(InterfaceWithExplicitImpl<int>) }
, new LogInvocationInterceptor());
148 public void NonGenInterfaceWithParentGenClassImplementingGenInterface()
150 generator
.CreateInterfaceProxyWithoutTarget(typeof(IUserRepository
),
151 new Type
[] { typeof(InterfaceWithExplicitImpl<int>) }
, new LogInvocationInterceptor());
155 public void WithoutTarget()
157 generator
.CreateInterfaceProxyWithoutTarget(typeof(InterfaceWithExplicitImpl
<int>), new LogInvocationInterceptor());