public void Execute_ProxyInterface_ProxyGetsCreated()
        {
            //Assign
            Type type        = typeof(IStubInterface);
            var  glassConfig = Substitute.For <IGlassConfiguration>();
            var  service     = Substitute.For <IAbstractService>();

            Context context = Context.Create(Substitute.For <IDependencyResolver>());

            AbstractTypeCreationContext abstractTypeCreationContext = new TestTypeCreationContext();

            abstractTypeCreationContext.Options = new TestGetOptions()
            {
                Type = typeof(IStubInterface)
            };

            var configuration = Substitute.For <AbstractTypeConfiguration>();

            configuration.Type = type;

            ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);

            //Act
            _task.Execute(args);

            //Assert
            Assert.IsNotNull(args.Result);
            Assert.IsTrue(args.Result is IStubInterface);
            Assert.IsFalse(args.Result.GetType() == typeof(IStubInterface));
        }
        public void Execute_ResultAlreadySet_DoesNoWork()
        {
            //Assign
            Type type     = typeof(IStubInterface);
            var  resolver = Substitute.For <IDependencyResolver>();
            var  service  = Substitute.For <IAbstractService>();

            Context context = Context.Create(resolver);

            AbstractTypeCreationContext abstractTypeCreationContext = new TestTypeCreationContext();

            abstractTypeCreationContext.Options = new TestGetOptions()
            {
                Type = typeof(IStubInterface)
            };


            var configuration = Substitute.For <AbstractTypeConfiguration>();

            configuration.Type = type;

            ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);

            args.Result = string.Empty;

            //Act
            _task.Execute(args);

            //Assert
            Assert.IsNotNull(args.Result);
            Assert.IsTrue(args.Result is string);
        }
        public void Execute_ConcreteClass_ObjectNotCreated()
        {
            //Assign
            Type type    = typeof(StubClass);
            var  service = Substitute.For <IAbstractService>();

            Context context = Context.Create(Substitute.For <IDependencyResolver>());

            AbstractTypeCreationContext abstractTypeCreationContext = new TestTypeCreationContext();

            abstractTypeCreationContext.Options = new TestGetOptions()
            {
                Type = type
            };


            var configuration = Substitute.For <AbstractTypeConfiguration>();

            configuration.Type = type;

            ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);

            //Act
            _task.Execute(args);

            //Assert
            Assert.IsNull(args.Result);
        }
Ejemplo n.º 4
0
        public void Execute_ResultAlreadySet_DoesNotRunLoader()
        {
            //Arrange

            var resolver    = Substitute.For <IDependencyResolver>();
            var context     = Context.Create(resolver);
            var typeContext = new TestTypeCreationContext();

            typeContext.Options = new TestGetOptions()
            {
                Type = typeof(StubClass)
            };


            var args = new ConfigurationResolverArgs(context, typeContext, null);

            args.Result = new StubTypeConfiguration();

            var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>();

            Assert.AreEqual(0, context.TypeConfigurations.Count);

            //Act
            task.Execute(args);

            //Assert
            Assert.AreEqual(0, context.TypeConfigurations.Count);
        }
Ejemplo n.º 5
0
        public void Execute_RunsLoader_TypeAddedToContext()
        {
            //Arrange

            var resolver = Substitute.For <IDependencyResolver>();
            var context  = Context.Create(resolver);

            context.Config = new Config();
            var typeContext = new TestTypeCreationContext();

            typeContext.Options = new TestGetOptions()
            {
                Type = typeof(StubClass)
            };

            var args = new ConfigurationResolverArgs(context, typeContext, null);


            var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>();

            Assert.AreEqual(0, context.TypeConfigurations.Count);

            //Act
            task.Execute(args);

            //Assert
            Assert.AreEqual(1, context.TypeConfigurations.Count);
            Assert.IsNotNull(context.TypeConfigurations[typeof(StubClass)]);
            Assert.AreEqual(typeof(StubClass), args.Result.Type);
        }
Ejemplo n.º 6
0
        public void Execute_OnDemandDisabled_ThrowsException()
        {
            //Arrange

            var resolver = Substitute.For <IDependencyResolver>();
            var context  = Context.Create(resolver);

            context.Config = new Config();
            context.Config.OnDemandMappingEnabled = false;

            var typeContext = new TestTypeCreationContext();

            typeContext.Options = new TestGetOptions()
            {
                Type = typeof(StubClass)
            };

            var args = new ConfigurationResolverArgs(context, typeContext, null);


            var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>();

            Assert.AreEqual(0, context.TypeConfigurations.Count);

            //Act
            Assert.Throws <MapperException>(
                () => { task.Execute(args); },
                Constants.Errors.OnDemandDisabled);
        }
Ejemplo n.º 7
0
        public void Execute_ConcreteType_TypeCreated()
        {
            //Assign
            Type type = typeof(StubClass);

            var service = Substitute.For <IAbstractService>();

            Context context = Context.Create(Substitute.For <IDependencyResolver>());

            AbstractTypeCreationContext abstractTypeCreationContext = new TestTypeCreationContext();

            abstractTypeCreationContext.Options = new TestGetOptions()
            {
                Type = typeof(StubClass),
                Lazy = LazyLoading.Disabled
            };



            var configuration = Substitute.For <AbstractTypeConfiguration>();

            configuration.ConstructorMethods = Utilities.CreateConstructorDelegates(type);
            configuration.Type = type;


            ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);

            //Act
            _task.Execute(args);

            //Assert
            Assert.IsNotNull(args.Result);
            Assert.IsTrue(args.Result is StubClass);
            Assert.IsTrue(args.Result.GetType() == typeof(StubClass));
        }
        public void Execute_TwoClassesWithTheSameName_ProxyGetsCreated()
        {
            //Assign
            var glassConfig = Substitute.For <IGlassConfiguration>();
            var service     = Substitute.For <IAbstractService>();

            Context context = Context.Create(Substitute.For <IDependencyResolver>());


            AbstractTypeCreationContext abstractTypeCreationContext1 = new TestTypeCreationContext();

            abstractTypeCreationContext1.Options = new TestGetOptions()
            {
                Type = typeof(NS1.ProxyTest1)
            };

            var configuration1 = Substitute.For <AbstractTypeConfiguration>();

            configuration1.Type = typeof(NS1.ProxyTest1);

            ObjectConstructionArgs args1 = new ObjectConstructionArgs(context, abstractTypeCreationContext1, configuration1, service);


            AbstractTypeCreationContext abstractTypeCreationContext2 = new TestTypeCreationContext();

            abstractTypeCreationContext2.Options = new TestGetOptions()
            {
                Type = typeof(NS2.ProxyTest1)
            };

            var configuration2 = Substitute.For <AbstractTypeConfiguration>();

            configuration2.Type = typeof(NS2.ProxyTest1);;

            ObjectConstructionArgs args2 = new ObjectConstructionArgs(context, abstractTypeCreationContext2, configuration2, service);

            //Act
            _task.Execute(args1);
            _task.Execute(args2);

            //Assert
            Assert.IsNotNull(args1.Result);
            Assert.IsTrue(args1.Result is NS1.ProxyTest1);
            Assert.IsFalse(args1.Result.GetType() == typeof(NS1.ProxyTest1));

            Assert.IsNotNull(args2.Result);
            Assert.IsTrue(args2.Result is NS2.ProxyTest1);
            Assert.IsFalse(args2.Result.GetType() == typeof(NS2.ProxyTest1));
        }
Ejemplo n.º 9
0
        public void Execute_MultipleRequestsUnloadedType_AddsOnlyOnceToContext()
        {
            //Arrange
            var resolver    = Substitute.For <IDependencyResolver>();
            var context     = Context.Create(resolver);
            var typeContext = new TestTypeCreationContext();

            context.Config = new Config();



            var task     = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>();
            var argsList = new List <ConfigurationResolverArgs>();

            Action taskFunc = () =>
            {
                typeContext.Options = new TestGetOptions()
                {
                    Type = typeof(StubClass)
                };
                var args = new ConfigurationResolverArgs(context, typeContext, null);

                argsList.Add(args);

                task.Execute(args);
            };


            Assert.AreEqual(0, context.TypeConfigurations.Count);

            //Act
            var tasks = new []
            {
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
            };

            Parallel.ForEach(tasks, x => x.Start());
            Task.WaitAll(tasks);

            //Assert
            Assert.AreEqual(1, context.TypeConfigurations.Count);
            Assert.IsTrue(argsList.All(x => x.Result.Type == typeof(StubClass)));
        }