2012-07-26 21:12:54 +06:00
using System ;
2012-08-09 04:15:35 +06:00
using System.Configuration ;
2012-07-26 21:12:54 +06:00
using System.IO ;
using System.Reflection ;
2012-08-09 04:15:35 +06:00
using SqlCE4Umbraco ;
2012-07-31 02:34:57 +06:00
using log4net.Config ;
2012-08-09 04:15:35 +06:00
using umbraco.DataLayer ;
2012-08-17 04:27:47 +06:00
using GlobalSettings = umbraco . GlobalSettings ;
2012-07-26 21:12:54 +06:00
2012-07-31 00:02:27 +06:00
namespace Umbraco.Tests.TestHelpers
2012-08-17 04:27:47 +06:00
{
2012-07-26 21:12:54 +06:00
/// <summary>
/// Common helper properties and methods useful to testing
/// </summary>
public static class TestHelper
{
2012-08-09 04:15:35 +06:00
/// <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 ( ) ;
}
}
2012-07-26 21:12:54 +06:00
/// <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 + "/" ) ;
}
2012-07-31 02:34:57 +06:00
public static void SetupLog4NetForTests ( )
{
XmlConfigurator . Configure ( new FileInfo ( MapPathForTest ( "~/unit-test-log4net.config" ) ) ) ;
}
2012-07-26 21:12:54 +06:00
}
}