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
.Test
18 using NUnit
.Framework
;
19 using Castle
.DynamicProxy
.Test
.ClassInterfaces
;
22 public class MethodInvocationTargetTestCase
25 public void AttributesOnTargetClasses()
27 ProxyGenerator _generator
= new ProxyGenerator();
29 AttributeCheckerInterceptor interceptor
= new AttributeCheckerInterceptor();
31 object proxy
= _generator
.CreateClassProxy(typeof (MyInterfaceImpl
), interceptor
);
33 IMyInterface inter
= (IMyInterface
) proxy
;
35 Assert
.AreEqual(45, inter
.Calc(20, 25));
36 Assert
.IsTrue(interceptor
.LastTargethasAttribute
);
37 Assert
.IsTrue(interceptor
.LastMethodHasAttribute
);
39 Assert
.AreEqual(48, inter
.Calc(20, 25, 1, 2));
40 Assert
.IsTrue(interceptor
.LastMethodHasAttribute
);
41 Assert
.IsTrue(interceptor
.LastTargethasAttribute
);
43 inter
.Name
= "hammett";
44 Assert
.IsFalse(interceptor
.LastMethodHasAttribute
);
45 Assert
.IsTrue(interceptor
.LastTargethasAttribute
);
49 public void AttributesOnTargetClassesWithInterfaceProxy()
51 ProxyGenerator _generator
= new ProxyGenerator();
53 AttributeCheckerInterceptor interceptor
= new AttributeCheckerInterceptor();
55 object proxy
= _generator
.CreateProxy(typeof (IMyInterface
), interceptor
, new MyInterfaceImpl());
57 IMyInterface inter
= (IMyInterface
) proxy
;
59 Assert
.AreEqual(45, inter
.Calc(20, 25));
60 Assert
.IsTrue(interceptor
.LastMethodHasAttribute
);
61 Assert
.IsTrue(interceptor
.LastTargethasAttribute
);
63 Assert
.AreEqual(48, inter
.Calc(20, 25, 1, 2));
64 Assert
.IsTrue(interceptor
.LastMethodHasAttribute
);
65 Assert
.IsTrue(interceptor
.LastTargethasAttribute
);
67 inter
.Name
= "hammett";
68 Assert
.IsFalse(interceptor
.LastMethodHasAttribute
);
69 Assert
.IsTrue(interceptor
.LastTargethasAttribute
);
72 public class AttributeCheckerInterceptor
: StandardInterceptor
74 bool lastMethodHasAttribute
;
75 bool lastTargethasAttribute
;
77 protected override void PreProceed(IInvocation invocation
, params object[] args
)
79 lastMethodHasAttribute
= invocation
.MethodInvocationTarget
.IsDefined(typeof (MyAttribute
), false);
80 lastTargethasAttribute
= invocation
.InvocationTarget
.GetType().IsDefined(typeof (MyAttribute
), true);
83 public bool LastMethodHasAttribute
85 get { return lastMethodHasAttribute; }
88 public bool LastTargethasAttribute
90 get { return lastTargethasAttribute; }