Fixed test issue - PEVerify is now called with a quoted assembly path (to allow for...
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / BasicInterfaceProxyWithoutTargetTestCase.cs
blob76e4dcd66329365d2cc67f799016d1bfdd6d73ca
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 using System;
18 using System.Collections.Generic;
19 using System.Data;
20 using Castle.Core.Interceptor;
21 using Castle.DynamicProxy.Tests.Interceptors;
22 using Castle.DynamicProxy.Tests.InterClasses;
23 using NUnit.Framework;
25 [TestFixture]
26 public class BasicInterfaceProxyWithoutTargetTestCase : BasePEVerifyTestCase
28 [Test]
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());
38 service.Sum(1, 2);
41 [Test]
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);
52 [Test]
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());
60 service.Sum(2, 2);
63 [Test]
64 public void ProxyWithGenericTypeThatInheritFromGenericType()
66 // Only PEVerify is enough
67 generator.CreateInterfaceProxyWithoutTarget<IList<int>>(new ThrowingInterceptor());
70 [Test]
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");