2012-10-18 11:49:44 -02:00
using System ;
using System.Configuration ;
using System.Data.SqlServerCe ;
using System.IO ;
2012-10-25 18:38:23 -02:00
using System.Web.Routing ;
using System.Xml ;
2012-10-18 11:49:44 -02:00
using NUnit.Framework ;
2012-12-15 10:33:29 +05:00
using SQLCE4Umbraco ;
2012-10-25 18:38:23 -02:00
using Umbraco.Core ;
2012-11-02 10:18:07 -01:00
using Umbraco.Core.Configuration ;
2012-10-25 18:38:23 -02:00
using Umbraco.Core.ObjectResolution ;
2012-10-18 11:49:44 -02:00
using Umbraco.Core.Persistence ;
2012-12-27 19:53:01 -01:00
using Umbraco.Core.Persistence.SqlSyntax ;
2012-12-11 12:03:36 +05:00
using Umbraco.Core.Persistence.UnitOfWork ;
using Umbraco.Core.Publishing ;
2012-11-12 07:40:11 -01:00
using Umbraco.Core.Services ;
2012-10-25 18:38:23 -02:00
using Umbraco.Tests.Stubs ;
using Umbraco.Web ;
using Umbraco.Web.Routing ;
using umbraco.BusinessLogic ;
2012-10-18 11:49:44 -02:00
namespace Umbraco.Tests.TestHelpers
{
2012-12-11 12:03:36 +05:00
2012-10-18 11:49:44 -02:00
/// <summary>
/// Use this abstract class for tests that requires a Sql Ce database populated with the umbraco db schema.
2013-01-02 10:44:28 -01:00
/// The PetaPoco Database class should be used through the <see cref="DefaultDatabaseFactory"/>.
2012-10-18 11:49:44 -02:00
/// </summary>
2012-10-30 19:27:47 -01:00
[TestFixture, RequiresSTA]
2012-10-18 11:49:44 -02:00
public abstract class BaseDatabaseFactoryTest
{
[SetUp]
public virtual void Initialize ( )
{
2012-10-30 15:03:58 -01:00
TestHelper . SetupLog4NetForTests ( ) ;
2012-11-29 09:07:14 -01:00
TestHelper . InitializeContentDirectories ( ) ;
2012-10-30 15:03:58 -01:00
2012-10-18 11:49:44 -02:00
string path = TestHelper . CurrentAssemblyDirectory ;
AppDomain . CurrentDomain . SetData ( "DataDirectory" , path ) ;
2012-11-02 10:18:07 -01:00
UmbracoSettings . UseLegacyXmlSchema = false ;
2012-12-09 09:01:00 +05:00
2012-12-10 02:58:23 +05:00
RepositoryResolver . Current = new RepositoryResolver (
new RepositoryFactory ( ) ) ;
2012-12-31 14:52:49 -01:00
2013-01-02 10:44:28 -01:00
//Ensure that any database connections from a previous test is disposed. This is really just double safety as its also done in the TearDown.
if ( ApplicationContext ! = null & & DatabaseContext ! = null )
DatabaseContext . Database . Dispose ( ) ;
2012-12-31 14:52:49 -01:00
SqlCeContextGuardian . CloseBackgroundConnection ( ) ;
2012-10-18 11:49:44 -02:00
//Delete database file before continueing
2012-11-09 14:45:28 -01:00
string filePath = string . Concat ( path , "\\UmbracoPetaPocoTests.sdf" ) ;
2012-10-18 11:49:44 -02:00
if ( File . Exists ( filePath ) )
{
File . Delete ( filePath ) ;
}
//Get the connectionstring settings from config
2012-12-31 14:52:49 -01:00
var settings = ConfigurationManager . ConnectionStrings [ Core . Configuration . GlobalSettings . UmbracoConnectionName ] ;
ConfigurationManager . AppSettings . Set ( Core . Configuration . GlobalSettings . UmbracoConnectionName , @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\UmbracoPetaPocoTests.sdf" ) ;
2012-11-29 09:07:14 -01:00
2012-10-18 11:49:44 -02:00
//Create the Sql CE database
var engine = new SqlCeEngine ( settings . ConnectionString ) ;
engine . CreateDatabase ( ) ;
2012-11-01 15:06:35 -01:00
Resolution . Freeze ( ) ;
2012-12-14 08:06:32 +05:00
ApplicationContext . Current = new ApplicationContext (
//assign the db context
new DatabaseContext ( new DefaultDatabaseFactory ( ) ) ,
//assign the service context
new ServiceContext ( new PetaPocoUnitOfWorkProvider ( ) , new FileUnitOfWorkProvider ( ) , new PublishingStrategy ( ) ) ) { IsReady = true } ;
2012-10-30 15:20:54 -01:00
//Configure the Database and Sql Syntax based on connection string set in config
DatabaseContext . Initialize ( ) ;
//Create the umbraco database and its base data
2012-11-29 12:40:48 -01:00
DatabaseContext . Database . CreateDatabaseSchema ( ) ;
2012-10-18 11:49:44 -02:00
}
2012-11-29 09:07:14 -01:00
2012-10-18 11:49:44 -02:00
[TearDown]
public virtual void TearDown ( )
{
2012-12-11 12:03:36 +05:00
DatabaseContext . Database . Dispose ( ) ;
2012-12-15 10:33:29 +05:00
//reset the app context
2012-12-27 19:53:01 -01:00
ApplicationContext . ApplicationCache . ClearAllCache ( ) ;
SyntaxConfig . SqlSyntaxProvider = null ;
2013-01-02 10:44:28 -01:00
//legacy API database connection close - because a unit test using PetaPoco db-layer can trigger the usage of SqlHelper we need to ensure that a possible connection is closed.
2012-12-15 10:33:29 +05:00
SqlCeContextGuardian . CloseBackgroundConnection ( ) ;
ApplicationContext . Current = null ;
2013-01-16 13:31:04 -01:00
Resolution . Unfreeze ( ) ;
2012-12-15 10:33:29 +05:00
RepositoryResolver . Reset ( ) ;
2012-12-11 12:03:36 +05:00
2012-11-29 09:07:14 -01:00
TestHelper . CleanContentDirectories ( ) ;
2012-12-15 10:33:29 +05:00
2012-10-30 15:03:58 -01:00
string path = TestHelper . CurrentAssemblyDirectory ;
2012-10-18 11:49:44 -02:00
AppDomain . CurrentDomain . SetData ( "DataDirectory" , null ) ;
2012-10-30 15:03:58 -01:00
2012-11-09 14:45:28 -01:00
string filePath = string . Concat ( path , "\\UmbracoPetaPocoTests.sdf" ) ;
2012-10-30 15:03:58 -01:00
if ( File . Exists ( filePath ) )
{
2012-12-15 10:33:29 +05:00
File . Delete ( filePath ) ;
2012-10-30 15:03:58 -01:00
}
2012-10-18 11:49:44 -02:00
}
2012-10-25 18:38:23 -02:00
2012-12-14 08:06:32 +05:00
protected ApplicationContext ApplicationContext
{
get { return ApplicationContext . Current ; }
}
2012-10-25 18:38:23 -02:00
2012-12-14 08:06:32 +05:00
protected ServiceContext ServiceContext
{
get { return ApplicationContext . Services ; }
}
2012-10-30 15:03:58 -01:00
2012-12-14 08:06:32 +05:00
protected DatabaseContext DatabaseContext
{
get { return ApplicationContext . DatabaseContext ; }
}
2012-10-30 15:03:58 -01:00
2012-10-25 18:38:23 -02:00
protected UmbracoContext GetUmbracoContext ( string url , int templateId , RouteData routeData = null )
{
var ctx = new UmbracoContext (
GetHttpContextFactory ( url , routeData ) . HttpContext ,
ApplicationContext ,
GetRoutesCache ( ) ) ;
SetupUmbracoContextForTest ( ctx , templateId ) ;
return ctx ;
}
protected FakeHttpContextFactory GetHttpContextFactory ( string url , RouteData routeData = null )
{
var factory = routeData ! = null
? new FakeHttpContextFactory ( url , routeData )
: new FakeHttpContextFactory ( url ) ;
//set the state helper
StateHelper . HttpContext = factory . HttpContext ;
return factory ;
}
internal virtual IRoutesCache GetRoutesCache ( )
{
return new FakeRoutesCache ( ) ;
}
/// <summary>
/// Initlializes the UmbracoContext with specific XML
/// </summary>
/// <param name="umbracoContext"></param>
/// <param name="templateId"></param>
protected void SetupUmbracoContextForTest ( UmbracoContext umbracoContext , int templateId )
{
umbracoContext . GetXmlDelegate = ( ) = >
{
var xDoc = new XmlDocument ( ) ;
//create a custom xml structure to return
xDoc . LoadXml ( GetXmlContent ( templateId ) ) ;
//return the custom x doc
return xDoc ;
} ;
}
protected virtual string GetXmlContent ( int templateId )
{
return @"<?xml version=""1.0"" encoding=""utf-8" "?>
< ! DOCTYPE root [
< ! ELEMENT Home ANY >
< ! ATTLIST Home id ID # REQUIRED >
< ! ELEMENT CustomDocument ANY >
< ! ATTLIST CustomDocument id ID # REQUIRED >
] >
< root id = "" - 1 "" >
< Home id = "" 1046 "" parentID = "" - 1 "" level = "" 1 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1044 "" template = "" " + templateId + @" "" sortOrder = "" 1 "" createDate = "" 2012 - 06 - 12 T14 : 13 : 17 "" updateDate = "" 2012 - 07 - 20 T18 : 50 : 43 "" nodeName = "" Home "" urlName = "" home "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1046 "" isDoc = "" "" >
< content > < ! [ CDATA [ ] ] > < / content >
< umbracoUrlAlias > < ! [ CDATA [ this / is / my / alias , anotheralias ] ] > < / umbracoUrlAlias >
< umbracoNaviHide > 1 < / umbracoNaviHide >
< Home id = "" 1173 "" parentID = "" 1046 "" level = "" 2 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1044 "" template = "" " + templateId + @" "" sortOrder = "" 2 "" createDate = "" 2012 - 07 - 20 T18 : 06 : 45 "" updateDate = "" 2012 - 07 - 20 T19 : 07 : 31 "" nodeName = "" Sub1 "" urlName = "" sub1 "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1046 , 1173 "" isDoc = "" "" >
< content > < ! [ CDATA [ < div > This is some content < / div > ] ] > < / content >
< umbracoUrlAlias > < ! [ CDATA [ page2 / alias , 2 ndpagealias ] ] > < / umbracoUrlAlias >
< Home id = "" 1174 "" parentID = "" 1173 "" level = "" 3 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1044 "" template = "" " + templateId + @" "" sortOrder = "" 2 "" createDate = "" 2012 - 07 - 20 T18 : 07 : 54 "" updateDate = "" 2012 - 07 - 20 T19 : 10 : 27 "" nodeName = "" Sub2 "" urlName = "" sub2 "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1046 , 1173 , 1174 "" isDoc = "" "" >
< content > < ! [ CDATA [ ] ] > < / content >
< umbracoUrlAlias > < ! [ CDATA [ only / one / alias ] ] > < / umbracoUrlAlias >
< creatorName > < ! [ CDATA [ Custom data with same property name as the member name ] ] > < / creatorName >
< / Home >
< Home id = "" 1176 "" parentID = "" 1173 "" level = "" 3 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1044 "" template = "" " + templateId + @" "" sortOrder = "" 3 "" createDate = "" 2012 - 07 - 20 T18 : 08 : 08 "" updateDate = "" 2012 - 07 - 20 T19 : 10 : 52 "" nodeName = "" Sub 3 "" urlName = "" sub - 3 "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1046 , 1173 , 1176 "" isDoc = "" "" >
< content > < ! [ CDATA [ ] ] > < / content >
< / Home >
< CustomDocument id = "" 1177 "" parentID = "" 1173 "" level = "" 3 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1234 "" template = "" " + templateId + @" "" sortOrder = "" 4 "" createDate = "" 2012 - 07 - 16 T15 : 26 : 59 "" updateDate = "" 2012 - 07 - 18 T14 : 23 : 35 "" nodeName = "" custom sub 1 "" urlName = "" custom - sub - 1 "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1046 , 1173 , 1177 "" isDoc = "" "" / >
< CustomDocument id = "" 1178 "" parentID = "" 1173 "" level = "" 3 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1234 "" template = "" " + templateId + @" "" sortOrder = "" 4 "" createDate = "" 2012 - 07 - 16 T15 : 26 : 59 "" updateDate = "" 2012 - 07 - 16 T14 : 23 : 35 "" nodeName = "" custom sub 2 "" urlName = "" custom - sub - 2 "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1046 , 1173 , 1178 "" isDoc = "" "" / >
< / Home >
< Home id = "" 1175 "" parentID = "" 1046 "" level = "" 2 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1044 "" template = "" " + templateId + @" "" sortOrder = "" 3 "" createDate = "" 2012 - 07 - 20 T18 : 08 : 01 "" updateDate = "" 2012 - 07 - 20 T18 : 49 : 32 "" nodeName = "" Sub 2 "" urlName = "" sub - 2 "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1046 , 1175 "" isDoc = "" "" > < content > < ! [ CDATA [ ] ] > < / content >
< / Home >
< / Home >
< CustomDocument id = "" 1172 "" parentID = "" - 1 "" level = "" 1 "" writerID = "" 0 "" creatorID = "" 0 "" nodeType = "" 1234 "" template = "" " + templateId + @" "" sortOrder = "" 2 "" createDate = "" 2012 - 07 - 16 T15 : 26 : 59 "" updateDate = "" 2012 - 07 - 18 T14 : 23 : 35 "" nodeName = "" Test "" urlName = "" test - page "" writerName = "" admin "" creatorName = "" admin "" path = "" - 1 , 1172 "" isDoc = "" "" / >
< / root > ";
}
2012-10-18 11:49:44 -02:00
}
}