Files
Umbraco-CMS/src/Umbraco.Tests/TestHelpers/TestHelper.cs
Shannon Deminick 5213d6de5c renamed IContentStore to IPublishedContentStore. Migrating DynamicNode to Umbraco.Core and cleaning up it's supported codebase.
Starting writing a few unit tests for the new DynamicNode codebase. Will of course leave the original DynamicNode stuff in its
current place for backwards compatibility but will deprecate many of the classes which will call the new ones.
2012-08-17 04:27:47 +06:00

79 lines
2.4 KiB
C#

using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using SqlCE4Umbraco;
using log4net.Config;
using umbraco.DataLayer;
using GlobalSettings = umbraco.GlobalSettings;
namespace Umbraco.Tests.TestHelpers
{
/// <summary>
/// Common helper properties and methods useful to testing
/// </summary>
public static class TestHelper
{
/// <summary>
/// Clears an initialized database
/// </summary>
public static void ClearDatabase()
{
var dataHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN) as SqlCEHelper;
if (dataHelper == null)
throw new InvalidOperationException("The sql helper for unit tests must be of type SqlCEHelper, check the ensure the connection string used for this test is set to use SQLCE");
dataHelper.ClearDatabase();
}
/// <summary>
/// Initializes a new database
/// </summary>
public static void InitializeDatabase()
{
ConfigurationManager.AppSettings.Set("umbracoDbDSN", @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\Umbraco.sdf");
ClearDatabase();
var dataHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
var installer = dataHelper.Utility.CreateInstaller();
if (installer.CanConnect)
{
installer.Install();
}
}
/// <summary>
/// Gets the current assembly directory.
/// </summary>
/// <value>The assembly directory.</value>
static public string CurrentAssemblyDirectory
{
get
{
var codeBase = Assembly.GetCallingAssembly().CodeBase;
var uri = new Uri(codeBase);
var path = uri.LocalPath;
return Path.GetDirectoryName(path);
}
}
/// <summary>
/// Maps the given <paramref name="relativePath"/> making it rooted on <see cref="CurrentAssemblyDirectory"/>. <paramref name="relativePath"/> must start with <code>~/</code>
/// </summary>
/// <param name="relativePath">The relative path.</param>
/// <returns></returns>
public static string MapPathForTest(string relativePath)
{
if (!relativePath.StartsWith("~/"))
throw new ArgumentException("relativePath must start with '~/'", "relativePath");
return relativePath.Replace("~/", CurrentAssemblyDirectory + "/");
}
public static void SetupLog4NetForTests()
{
XmlConfigurator.Configure(new FileInfo(MapPathForTest("~/unit-test-log4net.config")));
}
}
}