|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using Implab.Components;
|
|
|
using Implab.Diagnostics;
|
|
|
using Implab.ServiceHost.Test.Mock;
|
|
|
using Implab.ServiceHost.Unity;
|
|
|
using Unity;
|
|
|
using Xunit;
|
|
|
|
|
|
namespace Implab.Test {
|
|
|
|
|
|
public class UnityConfigTest {
|
|
|
|
|
|
[Fact]
|
|
|
public void CreateContainer() {
|
|
|
var container = new UnityContainer();
|
|
|
|
|
|
container.LoadXmlConfiguration(Path.Combine("data","container","empty.xml"));
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void SimpleServicesContainer() {
|
|
|
var container = new UnityContainer();
|
|
|
|
|
|
container.LoadXmlConfiguration(Path.Combine("data","container","simple.services.xml"));
|
|
|
|
|
|
// named service registration
|
|
|
var baz1 = container.Resolve<Baz>("Baz1");
|
|
|
|
|
|
Assert.Equal(new [] {1,2,3}, baz1.Numbers);
|
|
|
|
|
|
// default service registration
|
|
|
var baz = container.Resolve<Baz>();
|
|
|
|
|
|
Assert.Equal(new [] {2,5,5,1,3}, baz.Nuts.Select(x => x.Size));
|
|
|
|
|
|
// ServiceFactory registered without a name
|
|
|
// explicitly provides 'Baz2' service
|
|
|
var baz2 = container.Resolve<Baz>("Baz2");
|
|
|
|
|
|
// ServiceFactory registered with name 'Baz3'
|
|
|
// provides by default the service registration with same name
|
|
|
var baz3 = container.Resolve<Baz>("Baz3");
|
|
|
|
|
|
// This factory uses GuidGenerator registration
|
|
|
// to generate a new id value
|
|
|
Assert.NotEqual(Guid.Empty, baz3.Id);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void GenericServicesContainer() {
|
|
|
var container = new UnityContainer();
|
|
|
container.LoadXmlConfiguration(Path.Combine("data","container","generic.services.xml"));
|
|
|
|
|
|
// resolve using a generig interface mapping
|
|
|
var box = container.Resolve<IBox<Baz.Nut>>();
|
|
|
|
|
|
Assert.NotNull(box.GetBoxValue());
|
|
|
|
|
|
// registered generic defines dependency of type {T}
|
|
|
// in this case it should resolve to the default Nut with Size=2
|
|
|
Assert.Equal(box.GetBoxValue().Size,2);
|
|
|
|
|
|
// generic factory which provides generic services
|
|
|
var box2 = container.Resolve<IBox<Baz.Nut>>();
|
|
|
|
|
|
var set1 = container.Resolve<SetOfBoxes<Baz.Nut>>();
|
|
|
|
|
|
Assert.Equal(new int?[] {null,2,1}, set1.Boxes?.Select(x => x.GetBoxValue()?.Size));
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void SerializedInstances() {
|
|
|
var container = new UnityContainer();
|
|
|
container.LoadXmlConfiguration(Path.Combine("data","container","serialized.instances.xml"));
|
|
|
|
|
|
var p1 = container.Resolve<Person>("p1");
|
|
|
var p2 = container.Resolve<Person>("p2");
|
|
|
|
|
|
Assert.Equal("Com", p1.FirstName);
|
|
|
Assert.True(p1.AgeSpecified);
|
|
|
Assert.Equal(99, p1.Age);
|
|
|
|
|
|
Assert.Equal("Javolta", p2.LastName);
|
|
|
Assert.True(p2.AgeSpecified);
|
|
|
Assert.Equal(88, p2.Age);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void ArrayDependency() {
|
|
|
var container = new UnityContainer();
|
|
|
|
|
|
container.LoadXmlConfiguration(Path.Combine("data","container","array.dependency.xml"));
|
|
|
|
|
|
// IEnumerable<T> and T[] are special cases:
|
|
|
// - T[] resolves to an array of all named registrations
|
|
|
// - IEnumerable<T> resolves to all registrations (including the default one)
|
|
|
//
|
|
|
// This rule works always regardless explicit registrations were provided
|
|
|
|
|
|
var arr = container.Resolve<Baz.Nut[]>();
|
|
|
Assert.Equal(new [] {5,3,1}, arr?.Select(x => x.Size));
|
|
|
|
|
|
var a1 = container.Resolve<Baz.Nut[]>("a1");
|
|
|
Assert.Equal(new [] {5,3,1}, a1?.Select(x => x.Size));
|
|
|
|
|
|
var enm = container.Resolve<IEnumerable<Baz.Nut>>();
|
|
|
Assert.Equal(new [] {5,3,1,2}, enm?.Select(x => x.Size));
|
|
|
|
|
|
// default service registration
|
|
|
var baz = container.Resolve<Baz>();
|
|
|
|
|
|
Assert.Equal(new [] {5,3,1}, baz.Nuts.Select(x => x.Size));
|
|
|
|
|
|
var baz2 = container.Resolve<Baz>("baz2");
|
|
|
|
|
|
Assert.Equal(new [] {2,5,5,1,3}, baz2.Nuts.Select(x => x.Size));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|