Fixed base test classes to ensure that if/when an issue goes wrong removing the database file that the class can

continue to TearDown (or force a teardown if it is starting up)
This commit is contained in:
Shannon Deminick
2013-02-20 05:54:41 +06:00
parent 7efc06429c
commit 694d252ce5
2 changed files with 52 additions and 22 deletions

View File

@@ -8,6 +8,7 @@ using NUnit.Framework;
using SQLCE4Umbraco;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
@@ -33,28 +34,37 @@ namespace Umbraco.Tests.TestHelpers
public virtual void Initialize()
{
TestHelper.SetupLog4NetForTests();
Resolution.Reset();
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))
try
{
File.Delete(filePath);
//Delete database file before continueing
string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
catch (Exception)
{
//if this doesn't work we have to make sure everything is reset! otherwise
// well run into issues because we've already set some things up
TearDown();
throw;
}
UmbracoSettings.UseLegacyXmlSchema = false;
RepositoryResolver.Current = new RepositoryResolver(
new RepositoryFactory());
//Get the connectionstring settings from config
var settings = ConfigurationManager.ConnectionStrings[Core.Configuration.GlobalSettings.UmbracoConnectionName];
@@ -110,13 +120,23 @@ namespace Umbraco.Tests.TestHelpers
string path = TestHelper.CurrentAssemblyDirectory;
AppDomain.CurrentDomain.SetData("DataDirectory", null);
string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
PluginManager.Current = null;
UmbracoSettings.ResetSetters();
try
{
string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
catch (Exception ex)
{
LogHelper.Error<BaseDatabaseFactoryTest>("Could not remove the old database file", ex);
//We will swallow this exception! That's because a sub class might require further teardown logic.
}
}
protected ApplicationContext ApplicationContext

View File

@@ -30,20 +30,30 @@ namespace Umbraco.Tests.TestHelpers
TestHelper.SetupLog4NetForTests();
TestHelper.InitializeContentDirectories();
UmbracoSettings.UseLegacyXmlSchema = false;
string path = TestHelper.CurrentAssemblyDirectory;
AppDomain.CurrentDomain.SetData("DataDirectory", path);
//If the Database Provider is Sql Ce we need to ensure the database
if (ProviderName.Contains("SqlServerCe"))
{
//Delete database file before continueing
string filePath = string.Concat(path, "\\test.sdf");
if (File.Exists(filePath))
try
{
File.Delete(filePath);
//Delete database file before continueing
string filePath = string.Concat(path, "\\test.sdf");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
catch (Exception)
{
//if this doesn't work we have to make sure everything is reset! otherwise
// well run into issues because we've already set some things up
TearDown();
throw;
}
UmbracoSettings.UseLegacyXmlSchema = false;
//Create the Sql CE database
var engine = new SqlCeEngine(ConnectionString);