From e02eef60367edd884103f3962bac51c3af7488d4 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Thu, 2 Jul 2009 23:05:02 +0000 Subject: [PATCH] 2009-07-03 Marek Habersack * 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 --- .../System.Web/System.Web_test.dll.sources | 1 + .../Test/mainsoft/NunitWeb/NunitWeb/ChangeLog | 13 +++ .../mainsoft/NunitWeb/NunitWeb/WebTest.cs | 22 ++++- .../WebTestResourcesSetupAttribute.cs | 99 +++++++++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTestResourcesSetupAttribute.cs diff --git a/mcs/class/System.Web/System.Web_test.dll.sources b/mcs/class/System.Web/System.Web_test.dll.sources index e0ae4328608..32637f2cdbd 100644 --- a/mcs/class/System.Web/System.Web_test.dll.sources +++ b/mcs/class/System.Web/System.Web_test.dll.sources @@ -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 diff --git a/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/ChangeLog b/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/ChangeLog index 7fc010bfd9e..d84da9349e8 100644 --- a/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/ChangeLog +++ b/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/ChangeLog @@ -1,3 +1,16 @@ +2009-07-03 Marek Habersack + + * 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 * WebTest.cs: Run () sets the internal field diff --git a/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTest.cs b/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTest.cs index 3134f8f8707..ada60c8445c 100644 --- a/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTest.cs +++ b/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTest.cs @@ -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 index 00000000000..413aab9ec64 --- /dev/null +++ b/mcs/class/System.Web/Test/mainsoft/NunitWeb/NunitWeb/WebTestResourcesSetupAttribute.cs @@ -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; + } + } +} -- 2.25.1