Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Composing/LazyCollectionBuilderTests.cs

193 lines
7.6 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
2016-08-07 16:45:41 +02:00
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2018-11-28 11:05:41 +01:00
using Moq;
2016-08-07 16:45:41 +02:00
using NUnit.Framework;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Tests.UnitTests.TestHelpers;
using Umbraco.Extensions;
2016-08-07 16:45:41 +02:00
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Composing
2016-08-07 16:45:41 +02:00
{
/// <summary>
/// Tests for lazy collection builder.
/// </summary>
/// <remarks>
/// Lazy collection builder does not throw on duplicate, just uses distinct types
/// so we don't have a test for duplicates as we had with resolvers in v7.
/// </remarks>
2017-07-20 11:21:28 +02:00
[TestFixture]
public class LazyCollectionBuilderTests
{
private IServiceCollection CreateRegister() => TestHelper.GetServiceCollection();
2016-08-07 16:45:41 +02:00
[Test]
2018-02-02 19:43:03 +01:00
public void LazyCollectionBuilderHandlesTypes()
2016-08-07 16:45:41 +02:00
{
IServiceCollection container = CreateRegister();
var composition = new UmbracoBuilder(container, Mock.Of<IConfiguration>(), TestHelper.GetMockedTypeLoader());
2018-11-28 17:35:12 +01:00
composition.WithCollectionBuilder<TestCollectionBuilder>()
2016-08-07 16:45:41 +02:00
.Add<TransientObject3>()
.Add<TransientObject2>()
.Add<TransientObject3>()
.Add<TransientObject1>();
IServiceProvider factory = composition.CreateServiceProvider();
2018-11-28 12:59:40 +01:00
TestCollection values = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreEqual(3, values.Count());
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
TestCollection other = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreNotSame(values, other); // transient
ITestInterface o1 = other.FirstOrDefault(x => x is TransientObject1);
2016-08-07 16:45:41 +02:00
Assert.IsFalse(values.Contains(o1)); // transient
}
[Test]
2018-02-02 19:43:03 +01:00
public void LazyCollectionBuilderHandlesProducers()
2016-08-07 16:45:41 +02:00
{
IServiceCollection container = CreateRegister();
var composition = new UmbracoBuilder(container, Mock.Of<IConfiguration>(), TestHelper.GetMockedTypeLoader());
2018-11-28 17:35:12 +01:00
composition.WithCollectionBuilder<TestCollectionBuilder>()
2016-09-23 20:18:25 +02:00
.Add(() => new[] { typeof(TransientObject3), typeof(TransientObject2) })
.Add(() => new[] { typeof(TransientObject3), typeof(TransientObject2) })
.Add(() => new[] { typeof(TransientObject1) });
2016-08-07 16:45:41 +02:00
IServiceProvider factory = composition.CreateServiceProvider();
2018-11-28 12:59:40 +01:00
TestCollection values = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreEqual(3, values.Count());
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
TestCollection other = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreNotSame(values, other); // transient
ITestInterface o1 = other.FirstOrDefault(x => x is TransientObject1);
2016-08-07 16:45:41 +02:00
Assert.IsFalse(values.Contains(o1)); // transient
}
[Test]
2018-02-02 19:43:03 +01:00
public void LazyCollectionBuilderHandlesTypesAndProducers()
2016-08-07 16:45:41 +02:00
{
IServiceCollection container = CreateRegister();
var composition = new UmbracoBuilder(container, Mock.Of<IConfiguration>(), TestHelper.GetMockedTypeLoader());
2018-11-28 17:35:12 +01:00
composition.WithCollectionBuilder<TestCollectionBuilder>()
2016-08-07 16:45:41 +02:00
.Add<TransientObject3>()
.Add<TransientObject2>()
.Add<TransientObject3>()
2016-09-23 20:18:25 +02:00
.Add(() => new[] { typeof(TransientObject1) });
2016-08-07 16:45:41 +02:00
IServiceProvider factory = composition.CreateServiceProvider();
2018-11-28 12:59:40 +01:00
TestCollection values = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreEqual(3, values.Count());
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
TestCollection other = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreNotSame(values, other); // transient
ITestInterface o1 = other.FirstOrDefault(x => x is TransientObject1);
2016-08-07 16:45:41 +02:00
Assert.IsFalse(values.Contains(o1)); // transient
}
2017-07-20 11:21:28 +02:00
[Test]
2018-02-02 19:43:03 +01:00
public void LazyCollectionBuilderThrowsOnIllegalTypes()
2017-07-20 11:21:28 +02:00
{
IServiceCollection container = CreateRegister();
var composition = new UmbracoBuilder(container, Mock.Of<IConfiguration>(), TestHelper.GetMockedTypeLoader());
2016-08-07 16:45:41 +02:00
2018-11-28 17:35:12 +01:00
composition.WithCollectionBuilder<TestCollectionBuilder>()
2016-08-07 16:45:41 +02:00
.Add<TransientObject3>()
// illegal, does not implement the interface!
////.Add<TransientObject4>()
2016-08-07 16:45:41 +02:00
// legal so far...
2016-09-23 20:18:25 +02:00
.Add(() => new[] { typeof(TransientObject4) });
2016-08-07 16:45:41 +02:00
2017-07-20 11:21:28 +02:00
Assert.Throws<InvalidOperationException>(() =>
{
// but throws here when trying to register the types, right before creating the factory
IServiceProvider factory = composition.CreateServiceProvider();
2017-07-20 11:21:28 +02:00
});
}
2016-08-07 16:45:41 +02:00
[Test]
2018-02-02 19:43:03 +01:00
public void LazyCollectionBuilderCanExcludeTypes()
2016-08-07 16:45:41 +02:00
{
IServiceCollection container = CreateRegister();
var composition = new UmbracoBuilder(container, Mock.Of<IConfiguration>(), TestHelper.GetMockedTypeLoader());
2016-08-07 16:45:41 +02:00
2018-11-28 17:35:12 +01:00
composition.WithCollectionBuilder<TestCollectionBuilder>()
2016-08-07 16:45:41 +02:00
.Add<TransientObject3>()
2016-09-23 20:18:25 +02:00
.Add(() => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) })
2016-08-07 16:45:41 +02:00
.Exclude<TransientObject3>();
IServiceProvider factory = composition.CreateServiceProvider();
2018-11-28 12:59:40 +01:00
TestCollection values = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreEqual(2, values.Count());
Assert.IsFalse(values.Select(x => x.GetType())
.Contains(typeof(TransientObject3)));
Assert.IsTrue(values.Select(x => x.GetType())
.ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2) }));
TestCollection other = factory.GetRequiredService<TestCollection>();
2016-08-07 16:45:41 +02:00
Assert.AreNotSame(values, other); // transient
ITestInterface o1 = other.FirstOrDefault(x => x is TransientObject1);
2016-08-07 16:45:41 +02:00
Assert.IsFalse(values.Contains(o1)); // transient
}
2017-07-20 11:21:28 +02:00
private interface ITestInterface
{
}
2016-08-07 16:45:41 +02:00
2017-07-20 11:21:28 +02:00
private class TransientObject1 : ITestInterface
{
}
2016-08-07 16:45:41 +02:00
2017-07-20 11:21:28 +02:00
private class TransientObject2 : ITestInterface
{
}
2016-08-07 16:45:41 +02:00
2017-07-20 11:21:28 +02:00
private class TransientObject3 : ITestInterface
{
}
2016-08-07 16:45:41 +02:00
private class TransientObject4
{
}
2016-08-07 16:45:41 +02:00
2018-02-02 19:43:03 +01:00
// ReSharper disable once ClassNeverInstantiated.Local
2016-08-07 16:45:41 +02:00
private class TestCollectionBuilder : LazyCollectionBuilderBase<TestCollectionBuilder, TestCollection, ITestInterface>
{
protected override TestCollectionBuilder This => this;
protected override ServiceLifetime CollectionLifetime => ServiceLifetime.Transient; // transient
2016-08-07 16:45:41 +02:00
}
2018-02-02 19:43:03 +01:00
// ReSharper disable once ClassNeverInstantiated.Local
2016-08-07 16:45:41 +02:00
private class TestCollection : BuilderCollectionBase<ITestInterface>
{
public TestCollection(Func<IEnumerable<ITestInterface>> items) : base(items)
{
}
2016-08-07 16:45:41 +02:00
}
2017-07-20 11:21:28 +02:00
}
}