Fixing unit tests, decouples more singleton accessors

This commit is contained in:
Shannon
2015-07-15 16:28:50 +02:00
parent 2a63519e8c
commit 0efeba3744
11 changed files with 85 additions and 49 deletions

View File

@@ -31,6 +31,7 @@ namespace Umbraco.Core
if (dbContext == null) throw new ArgumentNullException("dbContext");
if (serviceContext == null) throw new ArgumentNullException("serviceContext");
if (cache == null) throw new ArgumentNullException("cache");
if (logger == null) throw new ArgumentNullException("logger");
_databaseContext = dbContext;
_services = serviceContext;
ApplicationCache = cache;
@@ -45,7 +46,7 @@ namespace Umbraco.Core
/// <param name="dbContext"></param>
/// <param name="serviceContext"></param>
/// <param name="cache"></param>
[Obsolete("Use the other constructor specifying an ILogger instead")]
[Obsolete("Use the other constructor specifying a ProfilingLogger instead")]
public ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache)
: this(dbContext, serviceContext, cache,
new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler))
@@ -56,10 +57,25 @@ namespace Umbraco.Core
/// Creates a basic app context
/// </summary>
/// <param name="cache"></param>
[Obsolete("Use the other constructor specifying a ProfilingLogger instead")]
public ApplicationContext(CacheHelper cache)
{
ApplicationCache = cache;
ProfilingLogger = new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler);
Init();
}
/// <summary>
/// Creates a basic app context
/// </summary>
/// <param name="cache"></param>
/// <param name="logger"></param>
public ApplicationContext(CacheHelper cache, ProfilingLogger logger)
{
if (cache == null) throw new ArgumentNullException("cache");
if (logger == null) throw new ArgumentNullException("logger");
ApplicationCache = cache;
ProfilingLogger = logger;
Init();
}

View File

@@ -457,7 +457,7 @@ namespace Umbraco.Core
// use the new DefaultShortStringHelper
ShortStringHelperResolver.Current = new ShortStringHelperResolver(
//new LegacyShortStringHelper());
new DefaultShortStringHelper().WithDefaultConfig());
new DefaultShortStringHelper(UmbracoConfig.For.UmbracoSettings()).WithDefaultConfig());
UrlSegmentProviderResolver.Current = new UrlSegmentProviderResolver(
ServiceProvider, LoggerResolver.Current.Logger,

View File

@@ -948,7 +948,7 @@ namespace Umbraco.Core
// as the ShortStringHelper is too important, so as long as it's not there
// already, we use a default one. That should never happen, but...
Logging.LogHelper.Warn<IShortStringHelper>("ShortStringHelperResolver.HasCurrent == false, fallback to default.");
_helper = new DefaultShortStringHelper().WithDefaultConfig();
_helper = new DefaultShortStringHelper(UmbracoConfig.For.UmbracoSettings()).WithDefaultConfig();
_helper.Freeze();
return _helper;
}

View File

@@ -5,6 +5,7 @@ using System.IO;
using System.Linq;
using System.Globalization;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
namespace Umbraco.Core.Strings
{
@@ -18,10 +19,20 @@ namespace Umbraco.Core.Strings
/// </remarks>
public class DefaultShortStringHelper : IShortStringHelper
{
private readonly IUmbracoSettingsSection _umbracoSettings;
#region Ctor and vars
[Obsolete("Use the other ctor that specifies all dependencies")]
public DefaultShortStringHelper()
{
_umbracoSettings = _umbracoSettings;
InitializeLegacyUrlReplaceCharacters();
}
public DefaultShortStringHelper(IUmbracoSettingsSection umbracoSettings)
{
_umbracoSettings = umbracoSettings;
InitializeLegacyUrlReplaceCharacters();
}
@@ -57,7 +68,7 @@ namespace Umbraco.Core.Strings
private void InitializeLegacyUrlReplaceCharacters()
{
foreach (var node in UmbracoConfig.For.UmbracoSettings().RequestHandler.CharCollection)
foreach (var node in _umbracoSettings.RequestHandler.CharCollection)
{
if(string.IsNullOrEmpty(node.Char) == false)
_urlReplaceCharacters[node.Char] = node.Replacement;
@@ -146,7 +157,7 @@ namespace Umbraco.Core.Strings
PreFilter = ApplyUrlReplaceCharacters,
PostFilter = x => CutMaxLength(x, 240),
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_', // letter, digit or underscore
StringType = (UmbracoConfig.For.UmbracoSettings().RequestHandler.ConvertUrlsToAscii ? CleanStringType.Ascii : CleanStringType.Utf8) | CleanStringType.LowerCase,
StringType = (_umbracoSettings.RequestHandler.ConvertUrlsToAscii ? CleanStringType.Ascii : CleanStringType.Utf8) | CleanStringType.LowerCase,
BreakTermsOnUpper = false,
Separator = '-'
}).WithConfig(CleanStringType.FileName, new Config
@@ -323,7 +334,7 @@ function validateSafeAlias(input, value, immediate, callback) {{
public string GetShortStringServicesJavaScript(string controllerPath)
{
return string.Format(SssjsFormat,
UmbracoConfig.For.UmbracoSettings().Content.ForceSafeAliases ? "true" : "false", controllerPath);
_umbracoSettings.Content.ForceSafeAliases ? "true" : "false", controllerPath);
}
#endregion

View File

@@ -12,6 +12,7 @@ using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Tests.TestHelpers;
using umbraco.interfaces;
using Umbraco.Core.Persistence;
namespace Umbraco.Tests.BootManagers
{
@@ -36,6 +37,18 @@ namespace Umbraco.Tests.BootManagers
_testApp = null;
}
/// <summary>
/// Inheritors can override this if they wish to create a custom application context
/// </summary>
protected override void SetupApplicationContext()
{
base.SetupApplicationContext();
var dbContextMock = new Mock<DatabaseContext>(Mock.Of<IDatabaseFactory>(), Mock.Of<ILogger>(), Mock.Of<ISqlSyntaxProvider>(), "test");
dbContextMock.Setup(x => x.CanConnect).Returns(true);
ApplicationContext.DatabaseContext = dbContextMock.Object;
}
protected override void FreezeResolution()
{
//don't freeze resolution, we'll do that in the boot manager

View File

@@ -11,6 +11,8 @@ using Umbraco.Core.Logging;
using Umbraco.Core.Profiling;
using umbraco;
using umbraco.cms.businesslogic.macro;
using Umbraco.Core.Configuration;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Macros
{
@@ -26,18 +28,14 @@ namespace Umbraco.Tests.Macros
new ObjectCacheRuntimeCacheProvider(),
new StaticCacheProvider(),
new NullCacheProvider());
ApplicationContext.Current = new ApplicationContext(cacheHelper);
ProfilerResolver.Current = new ProfilerResolver(new LogProfiler(Mock.Of<ILogger>()))
{
CanResolveBeforeFrozen = true
};
ApplicationContext.Current = new ApplicationContext(cacheHelper, new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
UmbracoConfig.For.SetUmbracoSettings(SettingsForTests.GetDefault());
}
[TearDown]
public void TearDown()
{
ProfilerResolver.Current.DisposeIfDisposable();
ProfilerResolver.Reset();
ApplicationContext.Current.ApplicationCache.ClearAllCache();
ApplicationContext.Current.DisposeIfDisposable();
ApplicationContext.Current = null;

View File

@@ -51,7 +51,7 @@ namespace Umbraco.Tests.Strings
[TestCase("WhoIsNumber6InTheVillage", "Who Is Number6 In The Village")] // issue is fixed
public void CompatibleDefaultReplacement(string input, string expected)
{
var helper = new DefaultShortStringHelper();
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault());
var output = input.Length < 2 ? input : helper.SplitPascalCasing(input, ' ').ToFirstUpperInvariant();
Assert.AreEqual(expected, output);
}

View File

@@ -15,14 +15,13 @@ using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Strings
{
[TestFixture]
public class DefaultShortStringHelperTests : BaseUmbracoConfigurationTest
public class DefaultShortStringHelperTests
{
private DefaultShortStringHelper _helper;
[SetUp]
public override void Initialize()
public void Initialize()
{
base.Initialize();
// NOTE: it is not possible to configure the helper once it has been assigned
// to the resolver and resolution has frozen. but, obviously, it is possible
@@ -32,7 +31,7 @@ namespace Umbraco.Tests.Strings
// NOTE pre-filters runs _before_ Recode takes place
// so there still may be utf8 chars even though you want ascii
_helper = new DefaultShortStringHelper()
_helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.FileName, new DefaultShortStringHelper.Config
{
//PreFilter = ClearFileChars, // done in IsTerm
@@ -80,9 +79,8 @@ namespace Umbraco.Tests.Strings
}
[TearDown]
public override void TearDown()
public void TearDown()
{
base.TearDown();
ShortStringHelperResolver.Reset();
}
@@ -131,11 +129,11 @@ namespace Umbraco.Tests.Strings
const string input = "ÆØÅ and æøå and 中文测试 and אודות האתר and größer БбДдЖж page";
var helper = new DefaultShortStringHelper().WithDefaultConfig(); // unicode
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault()).WithDefaultConfig(); // unicode
var output = helper.CleanStringForUrlSegment(input);
Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", output);
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelper.Config
{
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
@@ -149,7 +147,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringUnderscoreInTerm()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
// underscore is accepted within terms
@@ -159,7 +157,7 @@ namespace Umbraco.Tests.Strings
});
Assert.AreEqual("foo_bar*nil", helper.CleanString("foo_bar nil", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
// underscore is not accepted within terms
@@ -173,7 +171,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringLeadingChars()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
// letters and digits are valid leading chars
@@ -183,7 +181,7 @@ namespace Umbraco.Tests.Strings
});
Assert.AreEqual("0123foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
// only letters are valid leading chars
@@ -194,14 +192,14 @@ namespace Umbraco.Tests.Strings
Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));
Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123 foo_bar 543 nil 321", CleanStringType.Alias));
helper = new DefaultShortStringHelper().WithDefaultConfig();
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault()).WithDefaultConfig();
Assert.AreEqual("child2", helper.CleanStringForSafeAlias("1child2"));
}
[Test]
public void CleanStringTermOnUpper()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -211,7 +209,7 @@ namespace Umbraco.Tests.Strings
});
Assert.AreEqual("foo*Bar", helper.CleanString("fooBar", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -225,7 +223,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringAcronymOnNonUpper()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -238,7 +236,7 @@ namespace Umbraco.Tests.Strings
Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -255,7 +253,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringGreedyAcronyms()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -268,7 +266,7 @@ namespace Umbraco.Tests.Strings
Assert.AreEqual("foo*BA*nil", helper.CleanString("foo BAnil", CleanStringType.Alias));
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -285,7 +283,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringWhiteSpace()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -298,7 +296,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringSeparator()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -306,7 +304,7 @@ namespace Umbraco.Tests.Strings
});
Assert.AreEqual("foo*bar", helper.CleanString("foo bar", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -314,14 +312,14 @@ namespace Umbraco.Tests.Strings
});
Assert.AreEqual("foo bar", helper.CleanString("foo bar", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged
});
Assert.AreEqual("foobar", helper.CleanString("foo bar", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -333,7 +331,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringSymbols()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -387,7 +385,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringEncoding()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -396,7 +394,7 @@ namespace Umbraco.Tests.Strings
Assert.AreEqual("中文测试", helper.CleanString("中文测试", CleanStringType.Alias));
Assert.AreEqual("léger*中文测试*ZÔRG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));
helper = new DefaultShortStringHelper()
helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Ascii | CleanStringType.Unchanged,
@@ -415,7 +413,7 @@ namespace Umbraco.Tests.Strings
contentMock.Setup(x => x.ConvertUrlsToAscii).Returns(false);
SettingsForTests.ConfigureSettings(settings);
var helper = new DefaultShortStringHelper().WithDefaultConfig();
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault()).WithDefaultConfig();
const string input = "0123 中文测试 中文测试 léger ZÔRG (2) a?? *x";
@@ -436,7 +434,7 @@ namespace Umbraco.Tests.Strings
[Test]
public void CleanStringCasing()
{
var helper = new DefaultShortStringHelper()
var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
{
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
@@ -539,7 +537,7 @@ namespace Umbraco.Tests.Strings
//#endregion
//public void CleanStringWithUnderscore(string input, string expected, bool allowUnderscoreInTerm)
//{
// var helper = new DefaultShortStringHelper()
// var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
// .WithConfig(allowUnderscoreInTerm: allowUnderscoreInTerm);
// var output = helper.CleanString(input, CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.CamelCase);
// Assert.AreEqual(expected, output);

View File

@@ -71,7 +71,7 @@ namespace Umbraco.Tests.TestHelpers
TestHelper.CleanContentDirectories();
TestHelper.CleanUmbracoSettingsConfig();
//reset the app context, this should reset most things that require resetting like ALL resolvers
ObjectExtensions.DisposeIfDisposable(ApplicationContext.Current);
ApplicationContext.Current.DisposeIfDisposable();
ApplicationContext.Current = null;
ResetPluginManager();

View File

@@ -15,7 +15,7 @@ namespace Umbraco.Tests.UmbracoExamine
{
UmbracoExamineSearcher.DisableInitializationCheck = true;
BaseUmbracoIndexer.DisableInitializationCheck = true;
ShortStringHelperResolver.Current = new ShortStringHelperResolver(new DefaultShortStringHelper());
ShortStringHelperResolver.Current = new ShortStringHelperResolver(new DefaultShortStringHelper(SettingsForTests.GetDefault()));
Resolution.Freeze();
}

View File

@@ -1578,7 +1578,7 @@ namespace umbraco
//Trace out to profiling... doesn't actually profile, just for informational output.
if (excludeProfiling == false)
{
using (ProfilerResolver.Current.Profiler.Step(string.Format("{0}", message)))
using (ApplicationContext.Current.ProfilingLogger.TraceDuration<macro>(string.Format("{0}", message)))
{
}
}
@@ -1592,7 +1592,7 @@ namespace umbraco
//Trace out to profiling... doesn't actually profile, just for informational output.
if (excludeProfiling == false)
{
using (ProfilerResolver.Current.Profiler.Step(string.Format("Warning: {0}", message)))
using (ApplicationContext.Current.ProfilingLogger.TraceDuration<macro>(string.Format("Warning: {0}", message)))
{
}
}
@@ -1606,7 +1606,7 @@ namespace umbraco
//Trace out to profiling... doesn't actually profile, just for informational output.
if (excludeProfiling == false)
{
using (ProfilerResolver.Current.Profiler.Step(string.Format("{0}, Error: {1}", message, ex)))
using (ApplicationContext.Current.ProfilingLogger.TraceDuration<macro>(string.Format("{0}, Error: {1}", message, ex)))
{
}
}