|
|
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);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|