-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from iQuarc/develop
Develop
- Loading branch information
Showing
34 changed files
with
1,927 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
AppBoot/iQuarc.AppBoot.UnitTests/BootstrapperTests.RegisterBehaviors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
AppBoot/iQuarc.AppBoot.UnitTests/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
Oops, something went wrong.