From ae913ede6f03e62d92f60e9d4dbaf4d78bd38520 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Sun, 10 Apr 2016 00:07:03 +0200 Subject: [PATCH 1/2] Virtualized creating of application context Closed setting up of application context Opens up a whole bunch of doors. :) --- .../Routing/UrlRoutingTestBase.cs | 4 +- .../TestHelpers/BaseDatabaseFactoryTest.cs | 45 ++++++++++--------- .../TestHelpers/BaseUmbracoApplicationTest.cs | 13 ++++-- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs b/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs index 475909318a..be9928004b 100644 --- a/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs +++ b/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs @@ -57,10 +57,10 @@ namespace Umbraco.Tests.Routing public const int LangNlId = 337; public const int LangDkId = 338; - protected override void SetupApplicationContext() + protected override ApplicationContext CreateApplicationContext() { var settings = SettingsForTests.GetDefault(); - ApplicationContext.Current = new ApplicationContext( + return new ApplicationContext( new DatabaseContext(Mock.Of(), Logger, Mock.Of(), "test"), GetServiceContext(settings, Logger), CacheHelper, diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index 967998e517..d24a411f73 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -57,6 +57,7 @@ namespace Umbraco.Tests.TestHelpers private string _dbPath; //used to store (globally) the pre-built db with schema and initial data private static Byte[] _dbBytes; + private DefaultDatabaseFactory dbFactory; [SetUp] public override void Initialize() @@ -66,27 +67,11 @@ namespace Umbraco.Tests.TestHelpers var path = TestHelper.CurrentAssemblyDirectory; AppDomain.CurrentDomain.SetData("DataDirectory", path); - //disable cache - var cacheHelper = CacheHelper.CreateDisabledCacheHelper(); - - var dbFactory = new DefaultDatabaseFactory( + dbFactory = new DefaultDatabaseFactory( GetDbConnectionString(), GetDbProviderName(), Logger); - var repositoryFactory = new RepositoryFactory(cacheHelper, Logger, SqlSyntax, SettingsForTests.GenerateMockSettings()); - - var evtMsgs = new TransientMessagesFactory(); - _appContext = new ApplicationContext( - //assign the db context - new DatabaseContext(dbFactory, Logger, SqlSyntax, "System.Data.SqlServerCe.4.0"), - //assign the service context - new ServiceContext(repositoryFactory, new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy(evtMsgs, Logger), cacheHelper, Logger, evtMsgs), - cacheHelper, - ProfilingLogger) - { - IsReady = true - }; base.Initialize(); @@ -103,16 +88,32 @@ namespace Umbraco.Tests.TestHelpers } } + protected override ApplicationContext CreateApplicationContext() + { + //disable cache + var cacheHelper = CacheHelper.CreateDisabledCacheHelper(); + + var repositoryFactory = new RepositoryFactory(cacheHelper, Logger, SqlSyntax, SettingsForTests.GenerateMockSettings()); + + var evtMsgs = new TransientMessagesFactory(); + _appContext = new ApplicationContext( + //assign the db context + new DatabaseContext(dbFactory, Logger, SqlSyntax, "System.Data.SqlServerCe.4.0"), + //assign the service context + new ServiceContext(repositoryFactory, new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy(evtMsgs, Logger), cacheHelper, Logger, evtMsgs), + cacheHelper, + ProfilingLogger) + { + IsReady = true + }; + return _appContext; + } + protected virtual ISqlSyntaxProvider SqlSyntax { get { return new SqlCeSyntaxProvider(); } } - protected override void SetupApplicationContext() - { - ApplicationContext.Current = _appContext; - } - /// /// The database behavior to use for the test/fixture /// diff --git a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs index abf1edd3a5..83020fd386 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs @@ -147,17 +147,21 @@ namespace Umbraco.Tests.TestHelpers /// /// Inheritors can override this if they wish to create a custom application context /// - protected virtual void SetupApplicationContext() + protected void SetupApplicationContext() { + var applicationContext = CreateApplicationContext(); + ApplicationContext.Current = applicationContext; + } + protected virtual ApplicationContext CreateApplicationContext() + { var sqlSyntax = new SqlCeSyntaxProvider(); var repoFactory = new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), Logger, sqlSyntax, SettingsForTests.GenerateMockSettings()); var evtMsgs = new TransientMessagesFactory(); - ApplicationContext.Current = new ApplicationContext( + var applicationContext = new ApplicationContext( //assign the db context - new DatabaseContext(new DefaultDatabaseFactory(Core.Configuration.GlobalSettings.UmbracoConnectionName, Logger), - Logger, sqlSyntax, "System.Data.SqlServerCe.4.0"), + new DatabaseContext(new DefaultDatabaseFactory(Core.Configuration.GlobalSettings.UmbracoConnectionName, Logger), Logger, sqlSyntax, "System.Data.SqlServerCe.4.0"), //assign the service context new ServiceContext(repoFactory, new PetaPocoUnitOfWorkProvider(Logger), new FileUnitOfWorkProvider(), new PublishingStrategy(evtMsgs, Logger), CacheHelper, Logger, evtMsgs), CacheHelper, @@ -165,6 +169,7 @@ namespace Umbraco.Tests.TestHelpers { IsReady = true }; + return applicationContext; } /// From 1d428045b6e65dbb62040a8c0499f0349453d0e1 Mon Sep 17 00:00:00 2001 From: Lars-Erik Aabech Date: Mon, 18 Apr 2016 15:35:09 +0200 Subject: [PATCH 2/2] Don't remove virtual on SetupApplicationContext. Might be many external inheritors. --- src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs index 83020fd386..84b109a66a 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs @@ -147,7 +147,7 @@ namespace Umbraco.Tests.TestHelpers /// /// Inheritors can override this if they wish to create a custom application context /// - protected void SetupApplicationContext() + protected virtual void SetupApplicationContext() { var applicationContext = CreateApplicationContext(); ApplicationContext.Current = applicationContext;