Updated cache refresher factory to use PluginTypeResolver and added unit tests to show that it

resolves the objects, fixes up the GetAll() call since I'm not sure if that actually ever worked.
This commit is contained in:
shannon@ShandemVaio
2012-07-31 03:08:46 +06:00
parent c9a4b708aa
commit 5b8057f063
4 changed files with 148 additions and 22 deletions

View File

@@ -0,0 +1,112 @@
using System;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Tests.TestHelpers;
using umbraco.interfaces;
namespace Umbraco.Tests
{
[TestFixture]
public class CacheRefresherFactoryTests
{
[SetUp]
public void Initialize()
{
TestHelper.SetupLog4NetForTests();
//this ensures its reset
PluginTypeResolver.Current = new PluginTypeResolver();
//for testing, we'll specify which assemblies are scanned for the PluginTypeResolver
PluginTypeResolver.Current.AssembliesToScan = new[]
{
this.GetType().Assembly
};
}
[Test]
public void Find_All_Refreshers()
{
umbraco.presentation.cache.Factory.Initialize();
Assert.AreEqual(2, umbraco.presentation.cache.Factory._refreshers.Count);
}
[Test]
public void Get_All_Instances()
{
umbraco.presentation.cache.Factory.Initialize();
var factory = new umbraco.presentation.cache.Factory();
Assert.AreEqual(2, factory.GetAll().Count());
}
#region Classes for tests
public class CacheRefresher1 : ICacheRefresher
{
public Guid UniqueIdentifier
{
get { return new Guid("21350A89-4C0D-48B2-B25E-3EE19BFD59FF"); }
}
public string Name
{
get { return "CacheRefresher1"; }
}
public void RefreshAll()
{
throw new NotImplementedException();
}
public void Refresh(int Id)
{
throw new NotImplementedException();
}
public void Remove(int Id)
{
throw new NotImplementedException();
}
public void Refresh(Guid Id)
{
throw new NotImplementedException();
}
}
public class CacheRefresher2 : ICacheRefresher
{
public Guid UniqueIdentifier
{
get { return new Guid("9266CB73-1FDE-4CD9-8DBC-159C2D39BE5D"); }
}
public string Name
{
get { return "CacheRefresher2"; }
}
public void RefreshAll()
{
throw new NotImplementedException();
}
public void Refresh(int Id)
{
throw new NotImplementedException();
}
public void Remove(int Id)
{
throw new NotImplementedException();
}
public void Refresh(Guid Id)
{
throw new NotImplementedException();
}
}
#endregion
}
}

View File

@@ -56,6 +56,7 @@
<Compile Include="BusinessLogic\ApplicationTest.cs" />
<Compile Include="BusinessLogic\ApplicationTreeTest.cs" />
<Compile Include="BusinessLogic\BaseTest.cs" />
<Compile Include="CacheRefresherFactoryTests.cs" />
<Compile Include="ContentStoreTests.cs" />
<Compile Include="DataTypeFactoryTests.cs" />
<Compile Include="MacroControlFactoryTests.cs" />

View File

@@ -44,5 +44,15 @@ namespace Umbraco.Web
{
return resolver.ResolveAttributedTypes<XsltExtensionAttribute>();
}
/// <summary>
/// Returns all classes attributed with XsltExtensionAttribute attribute
/// </summary>
/// <param name="resolver"></param>
/// <returns></returns>
internal static IEnumerable<Type> ResolveCacheRefreshers(this PluginTypeResolver resolver)
{
return resolver.ResolveTypes<ICacheRefresher>();
}
}
}

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Web;
using Umbraco.Core;
using Umbraco.Web;
using umbraco.BusinessLogic.Utils;
using umbraco.interfaces;
using System.Collections.Generic;
@@ -14,7 +16,7 @@ namespace umbraco.presentation.cache
{
#region Declarations
private static readonly Dictionary<Guid, Type> _refreshers = new Dictionary<Guid, Type>();
internal static readonly ConcurrentDictionary<Guid, Type> _refreshers = new ConcurrentDictionary<Guid, Type>();
#endregion
@@ -32,16 +34,16 @@ namespace umbraco.presentation.cache
#region Methods
private static void Initialize()
internal static void Initialize()
{
var typeFinder = new Umbraco.Core.TypeFinder2();
var types = typeFinder.FindClassesOfType<ICacheRefresher>();
foreach (var t in types)
{
var typeInstance = Activator.CreateInstance(t) as ICacheRefresher;
if (typeInstance != null)
_refreshers.Add(typeInstance.UniqueIdentifier, t);
}
foreach(var t in PluginTypeResolver.Current.ResolveCacheRefreshers())
{
var instance = PluginTypeResolver.Current.CreateInstance<ICacheRefresher>(t);
if (instance != null)
{
_refreshers.TryAdd(instance.UniqueIdentifier, t);
}
}
}
public ICacheRefresher CacheRefresher(Guid CacheRefresherId)
@@ -56,26 +58,27 @@ namespace umbraco.presentation.cache
/// <returns></returns>
public ICacheRefresher GetNewObject(Guid CacheRefresherId)
{
ICacheRefresher newObject = Activator.CreateInstance(_refreshers[CacheRefresherId]) as ICacheRefresher;
return newObject;
return !_refreshers.ContainsKey(CacheRefresherId)
? null
: PluginTypeResolver.Current.CreateInstance<ICacheRefresher>(_refreshers[CacheRefresherId]);
}
/// <summary>
/// <summary>
/// Gets all ICacheRefreshers
/// </summary>
/// <returns></returns>
public ICacheRefresher[] GetAll()
{
ICacheRefresher[] retVal = new ICacheRefresher[_refreshers.Count];
int c = 0;
var retVal = new ICacheRefresher[_refreshers.Count];
var c = 0;
foreach (ICacheRefresher cr in _refreshers.Values)
{
retVal[c] = GetNewObject(cr.UniqueIdentifier);
c++;
}
foreach (var id in _refreshers.Keys)
{
retVal[c] = GetNewObject(id);
c++;
}
return retVal;
return retVal;
}
#endregion