1 // Copyright 2004-2008 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
;
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
;
26 public class GenerationHookTestCase
: BasePEVerifyTestCase
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
);
44 Assert
.IsFalse(proxy
.Valid
);
46 Assert
.AreEqual("get_Valid ", logger
.LogContents
);
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
);
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; }
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"))
115 public void NonVirtualMemberNotification(Type type
, MemberInfo memberInfo
)
117 Assert
.AreEqual(targetTypeToAssert
, type
);
119 nonVirtualMembers
.Add(memberInfo
);
122 public void MethodsInspected()