+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
#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)
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);
--- /dev/null
+//
+// 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;
+ }
+ }
+}