Somehow missed this with earlier checkin of fluent interface.
[castle.git] / Tools / DynamicProxy / Castle.DynamicProxy.Tests / MixinTestCase.cs
blob9b805985ffbc14f9c09a6d4c388873544e9c0ab0
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.Test
17 using System;
19 using NUnit.Framework;
21 using Castle.DynamicProxy.Test.Classes;
22 using Castle.DynamicProxy.Test.Mixins;
23 using Castle.DynamicProxy.Test.ClassInterfaces;
25 /// <summary>
26 /// Summary description for CachedTypeTestCase.
27 /// </summary>
28 #if !MONO
29 [TestFixture]
30 #endif
31 [Category("DotNetOnly")]
32 public class MixinTestCase
34 private ProxyGenerator _generator;
36 [SetUp]
37 public void Init()
39 _generator = new ProxyGenerator();
42 [Test]
43 public void SimpleMixin()
45 GeneratorContext context = new GeneratorContext();
46 SimpleMixin mixin_instance = new SimpleMixin();
47 context.AddMixinInstance( mixin_instance );
49 AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
51 object proxy = _generator.CreateCustomClassProxy(
52 typeof(SimpleClass), interceptor, context );
54 Assert.IsNotNull(proxy);
55 Assert.IsTrue( typeof(SimpleClass).IsAssignableFrom( proxy.GetType() ) );
57 Assert.IsFalse( interceptor.Invoked );
59 ISimpleMixin mixin = proxy as ISimpleMixin;
60 Assert.IsNotNull(mixin);
61 Assert.AreEqual(1, mixin.DoSomething());
63 Assert.IsTrue( interceptor.Invoked );
64 Assert.AreSame( proxy, interceptor.proxy );
65 Assert.AreSame( mixin_instance, interceptor.mixin );
68 [Test]
69 public void TwoMixins()
71 GeneratorContext context = new GeneratorContext();
72 SimpleMixin mixin1 = new SimpleMixin();
73 OtherMixin mixin2 = new OtherMixin();
75 context.AddMixinInstance( mixin1 );
76 context.AddMixinInstance( mixin2 );
78 AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
80 object proxy = _generator.CreateCustomClassProxy(
81 typeof(SimpleClass), interceptor, context );
83 Assert.IsFalse( interceptor.Invoked );
85 Assert.IsNotNull(proxy);
86 Assert.IsTrue( typeof(SimpleClass).IsAssignableFrom( proxy.GetType() ) );
88 ISimpleMixin mixin = proxy as ISimpleMixin;
89 Assert.IsNotNull(mixin);
90 Assert.AreEqual(1, mixin.DoSomething());
92 Assert.IsTrue( interceptor.Invoked );
93 Assert.AreSame( proxy, interceptor.proxy );
94 Assert.AreSame( mixin1, interceptor.mixin );
96 IOtherMixin other = proxy as IOtherMixin;
97 Assert.IsNotNull(other);
98 Assert.AreEqual(3, other.Sum(1,2));
99 Assert.IsTrue( interceptor.Invoked );
100 Assert.AreSame( proxy, interceptor.proxy );
101 Assert.AreSame( mixin2, interceptor.mixin );
105 [Test]
106 public void MixinImplementingMoreThanOneInterface()
108 GeneratorContext context = new GeneratorContext();
109 ComplexMixin mixin_instance = new ComplexMixin();
110 context.AddMixinInstance( mixin_instance );
112 AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
114 object proxy = _generator.CreateCustomClassProxy(
115 typeof(SimpleClass), interceptor, context );
117 Assert.IsNotNull(proxy);
118 Assert.IsTrue( typeof(SimpleClass).IsAssignableFrom( proxy.GetType() ) );
120 Assert.IsFalse( interceptor.Invoked );
122 IThird inter3 = proxy as IThird;
123 Assert.IsNotNull(inter3);
124 inter3.DoThird();
126 Assert.IsTrue( interceptor.Invoked );
127 Assert.AreSame( proxy, interceptor.proxy );
128 Assert.AreSame( mixin_instance, interceptor.mixin );
130 ISecond inter2 = proxy as ISecond;
131 Assert.IsNotNull(inter2);
132 inter2.DoSecond();
134 Assert.IsTrue( interceptor.Invoked );
135 Assert.AreSame( proxy, interceptor.proxy );
136 Assert.AreSame( mixin_instance, interceptor.mixin );
138 IFirst inter1 = proxy as IFirst;
139 Assert.IsNotNull(inter1);
140 inter1.DoFirst();
142 Assert.IsTrue( interceptor.Invoked );
143 Assert.AreSame( proxy, interceptor.proxy );
144 Assert.AreSame( mixin_instance, interceptor.mixin );
147 [Test]
148 public void MixinForInterfaces()
150 GeneratorContext context = new GeneratorContext();
151 SimpleMixin mixin_instance = new SimpleMixin();
152 context.AddMixinInstance( mixin_instance );
154 AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();
156 MyInterfaceImpl target = new MyInterfaceImpl();
158 object proxy = _generator.CreateCustomProxy(
159 typeof(IMyInterface), interceptor, target, context );
161 Assert.IsNotNull(proxy);
162 Assert.IsTrue( typeof(IMyInterface).IsAssignableFrom( proxy.GetType() ) );
164 Assert.IsFalse( interceptor.Invoked );
166 ISimpleMixin mixin = proxy as ISimpleMixin;
167 Assert.IsNotNull(mixin);
168 Assert.AreEqual(1, mixin.DoSomething());
170 Assert.IsTrue( interceptor.Invoked );
171 Assert.AreSame( proxy, interceptor.proxy );
172 Assert.AreSame( mixin_instance, interceptor.mixin );
176 public class AssertInvocationInterceptor : StandardInterceptor
178 public bool Invoked;
179 public object proxy;
180 public object mixin;
182 protected override void PreProceed(IInvocation invocation, params object[] args)
184 Invoked = true;
185 mixin = invocation.InvocationTarget;
186 proxy = invocation.Proxy;
188 base.PreProceed(invocation, args);