Fixes some merge issues, gets a bunch more tests operational.

This commit is contained in:
Shannon
2015-12-20 20:09:25 +01:00
parent 4d0b2b745a
commit 04e7979fe6
7 changed files with 154 additions and 142 deletions

View File

@@ -44,32 +44,6 @@ namespace Umbraco.Core
Init();
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="dbContext"></param>
/// <param name="serviceContext"></param>
/// <param name="cache"></param>
[Obsolete("Use the other constructor specifying a ProfilingLogger instead")]
public ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache)
: this(dbContext, serviceContext, cache,
new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler))
{
}
/// <summary>
/// Creates a basic app context
/// </summary>
/// <param name="cache"></param>
[Obsolete("Use the other constructor specifying a ProfilingLogger instead")]
public ApplicationContext(CacheHelper cache)
{
if (cache == null) throw new ArgumentNullException("cache");
ApplicationCache = cache;
ProfilingLogger = new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler);
Init();
}
/// <summary>
/// Creates a basic app context
/// </summary>
@@ -106,33 +80,6 @@ namespace Umbraco.Core
return Current;
}
/// <summary>
/// A method used to create and ensure that a global ApplicationContext singleton is created.
/// </summary>
/// <param name="cache"></param>
/// <param name="replaceContext">
/// If set to true will replace the current singleton instance - This should only be used for unit tests or on app
/// startup if for some reason the boot manager is not the umbraco boot manager.
/// </param>
/// <param name="dbContext"></param>
/// <param name="serviceContext"></param>
/// <returns></returns>
/// <remarks>
/// This is NOT thread safe
/// </remarks>
[Obsolete("Use the other method specifying an ProfilingLogger instead")]
public static ApplicationContext EnsureContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache, bool replaceContext)
{
if (Current != null)
{
if (!replaceContext)
return Current;
}
var ctx = new ApplicationContext(dbContext, serviceContext, cache);
Current = ctx;
return Current;
}
/// <summary>
/// A method used to create and ensure that a global ApplicationContext singleton is created.
/// </summary>

View File

@@ -48,7 +48,7 @@ namespace Umbraco.Core
protected ProfilingLogger ProfilingLogger { get; private set; }
private DisposableTimer _timer;
protected PluginManager PluginManager { get; private set; }
private CacheHelper _cacheHelper;
private bool _isInitialized = false;
private bool _isStarted = false;
@@ -88,16 +88,28 @@ namespace Umbraco.Core
if (_isInitialized)
throw new InvalidOperationException("The boot manager has already been initialized");
//Create logger/profiler, and their resolvers, these are special resolvers that can be resolved before frozen so we can start logging
LoggerResolver.Current = new LoggerResolver(_umbracoApplication.Logger) { CanResolveBeforeFrozen = true };
var profiler = CreateProfiler();
ProfilerResolver.Current = new ProfilerResolver(profiler) { CanResolveBeforeFrozen = true };
ProfilingLogger = new ProfilingLogger(_umbracoApplication.Logger, profiler);
ProfilingLogger = ProfilingLogger?? new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler);
ApplicationCache = CreateApplicationCache();
_timer = ProfilingLogger.TraceDuration<CoreBootManager>(
string.Format("Umbraco {0} application starting on {1}", UmbracoVersion.GetSemanticVersion().ToSemanticString(), NetworkHelper.MachineName),
"Umbraco application startup complete");
_cacheHelper = CreateApplicationCache();
ServiceProvider = new ActivatorServiceProvider();
PluginManager.Current = PluginManager = new PluginManager(ServiceProvider, _cacheHelper.RuntimeCache, ProfilingLogger, true);
ApplicationCache = CreateApplicationCache();
ServiceProvider = new ActivatorServiceProvider();
//create the plugin manager
//TODO: this is currently a singleton but it would be better if it weren't. Unfortunately the only way to get
// rid of this singleton would be to put it into IoC and then use the ServiceLocator pattern.
PluginManager.Current = PluginManager = new PluginManager(ServiceProvider, ApplicationCache.RuntimeCache, ProfilingLogger, true);
//build up core IoC servoces
ConfigureCoreServices(Container);
//set the singleton resolved from the core container
@@ -117,14 +129,24 @@ namespace Umbraco.Core
_appStartupEvtContainer.RegisterCollection<IApplicationEventHandler, PerScopeLifetime>(PluginManager.ResolveApplicationStartupHandlers());
//build up standard IoC services
ConfigureServices(Container);
ConfigureApplicationServices(Container);
InitializeResolvers();
InitializeModelMappers();
//now we need to call the initialize methods
//TODO: Make sure to try/catch the OnApplicationInitialized!!
Parallel.ForEach(_appStartupEvtContainer.GetAllInstances<IApplicationEventHandler>(), x => x.OnApplicationInitialized(UmbracoApplication, ApplicationContext));
Parallel.ForEach(_appStartupEvtContainer.GetAllInstances<IApplicationEventHandler>(), x =>
{
try
{
x.OnApplicationInitialized(UmbracoApplication, ApplicationContext);
}
catch (Exception ex)
{
ProfilingLogger.Logger.Error<CoreBootManager>("An error occurred running OnApplicationInitialized for handler " + x.GetType(), ex);
throw;
}
});
_isInitialized = true;
@@ -134,20 +156,19 @@ namespace Umbraco.Core
/// <summary>
/// Build the core container which contains all core things requird to build an app context
/// </summary>
private void ConfigureCoreServices(ServiceContainer container)
internal virtual void ConfigureCoreServices(ServiceContainer container)
{
container.Register<IServiceContainer>(factory => container);
container.Register<ILogger>(factory => _umbracoApplication.Logger, new PerContainerLifetime());
container.Register<IProfiler>(factory => ProfilingLogger.Profiler, new PerContainerLifetime());
container.Register<ProfilingLogger>(factory => ProfilingLogger, new PerContainerLifetime());
var settings = UmbracoConfig.For.UmbracoSettings();
container.Register<IUmbracoSettingsSection>(factory => settings);
container.Register<IContentSection>(factory => settings.Content);
container.Register<IRequestHandlerSection>(factory => settings.RequestHandler);
container.Register<IUmbracoSettingsSection>(factory => UmbracoConfig.For.UmbracoSettings());
container.Register<IContentSection>(factory => factory.GetInstance<IUmbracoSettingsSection>().Content);
container.Register<IRequestHandlerSection>(factory => factory.GetInstance<IUmbracoSettingsSection>().RequestHandler);
//TODO: Add the other config areas...
container.Register<CacheHelper>(factory => _cacheHelper, new PerContainerLifetime());
container.Register<IRuntimeCacheProvider>(factory => _cacheHelper.RuntimeCache, new PerContainerLifetime());
container.Register<CacheHelper>(factory => ApplicationCache, new PerContainerLifetime());
container.Register<IRuntimeCacheProvider>(factory => ApplicationCache.RuntimeCache, new PerContainerLifetime());
container.Register<IServiceProvider, ActivatorServiceProvider>();
container.Register<PluginManager>(factory => PluginManager, new PerContainerLifetime());
container.Register<IDatabaseFactory>(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance<ILogger>()));
@@ -185,7 +206,7 @@ namespace Umbraco.Core
/// Called to customize the IoC container
/// </summary>
/// <param name="container"></param>
internal virtual void ConfigureServices(ServiceContainer container)
internal virtual void ConfigureApplicationServices(ServiceContainer container)
{
}
@@ -270,9 +291,19 @@ namespace Umbraco.Core
if (_isStarted)
throw new InvalidOperationException("The boot manager has already been initialized");
//TODO: Make sure to try/catch the OnApplicationInitialized!!
//call OnApplicationStarting of each application events handler
Parallel.ForEach(_appStartupEvtContainer.GetAllInstances<IApplicationEventHandler>(), x => x.OnApplicationStarting(UmbracoApplication, ApplicationContext));
Parallel.ForEach(_appStartupEvtContainer.GetAllInstances<IApplicationEventHandler>(), x =>
{
try
{
x.OnApplicationStarting(UmbracoApplication, ApplicationContext);
}
catch (Exception ex)
{
ProfilingLogger.Logger.Error<CoreBootManager>("An error occurred running OnApplicationStarting for handler " + x.GetType(), ex);
throw;
}
});
if (afterStartup != null)
{
@@ -307,9 +338,18 @@ namespace Umbraco.Core
//call OnApplicationStarting of each application events handler
//TODO: Make sure to try/catch the OnApplicationInitialized!!
Parallel.ForEach(_appStartupEvtContainer.GetAllInstances<IApplicationEventHandler>(), x => x.OnApplicationStarted(UmbracoApplication, ApplicationContext));
Parallel.ForEach(_appStartupEvtContainer.GetAllInstances<IApplicationEventHandler>(), x =>
{
try
{
x.OnApplicationStarted(UmbracoApplication, ApplicationContext);
}
catch (Exception ex)
{
ProfilingLogger.Logger.Error<CoreBootManager>("An error occurred running OnApplicationStarted for handler " + x.GetType(), ex);
throw;
}
});
//end the current scope which was created to intantiate all of the startup handlers
_appStartupEvtContainer.EndCurrentScope();
@@ -372,8 +412,9 @@ namespace Umbraco.Core
/// </summary>
protected virtual void InitializeResolvers()
{
var manifestParser = new ManifestParser(ProfilingLogger.Logger, new DirectoryInfo(IOHelper.MapPath("~/App_Plugins")), _cacheHelper.RuntimeCache);
var manifestBuilder = new ManifestBuilder(_cacheHelper.RuntimeCache, manifestParser);
var manifestParser = new ManifestParser(ProfilingLogger.Logger, new DirectoryInfo(IOHelper.MapPath("~/App_Plugins")), ApplicationCache.RuntimeCache);
var manifestBuilder = new ManifestBuilder(ApplicationCache.RuntimeCache, manifestParser);
PropertyEditorResolver.Current = new PropertyEditorResolver(
Container, ProfilingLogger.Logger, () => PluginManager.ResolvePropertyEditors(),

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using LightInject;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
@@ -12,6 +13,7 @@ using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Tests.TestHelpers;
using umbraco.interfaces;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Persistence;
using Umbraco.Core.Profiling;
using Umbraco.Core.Services;
@@ -21,23 +23,12 @@ namespace Umbraco.Tests.BootManagers
[TestFixture]
public class CoreBootManagerTests : BaseUmbracoConfigurationTest
{
private TestApp _testApp;
[SetUp]
public override void Initialize()
{
base.Initialize();
_testApp = new TestApp();
}
[TearDown]
public override void TearDown()
{
base.TearDown();
_testApp = null;
ResolverCollection.ResetAll();
TestApplicationEventHandler.Reset();
}
@@ -50,6 +41,16 @@ namespace Umbraco.Tests.BootManagers
{
return new TestBootManager(this, new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
}
private ILogger _logger;
/// <summary>
/// Returns the logger instance for the application - this will be used throughout the entire app
/// </summary>
public override ILogger Logger
{
get { return _logger ?? (_logger = Mock.Of<ILogger>()); }
}
}
/// <summary>
@@ -61,16 +62,35 @@ namespace Umbraco.Tests.BootManagers
: base(umbracoApplication, logger)
{
}
internal override void ConfigureCoreServices(ServiceContainer container)
{
base.ConfigureCoreServices(container);
container.Register<IUmbracoSettingsSection>(factory => SettingsForTests.GetDefault());
container.Register<DatabaseContext>(factory => new DatabaseContext(
factory.GetInstance<IDatabaseFactory>(),
factory.GetInstance<ILogger>(),
factory.GetInstance<SqlSyntaxProviders>()), new PerContainerLifetime());
}
}
/// <summary>
/// test event handler
/// </summary>
public class TestApplicationEventHandler : IApplicationEventHandler
public class TestApplicationEventHandler : DisposableObject, IApplicationEventHandler
{
public static void Reset()
{
Initialized = false;
Starting = false;
Started = false;
Disposed = false;
}
public static bool Initialized = false;
public static bool Starting = false;
public static bool Started = false;
public static bool Disposed = false;
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
@@ -86,20 +106,39 @@ namespace Umbraco.Tests.BootManagers
{
Started = true;
}
protected override void DisposeResources()
{
Disposed = true;
}
}
[Test]
public void Disposes_App_Startup_Handlers_After_Startup()
{
using (var app = new TestApp())
{
app.StartApplication(app, new EventArgs());
Assert.IsTrue(TestApplicationEventHandler.Disposed);
}
}
[Test]
public void Handle_IApplicationEventHandler_Objects_Outside_Web_Context()
{
_testApp.StartApplication(_testApp, new EventArgs());
using (var app = new TestApp())
{
app.StartApplication(app, new EventArgs());
Assert.IsTrue(TestApplicationEventHandler.Initialized);
Assert.IsTrue(TestApplicationEventHandler.Starting);
Assert.IsTrue(TestApplicationEventHandler.Started);
Assert.IsTrue(TestApplicationEventHandler.Initialized);
Assert.IsTrue(TestApplicationEventHandler.Starting);
Assert.IsTrue(TestApplicationEventHandler.Started);
}
}
[Test]
public void Raises_Events()
public void Raises_Starting_Events()
{
using (var app = new TestApp())
{
@@ -119,13 +158,11 @@ namespace Umbraco.Tests.BootManagers
app.ApplicationStarting += starting;
app.ApplicationStarted += started;
_testApp.StartApplication(_testApp, new EventArgs());
app.StartApplication(app, new EventArgs());
app.ApplicationStarting -= starting;
app.ApplicationStarting -= started;
}
}
}

View File

@@ -21,27 +21,6 @@ namespace Umbraco.Tests.Cache.PublishedCache
private ContextualPublishedContentCache _cache;
private XmlDocument _xml;
private string GetLegacyXml()
{
return @"<?xml version=""1.0"" encoding=""utf-8""?><!DOCTYPE root[
<!ELEMENT node ANY> <!ATTLIST node id ID #REQUIRED> <!ELEMENT data ANY>
]>
<root id=""-1"">
<node id=""1046"" parentID=""-1"" level=""1"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""1045"" sortOrder=""2"" createDate=""2012-06-12T14:13:17"" updateDate=""2012-07-20T18:50:43"" nodeName=""Home"" urlName=""home"" writerName=""admin"" creatorName=""admin"" path=""-1,1046"" nodeTypeAlias=""Home"" ><content><![CDATA[]]></content>
<node id=""1173"" parentID=""1046"" level=""2"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""1045"" sortOrder=""1"" createDate=""2012-07-20T18:06:45"" updateDate=""2012-07-20T19:07:31"" nodeName=""Sub1"" urlName=""sub1"" writerName=""admin"" creatorName=""admin"" path=""-1,1046,1173"" nodeTypeAlias=""Home""><content><![CDATA[]]></content>
<node id=""1174"" parentID=""1173"" level=""3"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""1045"" sortOrder=""1"" createDate=""2012-07-20T18:07:54"" updateDate=""2012-07-20T19:10:27"" nodeName=""Sub2"" urlName=""sub2"" writerName=""admin"" creatorName=""admin"" path=""-1,1046,1173,1174"" nodeTypeAlias=""Home"" ><content><![CDATA[]]></content>
</node>
<node id=""1176"" parentID=""1173"" level=""3"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""1045"" sortOrder=""2"" createDate=""2012-07-20T18:08:08"" updateDate=""2012-07-20T19:10:52"" nodeName=""Sub 3"" urlName=""sub-3"" writerName=""admin"" creatorName=""admin"" path=""-1,1046,1173,1176"" nodeTypeAlias=""Home"" ><content><![CDATA[]]></content>
</node>
</node>
<node id=""1175"" parentID=""1046"" level=""2"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""1045"" sortOrder=""2"" createDate=""2012-07-20T18:08:01"" updateDate=""2012-07-20T18:49:32"" nodeName=""Sub 2"" urlName=""sub-2"" writerName=""admin"" creatorName=""admin"" path=""-1,1046,1175"" nodeTypeAlias=""Home"" ><content><![CDATA[]]></content>
</node>
</node>
<node id=""1172"" parentID=""-1"" level=""1"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""1045"" sortOrder=""3"" createDate=""2012-07-16T15:26:59"" updateDate=""2012-07-18T14:23:35"" nodeName=""Test"" urlName=""test"" writerName=""admin"" creatorName=""admin"" path=""-1,1172"" nodeTypeAlias=""Home"" />
</root>";
}
private string GetXml()
{
return @"<?xml version=""1.0"" encoding=""utf-8""?><!DOCTYPE root[

View File

@@ -85,16 +85,6 @@ namespace Umbraco.Tests.TestHelpers
get { return _disabledCacheHelper ?? (_disabledCacheHelper = CacheHelper.CreateDisabledCacheHelper()); }
}
private MappingResolver _mappingResolver;
protected IMappingResolver MappingResolver
{
get
{
return _mappingResolver ??
(_mappingResolver = new MappingResolver(Container, Mock.Of<ILogger>(), () => PluginManager.Current.ResolveAssignedMapperTypes()));
}
}
protected virtual ISqlSyntaxProvider SqlSyntax
{
get { return new SqlCeSyntaxProvider(); }
@@ -118,12 +108,12 @@ namespace Umbraco.Tests.TestHelpers
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,
repositoryFactory,
new PetaPocoUnitOfWorkProvider(dbFactory),
new FileUnitOfWorkProvider(),
new PublishingStrategy(evtMsgs, Logger),
cacheHelper,
Logger,
evtMsgs,
Enumerable.Empty<IUrlSegmentProvider>()),
cacheHelper,
@@ -132,7 +122,7 @@ namespace Umbraco.Tests.TestHelpers
IsReady = true
};
base.Initialize();
ApplicationContext.Current = _appContext;
using (ProfilingLogger.TraceDuration<BaseDatabaseFactoryTest>("init"))
{
@@ -143,8 +133,8 @@ namespace Umbraco.Tests.TestHelpers
InitializeDatabase();
//ensure the configuration matches the current version for tests
SettingsForTests.ConfigurationStatus = UmbracoVersion.GetSemanticVersion().ToSemanticString();
}
SettingsForTests.ConfigurationStatus = UmbracoVersion.Current.ToString(3);
}
}
/// <summary>

View File

@@ -109,6 +109,16 @@ namespace Umbraco.Tests.TestHelpers
private static readonly object Locker = new object();
private MappingResolver _mappingResolver;
protected IMappingResolver MappingResolver
{
get
{
return _mappingResolver ??
(_mappingResolver = new MappingResolver(Container, Mock.Of<ILogger>(), () => PluginManager.Current.ResolveAssignedMapperTypes()));
}
}
private static void InitializeLegacyMappingsForCoreEditors()
{
lock (Locker)
@@ -181,7 +191,7 @@ namespace Umbraco.Tests.TestHelpers
{
var sqlSyntax = new SqlCeSyntaxProvider();
var repoFactory = new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), Logger, sqlSyntax, SettingsForTests.GenerateMockSettings(), Mock.Of<IMappingResolver>());
var repoFactory = new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), Logger, sqlSyntax, SettingsForTests.GenerateMockSettings(), MappingResolver);
var evtMsgs = new TransientMessagesFactory();
ApplicationContext.Current = new ApplicationContext(

View File

@@ -308,16 +308,24 @@ namespace Umbraco.Web
}
/// <summary>
/// Called to customize the IoC container
/// Build the core container which contains all core things requird to build an app context
/// </summary>
/// <param name="container"></param>
internal override void ConfigureServices(ServiceContainer container)
internal override void ConfigureCoreServices(ServiceContainer container)
{
base.ConfigureServices(container);
base.ConfigureCoreServices(container);
//Replace services:
container.Register<IEventMessagesFactory, RequestLifespanMessagesFactory>();
}
/// <summary>
/// Called to customize the IoC container
/// </summary>
/// <param name="container"></param>
internal override void ConfigureApplicationServices(ServiceContainer container)
{
base.ConfigureApplicationServices(container);
//IoC setup for LightInject for mvc/webapi
Container.EnableMvc();
Container.RegisterMvcControllers(PluginManager);