Exemplo n.º 1
0
        /// <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.");
            }
        }
Exemplo n.º 2
0
        public void SimpleServiceContainerFallbackTest()
        {
            int removedServicesCount = 0;

            SimpleServiceContainer container = new SimpleServiceContainer();

            container.Add(typeof(IAddService), new AddServiceImpl(), o => removedServicesCount++);

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

            Assert.That(removedServicesCount, Is.EqualTo(0));
            container.Remove(typeof(IAddService));
            Assert.That(removedServicesCount, Is.EqualTo(1));

            Assert.That(container.GetService(typeof(IAddService)), Is.Null);
            Assert.That(container.GetService <IAddService>(), Is.Null);
            Assert.Throws <CKException>(() => container.GetService <IAddService>(true));
        }