Simple impl to MockRailsEngineContext.Url
[castle.git] / Tools / DynamicProxy / Castle.DynamicProxy.Tests / MethodsWithAttributesOnParameters.cs
blob958ad0a6009109dc72de71dcf791d79d38060285
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;
18 using System.Reflection;
19 using NUnit.Framework;
21 [TestFixture]
22 public class MethodsWithAttributesOnParameters
24 [Test]
25 [ExpectedException(typeof(ArgumentException),"No default value for argument")]
26 public void ParametersAreCopiedToProxiedObject()
28 ProxyGenerator pg = new ProxyGenerator();
29 ClassWithAttributesOnMethodParameters requiredObj = (ClassWithAttributesOnMethodParameters)pg.CreateClassProxy(typeof(ClassWithAttributesOnMethodParameters), new RequiredParamInterceptor());
30 requiredObj.MethodOne(-1);
33 [Test]
34 public void CanGetParameterAttributeFromProxiedObject()
36 ProxyGenerator pg = new ProxyGenerator();
37 ClassWithAttributesOnMethodParameters requiredObj = (ClassWithAttributesOnMethodParameters)pg.CreateClassProxy(typeof(ClassWithAttributesOnMethodParameters), new RequiredParamInterceptor());
38 requiredObj.MethodTwo(null);
42 public class RequiredParamInterceptor :IInterceptor
44 public object Intercept(IInvocation invocation, params object[] args)
46 ParameterInfo[] parameters = invocation.Method.GetParameters();
47 for (int i = 0; i < parameters.Length; i++)
49 if(parameters[i].IsDefined(typeof(RequiredAttribute),false))
51 RequiredAttribute required = parameters[i].GetCustomAttributes(typeof(RequiredAttribute),false)[0] as RequiredAttribute;
52 if( (required.BadValue == null && args[i] == null) ||
53 (required.BadValue != null && required.BadValue.Equals(args[i])
56 args[i] = required.DefaultValue;
60 return invocation.Proceed(args);
64 public class ClassWithAttributesOnMethodParameters
66 public virtual void MethodOne([Required(BadValue=-1)]int val)
68 Assert.IsFalse(val==-1);
71 public virtual void MethodTwo([Required("")]string name)
73 Assert.IsNotNull(name);
77 public class RequiredAttribute : Attribute
79 object defaultValue;
80 public object BadValue;
81 private bool hasDefault=false;
83 public RequiredAttribute(){}
85 public RequiredAttribute(object defaultValue)
87 hasDefault = true;
88 this.defaultValue = defaultValue;
92 public object DefaultValue
94 get
96 if(!hasDefault)
97 throw new ArgumentException("No default value for argument");
98 return defaultValue;