Somehow missed this with earlier checkin of fluent interface.
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / BasicInterfaceProxyTestCase.cs
blobc7b1cf91b0c365a8686da334449c795b108126d0
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.Reflection;
19 using Castle.Core.Interceptor;
20 using Castle.DynamicProxy.Tests.BugsReported;
21 using Castle.DynamicProxy.Tests.Interceptors;
22 using Castle.DynamicProxy.Tests.InterClasses;
23 using NUnit.Framework;
25 [TestFixture]
26 public class BasicInterfaceProxyTestCase : BasePEVerifyTestCase
28 [Test]
29 public void BasicInterfaceProxyWithValidTarget()
31 LogInvocationInterceptor logger = new LogInvocationInterceptor();
33 IService service = (IService)
34 generator.CreateInterfaceProxyWithTarget(
35 typeof(IService), new ServiceImpl(), logger);
37 Assert.AreEqual(3, service.Sum(1, 2));
39 Assert.AreEqual("Sum ", logger.LogContents);
42 [Test]
43 public void Caching()
45 IService service = (IService)
46 generator.CreateInterfaceProxyWithTarget(
47 typeof(IService), new ServiceImpl(), new StandardInterceptor());
48 service = (IService)
49 generator.CreateInterfaceProxyWithTarget(
50 typeof(IService), new ServiceImpl(), new StandardInterceptor());
51 service = (IService)
52 generator.CreateInterfaceProxyWithTarget(
53 typeof(IService), new ServiceImpl(), new StandardInterceptor());
54 service = (IService)
55 generator.CreateInterfaceProxyWithTarget(
56 typeof(IService), new ServiceImpl(), new StandardInterceptor());
59 [Test]
60 public void BasicInterfaceProxyWithValidTarget2()
62 LogInvocationInterceptor logger = new LogInvocationInterceptor();
64 IService2 service = (IService2)
65 generator.CreateInterfaceProxyWithTarget(
66 typeof(IService2), new Service2(), logger);
68 service.DoOperation2();
70 Assert.AreEqual("DoOperation2 ", logger.LogContents);
73 [Test]
74 public void InterfaceInheritance()
76 LogInvocationInterceptor logger = new LogInvocationInterceptor();
78 IService service = (IExtendedService)
79 generator.CreateInterfaceProxyWithTarget(
80 typeof(IExtendedService), new ServiceImpl(), logger);
82 Assert.AreEqual(3, service.Sum(1, 2));
84 Assert.AreEqual("Sum ", logger.LogContents);
87 [Test]
88 public void Indexer()
90 LogInvocationInterceptor logger = new LogInvocationInterceptor();
92 InterfaceWithIndexer service = (InterfaceWithIndexer)
93 generator.CreateInterfaceProxyWithTarget(
94 typeof(InterfaceWithIndexer), new ClassWithIndexer(), logger);
96 Assert.AreEqual(1, service[1]);
98 Assert.AreEqual("get_Item ", logger.LogContents);
101 [Test]
102 public void ProxyTypeWithMultiDimentionalArrayAsParameter()
104 generator.CreateInterfaceProxyWithTarget<IClassWithMultiDimentionalArray>(
105 new ClassWithMultiDimentionalArray(),
106 new LogInvocationInterceptor());
109 [Test]
110 [ExpectedException(typeof(ArgumentException))]
111 public void CantCreateInterfaceTargetedProxyWithoutInterface()
113 IService2 service = (IService2)
114 generator.CreateInterfaceProxyWithTargetInterface(
115 typeof(Service2), new Service2());
118 [Test]
119 public void InterfaceTargetTypeProducesInvocationsThatCanChangeTarget()
121 LogInvocationInterceptor logger = new LogInvocationInterceptor();
122 AssertCanChangeTargetInterceptor invocationChecker = new AssertCanChangeTargetInterceptor();
124 IService2 service = (IService2)
125 generator.CreateInterfaceProxyWithTargetInterface(
126 typeof(IService2), new Service2(), invocationChecker, logger);
128 service.DoOperation2();
130 Assert.AreEqual("DoOperation2 ", logger.LogContents);
133 [Test]
134 public void ChangingInvocationTargetSucceeds()
136 LogInvocationInterceptor logger = new LogInvocationInterceptor();
138 IService service = (IService)
139 generator.CreateInterfaceProxyWithTargetInterface(
140 typeof(IService), new AlwaysThrowsServiceImpl(), new ChangeTargetInterceptor(new ServiceImpl()),
141 logger);
143 Assert.AreEqual(20, service.Sum(10, 10));
147 /// <summary>
148 /// See http://support.castleproject.org/browse/DYNPROXY-43
149 /// </summary>
150 [Test]
151 public void MethodParamNamesAreReplicated()
153 // Try with interface proxy (with target)
154 IMyInterface i = generator.CreateInterfaceProxyWithTarget(typeof(IMyInterface), new MyClass(),
155 new StandardInterceptor()) as IMyInterface;
157 ParameterInfo[] methodParams = GetMyTestMethodParams(i.GetType());
158 Assert.AreEqual("myParam", methodParams[0].Name);
161 private ParameterInfo[] GetMyTestMethodParams(Type type)
163 MethodInfo methodInfo = type.GetMethod("MyTestMethod");
164 return methodInfo.GetParameters();