ObjectContainer !

[TestClass]
    public class ObjectContainerTests
    {
        public ObjectContainerTests()
        {
           
        }

        [ClassInitialize()]
        public static void Initialize(TestContext testContext) {

            var provider = new Mock<IExportProvider>();

            provider
                .Setup<IEnumerable<Contract>>(i=>i.FindExports(typeof(IContract)))
                .Returns(()=>{

                    var contracts = new List<Contract>();

                    contracts.Add(new Contract { ExportType = typeof(Contract1), Index = 1 });
                    contracts.Add(new Contract { ExportType = typeof(Contract2), Index = 0 }); //set it as first export
                    contracts.Add(new Contract { ExportType = typeof(Contract3), Name = "ContractName", Index = 2 });

                    return contracts;
                });

                     
            provider
                .Setup<IEnumerable<Contract>>(i=>i.FindExports(typeof(IMissingContract)))
                .Returns(()=>{
                    var enumerable = Enumerable.Empty<Contract>();
                    return enumerable;
                });


            provider
                .Setup<IEnumerable<Contract>>(i => i.FindExports(typeof(MissingContract)))
                .Returns(() =>
                {
                    var enumerable = Enumerable.Empty<Contract>();
                    return enumerable;
                });

     
            provider
                .Setup(i => i.FindExports(typeof(IActivatorContext)))
                .Returns(() =>
                {
                    var contracts = new List<Contract>()
                    {
                        new Contract { ExportType = typeof(ActivatorContext)}
                    };

                    return contracts;
                    
                });

            provider
                .Setup(i => i.FindExports(typeof(IOrderedExport)))
                .Returns(() =>
                    {
                        return new List<Contract>()
                        {
                            new Contract{ ExportType = typeof(OrderedExport1), Index = 2},
                            new Contract{ ExportType = typeof(OrderedExport2), Index = 1},
                            new Contract{ ExportType = typeof(OrderedExport3), Index = 0},
                        };
                    });


            ExportProviders.Default = provider.Object;

            var factory = new Mock<IObjectFacotry>();
            
            factory
                .Setup(i => i.CreateInstance(typeof(MissingContract)))
                .Returns(() => new MissingContract());

            factory
                .Setup(i => i.CreateInstance(typeof(Contract1)))
                .Returns(() => new Contract1());


            factory
                .Setup(i => i.CreateInstance(typeof(Contract2)))
                .Returns(() => new Contract2());

            factory
                .Setup(i => i.CreateInstance(typeof(Contract3)))
                .Returns(() => new Contract3());

            factory
                .Setup(i => i.CreateInstance(typeof(ActivatorContext)))
                .Returns(() => new ActivatorContext());

            factory
                .Setup(i => i.CreateInstance(typeof(OrderedExport1)))
                .Returns(() => new OrderedExport1());

            factory
                .Setup(i => i.CreateInstance(typeof(OrderedExport2)))
                .Returns(() => new OrderedExport2());

            factory
                .Setup(i => i.CreateInstance(typeof(OrderedExport3)))
                .Returns(() => new OrderedExport3());

            factory.Setup(i => i.CreateInstance(typeof(FastHandler)))
                .Returns(() => new FastHandler());

            ObjectContainer.ObjectFactory = factory.Object;

        }

        #region Mock
        interface IContract
        {
        }

        class Contract1 : IContract
        {
        }

        class Contract2 : IContract
        {
        }

        class Contract3 : IContract
        {
        }


        interface IMissingContract
        {
        }

        class MissingContract
        {

        }

        class FastHandler
        {
            public string Name
            {
                get;
                set;
            }
        }

        class ActivatorContext : IActivatorContext
        {

            public ActivatorContext()
            {
                this.Handlers = new List<CreateHandler>
                {
                    new CreateHandler { Type = typeof(FastHandler), Invoker = ()=>new FastHandler { Name = "FastHandler" }}
                };
            }

            List<CreateHandler> Handlers
            {
                get;
                set;
            }

            public IEnumerable<CreateHandler> GetHandlers()
            {
                return Handlers;
            }
        }

        interface IOrderedExport
        {
        }

        class OrderedExport1 : IOrderedExport
        {
        }

        class OrderedExport2 : IOrderedExport
        {
        }

        class OrderedExport3 : IOrderedExport
        {
        }

        abstract class AbstractClass
        {
        }

        #endregion

        [TestMethod]
        public void call_create_instace_should_return_an_instance_of_itself_when_a_type_that_is_not_an_interface_can_not_find_any_exports()
        {
            var actual = ObjectContainer.CreateInstance<MissingContract>();
            Assert.IsInstanceOfType(actual, typeof(MissingContract));
        }

        [TestMethod]
        [ExpectedException(typeof(MissingContractException))]
        public void call_create_instance_should_throw_missing_contract_exception_when_a_type_that_is_an_interface_can_not_find_any_exports()
        {
            ObjectContainer.CreateInstance<IMissingContract>();
        }

        [TestMethod]
        public void call_try_create_instace_should_return_null_when_a_type_that_is_an_interface_can_not_find_any_exports()
        {
            var actual = ObjectContainer.TryCreateInstance<IMissingContract>();
            Assert.AreEqual(default(IMissingContract), actual);
        }

        [TestMethod]
        public void call_create_instances_should_return_an_empty_enumerable_when_a_type_is_an_interface_can_not_find_any_exports()
        {
            var actual = ObjectContainer.CreateInstances<IMissingContract>();
            Assert.AreEqual(0, actual.Count());
        }

        [TestMethod]
        public void call_create_instaces_should_return_the_same_objects_as_the_exports()
        {
            var actual = ObjectContainer.CreateInstances<IContract>();

            Assert.AreEqual(3, actual.Count());
            Assert.IsTrue(actual.Any(i => i is Contract1));
            Assert.IsTrue(actual.Any(i => i is Contract2));
            Assert.IsTrue(actual.Any(i => i is Contract3));

        }

        [TestMethod]
        public void call_create_instance_should_return_the_first_export_ordered_by_index()
        {
            var actual = ObjectContainer.CreateInstance<IContract>();
            Assert.IsInstanceOfType(actual, typeof(Contract2));
        }

        [TestMethod]
        public void call_create_instance_with_contractname_should_return_an_instance_of_the_named_export()
        {
            var actual = ObjectContainer.CreateInstance<IContract>("ContractName");
            Assert.IsInstanceOfType(actual, typeof(Contract3));
        }

        [TestMethod]
        public void should_use_fast_handler_instead_of_the_create_instacne_of_object_factory()
        {
            ObjectContainer.InitHandlers();//reset the handlers

            var actual = ObjectContainer.CreateInstance<FastHandler>();

            if (actual == null)
            {
                throw new NotSupportedException();
            }
            Assert.AreEqual("FastHandler", actual.Name);
        }

        [TestMethod]
        public void the_order_of_instances_should_be_the_same_as_the_order_of_exports()
        {
            var actual = ObjectContainer.CreateInstances<IOrderedExport>();

            Assert.AreEqual(3, actual.Count());
            Assert.IsInstanceOfType(actual.First(), typeof(OrderedExport3));
            Assert.IsInstanceOfType(actual.Skip(1).First(), typeof(OrderedExport2));
            Assert.IsInstanceOfType(actual.Last(), typeof(OrderedExport1));
        }

        [TestMethod]
        [ExpectedException(typeof(MissingContractException))]
        public void should_throw_the_missing_contract_exception_when_the_type_that_is_an_abstract_class_can_not_find_any_exports()
        {
            var actual = ObjectContainer.CreateInstance<AbstractClass>();
        }
    }

Last edited Dec 31, 2010 at 2:00 PM by cnlangzi, version 2

Comments

No comments yet.