Files
Umbraco-CMS/src/Umbraco.Tests/Components/ComponentTests.cs

221 lines
7.7 KiB
C#
Raw Normal View History

2016-08-25 15:09:51 +02:00
using System;
using System.Collections.Generic;
2016-12-15 08:57:23 +01:00
using System.Linq;
2016-08-25 15:09:51 +02:00
using LightInject;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
2016-08-25 15:09:51 +02:00
using Umbraco.Core.Components;
using Umbraco.Core.Logging;
2016-12-15 08:57:23 +01:00
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Mappers;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.TestHelpers.Stubs;
2016-08-25 15:09:51 +02:00
namespace Umbraco.Tests.Components
{
[TestFixture]
2016-11-05 19:23:55 +01:00
public class ComponentTests
2016-08-25 15:09:51 +02:00
{
private static readonly List<Type> Composed = new List<Type>();
private static readonly List<string> Initialized = new List<string>();
2016-11-05 19:23:55 +01:00
private static IServiceContainer MockContainer(Action<Mock<IServiceContainer>> setup = null)
{
2016-12-15 08:57:23 +01:00
// fixme use IDatabaseFactory vs UmbracoDatabaseFactory, clean it all up!
var testObjects = new TestObjects(null);
var logger = Mock.Of<ILogger>();
var s = testObjects.GetDefaultSqlSyntaxProviders(logger);
var f = new UmbracoDatabaseFactory(s, logger, new TestDatabaseScopeAccessor(), new MapperCollection(Enumerable.Empty<BaseMapper>()));
2016-11-05 19:23:55 +01:00
var mock = new Mock<IServiceContainer>();
mock.Setup(x => x.GetInstance<ProfilingLogger>()).Returns(new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
2016-12-15 08:57:23 +01:00
mock.Setup(x => x.GetInstance<DatabaseContext>()).Returns(new DatabaseContext(f));
2016-11-05 19:23:55 +01:00
setup?.Invoke(mock);
return mock.Object;
}
2016-08-25 15:09:51 +02:00
[Test]
public void Boot()
{
2016-11-05 19:23:55 +01:00
var container = MockContainer();
2016-08-25 15:09:51 +02:00
var thing = new BootLoader(container);
Composed.Clear();
thing.Boot(new [] { typeof (Component1), typeof (Component2), typeof (Component3), typeof (Component4) }, RuntimeLevel.Unknown);
2016-08-25 15:09:51 +02:00
Assert.AreEqual(4, Composed.Count);
Assert.AreEqual(typeof(Component1), Composed[0]);
Assert.AreEqual(typeof(Component4), Composed[1]);
Assert.AreEqual(typeof(Component2), Composed[2]);
Assert.AreEqual(typeof(Component3), Composed[3]);
}
[Test]
public void BrokenDependency()
{
2016-11-05 19:23:55 +01:00
var container = MockContainer();
2016-08-25 15:09:51 +02:00
var thing = new BootLoader(container);
Composed.Clear();
try
{
thing.Boot(new[] { typeof(Component1), typeof(Component2), typeof(Component3) }, RuntimeLevel.Unknown);
2016-08-25 15:09:51 +02:00
Assert.Fail("Expected exception.");
}
catch (Exception e)
{
Assert.AreEqual("Broken component dependency: Umbraco.Tests.Components.ComponentTests+Component2 -> Umbraco.Tests.Components.ComponentTests+Component4.", e.Message);
}
}
[Test]
public void Initialize()
{
2016-11-05 19:23:55 +01:00
var container = MockContainer(m =>
{
m.Setup(x => x.TryGetInstance(It.Is<Type>(t => t == typeof (ISomeResource)))).Returns(() => new SomeResource());
});
2016-08-25 15:09:51 +02:00
var thing = new BootLoader(container);
Composed.Clear();
thing.Boot(new[] { typeof(Component1), typeof(Component5) }, RuntimeLevel.Unknown);
2016-08-25 15:09:51 +02:00
Assert.AreEqual(2, Composed.Count);
Assert.AreEqual(typeof(Component1), Composed[0]);
Assert.AreEqual(typeof(Component5), Composed[1]);
Assert.AreEqual(1, Initialized.Count);
Assert.AreEqual("Umbraco.Tests.Components.ComponentTests+SomeResource", Initialized[0]);
}
2016-08-31 18:25:18 +02:00
[Test]
public void Requires1()
2016-08-25 15:09:51 +02:00
{
2016-11-05 19:23:55 +01:00
var container = MockContainer();
2016-08-31 18:25:18 +02:00
var thing = new BootLoader(container);
Composed.Clear();
thing.Boot(new[] { typeof(Component6), typeof(Component7), typeof(Component8) }, RuntimeLevel.Unknown);
2016-08-31 18:25:18 +02:00
Assert.AreEqual(2, Composed.Count);
Assert.AreEqual(typeof(Component6), Composed[0]);
Assert.AreEqual(typeof(Component8), Composed[1]);
2016-08-25 15:09:51 +02:00
}
2016-08-31 18:25:18 +02:00
[Test]
public void Requires2()
2016-08-25 15:09:51 +02:00
{
2016-11-05 19:23:55 +01:00
var container = MockContainer();
2016-08-31 18:25:18 +02:00
var thing = new BootLoader(container);
Composed.Clear();
thing.Boot(new[] { typeof(Component9), typeof(Component2), typeof(Component4) }, RuntimeLevel.Unknown);
2016-08-31 18:25:18 +02:00
Assert.AreEqual(3, Composed.Count);
Assert.AreEqual(typeof(Component4), Composed[0]);
Assert.AreEqual(typeof(Component2), Composed[1]);
Assert.AreEqual(typeof(Component9), Composed[2]);
2016-08-25 15:09:51 +02:00
}
[Test]
public void WeakDependencies()
{
2016-11-05 19:23:55 +01:00
var container = MockContainer();
var thing = new BootLoader(container);
Composed.Clear();
thing.Boot(new[] { typeof(Component10) }, RuntimeLevel.Unknown);
Assert.AreEqual(1, Composed.Count);
Assert.AreEqual(typeof(Component10), Composed[0]);
thing = new BootLoader(container);
Composed.Clear();
Assert.Throws<Exception>(() => thing.Boot(new[] { typeof(Component11) }, RuntimeLevel.Unknown));
thing = new BootLoader(container);
Composed.Clear();
Assert.Throws<Exception>(() => thing.Boot(new[] { typeof(Component2) }, RuntimeLevel.Unknown));
thing = new BootLoader(container);
Composed.Clear();
thing.Boot(new[] { typeof(Component12) }, RuntimeLevel.Unknown);
Assert.AreEqual(1, Composed.Count);
Assert.AreEqual(typeof(Component12), Composed[0]);
}
[Test]
public void DisableMissing()
{
2016-11-05 19:23:55 +01:00
var container = MockContainer();
var thing = new BootLoader(container);
Composed.Clear();
thing.Boot(new[] { typeof(Component6), typeof(Component8) }, RuntimeLevel.Unknown); // 8 disables 7 which is not in the list
Assert.AreEqual(2, Composed.Count);
Assert.AreEqual(typeof(Component6), Composed[0]);
Assert.AreEqual(typeof(Component8), Composed[1]);
}
2016-08-31 18:25:18 +02:00
public class TestComponentBase : UmbracoComponentBase
2016-08-25 15:09:51 +02:00
{
public override void Compose(Composition composition)
2016-08-25 15:09:51 +02:00
{
base.Compose(composition);
2016-08-25 15:09:51 +02:00
Composed.Add(GetType());
}
}
2016-08-31 18:25:18 +02:00
public class Component1 : TestComponentBase
{ }
[RequireComponent(typeof(Component4))]
public class Component2 : TestComponentBase, IUmbracoCoreComponent
{ }
public class Component3 : TestComponentBase, IUmbracoUserComponent
{ }
2016-08-25 15:09:51 +02:00
2016-08-31 18:25:18 +02:00
public class Component4 : TestComponentBase
{ }
public class Component5 : TestComponentBase
{
2016-08-25 15:09:51 +02:00
public void Initialize(ISomeResource resource)
{
Initialized.Add(resource.GetType().FullName);
}
}
2016-08-31 18:25:18 +02:00
[DisableComponent]
public class Component6 : TestComponentBase
{ }
public class Component7 : TestComponentBase
{ }
[DisableComponent(typeof(Component7))]
[EnableComponent(typeof(Component6))]
public class Component8 : TestComponentBase
{ }
public interface ITestComponent : IUmbracoUserComponent
{ }
public class Component9 : TestComponentBase, ITestComponent
{ }
[RequireComponent(typeof(ITestComponent))]
public class Component10 : TestComponentBase
{ }
[RequireComponent(typeof(ITestComponent), false)]
public class Component11 : TestComponentBase
{ }
[RequireComponent(typeof(Component4), true)]
public class Component12 : TestComponentBase, IUmbracoCoreComponent
{ }
2016-08-25 15:09:51 +02:00
public interface ISomeResource { }
public class SomeResource : ISomeResource { }
}
}