2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Web / Test / mainsoft / NunitWeb / NunitWeb / WebTestResourcesSetupAttribute.cs
1 //
2 // WebTestResourcesSetupAttribute.cs
3 //
4 // Authors:
5 //   Marek Habersack (mhabersack@novell.com)
6 //
7 // (C) 2009 Novell, Inc (http://novell.com/)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Reflection;
33
34 namespace MonoTests.SystemWeb.Framework
35 {
36         [AttributeUsage (AttributeTargets.Assembly, AllowMultiple=false)]
37         public class WebTestResourcesSetupAttribute : Attribute
38         {
39                 public delegate void SetupHandler ();
40                 
41                 Type type;
42                 string methodName;
43                 
44                 public Type Type {
45                         get { return type; }
46                         set {
47                                 if (value == null)
48                                         throw new ArgumentNullException ("value");
49                                 
50                                 type = value;
51                         }
52                 }
53
54                 public string MethodName {
55                         get { return methodName; }
56                         set {
57                                 if (value == null || value.Length == 0)
58                                         throw new ArgumentNullException ("value");
59                                 methodName = value;
60                         }
61                 }
62
63                 public SetupHandler Handler {
64                         get { return FindHandler (Type, MethodName); }
65                 }
66                 
67                 public WebTestResourcesSetupAttribute (Type type, string methodName)
68                 {
69                         Type = type;
70                         MethodName = methodName;
71                 }
72
73                 SetupHandler FindHandler (Type type, string methodName)
74                 {
75                         SetupHandler ret;
76                         MethodInfo mi = type.GetMethod (methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, Type.EmptyTypes, null);
77                         
78                         if (mi == null)
79                                 throw new InvalidOperationException ("Method '" + methodName + "' cannot be found in type '" + type + "'.");
80
81                         if (mi.IsAbstract ||
82 #if NET_2_0
83                             mi.IsGenericMethodDefinition ||
84 #endif
85                             mi.ReturnType != typeof (void) || mi.GetParameters ().Length > 0)
86                                 throw new InvalidOperationException ("Method '" + methodName + "' must return void and take no parameters.");
87
88 #if NET_2_0
89                         ret = Delegate.CreateDelegate (typeof (SetupHandler), null, mi, false) as SetupHandler;
90 #else
91                         ret = Delegate.CreateDelegate (typeof (SetupHandler), mi) as SetupHandler;
92 #endif
93                         if (ret == null)
94                                 throw new InvalidOperationException ("Failed to create a delegate to method '" + methodName + "'.");
95
96                         return ret;
97                 }
98         }
99 }