Beispiel #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.");
            }
        }
Beispiel #2
0
 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));
 }