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
.Tests
17 using NUnit
.Framework
;
20 public class MixinTestCase
: BasePEVerifyTestCase
23 // public void SimpleMixin()
25 // GeneratorContext context = new GeneratorContext();
26 // SimpleMixin mixin_instance = new SimpleMixin();
27 // context.AddMixinInstance(mixin_instance);
29 // AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
31 // object proxy = generator.CreateCustomClassProxy(
32 // typeof(SimpleClass), interceptor, context);
34 // Assert.IsNotNull(proxy);
35 // Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));
37 // Assert.IsFalse(interceptor.Invoked);
39 // ISimpleMixin mixin = proxy as ISimpleMixin;
40 // Assert.IsNotNull(mixin);
41 // Assert.AreEqual(1, mixin.DoSomething());
43 // Assert.IsTrue(interceptor.Invoked);
44 // Assert.AreSame(proxy, interceptor.proxy);
45 // Assert.AreSame(mixin_instance, interceptor.mixin);
49 // public void TwoMixins()
51 // GeneratorContext context = new GeneratorContext();
52 // SimpleMixin mixin1 = new SimpleMixin();
53 // OtherMixin mixin2 = new OtherMixin();
55 // context.AddMixinInstance(mixin1);
56 // context.AddMixinInstance(mixin2);
58 // AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
60 // object proxy = generator.CreateCustomClassProxy(
61 // typeof(SimpleClass), interceptor, context);
63 // Assert.IsFalse(interceptor.Invoked);
65 // Assert.IsNotNull(proxy);
66 // Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));
68 // ISimpleMixin mixin = proxy as ISimpleMixin;
69 // Assert.IsNotNull(mixin);
70 // Assert.AreEqual(1, mixin.DoSomething());
72 // Assert.IsTrue(interceptor.Invoked);
73 // Assert.AreSame(proxy, interceptor.proxy);
74 // Assert.AreSame(mixin1, interceptor.mixin);
76 // IOtherMixin other = proxy as IOtherMixin;
77 // Assert.IsNotNull(other);
78 // Assert.AreEqual(3, other.Sum(1, 2));
79 // Assert.IsTrue(interceptor.Invoked);
80 // Assert.AreSame(proxy, interceptor.proxy);
81 // Assert.AreSame(mixin2, interceptor.mixin);
86 // public void MixinImplementingMoreThanOneInterface()
88 // GeneratorContext context = new GeneratorContext();
89 // ComplexMixin mixin_instance = new ComplexMixin();
90 // context.AddMixinInstance(mixin_instance);
92 // AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
94 // object proxy = generator.CreateCustomClassProxy(
95 // typeof(SimpleClass), interceptor, context);
97 // Assert.IsNotNull(proxy);
98 // Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));
100 // Assert.IsFalse(interceptor.Invoked);
102 // IThird inter3 = proxy as IThird;
103 // Assert.IsNotNull(inter3);
106 // Assert.IsTrue(interceptor.Invoked);
107 // Assert.AreSame(proxy, interceptor.proxy);
108 // Assert.AreSame(mixin_instance, interceptor.mixin);
110 // ISecond inter2 = proxy as ISecond;
111 // Assert.IsNotNull(inter2);
112 // inter2.DoSecond();
114 // Assert.IsTrue(interceptor.Invoked);
115 // Assert.AreSame(proxy, interceptor.proxy);
116 // Assert.AreSame(mixin_instance, interceptor.mixin);
118 // IFirst inter1 = proxy as IFirst;
119 // Assert.IsNotNull(inter1);
122 // Assert.IsTrue(interceptor.Invoked);
123 // Assert.AreSame(proxy, interceptor.proxy);
124 // Assert.AreSame(mixin_instance, interceptor.mixin);
128 // public void MixinForInterfaces()
130 // GeneratorContext context = new GeneratorContext();
131 // SimpleMixin mixin_instance = new SimpleMixin();
132 // context.AddMixinInstance(mixin_instance);
134 // AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
136 // MyInterfaceImpl target = new MyInterfaceImpl();
138 // object proxy = generator.CreateCustomProxy(
139 // typeof(IMyInterface), interceptor, target, context);
141 // Assert.IsNotNull(proxy);
142 // Assert.IsTrue(typeof(IMyInterface).IsAssignableFrom(proxy.GetType()));
144 // Assert.IsFalse(interceptor.Invoked);
146 // ISimpleMixin mixin = proxy as ISimpleMixin;
147 // Assert.IsNotNull(mixin);
148 // Assert.AreEqual(1, mixin.DoSomething());
150 // Assert.IsTrue(interceptor.Invoked);
151 // Assert.AreSame(proxy, interceptor.proxy);
152 // Assert.AreSame(mixin_instance, interceptor.mixin);
156 // public class AssertInvocationInterceptor : StandardInterceptor
158 // public bool Invoked;
159 // public object proxy;
160 // public object mixin;
162 // protected override void PreProceed(IInvocation invocation, params object[] args)
165 // mixin = invocation.InvocationTarget;
166 // proxy = invocation.Proxy;
168 // base.PreProceed(invocation, args);