using System;
using System.Configuration;
using System.Data.SqlServerCe;
using System.IO;
using System.Web.Routing;
using System.Xml;
using NUnit.Framework;
using SQLCE4Umbraco;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Tests.Stubs;
using Umbraco.Web;
using Umbraco.Web.Routing;
using umbraco.BusinessLogic;
namespace Umbraco.Tests.TestHelpers
{
///
/// Use this abstract class for tests that requires a Sql Ce database populated with the umbraco db schema.
/// The PetaPoco Database class should be used through the .
///
[TestFixture, RequiresSTA]
public abstract class BaseDatabaseFactoryTest
{
[SetUp]
public virtual void Initialize()
{
TestHelper.SetupLog4NetForTests();
TestHelper.InitializeContentDirectories();
string path = TestHelper.CurrentAssemblyDirectory;
AppDomain.CurrentDomain.SetData("DataDirectory", path);
UmbracoSettings.UseLegacyXmlSchema = false;
RepositoryResolver.Current = new RepositoryResolver(
new RepositoryFactory());
//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();
SqlCeContextGuardian.CloseBackgroundConnection();
//Delete database file before continueing
string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
//Get the connectionstring settings from config
var settings = ConfigurationManager.ConnectionStrings[Core.Configuration.GlobalSettings.UmbracoConnectionName];
ConfigurationManager.AppSettings.Set(Core.Configuration.GlobalSettings.UmbracoConnectionName, @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\UmbracoPetaPocoTests.sdf");
//Create the Sql CE database
var engine = new SqlCeEngine(settings.ConnectionString);
engine.CreateDatabase();
Resolution.Freeze();
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 };
//Configure the Database and Sql Syntax based on connection string set in config
DatabaseContext.Initialize();
//Create the umbraco database and its base data
DatabaseContext.Database.CreateDatabaseSchema();
}
[TearDown]
public virtual void TearDown()
{
DatabaseContext.Database.Dispose();
//reset the app context
ApplicationContext.ApplicationCache.ClearAllCache();
SyntaxConfig.SqlSyntaxProvider = null;
//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.
SqlCeContextGuardian.CloseBackgroundConnection();
ApplicationContext.Current = null;
Resolution.Reset();
RepositoryResolver.Reset();
TestHelper.CleanContentDirectories();
string path = TestHelper.CurrentAssemblyDirectory;
AppDomain.CurrentDomain.SetData("DataDirectory", null);
string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
protected ApplicationContext ApplicationContext
{
get { return ApplicationContext.Current; }
}
protected ServiceContext ServiceContext
{
get { return ApplicationContext.Services; }
}
protected DatabaseContext DatabaseContext
{
get { return ApplicationContext.DatabaseContext; }
}
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();
}
///
/// Initlializes the UmbracoContext with specific XML
///
///
///
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 @"
]>
1
This is some content]]>
";
}
}
}