Fixed test issue - PEVerify is now called with a quoted assembly path (to allow for...
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy.Tests / MethodsWithAttributesOnParametersTestCase.cs
blob2537a3ffb04010f586a8e02af2d93877049160e9
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 NUnit.Framework;
22 [TestFixture]
23 public class MethodsWithAttributesOnParametersTestCase : BasePEVerifyTestCase
25 [Test]
26 [ExpectedException(typeof(ArgumentException), "No default value for argument")]
27 public void ParametersAreCopiedToProxiedObject()
29 ClassWithAttributesOnMethodParameters requiredObj = (ClassWithAttributesOnMethodParameters)
30 generator.CreateClassProxy(
31 typeof(ClassWithAttributesOnMethodParameters),
32 new RequiredParamInterceptor());
34 requiredObj.MethodOne(-1);
37 [Test]
38 public void CanGetParameterAttributeFromProxiedObject()
40 ClassWithAttributesOnMethodParameters requiredObj = (ClassWithAttributesOnMethodParameters)
41 generator.CreateClassProxy(
42 typeof(ClassWithAttributesOnMethodParameters),
43 new RequiredParamInterceptor());
45 requiredObj.MethodTwo(null);
49 public class RequiredParamInterceptor : IInterceptor
51 public void Intercept(IInvocation invocation)
53 ParameterInfo[] parameters = invocation.Method.GetParameters();
55 object[] args = invocation.Arguments;
57 for(int i = 0; i < parameters.Length; i++)
59 if (parameters[i].IsDefined(typeof(RequiredAttribute), false))
61 RequiredAttribute required =
62 parameters[i].GetCustomAttributes(typeof(RequiredAttribute), false)[0] as RequiredAttribute;
64 if ((required.BadValue == null && args[i] == null) ||
65 (required.BadValue != null && required.BadValue.Equals(args[i])))
67 args[i] = required.DefaultValue;
72 invocation.Proceed();
76 public class ClassWithAttributesOnMethodParameters
78 public virtual void MethodOne([Required(BadValue = -1)] int val)
80 Assert.IsFalse(val == -1);
83 public virtual void MethodTwo([Required("")] string name)
85 Assert.IsNotNull(name);
89 public class RequiredAttribute : Attribute
91 private object defaultValue;
92 private bool hasDefault = false;
93 public object BadValue;
95 public RequiredAttribute()
99 public RequiredAttribute(object defaultValue)
101 hasDefault = true;
102 this.defaultValue = defaultValue;
105 public object DefaultValue
109 if (!hasDefault)
111 throw new ArgumentException("No default value for argument");
113 return defaultValue;