Skip to content

Commit

Permalink
Merge pull request #2 from iQuarc/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
crissian committed Nov 30, 2014
2 parents ede8bbb + a6ebac8 commit 5ac1058
Show file tree
Hide file tree
Showing 34 changed files with 1,927 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using iQuarc.SystemEx;
using Microsoft.Practices.ServiceLocation;
using Moq;
using Xunit;

namespace iQuarc.AppBoot.UnitTests
{
public class BootstrapperTestsWithMoreRegisterBehaviors
{
[Fact]
public void Run_TypesOnSameInterfaceRegisteredByDifferentBehaviors_LastRegisteredBehaviorOverwrites()
{
DummyAssembly assembly = new DummyAssembly(typeof (Implementation), typeof (ProxyImpl));
DummyContainer dummyContainer = new DummyContainer();
Bootstrapper bootstrapper = new Bootstrapper(new[] {assembly}, dummyContainer);

IRegistrationBehavior proxyBeh = new DummyBehavior("Proxy");
IRegistrationBehavior serviceBeh = new DummyBehavior("Service");
bootstrapper.AddRegistrationBehavior(proxyBeh);
bootstrapper.AddRegistrationBehavior(serviceBeh);

bootstrapper.Run();

ServiceInfo resolved = dummyContainer.GetRegistration(typeof (IService));
Assert.Equal(typeof (Implementation), resolved.To);
}

private interface IService
{
}

[Service("Service")] //using contract name as annotation
private class Implementation : IService
{
}

[Service("Proxy")] //using contract name as annotation
private class ProxyImpl : IService
{
}

private class DummyBehavior : IRegistrationBehavior
{
private readonly string annotation;

public DummyBehavior(string annotation)
{
this.annotation = annotation;
}

public IEnumerable<ServiceInfo> GetServicesFrom(Type type)
{
ServiceAttribute atr = type.GetAttribute<ServiceAttribute>();
if (atr.ContractName == annotation)
return new[] {new ServiceInfo(typeof (IService), type, Lifetime.Instance)};

return new ServiceInfo[] {};
}
}


private class DummyAssembly : Assembly
{
private readonly Type[] types;

public DummyAssembly(params Type[] types)
{
this.types = types;
}

public override Type[] GetTypes()
{
return types;
}
}

private class DummyContainer : IDependencyContainer
{
private readonly Dictionary<Type, ServiceInfo> dic = new Dictionary<Type, ServiceInfo>();

public DummyContainer()
{
AsServiceLocator = GetFakeServiceLocator();
}

public IServiceLocator AsServiceLocator { get; private set; }

public void RegisterService(ServiceInfo service)
{
dic[service.From] = service;
}

public ServiceInfo GetRegistration(Type from)
{
return dic[from];
}

public void RegisterInstance<T>(T instance)
{
}

private static IServiceLocator GetFakeServiceLocator()
{
IModule[] modules = {};

Mock<IServiceLocator> fakeServiceLocator = new Mock<IServiceLocator>();
IServiceLocator serviceLocator = fakeServiceLocator.Object;

fakeServiceLocator.Setup(s => s.GetInstance<Application>())
.Returns(new Application(modules));
fakeServiceLocator.Setup(s => s.GetInstance<IServiceLocator>())
.Returns(serviceLocator);

return serviceLocator;
}
}
}
}
111 changes: 111 additions & 0 deletions AppBoot/iQuarc.AppBoot.UnitTests/BootstrapperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Reflection;
using Microsoft.Practices.ServiceLocation;
using Moq;
using Xunit;

namespace iQuarc.AppBoot.UnitTests
{
public class BootstrapperTests
{
[Fact]
public void Run_AfterCalled_ServiceLocatorIsAvailableAsStaticSingleton()
{
IServiceLocator serviceLocatorStub = GetFakeServiceLocator();
Mock<IDependencyContainer> containerStub = GetFakeContainer(serviceLocatorStub);

Bootstrapper bootstrapper = GetTarget(containerStub);

bootstrapper.Run();

IServiceLocator staticLocator = ServiceLocator.Current;
Assert.Same(serviceLocatorStub, staticLocator);
}

[Fact]
public void Run_AfterCalled_ServiceLocatorRegisteredInTheContainer()
{
IServiceLocator serviceLocatorStub = GetFakeServiceLocator();
Mock<IDependencyContainer> containerMock = GetFakeContainer(serviceLocatorStub);

Bootstrapper boostrapper = GetTarget(containerMock);

boostrapper.Run();

containerMock.Verify(c => c.RegisterInstance(serviceLocatorStub), Times.Once);
}

[Fact]
public void Run_RegistrationBehaviorReturnsOneService_TypeRegistered()
{
Type testType = typeof (TestType);
ServiceInfo testSi = new ServiceInfo(testType, testType, "test contract", Lifetime.Instance);
IRegistrationBehavior regBehaviorStub = GetRegBehaviorStub(testSi);

Mock<IDependencyContainer> containerMock = GetFakeContainer();
Bootstrapper bootstrapper = GetTargetWithAssembly(containerMock);
bootstrapper.AddRegistrationBehavior(regBehaviorStub);

bootstrapper.Run();

containerMock.Verify(c => c.RegisterService(testSi), Times.AtLeastOnce);
}

private Bootstrapper GetTargetWithAssemblyAndFakeContainer()
{
Assembly[] assemblies = {typeof (BootstrapperTests).Assembly};
IDependencyContainer container = GetFakeContainer().Object;

return new Bootstrapper(assemblies, container);
}

private static Bootstrapper GetTarget(Mock<IDependencyContainer> fakeContainer)
{
return new Bootstrapper(new Assembly[] {}, fakeContainer.Object);
}

private Bootstrapper GetTargetWithAssembly(Mock<IDependencyContainer> container)
{
Assembly[] assemblies = {typeof (BootstrapperTests).Assembly};
return new Bootstrapper(assemblies, container.Object);
}

private static IServiceLocator GetFakeServiceLocator()
{
IModule[] modules = {};

Mock<IServiceLocator> fakeServiceLocator = new Mock<IServiceLocator>();
IServiceLocator serviceLocator = fakeServiceLocator.Object;

fakeServiceLocator.Setup(s => s.GetInstance<Application>())
.Returns(new Application(modules));
fakeServiceLocator.Setup(s => s.GetInstance<IServiceLocator>())
.Returns(serviceLocator);

return serviceLocator;
}

private static Mock<IDependencyContainer> GetFakeContainer(IServiceLocator serviceLocatorStub = null)
{
IServiceLocator serviceLocator = serviceLocatorStub ?? GetFakeServiceLocator();

Mock<IDependencyContainer> containerStub = new Mock<IDependencyContainer>();
containerStub.Setup(c => c.AsServiceLocator).Returns(serviceLocator);
return containerStub;
}

private static IRegistrationBehavior GetRegBehaviorStub(ServiceInfo si)
{
Mock<IRegistrationBehavior> regBehaviorStub = new Mock<IRegistrationBehavior>();
regBehaviorStub.Setup(r => r.GetServicesFrom(It.IsAny<Type>()))
.Returns(new[] {si});

return regBehaviorStub.Object;
}


private class TestType
{
}
}
}
39 changes: 39 additions & 0 deletions AppBoot/iQuarc.AppBoot.UnitTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

[assembly: AssemblyTitle("iQuarc.AppBoot.UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("iQuarc")]
[assembly: AssemblyProduct("iQuarc.AppBoot.UnitTests")]
[assembly: AssemblyCopyright("Copyright © iQuarc 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("ffbfe0fa-8497-492d-a151-486243e877f0")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 5ac1058

Please sign in to comment.