2009-07-03 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Thu, 2 Jul 2009 23:05:02 +0000 (23:05 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Thu, 2 Jul 2009 23:05:02 +0000 (23:05 -0000)
* WebTestResourcesSetupAttribute.cs: added. Can be used to specify
resources setup method alternative to the default
WebTest.CopyResources (). Useful in cases when WebTest is used
outside System.Web tests and different initial resources are
required.

* WebTest.cs: check whether the assembly WebTest is found in is
decorated with the WebTestResourcesSetup attribute and, if yes,
use handler specified by the attribute to perform initial
resources copying in EnsureHosting ().

svn path=/trunk/mcs/; revision=137334

mcs/class/System.Web/System.Web_test.dll.sources
mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/ChangeLog
mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTest.cs
mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTestResourcesSetupAttribute.cs [new file with mode: 0644]

index e0ae4328608c1015bc568d03e3c078369c20bc16..32637f2cdbd8460b0ba2cf43f06928a7a06b774a 100644 (file)
@@ -33,6 +33,7 @@ mainsoft/NunitWeb/NunitWeb/PostableWorkerRequest.cs
 mainsoft/NunitWeb/NunitWeb/Response.cs
 mainsoft/NunitWeb/NunitWeb/StandardUrl.cs
 mainsoft/NunitWeb/NunitWeb/WebTest.cs
+mainsoft/NunitWeb/NunitWeb/WebTestResourcesSetupAttribute.cs
 System.Web/AppBrowsersTest.cs
 System.Web/HttpBrowserCapabilitiesTest.cs
 System.Web/HttpCacheVaryByContentEncodingsTest.cs
index 7fc010bfd9e9f914288b86877c926157ec458dcd..d84da9349e8ae11b7386245250855194addc236d 100644 (file)
@@ -1,3 +1,16 @@
+2009-07-03  Marek Habersack  <mhabersack@novell.com>
+
+       * WebTestResourcesSetupAttribute.cs: added. Can be used to specify
+       resources setup method alternative to the default
+       WebTest.CopyResources (). Useful in cases when WebTest is used
+       outside System.Web tests and different initial resources are
+       required.
+
+       * WebTest.cs: check whether the assembly WebTest is found in is
+       decorated with the WebTestResourcesSetup attribute and, if yes,
+       use handler specified by the attribute to perform initial
+       resources copying in EnsureHosting ().
+
 2009-01-22  Marek Habersack  <mhabersack@novell.com>
 
        * WebTest.cs: Run () sets the internal field
index 3134f8f8707882393531b36501f722df5b2ab73c..ada60c8445c9f1c09f4cee8896e4224c4b91996f 100644 (file)
@@ -349,6 +349,21 @@ namespace MonoTests.SystemWeb.Framework
 #endif
                }
 
+               static WebTestResourcesSetupAttribute.SetupHandler CheckResourcesSetupHandler ()
+               {
+                       // It is assumed WebTest is included in the same assembly which contains the
+                       // tests themselves
+                       object[] attributes = typeof (WebTest).Assembly.GetCustomAttributes (typeof (WebTestResourcesSetupAttribute), true);
+                       if (attributes == null || attributes.Length == 0)
+                               return null;
+                       
+                       WebTestResourcesSetupAttribute attr = attributes [0] as WebTestResourcesSetupAttribute;
+                       if (attr == null)
+                               return null;
+
+                       return attr.Handler;
+               }
+               
                public static void EnsureHosting ()
                {
                        if (host != null)
@@ -360,7 +375,12 @@ namespace MonoTests.SystemWeb.Framework
                        host = AppDomain.CurrentDomain.GetData (HOST_INSTANCE_NAME) as MyHost;
                        if (host != null)
                                return;
-                       CopyResources ();
+                       WebTestResourcesSetupAttribute.SetupHandler resHandler = CheckResourcesSetupHandler ();
+                       if (resHandler == null)
+                               CopyResources ();
+                       else
+                               resHandler ();
+                       
                        foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies ())
                                LoadAssemblyRecursive (ass);
 
diff --git a/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTestResourcesSetupAttribute.cs b/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTestResourcesSetupAttribute.cs
new file mode 100644 (file)
index 0000000..413aab9
--- /dev/null
@@ -0,0 +1,99 @@
+//
+// WebTestResourcesSetupAttribute.cs
+//
+// Authors:
+//   Marek Habersack (mhabersack@novell.com)
+//
+// (C) 2009 Novell, Inc (http://novell.com/)
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Reflection;
+
+namespace MonoTests.SystemWeb.Framework
+{
+       [AttributeUsage (AttributeTargets.Assembly, AllowMultiple=false)]
+       public class WebTestResourcesSetupAttribute : Attribute
+       {
+               public delegate void SetupHandler ();
+               
+               Type type;
+               string methodName;
+               
+               public Type Type {
+                       get { return type; }
+                       set {
+                               if (type == null)
+                                       throw new ArgumentNullException ("value");
+                               
+                               type = value;
+                       }
+               }
+
+               public string MethodName {
+                       get { return methodName; }
+                       set {
+                               if (value == null || value.Length == 0)
+                                       throw new ArgumentNullException ("value");
+                               methodName = value;
+                       }
+               }
+
+               public SetupHandler Handler {
+                       get { return FindHandler (Type, MethodName); }
+               }
+               
+               public WebTestResourcesSetupAttribute (Type type, string methodName)
+               {
+                       Type = type;
+                       MethodName = methodName;
+               }
+
+               SetupHandler FindHandler (Type type, string methodName)
+               {
+                       SetupHandler ret;
+                       MethodInfo mi = type.GetMethod (methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, Type.EmptyTypes, null);
+                       
+                       if (mi == null)
+                               throw new InvalidOperationException ("Method '" + methodName + "' cannot be found in type '" + type + "'.");
+
+                       if (mi.IsAbstract ||
+#if NET_2_0
+                           mi.IsGenericMethodDefinition ||
+#endif
+                           mi.ReturnType != typeof (void) || mi.GetParameters ().Length > 0)
+                               throw new InvalidOperationException ("Method '" + methodName + "' must return void and take no parameters.");
+
+#if NET_2_0
+                       ret = Delegate.CreateDelegate (type, null, mi, false) as SetupHandler;
+#else
+                       ret = Delegate.CreateDelegate (type, mi) as SetupHandler;
+#endif
+                       if (ret == null)
+                               throw new InvalidOperationException ("Failed to create a delegate to method '" + methodName + "'.");
+
+                       return ret;
+               }
+       }
+}