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
18 using Castle
.Core
.Interceptor
;
19 using Castle
.DynamicProxy
.Tests
.Classes
;
20 using NUnit
.Framework
;
23 public class ClassWithAttributesTestCase
: BasePEVerifyTestCase
26 public void EnsureProxyHasAttributesOnClassAndMethods()
28 AttributedClass instance
= (AttributedClass
)
29 generator
.CreateClassProxy(typeof(AttributedClass
), new StandardInterceptor());
31 object[] attributes
= instance
.GetType().GetCustomAttributes(typeof(NonInheritableAttribute
), false);
32 Assert
.AreEqual(1, attributes
.Length
);
33 Assert
.IsInstanceOfType(typeof(NonInheritableAttribute
), attributes
[0]);
35 attributes
= instance
.GetType().GetMethod("Do1").GetCustomAttributes(typeof(NonInheritableAttribute
), false);
36 Assert
.AreEqual(1, attributes
.Length
);
37 Assert
.IsInstanceOfType(typeof(NonInheritableAttribute
), attributes
[0]);
41 public void EnsureProxyHasAttributesOnClassAndMethods_ComplexAttributes()
43 AttributedClass2 instance
= (AttributedClass2
)
44 generator
.CreateClassProxy(typeof(AttributedClass2
), new StandardInterceptor());
46 object[] attributes
= instance
.GetType().GetCustomAttributes(typeof(ComplexNonInheritableAttribute
), false);
47 Assert
.AreEqual(1, attributes
.Length
);
48 Assert
.IsInstanceOfType(typeof(ComplexNonInheritableAttribute
), attributes
[0]);
49 ComplexNonInheritableAttribute att
= (ComplexNonInheritableAttribute
) attributes
[0];
50 // (1, 2, true, "class", FileAccess.Write)
51 Assert
.AreEqual(1, att
.Id
);
52 Assert
.AreEqual(2, att
.Num
);
53 Assert
.AreEqual(true, att
.IsSomething
);
54 Assert
.AreEqual("class", att
.Name
);
55 Assert
.AreEqual(FileAccess
.Write
, att
.Access
);
57 attributes
= instance
.GetType().GetMethod("Do1").GetCustomAttributes(typeof(ComplexNonInheritableAttribute
), false);
58 Assert
.AreEqual(1, attributes
.Length
);
59 Assert
.IsInstanceOfType(typeof(ComplexNonInheritableAttribute
), attributes
[0]);
60 att
= (ComplexNonInheritableAttribute
) attributes
[0];
61 // (2, 3, "Do1", Access = FileAccess.ReadWrite)
62 Assert
.AreEqual(2, att
.Id
);
63 Assert
.AreEqual(3, att
.Num
);
64 Assert
.AreEqual(false, att
.IsSomething
);
65 Assert
.AreEqual("Do1", att
.Name
);
66 Assert
.AreEqual(FileAccess
.ReadWrite
, att
.Access
);
68 attributes
= instance
.GetType().GetMethod("Do2").GetCustomAttributes(typeof(ComplexNonInheritableAttribute
), false);
69 Assert
.AreEqual(1, attributes
.Length
);
70 Assert
.IsInstanceOfType(typeof(ComplexNonInheritableAttribute
), attributes
[0]);
71 att
= (ComplexNonInheritableAttribute
) attributes
[0];
72 // (3, 4, "Do2", IsSomething=true)
73 Assert
.AreEqual(3, att
.Id
);
74 Assert
.AreEqual(4, att
.Num
);
75 Assert
.AreEqual(true, att
.IsSomething
);
76 Assert
.AreEqual("Do2", att
.Name
);