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
18 using System
.Reflection
;
19 using Castle
.Core
.Interceptor
;
20 using Castle
.DynamicProxy
.Tests
.BugsReported
;
21 using Castle
.DynamicProxy
.Tests
.Interceptors
;
22 using Castle
.DynamicProxy
.Tests
.InterClasses
;
23 using NUnit
.Framework
;
26 public class BasicInterfaceProxyTestCase
: BasePEVerifyTestCase
29 public void BasicInterfaceProxyWithValidTarget()
31 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
33 IService service
= (IService
)
34 generator
.CreateInterfaceProxyWithTarget(
35 typeof(IService
), new ServiceImpl(), logger
);
37 Assert
.AreEqual(3, service
.Sum(1, 2));
39 Assert
.AreEqual("Sum ", logger
.LogContents
);
45 IService service
= (IService
)
46 generator
.CreateInterfaceProxyWithTarget(
47 typeof(IService
), new ServiceImpl(), new StandardInterceptor());
49 generator
.CreateInterfaceProxyWithTarget(
50 typeof(IService
), new ServiceImpl(), new StandardInterceptor());
52 generator
.CreateInterfaceProxyWithTarget(
53 typeof(IService
), new ServiceImpl(), new StandardInterceptor());
55 generator
.CreateInterfaceProxyWithTarget(
56 typeof(IService
), new ServiceImpl(), new StandardInterceptor());
60 public void BasicInterfaceProxyWithValidTarget2()
62 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
64 IService2 service
= (IService2
)
65 generator
.CreateInterfaceProxyWithTarget(
66 typeof(IService2
), new Service2(), logger
);
68 service
.DoOperation2();
70 Assert
.AreEqual("DoOperation2 ", logger
.LogContents
);
74 public void InterfaceInheritance()
76 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
78 IService service
= (IExtendedService
)
79 generator
.CreateInterfaceProxyWithTarget(
80 typeof(IExtendedService
), new ServiceImpl(), logger
);
82 Assert
.AreEqual(3, service
.Sum(1, 2));
84 Assert
.AreEqual("Sum ", logger
.LogContents
);
90 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
92 InterfaceWithIndexer service
= (InterfaceWithIndexer
)
93 generator
.CreateInterfaceProxyWithTarget(
94 typeof(InterfaceWithIndexer
), new ClassWithIndexer(), logger
);
96 Assert
.AreEqual(1, service
[1]);
98 Assert
.AreEqual("get_Item ", logger
.LogContents
);
102 public void ProxyTypeWithMultiDimentionalArrayAsParameter()
104 generator
.CreateInterfaceProxyWithTarget
<IClassWithMultiDimentionalArray
>(
105 new ClassWithMultiDimentionalArray(),
106 new LogInvocationInterceptor());
110 [ExpectedException(typeof(ArgumentException
))]
111 public void CantCreateInterfaceTargetedProxyWithoutInterface()
113 IService2 service
= (IService2
)
114 generator
.CreateInterfaceProxyWithTargetInterface(
115 typeof(Service2
), new Service2());
119 public void InterfaceTargetTypeProducesInvocationsThatCanChangeTarget()
121 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
122 AssertCanChangeTargetInterceptor invocationChecker
= new AssertCanChangeTargetInterceptor();
124 IService2 service
= (IService2
)
125 generator
.CreateInterfaceProxyWithTargetInterface(
126 typeof(IService2
), new Service2(), invocationChecker
, logger
);
128 service
.DoOperation2();
130 Assert
.AreEqual("DoOperation2 ", logger
.LogContents
);
134 public void ChangingInvocationTargetSucceeds()
136 LogInvocationInterceptor logger
= new LogInvocationInterceptor();
138 IService service
= (IService
)
139 generator
.CreateInterfaceProxyWithTargetInterface(
140 typeof(IService
), new AlwaysThrowsServiceImpl(), new ChangeTargetInterceptor(new ServiceImpl()),
143 Assert
.AreEqual(20, service
.Sum(10, 10));
148 /// See http://support.castleproject.org/browse/DYNPROXY-43
151 public void MethodParamNamesAreReplicated()
153 // Try with interface proxy (with target)
154 IMyInterface i
= generator
.CreateInterfaceProxyWithTarget(typeof(IMyInterface
), new MyClass(),
155 new StandardInterceptor()) as IMyInterface
;
157 ParameterInfo
[] methodParams
= GetMyTestMethodParams(i
.GetType());
158 Assert
.AreEqual("myParam", methodParams
[0].Name
);
161 private ParameterInfo
[] GetMyTestMethodParams(Type type
)
163 MethodInfo methodInfo
= type
.GetMethod("MyTestMethod");
164 return methodInfo
.GetParameters();