From 713484eb262c79020f7be6cbd1fbc628fb61f386 Mon Sep 17 00:00:00 2001 From: Stephan Date: Sun, 7 Aug 2016 16:45:41 +0200 Subject: [PATCH] Resvolution - Prepare, Tests --- .../CollectionBuilderBase.cs | 9 +- .../BootManagers/CoreBootManagerTests.cs | 2 + .../DistributedCache/DistributedCacheTests.cs | 2 + .../PublishedMediaCacheTests.cs | 5 +- .../PublishedContentMoreTests.cs | 2 + .../PublishedContent/PublishedContentTests.cs | 1 + .../PublishedContent/PublishedMediaTests.cs | 5 +- .../Resolvers/LazyCollectionBuilderTests.cs | 185 +++++++++++++++++ .../Resolvers/LazyManyObjectResolverTests.cs | 190 ------------------ .../Resolvers/ResolverBaseTest.cs | 7 + .../TestHelpers/BaseUmbracoApplicationTest.cs | 2 + src/Umbraco.Tests/Umbraco.Tests.csproj | 2 +- 12 files changed, 212 insertions(+), 200 deletions(-) create mode 100644 src/Umbraco.Tests/Resolvers/LazyCollectionBuilderTests.cs delete mode 100644 src/Umbraco.Tests/Resolvers/LazyManyObjectResolverTests.cs diff --git a/src/Umbraco.Core/DependencyInjection/CollectionBuilderBase.cs b/src/Umbraco.Core/DependencyInjection/CollectionBuilderBase.cs index f04b70a649..4a8c2ccf8a 100644 --- a/src/Umbraco.Core/DependencyInjection/CollectionBuilderBase.cs +++ b/src/Umbraco.Core/DependencyInjection/CollectionBuilderBase.cs @@ -124,9 +124,16 @@ namespace Umbraco.Core.DependencyInjection { if (_registrations != null) return; + var types = GetTypes(_types).ToArray(); + foreach (var type in types) + { + if (typeof(TItem).IsAssignableFrom(type) == false) + throw new InvalidOperationException($"Cannot register type {type.FullName} as it does not inherit from/implement {typeof(TItem).FullName}."); + } + var prefix = GetType().FullName + "_"; var i = 0; - foreach (var type in GetTypes(_types)) + foreach (var type in types) { var name = $"{prefix}{i++:00000}"; Container.Register(typeof(TItem), type, name); diff --git a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs index ca4b049745..782bd811bf 100644 --- a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs +++ b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs @@ -31,6 +31,8 @@ namespace Umbraco.Tests.BootManagers base.TearDown(); ResolverCollection.ResetAll(); TestApplicationEventHandler.Reset(); + + Current.Reset(); } diff --git a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs index 41c8a92cba..ba6d75305e 100644 --- a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs +++ b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs @@ -39,6 +39,8 @@ namespace Umbraco.Tests.Cache.DistributedCache { ServerRegistrarResolver.Reset(); ServerMessengerResolver.Reset(); + + Current.Reset(); } [Test] diff --git a/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs b/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs index 9c59c22151..ac4f2f99a0 100644 --- a/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs +++ b/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs @@ -30,10 +30,7 @@ namespace Umbraco.Tests.Cache.PublishedCache { protected override void FreezeResolution() { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - - UrlSegmentProviderCollectionBuilder.Register(container) + UrlSegmentProviderCollectionBuilder.Register(Container) .Append(); PublishedContentModelFactoryResolver.Current = new PublishedContentModelFactoryResolver(); diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs index f8cd3023a6..4e12b48d8f 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs @@ -79,6 +79,8 @@ namespace Umbraco.Tests.PublishedContent public override void TearDown() { + base.TearDown(); + PluginManager.Current = _pluginManager; ApplicationContext.Current.DisposeIfDisposable(); ApplicationContext.Current = null; diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs index 0c005753cc..6269e0f768 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs @@ -56,6 +56,7 @@ namespace Umbraco.Tests.PublishedContent public override void TearDown() { + base.TearDown(); PluginManager.Current = _pluginManager; ApplicationContext.Current.DisposeIfDisposable(); ApplicationContext.Current = null; diff --git a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs index 08ecb4b02e..955fd82e8c 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs @@ -37,10 +37,7 @@ namespace Umbraco.Tests.PublishedContent /// protected override void FreezeResolution() { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - - UrlSegmentProviderCollectionBuilder.Register(container) + UrlSegmentProviderCollectionBuilder.Register(Container) .Append(); base.FreezeResolution(); diff --git a/src/Umbraco.Tests/Resolvers/LazyCollectionBuilderTests.cs b/src/Umbraco.Tests/Resolvers/LazyCollectionBuilderTests.cs new file mode 100644 index 0000000000..fcaac5a80c --- /dev/null +++ b/src/Umbraco.Tests/Resolvers/LazyCollectionBuilderTests.cs @@ -0,0 +1,185 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using LightInject; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.DependencyInjection; + +namespace Umbraco.Tests.Resolvers +{ + [TestFixture] + public class LazyCollectionBuilderTests + { + [SetUp] + public void Initialize() + { + Current.Reset(); + } + + [TearDown] + public void TearDown() + { + Current.Reset(); + } + + // note + // 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 + + [Test] + public void LazyCollectionBuilderTypes() + { + var container = new ServiceContainer(); + container.ConfigureUmbracoCore(); + + TestCollectionBuilder.Register(container) + .Add() + .Add() + .Add() + .Add(); + + var values = container.GetInstance(); + + Assert.AreEqual(3, values.Count()); + Assert.IsTrue(values.Select(x => x.GetType()) + .ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); + + var other = container.GetInstance(); + Assert.AreNotSame(values, other); // transient + var o1 = other.FirstOrDefault(x => x is TransientObject1); + Assert.IsFalse(values.Contains(o1)); // transient + } + + [Test] + public void LazyCollectionBuilderProducers() + { + var container = new ServiceContainer(); + container.ConfigureUmbracoCore(); + + TestCollectionBuilder.Register(container) + .AddProducer(() => new[] { typeof(TransientObject3), typeof(TransientObject2) }) + .AddProducer(() => new[] { typeof(TransientObject3), typeof(TransientObject2) }) + .AddProducer(() => new[] { typeof(TransientObject1) }); + + var values = container.GetInstance(); + + Assert.AreEqual(3, values.Count()); + Assert.IsTrue(values.Select(x => x.GetType()) + .ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); + + var other = container.GetInstance(); + Assert.AreNotSame(values, other); // transient + var o1 = other.FirstOrDefault(x => x is TransientObject1); + Assert.IsFalse(values.Contains(o1)); // transient + } + + [Test] + public void LazyCollectionBuilderBoth() + { + var container = new ServiceContainer(); + container.ConfigureUmbracoCore(); + + TestCollectionBuilder.Register(container) + .Add() + .Add() + .Add() + .AddProducer(() => new[] { typeof(TransientObject1) }); + + var values = container.GetInstance(); + + Assert.AreEqual(3, values.Count()); + Assert.IsTrue(values.Select(x => x.GetType()) + .ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); + + var other = container.GetInstance(); + Assert.AreNotSame(values, other); // transient + var o1 = other.FirstOrDefault(x => x is TransientObject1); + Assert.IsFalse(values.Contains(o1)); // transient + } + + [Test] + public void LazyCollectionBuilderThrows() + { + var container = new ServiceContainer(); + container.ConfigureUmbracoCore(); + + TestCollectionBuilder.Register(container) + .Add() + + // illegal, does not implement the interface! + //.Add() + + // legal so far... + .AddProducer(() => new[] { typeof(TransientObject4) }); + + Assert.Throws(() => + { + // but throws here when trying to register the types + var values = container.GetInstance(); + }); + } + + [Test] + public void LazyCollectionBuilderExclude() + { + var container = new ServiceContainer(); + container.ConfigureUmbracoCore(); + + TestCollectionBuilder.Register(container) + .Add() + .AddProducer(() => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) }) + .Exclude(); + + var values = container.GetInstance(); + + 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) })); + + var other = container.GetInstance(); + Assert.AreNotSame(values, other); // transient + var o1 = other.FirstOrDefault(x => x is TransientObject1); + Assert.IsFalse(values.Contains(o1)); // transient + } + + #region Test classes + + private interface ITestInterface + { } + + private class TransientObject1 : ITestInterface + { } + + private class TransientObject2 : ITestInterface + { } + + private class TransientObject3 : ITestInterface + { } + + private class TransientObject4 + { } + + private class TestCollectionBuilder : LazyCollectionBuilderBase + { + public TestCollectionBuilder(IServiceContainer container) + : base(container) + { } + + protected override TestCollectionBuilder This => this; + + protected override ILifetime CollectionLifetime => null; // transient + } + + private class TestCollection : BuilderCollectionBase + { + public TestCollection(IEnumerable items) + : base(items) + { } + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Resolvers/LazyManyObjectResolverTests.cs b/src/Umbraco.Tests/Resolvers/LazyManyObjectResolverTests.cs deleted file mode 100644 index 9fcfdfe382..0000000000 --- a/src/Umbraco.Tests/Resolvers/LazyManyObjectResolverTests.cs +++ /dev/null @@ -1,190 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using Moq; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.Logging; -using Umbraco.Core.ObjectResolution; - -namespace Umbraco.Tests.Resolvers -{ - [TestFixture] - public class LazyManyObjectResolverTests - { - - [SetUp] - public void Initialize() - { - LazyResolver.Reset(); - } - - [TearDown] - public void TearDown() - { - LazyResolver.Reset(); - } - - [Test] - public void LazyResolverResolvesLazyTypes() - { - var resolver = new LazyResolver( - new ActivatorServiceProvider(), - Mock.Of(), - new[] { new Lazy(() => typeof (TransientObject3)) }); - - resolver.AddType(); - resolver.AddType(new Lazy(() => typeof(TransientObject2))); - - Resolution.Freeze(); - - Assert.IsFalse(resolver.HasResolvedTypes); - - var values = resolver.Objects; - - Assert.IsTrue(resolver.HasResolvedTypes); - - Assert.AreEqual(3, values.Count()); - Assert.IsTrue(values.Select(x => x.GetType()) - .ContainsAll(new [] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); - } - - [Test] - public void LazyResolverResolvesTypeProducers() - { - Func> types = () => new[] { typeof(TransientObject3), typeof(TransientObject2) }; - - var resolver = new LazyResolver( - new ActivatorServiceProvider(), - Mock.Of(), - types); - resolver.AddTypeListDelegate(() => new[] { typeof(TransientObject1)}); - - Resolution.Freeze(); - - var values = resolver.Objects; - - Assert.AreEqual(3, values.Count()); - Assert.IsTrue(values.Select(x => x.GetType()) - .ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); - } - - [Test] - public void LazyResolverResolvesBothWays() - { - Func> types = () => new[] { typeof(TransientObject3) }; - - var resolver = new LazyResolver( - new ActivatorServiceProvider(), - Mock.Of(), - types); - resolver.AddType(new Lazy(() => typeof(TransientObject2))); - resolver.AddType(); - - Resolution.Freeze(); - - var values = resolver.Objects; - - Assert.AreEqual(3, values.Count()); - Assert.IsTrue(values.Select(x => x.GetType()) - .ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) })); - } - - [Test] - public void LazyResolverThrowsOnDuplicate() - { - Func> types = () => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) }; - - var resolver = new LazyResolver( - new ActivatorServiceProvider(), - Mock.Of(), - types); - - // duplicate, but will not throw here - resolver.AddType(); - - Resolution.Freeze(); - - Assert.Throws(() => - { - var values = resolver.Objects; - }); - } - - [Test] - public void LazyResolverThrowsOnInvalidType() - { - Func> types = () => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) }; - - var resolver = new LazyResolver( - new ActivatorServiceProvider(), - Mock.Of(), - types); - - // invalid, but will not throw here - resolver.AddType(new Lazy(() => typeof(TransientObject4))); - - Resolution.Freeze(); - - Assert.Throws(() => - { - var values = resolver.Objects; - }); - } - - [Test] - public void LazyResolverSupportsRemove() - { - Func> types = () => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) }; - - var resolver = new LazyResolver( - new ActivatorServiceProvider(), - Mock.Of(), - types); - - resolver.RemoveType(typeof(TransientObject3)); - - Resolution.Freeze(); - - var values = resolver.Objects; - Assert.IsFalse(values.Select(x => x.GetType()).Contains(typeof(TransientObject3))); - Assert.IsTrue(values.Select(x => x.GetType()).ContainsAll(new[] { typeof(TransientObject2), typeof(TransientObject1) })); - } - - #region Test classes - - private interface ITestInterface - { } - - private class TransientObject1 : ITestInterface - { } - - private class TransientObject2 : ITestInterface - { } - - private class TransientObject3 : ITestInterface - { } - - private class TransientObject4 - { } - - private sealed class LazyResolver : LazyManyObjectsResolverBase - { - public LazyResolver(IServiceProvider serviceProvider, ILogger logger, IEnumerable> values) - :base (serviceProvider, logger, values, ObjectLifetimeScope.Transient) - { } - - public LazyResolver(IServiceProvider serviceProvider, ILogger logger, Func> typeList) - : base(serviceProvider, logger, typeList, ObjectLifetimeScope.Transient) - { } - - public IEnumerable Objects - { - get { return Values; } - } - } - - #endregion - } -} \ No newline at end of file diff --git a/src/Umbraco.Tests/Resolvers/ResolverBaseTest.cs b/src/Umbraco.Tests/Resolvers/ResolverBaseTest.cs index a03d5ff878..7244003015 100644 --- a/src/Umbraco.Tests/Resolvers/ResolverBaseTest.cs +++ b/src/Umbraco.Tests/Resolvers/ResolverBaseTest.cs @@ -5,6 +5,7 @@ using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Logging; +using Umbraco.Core.ObjectResolution; using Umbraco.Core.Plugins; using Umbraco.Core.Profiling; using Umbraco.Core._Legacy.PackageActions; @@ -32,6 +33,12 @@ namespace Umbraco.Tests.Resolvers }; } + [TearDown] + public void TearDown() + { + Resolution.Reset(); + } + protected virtual IEnumerable AssembliesToScan { get diff --git a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs index 1e9461ee9e..9d01fd1eea 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs @@ -87,6 +87,8 @@ namespace Umbraco.Tests.TestHelpers // reset plugin manager ResetPluginManager(); Container.Dispose(); + + Core.DependencyInjection.Current.Reset(); } protected virtual void ConfigureContainer() diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 493e92e5ea..d3915c1403 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -497,7 +497,7 @@ - +