diff --git a/.hgignore b/.hgignore index 289d9dd0d4..00d15820b6 100644 --- a/.hgignore +++ b/.hgignore @@ -41,3 +41,4 @@ _BuildOutput/* build/UmbracoCms.AllBinaries.zip build/UmbracoCms.WebPI.zip build/UmbracoCms.zip +src/Umbraco.Tests/config/applications.config diff --git a/src/SQLCE4Umbraco/Properties/AssemblyInfo.cs b/src/SQLCE4Umbraco/Properties/AssemblyInfo.cs index 0a47013489..f618c8d095 100644 --- a/src/SQLCE4Umbraco/Properties/AssemblyInfo.cs +++ b/src/SQLCE4Umbraco/Properties/AssemblyInfo.cs @@ -18,3 +18,5 @@ using System.Runtime.InteropServices; // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("04436b0a-1dc6-4ee1-9d96-4c04f1a9f429")] + +[assembly: InternalsVisibleTo("Umbraco.Tests")] diff --git a/src/SQLCE4Umbraco/SqlCEHelper.cs b/src/SQLCE4Umbraco/SqlCEHelper.cs index ceab974169..665106d58f 100644 --- a/src/SQLCE4Umbraco/SqlCEHelper.cs +++ b/src/SQLCE4Umbraco/SqlCEHelper.cs @@ -7,8 +7,10 @@ ***********************************************************************************/ using System; +using System.Collections.Generic; using System.Data; using System.Data.SqlServerCe; +using System.Linq; using System.Xml; using System.Diagnostics; using umbraco.DataLayer; @@ -44,6 +46,44 @@ namespace SqlCE4Umbraco } } + /// + /// Most likely only will be used for unit tests but will remove all tables from the database + /// + internal void ClearDatabase() + { + var localConnection = new SqlCeConnection(ConnectionString); + var dbFile = localConnection.Database; + if (System.IO.File.Exists(dbFile)) + { + var tables = new List(); + using (var reader = ExecuteReader("select table_name from information_schema.tables where TABLE_TYPE <> 'VIEW'")) + { + while (reader.Read()) + { + tables.Add(reader.GetString("TABLE_NAME")); + } + } + + while(tables.Any()) + { + for (var i = 0; i < tables.Count; i++) + { + var dropTable = "DROP TABLE " + tables[i]; + + try + { + ExecuteNonQuery(dropTable); + tables.Remove(tables[i]); + } + catch (SqlHelperException ex) + { + //this will occur because there is no cascade option, so we just wanna try the next one + } + } + } + } + } + /// /// Creates a new parameter for use with this specific implementation of ISqlHelper. /// diff --git a/test/umbraco.Test/ApplicationTest.cs b/src/Umbraco.Tests/BusinessLogic/ApplicationTest.cs similarity index 85% rename from test/umbraco.Test/ApplicationTest.cs rename to src/Umbraco.Tests/BusinessLogic/ApplicationTest.cs index fb6e9ef35a..05bef30a32 100644 --- a/test/umbraco.Test/ApplicationTest.cs +++ b/src/Umbraco.Tests/BusinessLogic/ApplicationTest.cs @@ -1,49 +1,22 @@ -using System.Collections.Generic; -using System.Configuration; -using umbraco; +using NUnit.Framework; using umbraco.BusinessLogic; -using Microsoft.VisualStudio.TestTools.UnitTesting; using System; -using umbraco.DataLayer; using System.Linq; -namespace Umbraco.LegacyTests +namespace Umbraco.Tests.BusinessLogic { - [TestClass()] - public abstract class BaseTest - { - [TestInitialize] - public void Initialize() - { - ConfigurationManager.AppSettings.Set("umbracoDbDSN", @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\Umbraco.sdf"); - - var dataHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN); - var installer = dataHelper.Utility.CreateInstaller(); - if (installer.CanConnect) - { - installer.Install(); - } - - Application.Apps = new List() - { - new Application("content", "content", "content", 0) - }; - } - } - - -/// + /// ///This is a test class for ApplicationTest and is intended ///to contain all ApplicationTest Unit Tests /// - [TestClass()] + [TestFixture()] public class ApplicationTest : BaseTest { /// /// Create a new application and delete it /// - [TestMethod()] + [Test()] public void Application_Make_New() { var name = Guid.NewGuid().ToString("N"); @@ -62,7 +35,7 @@ namespace Umbraco.LegacyTests /// Creates a new user, assigns the user to existing application, /// then deletes the user /// - [TestMethod()] + [Test()] public void Application_Create_New_User_Assign_Application_And_Delete_User() { var name = Guid.NewGuid().ToString("N"); @@ -89,7 +62,7 @@ namespace Umbraco.LegacyTests /// /// create a new application and assigne an new user and deletes the application making sure the assignments are removed /// - [TestMethod()] + [Test()] public void Application_Make_New_Assign_User_And_Delete() { var name = Guid.NewGuid().ToString("N"); @@ -107,13 +80,13 @@ namespace Umbraco.LegacyTests //assign the app user.addApplication(app.alias); //ensure it's added - Assert.AreEqual(1, user.Applications.Where(x => x.alias == app.alias).Count()); + Assert.AreEqual(1, user.Applications.Count(x => x.alias == app.alias)); //delete the app app.Delete(); //make sure the assigned applications are gone - Assert.AreEqual(0, user.Applications.Where(x => x.alias == name).Count()); + Assert.AreEqual(0, user.Applications.Count(x => x.alias == name)); } #region Tests to write diff --git a/src/Umbraco.Tests/BusinessLogic/BaseTest.cs b/src/Umbraco.Tests/BusinessLogic/BaseTest.cs new file mode 100644 index 0000000000..e97198a898 --- /dev/null +++ b/src/Umbraco.Tests/BusinessLogic/BaseTest.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using NUnit.Framework; +using SqlCE4Umbraco; +using umbraco.BusinessLogic; +using umbraco.DataLayer; +using umbraco.IO; +using GlobalSettings = umbraco.GlobalSettings; + +namespace Umbraco.Tests.BusinessLogic +{ + [TestFixture] + public abstract class BaseTest + { + /// + /// Removes any resources that were used for the test + /// + [TearDown] + public void Dispose() + { + ClearDatabase(); + } + + /// + /// Ensures everything is setup to allow for unit tests to execute for each test + /// + [SetUp] + public void Initialize() + { + InitializeDatabase(); + InitializeApps(); + InitializeAppConfigFile(); + InitializeTreeConfigFile(); + } + + private 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(); + } + + private void InitializeDatabase() + { + ConfigurationManager.AppSettings.Set("umbracoDbDSN", @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\Umbraco.sdf"); + + var dataHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN); + var installer = dataHelper.Utility.CreateInstaller(); + if (installer.CanConnect) + { + installer.Install(); + } + } + + private void InitializeApps() + { + Application.Apps = new List() + { + new Application("content", "content", "content", 0) + }; + } + + private void InitializeAppConfigFile() + { + Application.AppConfigFilePath = IOHelper.MapPath(SystemDirectories.Config + "/" + Application.AppConfigFileName, false); + } + + private void InitializeTreeConfigFile() + { + ApplicationTree.TreeConfigFilePath = IOHelper.MapPath(SystemDirectories.Config + "/" + ApplicationTree.TreeConfigFileName, false); + } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 2acaa93b6a..9e58738611 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -31,7 +31,11 @@ 4 + + ..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll + + @@ -40,9 +44,32 @@ + + + + + + + + {5BA5425F-27A7-4677-865E-82246498AA2E} + SqlCE4Umbraco + + + {E469A9CE-1BEC-423F-AC44-713CD72457EA} + umbraco.businesslogic + + + {C7CB79F0-1C97-4B33-BFA7-00731B579AE2} + umbraco.datalayer + + + + xcopy "$(ProjectDir)"..\..\lib\SQLCE4\amd64\*.* "$(TargetDir)amd64\" /Y /F /E /D +xcopy "$(ProjectDir)"..\..\lib\SQLCE4\x86\*.* "$(TargetDir)x86\" /Y /F /E /D +