From da0217d3304aad83da18891261a12fe4dd5aa77b Mon Sep 17 00:00:00 2001 From: "shannon@ShandemVaio" Date: Fri, 27 Jul 2012 12:29:41 +0600 Subject: [PATCH] Updated PackageAction to use PluginTypeResolver and added unit test to ensure they are found. --- .../PackageActionFactoryTests.cs | 82 ++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + .../PluginTypeResolverExtensions.cs | 10 ++ .../PackageInstance/PackageActions.cs | 141 +++++++++--------- 4 files changed, 166 insertions(+), 68 deletions(-) create mode 100644 src/Umbraco.Tests/PackageActionFactoryTests.cs diff --git a/src/Umbraco.Tests/PackageActionFactoryTests.cs b/src/Umbraco.Tests/PackageActionFactoryTests.cs new file mode 100644 index 0000000000..a05e9764bb --- /dev/null +++ b/src/Umbraco.Tests/PackageActionFactoryTests.cs @@ -0,0 +1,82 @@ +using System; +using System.Linq; +using System.Xml; +using NUnit.Framework; +using Umbraco.Core; +using umbraco.cms.businesslogic.packager; +using umbraco.interfaces; + +namespace Umbraco.Tests +{ + [TestFixture] + public class PackageActionFactoryTests + { + [SetUp] + public void Initialize() + { + //for testing, we'll specify which assemblies are scanned for the PluginTypeResolver + PluginTypeResolver.Current.AssembliesToScan = new[] + { + this.GetType().Assembly + }; + } + + /// + /// Ensures all IPackageActions are found + /// + [Test] + public void Find_Package_Actions() + { + var actions = PackageAction.PackageActions; + Assert.AreEqual(2, actions.Count()); + } + + #region Classes for tests + public class PackageAction1 : IPackageAction + { + public bool Execute(string packageName, XmlNode xmlData) + { + throw new NotImplementedException(); + } + + public string Alias() + { + return "pa1"; + } + + public bool Undo(string packageName, XmlNode xmlData) + { + throw new NotImplementedException(); + } + + public XmlNode SampleXml() + { + throw new NotImplementedException(); + } + } + + public class PackageAction2 : IPackageAction + { + public bool Execute(string packageName, XmlNode xmlData) + { + throw new NotImplementedException(); + } + + public string Alias() + { + return "pa2"; + } + + public bool Undo(string packageName, XmlNode xmlData) + { + throw new NotImplementedException(); + } + + public XmlNode SampleXml() + { + throw new NotImplementedException(); + } + } + #endregion + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 3aba5e8def..89195b7179 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -52,6 +52,7 @@ + diff --git a/src/umbraco.cms/PluginTypeResolverExtensions.cs b/src/umbraco.cms/PluginTypeResolverExtensions.cs index e98fac8d35..3588a855dc 100644 --- a/src/umbraco.cms/PluginTypeResolverExtensions.cs +++ b/src/umbraco.cms/PluginTypeResolverExtensions.cs @@ -55,5 +55,15 @@ namespace umbraco.cms return resolver.ResolveTypes(); } + /// + /// Returns all available IPackageAction in application + /// + /// + /// + internal static IEnumerable ResolvePackageActions(this PluginTypeResolver resolver) + { + return resolver.ResolveTypes(); + } + } } \ No newline at end of file diff --git a/src/umbraco.cms/businesslogic/Packager/PackageInstance/PackageActions.cs b/src/umbraco.cms/businesslogic/Packager/PackageInstance/PackageActions.cs index 36f24a918e..151de71d00 100644 --- a/src/umbraco.cms/businesslogic/Packager/PackageInstance/PackageActions.cs +++ b/src/umbraco.cms/businesslogic/Packager/PackageInstance/PackageActions.cs @@ -3,7 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Web; using System.Xml; - +using Umbraco.Core; using umbraco.BasePages; using umbraco.BusinessLogic.Utils; using umbraco.cms.businesslogic.web; @@ -11,79 +11,84 @@ using umbraco.cms.businesslogic.workflow; using umbraco.interfaces; -namespace umbraco.cms.businesslogic.packager { +namespace umbraco.cms.businesslogic.packager +{ - /// - /// Package actions are executed on packge install / uninstall. - /// - public class PackageAction { - private static readonly List _packageActions = new List(); + /// + /// Package actions are executed on packge install / uninstall. + /// + public class PackageAction + { + internal static readonly List PackageActions = new List(); - /// - /// Initializes the class. - /// - static PackageAction(){ - RegisterPackageActions(); - } - - private static void RegisterPackageActions() - { - var typeFinder = new Umbraco.Core.TypeFinder2(); - var types = typeFinder.FindClassesOfType(); - foreach (var t in types) - { - var typeInstance = Activator.CreateInstance(t) as IPackageAction; - if (typeInstance != null) - { - _packageActions.Add(typeInstance); - - if (HttpContext.Current != null) - HttpContext.Current.Trace.Write("registerPackageActions", " + Adding package action '" + typeInstance.Alias()); - } - } - } + /// + /// Initializes the class. + /// + static PackageAction() + { + RegisterPackageActions(); + } - /// - /// Runs the package action with the specified action alias. - /// - /// Name of the package. - /// The action alias. - /// The action XML. - public static void RunPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml) { + private static void RegisterPackageActions() + { + PackageActions.AddRange( + PluginTypeResolver.Current.CreateInstances( + PluginTypeResolver.Current.ResolvePackageActions())); + } - foreach (IPackageAction ipa in _packageActions) { - try { - if (ipa.Alias() == actionAlias) { + /// + /// Runs the package action with the specified action alias. + /// + /// Name of the package. + /// The action alias. + /// The action XML. + public static void RunPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml) + { - ipa.Execute(packageName, actionXml); - } - } catch (Exception ipaExp) { - BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error loading package action '{0}' for package {1}: {2}", - ipa.Alias(), packageName, ipaExp)); - } - } - } + foreach (IPackageAction ipa in PackageActions) + { + try + { + if (ipa.Alias() == actionAlias) + { - /// - /// Undos the package action with the specified action alias. - /// - /// Name of the package. - /// The action alias. - /// The action XML. - public static void UndoPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml) { + ipa.Execute(packageName, actionXml); + } + } + catch (Exception ipaExp) + { + BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error loading package action '{0}' for package {1}: {2}", + ipa.Alias(), packageName, ipaExp)); + } + } + } - foreach (IPackageAction ipa in _packageActions) { - try { - if (ipa.Alias() == actionAlias) { + /// + /// Undos the package action with the specified action alias. + /// + /// Name of the package. + /// The action alias. + /// The action XML. + public static void UndoPackageAction(string packageName, string actionAlias, System.Xml.XmlNode actionXml) + { - ipa.Undo(packageName, actionXml); - } - } catch (Exception ipaExp) { - BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error undoing package action '{0}' for package {1}: {2}", - ipa.Alias(), packageName, ipaExp)); - } - } - } - - } + foreach (IPackageAction ipa in PackageActions) + { + try + { + if (ipa.Alias() == actionAlias) + { + + ipa.Undo(packageName, actionXml); + } + } + catch (Exception ipaExp) + { + BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, string.Format("Error undoing package action '{0}' for package {1}: {2}", + ipa.Alias(), packageName, ipaExp)); + } + } + } + + } }