2020-11-10 08:50:47 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2020-09-21 21:06:24 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2020-11-17 07:56:04 +00:00
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
2020-11-18 17:40:23 +00:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-09-21 21:06:24 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-03-13 19:10:21 +11:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-09-21 21:06:24 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2020-03-13 19:10:21 +11:00
|
|
|
|
using Moq;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
using NUnit.Framework;
|
2020-09-15 15:14:44 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
using Umbraco.Core;
|
2020-11-18 17:40:23 +00:00
|
|
|
|
using Umbraco.Core.Builder;
|
2020-04-20 06:19:59 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
using Umbraco.Core.Composing;
|
2020-09-21 21:06:24 +02:00
|
|
|
|
using Umbraco.Core.Configuration.Models;
|
2020-11-10 08:50:47 +00:00
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
using Umbraco.Core.Persistence.Mappers;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
using Umbraco.Core.Runtime;
|
2020-09-21 21:06:24 +02:00
|
|
|
|
using Umbraco.Extensions;
|
2020-03-13 15:59:04 +11:00
|
|
|
|
using Umbraco.Tests.Common;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
using Umbraco.Tests.Integration.Extensions;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
using Umbraco.Tests.Integration.Implementations;
|
2020-03-24 11:53:56 +11:00
|
|
|
|
using Umbraco.Tests.Integration.Testing;
|
2020-11-18 17:40:23 +00:00
|
|
|
|
using Umbraco.Web.Common.Builder;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Integration
|
|
|
|
|
|
{
|
2020-03-24 11:53:56 +11:00
|
|
|
|
|
2020-03-13 14:43:41 +11:00
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class RuntimeTests
|
|
|
|
|
|
{
|
2020-03-24 14:48:32 +11:00
|
|
|
|
[TearDown]
|
|
|
|
|
|
public void TearDown()
|
|
|
|
|
|
{
|
|
|
|
|
|
MyComponent.Reset();
|
|
|
|
|
|
MyComposer.Reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-30 20:55:13 +11:00
|
|
|
|
[SetUp]
|
|
|
|
|
|
public void Setup()
|
2020-03-24 11:53:56 +11:00
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
MyComponent.Reset();
|
|
|
|
|
|
MyComposer.Reset();
|
2020-03-24 11:53:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-27 11:39:17 +01:00
|
|
|
|
/// Calling AddUmbracoCore to configure the container
|
2020-03-24 11:53:56 +11:00
|
|
|
|
/// </summary>
|
2020-03-13 19:10:21 +11:00
|
|
|
|
[Test]
|
2020-03-24 14:48:32 +11:00
|
|
|
|
public async Task AddUmbracoCore()
|
2020-03-13 19:10:21 +11:00
|
|
|
|
{
|
2020-03-25 18:21:44 +11:00
|
|
|
|
var testHelper = new TestHelper();
|
2020-03-24 11:53:56 +11:00
|
|
|
|
|
2020-03-24 14:48:32 +11:00
|
|
|
|
var hostBuilder = new HostBuilder()
|
2020-10-26 10:47:14 +00:00
|
|
|
|
.UseUmbraco()
|
2020-03-24 11:53:56 +11:00
|
|
|
|
.ConfigureServices((hostContext, services) =>
|
|
|
|
|
|
{
|
2020-03-25 18:21:44 +11:00
|
|
|
|
var webHostEnvironment = testHelper.GetWebHostEnvironment();
|
2020-04-29 08:56:42 +02:00
|
|
|
|
services.AddSingleton(testHelper.DbProviderFactoryCreator);
|
2020-03-30 20:55:13 +11:00
|
|
|
|
services.AddRequiredNetCoreServices(testHelper, webHostEnvironment);
|
2020-03-24 11:53:56 +11:00
|
|
|
|
|
|
|
|
|
|
// Add it!
|
2020-11-20 12:24:16 +00:00
|
|
|
|
var typeLoader = services.AddTypeLoader(GetType().Assembly, webHostEnvironment,
|
2020-11-20 11:48:32 +00:00
|
|
|
|
testHelper.ConsoleLoggerFactory, AppCaches.NoCache, hostContext.Configuration);
|
|
|
|
|
|
|
|
|
|
|
|
var builder = new UmbracoBuilder(services, hostContext.Configuration, typeLoader, testHelper.ConsoleLoggerFactory);
|
|
|
|
|
|
builder.Services.AddUnique<AppCaches>(AppCaches.NoCache);
|
2020-11-19 09:06:04 +00:00
|
|
|
|
builder.AddConfiguration();
|
2020-11-20 12:24:16 +00:00
|
|
|
|
builder.AddUmbracoCore();
|
2020-03-24 14:48:32 +11:00
|
|
|
|
});
|
2020-03-24 11:53:56 +11:00
|
|
|
|
|
2020-03-24 14:48:32 +11:00
|
|
|
|
var host = await hostBuilder.StartAsync();
|
2020-03-25 15:06:22 +11:00
|
|
|
|
var app = new ApplicationBuilder(host.Services);
|
2020-03-24 11:53:56 +11:00
|
|
|
|
|
2020-03-24 14:48:32 +11:00
|
|
|
|
// assert results
|
2020-03-25 15:06:22 +11:00
|
|
|
|
var runtimeState = app.ApplicationServices.GetRequiredService<IRuntimeState>();
|
|
|
|
|
|
var mainDom = app.ApplicationServices.GetRequiredService<IMainDom>();
|
2020-03-24 11:53:56 +11:00
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
|
Assert.IsFalse(mainDom.IsMainDom); // We haven't "Started" the runtime yet
|
2020-03-24 14:48:32 +11:00
|
|
|
|
Assert.IsNull(runtimeState.BootFailedException);
|
2020-03-25 15:06:22 +11:00
|
|
|
|
Assert.IsFalse(MyComponent.IsInit); // We haven't "Started" the runtime yet
|
2020-03-24 14:48:32 +11:00
|
|
|
|
|
|
|
|
|
|
await host.StopAsync();
|
2020-03-24 11:53:56 +11:00
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
|
Assert.IsFalse(MyComponent.IsTerminated); // we didn't "Start" the runtime so nothing was registered for shutdown
|
2020-03-24 11:53:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Calling AddUmbracoCore to configure the container and UseUmbracoCore to start the runtime
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2020-03-24 14:48:32 +11:00
|
|
|
|
[Test]
|
2020-03-25 15:06:22 +11:00
|
|
|
|
public async Task UseUmbracoCore()
|
2020-03-24 14:48:32 +11:00
|
|
|
|
{
|
2020-03-25 15:06:22 +11:00
|
|
|
|
var testHelper = new TestHelper();
|
2020-03-24 14:48:32 +11:00
|
|
|
|
|
|
|
|
|
|
var hostBuilder = new HostBuilder()
|
2020-10-26 10:47:14 +00:00
|
|
|
|
.UseUmbraco()
|
2020-03-24 14:48:32 +11:00
|
|
|
|
.ConfigureServices((hostContext, services) =>
|
2020-03-25 18:21:44 +11:00
|
|
|
|
{
|
|
|
|
|
|
var webHostEnvironment = testHelper.GetWebHostEnvironment();
|
2020-04-29 08:56:42 +02:00
|
|
|
|
services.AddSingleton(testHelper.DbProviderFactoryCreator);
|
2020-03-30 20:55:13 +11:00
|
|
|
|
services.AddRequiredNetCoreServices(testHelper, webHostEnvironment);
|
2020-11-20 11:48:32 +00:00
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
|
// Add it!
|
2020-11-19 09:06:04 +00:00
|
|
|
|
|
2020-11-20 12:24:16 +00:00
|
|
|
|
var typeLoader = services.AddTypeLoader(GetType().Assembly,
|
2020-11-20 11:48:32 +00:00
|
|
|
|
webHostEnvironment, testHelper.ConsoleLoggerFactory, AppCaches.NoCache,
|
|
|
|
|
|
hostContext.Configuration);
|
|
|
|
|
|
|
|
|
|
|
|
var builder = new UmbracoBuilder(services, hostContext.Configuration, typeLoader, testHelper.ConsoleLoggerFactory);
|
|
|
|
|
|
builder.Services.AddUnique<AppCaches>(AppCaches.NoCache);
|
2020-11-19 09:06:04 +00:00
|
|
|
|
builder.AddConfiguration()
|
2020-11-20 12:24:16 +00:00
|
|
|
|
.AddUmbracoCore()
|
2020-11-19 09:06:04 +00:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
2020-11-17 07:56:04 +00:00
|
|
|
|
services.AddRouting(); // LinkGenerator
|
2020-03-24 14:48:32 +11:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var host = await hostBuilder.StartAsync();
|
2020-03-25 15:06:22 +11:00
|
|
|
|
var app = new ApplicationBuilder(host.Services);
|
2020-03-24 14:48:32 +11:00
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
|
app.UseUmbracoCore();
|
2020-03-24 14:48:32 +11:00
|
|
|
|
|
|
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
|
// assert results
|
|
|
|
|
|
var runtimeState = app.ApplicationServices.GetRequiredService<IRuntimeState>();
|
|
|
|
|
|
var mainDom = app.ApplicationServices.GetRequiredService<IMainDom>();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(mainDom.IsMainDom);
|
|
|
|
|
|
Assert.IsNull(runtimeState.BootFailedException);
|
|
|
|
|
|
Assert.IsTrue(MyComponent.IsInit);
|
|
|
|
|
|
|
|
|
|
|
|
await host.StopAsync();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(MyComponent.IsTerminated);
|
2020-03-24 14:48:32 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-13 15:59:04 +11:00
|
|
|
|
public class MyComposer : IUserComposer
|
|
|
|
|
|
{
|
2020-11-18 17:40:23 +00:00
|
|
|
|
public void Compose(IUmbracoBuilder builder)
|
2020-03-13 15:59:04 +11:00
|
|
|
|
{
|
2020-11-18 17:40:23 +00:00
|
|
|
|
builder.Components().Append<MyComponent>();
|
2020-03-13 15:59:04 +11:00
|
|
|
|
IsComposed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-24 14:48:32 +11:00
|
|
|
|
public static void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsComposed = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-13 15:59:04 +11:00
|
|
|
|
public static bool IsComposed { get; private set; }
|
|
|
|
|
|
}
|
2020-03-23 17:35:04 +11:00
|
|
|
|
|
|
|
|
|
|
public class MyComponent : IComponent
|
|
|
|
|
|
{
|
|
|
|
|
|
public static bool IsInit { get; private set; }
|
|
|
|
|
|
public static bool IsTerminated { get; private set; }
|
|
|
|
|
|
|
2020-09-21 13:04:57 +02:00
|
|
|
|
private readonly ILogger<MyComponent> _logger;
|
2020-03-23 17:35:04 +11:00
|
|
|
|
|
2020-09-21 13:04:57 +02:00
|
|
|
|
public MyComponent(ILogger<MyComponent> logger)
|
2020-03-23 17:35:04 +11:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsInit = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Terminate()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsTerminated = true;
|
|
|
|
|
|
}
|
2020-03-24 14:48:32 +11:00
|
|
|
|
|
|
|
|
|
|
public static void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsTerminated = false;
|
|
|
|
|
|
IsInit = false;
|
|
|
|
|
|
}
|
2020-03-23 17:35:04 +11:00
|
|
|
|
}
|
2020-03-13 14:43:41 +11:00
|
|
|
|
}
|
2020-03-13 15:59:04 +11:00
|
|
|
|
|
2020-03-27 11:39:17 +01:00
|
|
|
|
|
2020-03-13 14:43:41 +11:00
|
|
|
|
}
|