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
.Collections
.Generic
;
20 using Castle
.Core
.Interceptor
;
21 using Castle
.DynamicProxy
.Tests
.Interceptors
;
22 using Castle
.DynamicProxy
.Tests
.InterClasses
;
23 using NUnit
.Framework
;
26 public class BasicInterfaceProxyWithoutTargetTestCase
: BasePEVerifyTestCase
29 [ExpectedException(typeof(NotImplementedException
),
30 "This is a DynamicProxy2 error: the interceptor attempted to 'Proceed' for a method without a target, for example, an interface method or an abstract method"
32 public void BasicInterfaceProxyWithValidTarget_ThrowsIfInterceptorCallsProceed()
34 IService service
= (IService
)
35 generator
.CreateInterfaceProxyWithoutTarget(
36 typeof(IService
), new StandardInterceptor());
42 public void CanReplaceReturnValueOfInterfaceMethod()
44 IService service
= (IService
)
45 generator
.CreateInterfaceProxyWithoutTarget(
46 typeof(IService
), new ReturnThreeInterceptor());
48 int result
= service
.Sum(2, 2);
49 Assert
.AreEqual(3, result
);
53 [ExpectedException(typeof(DBConcurrencyException
), "Because I feel like it")]
54 public void CanThrowExceptionFromInterceptorOfInterfaceMethod()
56 IService service
= (IService
)
57 generator
.CreateInterfaceProxyWithoutTarget(
58 typeof(IService
), new ThrowingInterceptor());
64 public void ProxyWithGenericTypeThatInheritFromGenericType()
66 // Only PEVerify is enough
67 generator
.CreateInterfaceProxyWithoutTarget
<IList
<int>>(new ThrowingInterceptor());
71 public void ProducesInvocationsThatCantChangeTarget()
73 IService service
= (IService
)
74 generator
.CreateInterfaceProxyWithoutTarget(
75 typeof(IService
), new AssertCannotChangeTargetInterceptor(), new ReturnThreeInterceptor());
77 int result
= service
.Sum(2, 2);
78 Assert
.AreEqual(3, result
);
81 public class ReturnThreeInterceptor
: IInterceptor
83 public void Intercept(IInvocation invocation
)
85 invocation
.ReturnValue
= 3;
89 public class ThrowingInterceptor
: IInterceptor
91 public void Intercept(IInvocation invocation
)
93 throw new DBConcurrencyException("Because I feel like it");