private static void IServiceContainerConformanceAddRemove(ISimpleServiceContainer container, Func <IAddService> creatorFunc) { Assert.That(container.GetService <IAddService>() == null, "Starting with no IAddService."); container.Add <IAddService>(creatorFunc); Assert.Throws <CKException>(() => container.Add <IAddService>(creatorFunc), "Adding an already existing service throws an exception."); Assert.That(container.GetService <IAddService>() != null, "Deferred creation occured."); container.Remove(typeof(IAddService)); Assert.That(container.GetService <IAddService>() == null, "Remove works."); // Removing an unexisting service is okay. container.Remove(typeof(IAddService)); bool removed = false; container.Add <IAddService>(creatorFunc, s => removed = true); container.Remove(typeof(IAddService)); Assert.That(removed == false, "Since the service has never been required, it has not been created, hence, OnRemove action has not been called."); container.Add <IAddService>(creatorFunc, s => removed = true); Assert.That(container.GetService <IAddService>() != null, "Service has been created."); container.Remove(typeof(IAddService)); Assert.That(removed, "This time, OnRemove action has been called."); removed = false; container.Add <IAddService>(new AddServiceImpl(), s => removed = true); container.Remove(typeof(IAddService)); Assert.That(removed, "Since the service instance has been added explicitely, OnRemove action has been called."); }
private static void IServiceContainerConformanceAddRemove(ISimpleServiceContainer container, Func <IAddService> creatorFunc) { container.GetService <IAddService>().Should().BeNull("Starting with no IAddService."); container.Add(creatorFunc); container.Invoking(sut => sut.Add(creatorFunc)).Should().Throw <Exception>("Adding an already existing service throws an exception."); container.GetService <IAddService>().Should().NotBeNull("Deferred creation occured."); container.Remove(typeof(IAddService)); container.GetService <IAddService>().Should().BeNull("Remove works."); // Removing an unexisting service is okay. container.Remove(typeof(IAddService)); bool removed = false; container.Add <IAddService>(creatorFunc, s => removed = true); container.Remove(typeof(IAddService)); removed.Should().BeFalse("Since the service has never been required, it has not been created, hence, OnRemove action has not been called."); container.Add <IAddService>(creatorFunc, s => removed = true); container.GetService <IAddService>().Should().NotBeNull("Service has been created."); container.Remove(typeof(IAddService)); removed.Should().BeTrue("This time, OnRemove action has been called."); removed = false; container.Add <IAddService>(new AddServiceImpl(), s => removed = true); container.Remove(typeof(IAddService)); removed.Should().BeTrue("Since the service instance has been added explicitely, OnRemove action has been called."); }
/// <summary> /// Tests the fact that the ISimpleServiceContainer set as parameter is conform to the way the interface should be used. /// </summary> /// <typeparam name="T">the service implemented by the servicecontainer's baseprovider </typeparam> /// <param name="container">the ISimpleServiceContainer implementation to test</param> /// <param name="baseProviderServiceToTest"></param> public void IServiceContainerConformanceTest <T>(ISimpleServiceContainer container, ISimpleServiceContainer baseProvider, T baseProviderServiceToTest) { Func <IAddService> creatorFunc = () => new AddServiceImpl(); IServiceContainerCoAndContravariance(container, creatorFunc); IServiceContainerConformanceAddRemove(container, creatorFunc); IServiceContainerConformanceAddFailsWhenExisting(container, creatorFunc); IServiceContainerConformanceRemoveRecursive(container); container.Add <IAddService>(creatorFunc); container.Add <ISubstractService>(new SubstractServiceImpl()); IAddService service = container.GetService <IAddService>(); Assert.That(service != null); Assert.That(service.GetType(), Is.EqualTo(typeof(AddServiceImpl))); Assert.That(service.Add(1, 1), Is.EqualTo(2)); ISubstractService substractService = container.GetService <ISubstractService>(); Assert.That(substractService != null); Assert.That(substractService.GetType(), Is.EqualTo(typeof(SubstractServiceImpl))); Assert.That(substractService.Substract(1, 1), Is.EqualTo(0)); //clear test container.Clear(); Assert.That(container.GetService <IAddService>(), Is.Null); Assert.That(container.GetService <ISubstractService>(), Is.Null); //base provider test if (baseProvider != null && baseProviderServiceToTest != null) { T baseService = container.GetService <T>(); Assert.That(baseService != null, "The baseProvider contains the specified service."); container.Remove(typeof(T)); baseService = container.GetService <T>(); Assert.That(baseService != null, "Trying to remove a base service from a child provider does nothing."); container.AddDisabled(typeof(T)); Assert.That(container.GetService <T>(), Is.Null, "Access to this service is disabled"); baseProvider.Remove(typeof(T)); Assert.That(container.GetService <T>(), Is.Null, "Access to this service is disabled & The service doesn't exist anymore on the baseProvider"); container.Remove(typeof(T)); Assert.That(container.GetService <T>(), Is.Null, "The service doesn't exist anymore on the baseProvider"); baseProvider.Add(baseProviderServiceToTest); Assert.That(container.GetService <T>(), Is.Not.Null, "Back to the beginning's state, the service is retrieved from the base provider."); } }
private static void IServiceContainerConformanceRemoveRecursive(ISimpleServiceContainer container) { bool removedCall = false; container.Add <IAddService>(new AddServiceImpl(), s => { removedCall = true; container.Remove(typeof(IAddService)); }); Assert.That(container.GetService <IAddService>(), Is.Not.Null); container.Remove(typeof(IAddService)); Assert.That(removedCall, "OnRemove has been called and can safely remove the service again without stack overflow exception."); }
private static void IServiceContainerConformanceRemoveRecursive(ISimpleServiceContainer container) { bool removedCall = false; container.Add <IAddService>(new AddServiceImpl(), s => { removedCall = true; container.Remove(typeof(IAddService)); }); container.GetService <IAddService>(false).Should().NotBeNull(); container.Remove(typeof(IAddService)); removedCall.Should().BeTrue("OnRemove has been called and can safely remove the service again without stack overflow exception."); }
private static void IServiceContainerConformanceAddFailsWhenExisting(ISimpleServiceContainer container, Func <IAddService> creatorFunc) { container.Add <IAddService>(new AddServiceImpl()); Assert.Throws <CKException>(() => container.Add(creatorFunc)); Assert.Throws <CKException>(() => container.Add <IAddService>(creatorFunc, s => { })); Assert.Throws <CKException>(() => container.Add(typeof(IAddService), new AddServiceImpl())); Assert.Throws <CKException>(() => container.Add(typeof(IAddService), new AddServiceImpl(), s => { })); Assert.Throws <CKException>(() => container.Add <IAddService>(new AddServiceImpl())); Assert.Throws <CKException>(() => container.Add <IAddService>(new AddServiceImpl(), s => { })); Assert.Throws <CKException>(() => container.AddDisabled(typeof(IAddService))); container.Remove(typeof(IAddService)); }
private static void IServiceContainerConformanceAddFailsWhenExisting(ISimpleServiceContainer container, Func <IAddService> creatorFunc) { container.Add <IAddService>(new AddServiceImpl()); container.Invoking(sut => sut.Add(creatorFunc)).Should().Throw <Exception>(); container.Invoking(sut => sut.Add <IAddService>(creatorFunc, s => { })).Should().Throw <Exception>(); container.Invoking(sut => sut.Add(typeof(IAddService), new AddServiceImpl())).Should().Throw <Exception>(); container.Invoking(sut => sut.Add(typeof(IAddService), new AddServiceImpl(), s => { })).Should().Throw <Exception>(); container.Invoking(sut => sut.Add <IAddService>(new AddServiceImpl())).Should().Throw <Exception>(); container.Invoking(sut => sut.Add <IAddService>(new AddServiceImpl(), s => { })).Should().Throw <Exception>(); container.Invoking(sut => sut.AddDisabled(typeof(IAddService))).Should().Throw <Exception>(); container.Remove(typeof(IAddService)); }
private static void IServiceContainerCoAndContravariance(ISimpleServiceContainer container, Func <IAddService> creatorFunc) { { _onRemoveServiceCalled = false; container.Add <IAddService>(new AddServiceImpl(), OnRemoveService); container.Remove(typeof(IAddService)); Assert.That(_onRemoveServiceCalled, "OnRemoveService has been called."); _onRemoveServiceCalled = false; container.Add <IAddService>(new AddServiceImpl(), OnRemoveBaseServiceType); container.Remove(typeof(IAddService)); Assert.That(_onRemoveServiceCalled, "OnRemoveBaseServiceType has been called."); _onRemoveServiceCalled = false; container.Add <IAddService>(new AddServiceImpl(), OnRemoveServiceObject); container.Remove(typeof(IAddService)); Assert.That(_onRemoveServiceCalled, "OnRemoveServiceObject has been called."); //container.Add<IAddService>( new AddServiceImpl(), OnRemoveDerivedServiceType ); //container.Remove( typeof( IAddService ) ); //container.Add<IAddService>( new AddServiceImpl(), OnRemoveUnrelatedType ); //container.Remove( typeof( IAddService ) ); } { _onRemoveServiceCalled = false; container.Add(creatorFunc, OnRemoveService); container.Remove(typeof(IAddService)); Assert.That(!_onRemoveServiceCalled, "Service has never been created."); container.Add <IAddService>(creatorFunc, OnRemoveBaseServiceType); container.Remove(typeof(IAddService)); Assert.That(!_onRemoveServiceCalled, "Service has never been created."); container.Add <IAddService>(creatorFunc, OnRemoveServiceObject); container.Remove(typeof(IAddService)); Assert.That(!_onRemoveServiceCalled, "Service has never been created."); } }
/// <summary> /// Type safe version to remove a registered type. /// </summary> /// <param name="this">This <see cref="ISimpleServiceContainer"/> object.</param> /// <returns>This object to enable fluent syntax.</returns> public static ISimpleServiceContainer Remove <T>(this ISimpleServiceContainer @this) { return(@this.Remove(typeof(T))); }