Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / GenerationHookTestCase.cs
blobb54e74e6ae8a17b21628ac715ea99a05f6d49e52
1 // Copyright 2004-2008 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;
19 using System.Reflection;
20 using Castle.DynamicProxy.Tests.Classes;
21 using Castle.DynamicProxy.Tests.Interceptors;
22 using Castle.DynamicProxy.Tests.InterClasses;
23 using NUnit.Framework;
25 [TestFixture]
26 public class GenerationHookTestCase : BasePEVerifyTestCase
28 [Test]
29 public void HookIsUsedForConcreteClassProxy()
31 LogInvocationInterceptor logger = new LogInvocationInterceptor();
32 LogHook hook = new LogHook(typeof(ServiceClass), true);
34 ProxyGenerationOptions options = new ProxyGenerationOptions(hook);
36 ServiceClass proxy = (ServiceClass) generator.CreateClassProxy(
37 typeof(ServiceClass), options, logger);
39 Assert.IsTrue(hook.Completed);
40 Assert.AreEqual(10, hook.AskedMembers.Count);
41 Assert.AreEqual(2, hook.NonVirtualMembers.Count);
43 proxy.Sum(1, 2);
44 Assert.IsFalse(proxy.Valid);
46 Assert.AreEqual("get_Valid ", logger.LogContents);
49 [Test]
50 public void HookIsUsedForInterfaceProxy()
52 LogInvocationInterceptor logger = new LogInvocationInterceptor();
53 LogHook hook = new LogHook(typeof(IService), false);
55 ProxyGenerationOptions options = new ProxyGenerationOptions(hook);
57 IService proxy = (IService)
58 generator.CreateInterfaceProxyWithTarget(
59 typeof(IService), new ServiceImpl(), options, logger);
61 Assert.IsTrue(hook.Completed);
62 Assert.AreEqual(10, hook.AskedMembers.Count);
63 Assert.AreEqual(0, hook.NonVirtualMembers.Count);
65 Assert.AreEqual(3, proxy.Sum(1, 2));
66 Assert.IsFalse(proxy.Valid);
68 Assert.AreEqual("Sum get_Valid ", logger.LogContents);
71 [Serializable]
72 public class LogHook : IProxyGenerationHook
74 private readonly Type targetTypeToAssert;
75 private readonly bool screeningEnabled;
76 private IList nonVirtualMembers = new ArrayList();
77 private IList askedMembers = new ArrayList();
78 private bool completed;
80 public LogHook(Type targetTypeToAssert, bool screeningEnabled)
82 this.targetTypeToAssert = targetTypeToAssert;
83 this.screeningEnabled = screeningEnabled;
86 public IList NonVirtualMembers
88 get { return nonVirtualMembers; }
91 public IList AskedMembers
93 get { return askedMembers; }
96 public bool Completed
98 get { return completed; }
101 public bool ShouldInterceptMethod(Type type, MethodInfo memberInfo)
103 Assert.AreEqual(targetTypeToAssert, type);
105 askedMembers.Add(memberInfo);
107 if (screeningEnabled && memberInfo.Name.StartsWith("Sum"))
109 return false;
112 return true;
115 public void NonVirtualMemberNotification(Type type, MemberInfo memberInfo)
117 Assert.AreEqual(targetTypeToAssert, type);
119 nonVirtualMembers.Add(memberInfo);
122 public void MethodsInspected()
124 completed = true;