public void Intercept_WithMultiplePropertiesAndLazyLoading_ReturnsExpectedPropertyValues()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            config.Type    = typeof(IStubTarget);
            context.IsLazy = true;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);
            var invocations = new List <IInvocation>();

            for (var i = 0; i < 100; i++)
            {
                invocations.Add(CreateInterceptedProperty(config, i.ToString()));
            }

            //Act
            foreach (var invocation in invocations)
            {
                interceptor.Intercept(invocation);

                // Assert
                Assert.AreEqual(invocation.Method.Name.Substring(4), invocation.ReturnValue);
            }
        }
        private static IInvocation CreateInterceptedProperty(StubAbstractTypeConfiguration config, string propertyName)
        {
            var invocation = Substitute.For <IInvocation>();

            var property = new StubAbstractPropertyConfiguration();
            var mapper   = new StubAbstractDataMapper();

            var getter = Substitute.For <MethodInfo>();

            getter.Attributes.Returns(MethodAttributes.SpecialName);
            getter.Name.Returns("get_" + propertyName);
            getter.ReturnType.Returns(typeof(string));

            var returnValue = propertyName;
            var info        = Substitute.For <FakePropertyInfo>(typeof(string), propertyName, typeof(IStubTarget));

            info.CanWrite.Returns(false);
            info.DeclaringType.Returns(typeof(IStubTarget));
            info.Name.Returns(propertyName);

            property.Mapper       = mapper;
            property.PropertyInfo = info;

            config.AddProperty(property);

            mapper.Value = returnValue;

            invocation.Method.Returns(getter);

            return(invocation);
        }
        public void Intercept_WithMultiplePropertiesAndLazyLoading_ReturnsExpectedPropertyValues()
        {
            //Arrange   
            var service = Substitute.For<IAbstractService>();
            var config = new StubAbstractTypeConfiguration();
            var context = Substitute.For<AbstractTypeCreationContext>();

            config.Type = typeof(IStubTarget);
            context.IsLazy = true;

            var args = new ObjectConstructionArgs(null, context, config, service, new ModelCounter())
            {
                Parameters = new Dictionary<string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);
            var invocations = new List<IInvocation>();

            for (var i = 0; i < 100; i++)
            {
                invocations.Add(CreateInterceptedProperty(config, i.ToString()));
            }

            //Act
            foreach (var invocation in invocations)
            {
                interceptor.Intercept(invocation);

                // Assert
                Assert.AreEqual(invocation.Method.Name.Substring(4), invocation.ReturnValue);
            }
        }
        public void Intercept_InterceptsMultiplePropertiesOnDifferentInterfaces_ReturnsExpectedPropertyValues()
        {
            //Arrange   
            var service = Substitute.For<IAbstractService>();
            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            var property1 = new StubAbstractPropertyConfiguration();
            var property2 = new StubAbstractPropertyConfiguration();

            var mapper1 = new StubAbstractDataMapper();
            var mapper2 = new StubAbstractDataMapper();

            var expected1 = "test random 1";
            var expected2 = "some other random";

            var propertyName1 = "Property1";
            var propertyName2 = "Property2";


            var info1 = new FakePropertyInfo(typeof(string), propertyName1, typeof(IStubTarget));
            var info2 = new FakePropertyInfo(typeof(string), propertyName2, typeof(IStubTarget2));

            config1.AddProperty(property1);
            config2.AddProperty(property2);

            property1.Mapper = mapper1;
            property2.Mapper = mapper2;
            property1.PropertyInfo = info1;
            property2.PropertyInfo = info2;

            mapper1.Value = expected1;
            mapper2.Value = expected2;

            var args = new ObjectConstructionArgs(null, null, config1, service);
            args.Parameters = new Dictionary<string, object>();
            args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] {config2};
            var interceptor = new MultiInterfacePropertyInterceptor(args);

            var invocation1 = Substitute.For<IInvocation>();
            var invocation2 = Substitute.For<IInvocation>();

            invocation1.Method.Returns(typeof(IStubTarget).GetProperty(propertyName1).GetGetMethod());
            invocation2.Method.Returns(typeof(IStubTarget2).GetProperty(propertyName2).GetGetMethod());

            //Act
            interceptor.Intercept(invocation1);
            interceptor.Intercept(invocation2);

            //Assert
            Assert.AreEqual(expected1, invocation1.ReturnValue);
            Assert.AreEqual(expected2, invocation2.ReturnValue);

        }
        public void Intercept_InterceptsMultiplePropertiesOnDifferentInterfaces_ReturnsExpectedPropertyValues()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            var property1 = new StubAbstractPropertyConfiguration();
            var property2 = new StubAbstractPropertyConfiguration();

            var mapper1 = new StubAbstractDataMapper();
            var mapper2 = new StubAbstractDataMapper();

            var expected1 = "test random 1";
            var expected2 = "some other random";

            var propertyName1 = "Property1";
            var propertyName2 = "Property2";


            var info1 = new FakePropertyInfo(typeof(string), propertyName1, typeof(IStubTarget));
            var info2 = new FakePropertyInfo(typeof(string), propertyName2, typeof(IStubTarget2));

            config1.AddProperty(property1);
            config2.AddProperty(property2);

            property1.Mapper       = mapper1;
            property2.Mapper       = mapper2;
            property1.PropertyInfo = info1;
            property2.PropertyInfo = info2;

            mapper1.Value = expected1;
            mapper2.Value = expected2;

            var args = new ObjectConstructionArgs(null, null, config1, service, new ModelCounter());

            args.Parameters = new Dictionary <string, object>();
            args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] { config2 };
            var interceptor = new MultiInterfacePropertyInterceptor(args);

            var invocation1 = Substitute.For <IInvocation>();
            var invocation2 = Substitute.For <IInvocation>();

            invocation1.Method.Returns(typeof(IStubTarget).GetProperty(propertyName1).GetGetMethod());
            invocation2.Method.Returns(typeof(IStubTarget2).GetProperty(propertyName2).GetGetMethod());

            //Act
            interceptor.Intercept(invocation1);
            interceptor.Intercept(invocation2);

            //Assert
            Assert.AreEqual(expected1, invocation1.ReturnValue);
            Assert.AreEqual(expected2, invocation2.ReturnValue);
        }
Beispiel #6
0
        public void Intercept_InterceptsProperties_SetsExpectedPropertyValue()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            context.Options = new GetOptions {
                Lazy = LazyLoading.Enabled
            };

            var propertyName = "Property1";

            var info = typeof(IStubTarget).GetProperty(propertyName);

            var property = new StubAbstractPropertyConfiguration();

            property.PropertyInfo = info;
            var mapper = new StubAbstractDataMapper();

            var expected = "test random 1";

            var info1 = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper       = mapper;
            property.PropertyInfo = info1;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args, new LazyLoadingHelper());

            var setInvocation = Substitute.For <IInvocation>();

            setInvocation.Arguments.Returns(new[] { expected });
            setInvocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetSetMethod());

            //Act
            interceptor.Intercept(setInvocation);

            //Assert
            var getInvocation = Substitute.For <IInvocation>();

            getInvocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            interceptor.Intercept(getInvocation);

            Assert.AreEqual(expected, getInvocation.ReturnValue);
        }
Beispiel #7
0
        public void Execute_ArgsNotNullOneInterface_ReturnsNull()
        {
            //Arrange

            var config1 = new StubAbstractTypeConfiguration();

            config1.Type = typeof(IStubTarget);

            var args = new ObjectConstructionArgs(null, null, config1, null, new ModelCounter());

            var task = new CreateMultiInferaceTask();

            //Act
            task.Execute(args);

            //Assert
            Assert.IsNull(args.Result);
        }
        public void Execute_ArgsNotNullOneInterface_ReturnsNull()
        {
            //Arrange

            var config1 = new StubAbstractTypeConfiguration();

            config1.Type = typeof(IStubTarget);

            var args = new ObjectConstructionArgs(null, null,  config1 , null, new ModelCounter());

            var task = new CreateMultiInferaceTask();

            //Act
            task.Execute(args);

            //Assert
            Assert.IsNull(args.Result);
        }
Beispiel #9
0
        public void Load_LoadContextGenericType_GenericTypeNotCreated()
        {
            //Assign
            var loader1 = Substitute.For <IConfigurationLoader>();
            var config1 = new StubAbstractTypeConfiguration();

            config1.Type    = typeof(Generic <>);
            config1.AutoMap = true;
            loader1.Load().Returns(new[] { config1 });

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

            context.Load(loader1);

            //Assert
            Assert.IsNotNull(Context.Default);
            Assert.AreEqual(Context.Contexts[Context.DefaultContextName], Context.Default);
            Assert.IsFalse(Context.Default.TypeConfigurations.ContainsKey(config1.Type));
        }
        public void Execute_ArgsNotNullMultipleTypesNotAllInterfaces_ReturnsNull()
        {
            //Arrange

            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            config1.Type = typeof(IStubTarget);
            config2.Type = typeof(StubClass);

            var args = new ObjectConstructionArgs(null, null, config2, null);
            //var args = new ObjectConstructionArgs(null, null, new[] { config1, config2 }, null);

            var task = new CreateMultiInferaceTask();

            //Act
            task.Execute(args);

            //Assert
            Assert.IsNull(args.Result);
        }
        public void Execute_ArgsNotNullMultipleTypesNotAllInterfaces_ReturnsNull()
        {
            //Arrange

            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            config1.Type = typeof(IStubTarget);
            config2.Type = typeof(StubClass);

            var args = new ObjectConstructionArgs(null, null, config2 , null);
            //var args = new ObjectConstructionArgs(null, null, new[] { config1, config2 }, null);

            var task = new CreateMultiInferaceTask();

            //Act
            task.Execute(args);

            //Assert
            Assert.IsNull(args.Result);
        }
Beispiel #12
0
        public void Load_LoadContextDerivedFromGenericType_CanGetTypeConfigsFromContext()
        {
            //Assign
            var loader1 = Substitute.For <IConfigurationLoader>();
            var config1 = new StubAbstractTypeConfiguration();

            config1.Type    = typeof(Sample);
            config1.AutoMap = true;
            loader1.Load().Returns(new[] { config1 });

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

            //Act
            var context = Context.Create(resolver);

            context.Load(loader1);

            //Assert
            Assert.IsNotNull(Context.Default);
            Assert.AreEqual(Context.Contexts[Context.DefaultContextName], Context.Default);
            Assert.IsTrue(Context.Default.TypeConfigurations.ContainsKey(config1.Type));
        }
        public void Intercept_InterceptsProperties_ReturnsExpectedPropertyValue()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            var property = new StubAbstractPropertyConfiguration();
            var mapper   = new StubAbstractDataMapper();

            var expected     = "test random 1";
            var propertyName = "Property1";

            var info = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper       = mapper;
            property.PropertyInfo = info;

            mapper.Value = expected;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);

            var invocation = Substitute.For <IInvocation>();

            invocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            //Act
            interceptor.Intercept(invocation);

            //Assert
            Assert.AreEqual(expected, invocation.ReturnValue);
        }
        public void Intercept_InterceptsProperties_ReturnsExpectedPropertyValue()
        {
            //Arrange   
            var service = Substitute.For<IAbstractService>();
            var config = new StubAbstractTypeConfiguration();
            var context = Substitute.For<AbstractTypeCreationContext>();

            var property = new StubAbstractPropertyConfiguration();
            var mapper = new StubAbstractDataMapper();

            var expected = "test random 1";
            var propertyName = "Property1";

            var info = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper = mapper;
            property.PropertyInfo = info;

            mapper.Value = expected;

            var args = new ObjectConstructionArgs(null, context, config, service, new ModelCounter())
            {
                Parameters = new Dictionary<string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);

            var invocation = Substitute.For<IInvocation>();

            invocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            //Act
            interceptor.Intercept(invocation);

            //Assert
            Assert.AreEqual(expected, invocation.ReturnValue);
        }
        public void Execute_ArgsNotNullMultipleInterface_ReturnsMultiInterfaceProxy()
        {
            //Arrange

            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            config1.Type = typeof(IStubTarget);
            config2.Type = typeof(IStubTarget2);

          //  var args = new ObjectConstructionArgs(null, null, new[] { config1, config2 }, null);
            var args = new ObjectConstructionArgs(null, null, config1,  null, new ModelCounter());
            args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] {config2};
            var task = new CreateMultiInferaceTask();

            //Act
            task.Execute(args);

            //Assert
            Assert.IsNotNull(args.Result);
            Assert.IsTrue(args.Result is IStubTarget);
            Assert.IsTrue(args.Result is IStubTarget2);
        }
Beispiel #16
0
        public void Execute_ArgsNotNullMultipleInterface_ReturnsMultiInterfaceProxy()
        {
            //Arrange

            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            config1.Type = typeof(IStubTarget);
            config2.Type = typeof(IStubTarget2);

            //  var args = new ObjectConstructionArgs(null, null, new[] { config1, config2 }, null);
            var args = new ObjectConstructionArgs(null, null, config1, null, new ModelCounter());

            args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] { config2 };
            var task = new CreateMultiInferaceTask();

            //Act
            task.Execute(args);

            //Assert
            Assert.IsNotNull(args.Result);
            Assert.IsTrue(args.Result is IStubTarget);
            Assert.IsTrue(args.Result is IStubTarget2);
        }
        private static IInvocation CreateInterceptedProperty(StubAbstractTypeConfiguration config, string propertyName)
        {
            var invocation = Substitute.For<IInvocation>();

            var property = new StubAbstractPropertyConfiguration();
            var mapper = new StubAbstractDataMapper();

            var getter = Substitute.For<MethodInfo>();
            getter.Attributes.Returns(MethodAttributes.SpecialName);
            getter.Name.Returns("get_" + propertyName);
            getter.ReturnType.Returns(typeof(string));

            var returnValue = propertyName;
            var info = Substitute.For<FakePropertyInfo>(typeof(string), propertyName, typeof(IStubTarget));
            info.CanWrite.Returns(false);
            info.DeclaringType.Returns(typeof(IStubTarget));
            info.Name.Returns(propertyName);

            property.Mapper = mapper;
            property.PropertyInfo = info;

            config.AddProperty(property);

            mapper.Value = returnValue;

            invocation.Method.Returns(getter);

            return invocation;
        }