Fixed test issue - PEVerify is now called with a quoted assembly path (to allow for...
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / GenerationHookTestCase.cs
blob8dfc2e19d08f60958704d881479fb601265e777d
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;
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 public class LogHook : IProxyGenerationHook
73 private readonly Type targetTypeToAssert;
74 private readonly bool screeningEnabled;
75 private IList nonVirtualMembers = new ArrayList();
76 private IList askedMembers = new ArrayList();
77 private bool completed;
79 public LogHook(Type targetTypeToAssert, bool screeningEnabled)
81 this.targetTypeToAssert = targetTypeToAssert;
82 this.screeningEnabled = screeningEnabled;
85 public IList NonVirtualMembers
87 get { return nonVirtualMembers; }
90 public IList AskedMembers
92 get { return askedMembers; }
95 public bool Completed
97 get { return completed; }
100 public bool ShouldInterceptMethod(Type type, MethodInfo memberInfo)
102 Assert.AreEqual(targetTypeToAssert, type);
104 askedMembers.Add(memberInfo);
106 if (screeningEnabled && memberInfo.Name.StartsWith("Sum"))
108 return false;
111 return true;
114 public void NonVirtualMemberNotification(Type type, MemberInfo memberInfo)
116 Assert.AreEqual(targetTypeToAssert, type);
118 nonVirtualMembers.Add(memberInfo);
121 public void MethodsInspected()
123 completed = true;