Exemplo n.º 1
0
        public void ResultOfMethodCallWithSameArgumentsIsCached()
        {
            IModule testModule = new InlineModule(m => m.Bind <CacheMock>().ToSelf());

            using (var kernel = new StandardKernel(new LinFuModule(), new CacheModule(), testModule))
            {
                var obj = kernel.Get <CacheMock>();

                CacheMock.ResetCounts();

                int result;

                result = obj.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

                result = obj.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

                result = obj.Multiply(4, 4);
                Assert.That(result, Is.EqualTo(16));
                Assert.That(CacheMock.MultiplyCount, Is.EqualTo(2));
            }
        }
Exemplo n.º 2
0
        public void HeuristicSelectsPropertiesIfTheyHaveMatchingBindings()
        {
            var testModule = new InlineModule(
                m => m.Bind <PocoForFieldAutoWiring>().ToSelf(),
                m => m.Bind <IServiceA>().To <ServiceImplA>(),
                m => m.Bind <IServiceB>().To <ServiceImplB>()
                );

            var options = new KernelOptions {
                InjectNonPublicMembers = true
            };

            using (var kernel = new StandardKernel(options, new AutoWiringModule(), testModule))
            {
                var mock = kernel.Get <PocoForFieldAutoWiring>();
                Assert.That(mock, Is.Not.Null);

                Assert.That(mock.Child, Is.Null);

                Assert.That(mock.ServiceA, Is.Not.Null);
                Assert.That(mock.ServiceA, Is.InstanceOfType(typeof(ServiceImplA)));

                Assert.That(mock.ServiceB, Is.Not.Null);
                Assert.That(mock.ServiceB, Is.InstanceOfType(typeof(ServiceImplB)));
            }
        }
Exemplo n.º 3
0
        public void ResultOfMethodCallNotCachedForCallsToDifferentInstances()
        {
            var testModule = new InlineModule(m => m.Bind <CacheMock>().ToSelf());

            using (var kernel = new StandardKernel(new LinFuModule(), new CacheModule(), testModule))
            {
                var obj1 = kernel.Get <CacheMock>();
                var obj2 = kernel.Get <CacheMock>();

                CacheMock.ResetCounts();

                int result;

                result = obj1.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

                result = obj1.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

                result = obj2.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMock.MultiplyCount, Is.EqualTo(2));
            }
        }
Exemplo n.º 4
0
        public void ResultOfMethodCallWithSameComplexTypeIsCached()
        {
            var testModule = new InlineModule(m => m.Bind <CacheMock>().ToSelf());

            using (var kernel = new StandardKernel(new LinFuModule(), new CacheModule(), testModule))
            {
                var obj = kernel.Get <CacheMock>();

                CacheMock.ResetCounts();

                string result;

                var simple1 = new SimpleObject();
                var simple2 = new SimpleObject();

                result = obj.Convert(simple1);
                Assert.That(result, Is.EqualTo(simple1.ToString()));
                Assert.That(CacheMock.ConvertCount, Is.EqualTo(1));

                result = obj.Convert(simple1);
                Assert.That(result, Is.EqualTo(simple1.ToString()));
                Assert.That(CacheMock.ConvertCount, Is.EqualTo(1));

                result = obj.Convert(simple2);
                Assert.That(result, Is.EqualTo(simple2.ToString()));
                Assert.That(CacheMock.ConvertCount, Is.EqualTo(2));
            }
        }
        public void MemberBasedBindingOfTypeDependencies()
        {
            IModule module = new InlineModule(
                m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.Member.Tag.EqualTo("A")),
                m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Member.Tag.EqualTo("B")),
                m => m.Bind(typeof(IMock)).To(typeof(ImplC)).Only(!When.Context.Member.Tag.EqualTo("A") & !When.Context.Member.Tag.EqualTo("B"))
                );

            using (var kernel = new StandardKernel(module))
            {
                var obj1 = kernel.Get <RequestsTagA>();
                Assert.That(obj1, Is.Not.Null);
                Assert.That(obj1.Child, Is.Not.Null);
                Assert.That(obj1.Child, Is.InstanceOfType(typeof(ImplA)));

                var obj2 = kernel.Get <RequestsTagB>();
                Assert.That(obj2, Is.Not.Null);
                Assert.That(obj2.Child, Is.Not.Null);
                Assert.That(obj2.Child, Is.InstanceOfType(typeof(ImplB)));

                var obj3 = kernel.Get <RequestsNoTag>();
                Assert.That(obj3, Is.Not.Null);
                Assert.That(obj3.Child, Is.Not.Null);
                Assert.That(obj3.Child, Is.InstanceOfType(typeof(ImplC)));
            }
        }
Exemplo n.º 6
0
        public void MemberBasedBindingOfTypeDependencies()
        {
            IModule module = new InlineModule(
                m => m.Bind(typeof(ObjectWithMethodInterceptor)).ToSelf(),
                m => m.Intercept(When.Request.Method.Name.StartsWith("F")).With <CountInterceptor>()
                );

            using (var kernel = new StandardKernel(module))
            {
                kernel.Components.Connect <IProxyFactory>(new DummyProxyFactory());

                var obj = kernel.Get <ObjectWithMethodInterceptor>();

                IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));

                IRequest request = new StandardRequest(
                    context,
                    obj,
                    typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
                    new object[0],
                    Type.EmptyTypes
                    );

                ICollection <IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
                Assert.That(interceptors.Count, Is.EqualTo(2));

                IEnumerator <IInterceptor> enumerator = interceptors.GetEnumerator();

                enumerator.MoveNext();
                Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));

                enumerator.MoveNext();
                Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
            }
        }
Exemplo n.º 7
0
        public void StaticInterceptorsNotRegisteredForMethodsDecoratedWithDoNotInterceptAttribute()
        {
            var module = new InlineModule(m => m.Bind <ObjectWithClassInterceptor>().ToSelf());

            using (var kernel = new StandardKernel(module))
            {
                kernel.Components.Connect <IProxyFactory>(new DummyProxyFactory());

                var obj = kernel.Get <ObjectWithClassInterceptor>();

                IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithClassInterceptor));

                IRequest request = new StandardRequest(
                    context,
                    obj,
                    typeof(ObjectWithClassInterceptor).GetMethod("Baz"),
                    new object[0],
                    Type.EmptyTypes
                    );

                ICollection <IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);

                Assert.That(interceptors.Count, Is.EqualTo(0));
            }
        }
        public void PropertiesAreInjectedWhenWithPropertiesAreUsedAndInjectAttributeIsSpecified()
        {
            var testModule = new InlineModule(
                m => m.Bind <IConfigurationSettings>()
                .To <ConfigurationSettings>()
                .Using <SingletonBehavior>()
                .WithPropertyValues(new Dictionary <string, string>
            {
                { "RootUrl", "http://*****:*****@d.com" },
                { "SupportEmail", "*****@*****.**" },
                { "SiteTitle", "Test" },
                { "ActivationUrl", "http://*****:*****@d.com", configSettings.PrimaryEmail);
                Assert.AreEqual("*****@*****.**", configSettings.SupportEmail);
                Assert.AreEqual("Test", configSettings.SiteTitle);
                Assert.AreEqual("http://localhost:2161/activate/{0}", configSettings.ActivationUrl);
            }
        }
Exemplo n.º 9
0
        public void StaticInterceptorsAreRegisteredFromAttributesDefinedOnMethods()
        {
            var module = new InlineModule(m => m.Bind <ObjectWithMethodInterceptor>().ToSelf());

            using (var kernel = new StandardKernel(module))
            {
                kernel.Components.Connect <IProxyFactory>(new DummyProxyFactory());

                var obj = kernel.Get <ObjectWithMethodInterceptor>();

                IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));

                IRequest request = new StandardRequest(
                    context,
                    obj,
                    typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
                    new object[0],
                    Type.EmptyTypes
                    );

                ICollection <IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
                IEnumerator <IInterceptor> enumerator   = interceptors.GetEnumerator();
                enumerator.MoveNext();

                Assert.That(interceptors.Count, Is.EqualTo(1));
                Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
            }
        }
Exemplo n.º 10
0
        public void TimeoutControlsCachedValue()
        {
            var testModule = new InlineModule(m => m.Bind <CacheMockWithTimeout>().ToSelf());

            using (var kernel = new StandardKernel(new LinFuModule(), new CacheModule(), testModule))
            {
                var obj = kernel.Get <CacheMockWithTimeout>();

                CacheMockWithTimeout.ResetCounts();

                int result;

                result = obj.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMockWithTimeout.MultiplyCount, Is.EqualTo(1));

                result = obj.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMockWithTimeout.MultiplyCount, Is.EqualTo(1));

                Thread.Sleep(500);

                result = obj.Multiply(2, 3);
                Assert.That(result, Is.EqualTo(6));
                Assert.That(CacheMockWithTimeout.MultiplyCount, Is.EqualTo(2));
            }
        }
Exemplo n.º 11
0
        public void GenericProviderThrowsExceptionForIncompatibleBinding()
        {
            var module = new InlineModule(m => m.Bind(typeof(IGenericObject <>)).To(typeof(IncompatibleGenericImpl <>)));

            using (var kernel = new StandardKernel(module))
            {
                kernel.Get <IGenericObject <string> >();
            }
        }
Exemplo n.º 12
0
        public void DefiningMultipleDefaultBindingsThrowsException()
        {
            var module = new InlineModule(
                m => m.Bind(typeof(IMock)).To(typeof(ImplA)),
                m => m.Bind(typeof(IMock)).To(typeof(ImplB))
                );

            var kernel = new StandardKernel(module);
        }
Exemplo n.º 13
0
        public void ServiceBoundTypesDeclaringMethodInterceptorsAreProxied()
        {
            var testModule = new InlineModule(m => m.Bind <IFoo>().To <ObjectWithMethodInterceptor>());

            using (var kernel = new StandardKernel(new DynamicProxy2Module(), testModule))
            {
                var obj = kernel.Get <IFoo>();
                Assert.That(obj, Is.Not.Null);
                Assert.That(obj, Is.InstanceOfType(typeof(IProxyTargetAccessor)));
            }
        }
Exemplo n.º 14
0
        public void HeuristicIgnoresParameterlessMethods()
        {
            var testModule = new InlineModule(m => m.Bind <PocoForMethodAutoWiring>().ToSelf());

            using (var kernel = new StandardKernel(new AutoWiringModule(), testModule))
            {
                var mock = kernel.Get <PocoForMethodAutoWiring>();
                Assert.That(mock, Is.Not.Null);
                Assert.That(mock.ParameterlessMethodWasCalled, Is.False);
            }
        }
Exemplo n.º 15
0
        public void SelfBoundTypesDeclaringMethodInterceptorsAreProxied()
        {
            var testModule = new InlineModule(m => m.Bind <ObjectWithMethodInterceptor>().ToSelf());

            using (var kernel = new StandardKernel(new LinFuModule(), testModule))
            {
                var obj = kernel.Get <ObjectWithMethodInterceptor>();
                Assert.That(obj, Is.Not.Null);
                Assert.That(obj, Is.InstanceOfType(typeof(IProxy)));
            }
        }
Exemplo n.º 16
0
        public void IncompatibleProviderAndBindingServiceTypeThrowsException()
        {
            var module  = new InlineModule(m => m.Bind(typeof(IMock)).To(typeof(ObjectWithNoInterfaces)));
            var options = new KernelOptions {
                IgnoreProviderCompatibility = false
            };

            using (var kernel = new StandardKernel(options, module))
            {
                kernel.Get <IMock>();
            }
        }
Exemplo n.º 17
0
        public void MultipleMatchingConditionalBindingsWithNoDefaultBindingThrowsException()
        {
            var module = new InlineModule(
                m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.Kernel.IsDefined),
                m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Kernel.IsDefined)
                );

            using (var kernel = new StandardKernel(module))
            {
                kernel.Get <IMock>();
            }
        }
Exemplo n.º 18
0
        public void SelfBoundTypesDeclaringMethodInterceptorsCanBeReleased()
        {
            var testModule = new InlineModule(m => m.Bind <ObjectWithMethodInterceptor>().ToSelf());

            using (var kernel = new StandardKernel(new LinFuModule(), testModule))
            {
                var obj = kernel.Get <ObjectWithMethodInterceptor>();
                Assert.That(obj, Is.Not.Null);

                kernel.Release(obj);
            }
        }
Exemplo n.º 19
0
        public void NoMatchingConditionalBindingsWithNoDefaultBindingThrowsException()
        {
            var module = new InlineModule(
                m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.Kernel.Configuration.EqualTo("foo")),
                m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Kernel.Configuration.EqualTo("bar"))
                );

            using (var kernel = new StandardKernel(module))
            {
                kernel.Get <IMock>();
            }
        }
Exemplo n.º 20
0
        public void CanBindToStaticFactoryMethodWithParameters()
        {
            var module = new InlineModule(
                m => m.Bind(typeof(IMock)).ToMethod(ctx => MockFactory.CreateStatic("foo", 42))
                );

            using (var kernel = new StandardKernel(module))
            {
                var mock = kernel.Get <IMock>();
                Assert.That(mock, Is.Not.Null);
                Assert.That(mock, Is.InstanceOfType(typeof(ImplB)));
            }
        }
Exemplo n.º 21
0
        public void IncompatibleProviderAllowedIfProviderCompatibilityIsIgnored()
        {
            var module  = new InlineModule(m => m.Bind(typeof(IMock)).To(typeof(ObjectWithNoInterfaces)));
            var options = new KernelOptions {
                IgnoreProviderCompatibility = true
            };

            using (var kernel = new StandardKernel(options, module))
            {
                var mock = kernel.Get(typeof(IMock)) as ObjectWithNoInterfaces;
                Assert.That(mock, Is.Not.Null);
            }
        }
Exemplo n.º 22
0
        public void ConditionalBindingViaPredicate()
        {
            var module = new InlineModule(
                m => m.Bind(typeof(IMock)).To(typeof(ImplA)).OnlyIf(c => c.IsRoot),
                m => m.Bind(typeof(IMock)).To(typeof(ImplB))
                );

            using (var kernel = new StandardKernel(module))
            {
                var mock = kernel.Get <IMock>();
                Assert.That(mock, Is.Not.Null);
                Assert.That(mock, Is.InstanceOfType(typeof(ImplA)));
            }
        }
Exemplo n.º 23
0
        public void ServiceBoundTypeReceivesFieldInjection()
        {
            var module = new InlineModule(m => m.Bind(typeof(IMock)).To(typeof(RequestsFieldInjection)));

            using (var kernel = new StandardKernel(module))
            {
                var mock = kernel.Get <IMock>();
                Assert.That(mock, Is.Not.Null);

                var typedMock = mock as RequestsFieldInjection;
                Assert.That(typedMock, Is.Not.Null);
                Assert.That(typedMock.Child, Is.Not.Null);
            }
        }
Exemplo n.º 24
0
        public void HybridInjectionWithBindingParameterThatCannotBeConvertedThrowsException()
        {
            IModule module = new InlineModule(
                m => m.Bind <HybridInjectionMock>()
                .ToSelf()
                .WithConstructorArgument("connectionString", "this is a connection string")
                .WithConstructorArgument("child", "invalid")
                );

            using (var kernel = new StandardKernel(module))
            {
                kernel.Get <HybridInjectionMock>();
            }
        }
Exemplo n.º 25
0
        public void CanBindGenericTypeDefinitions()
        {
            var module = new InlineModule(m => m.Bind(typeof(IGenericObject <>)).To(typeof(GenericImpl <>)));

            using (var kernel = new StandardKernel(module))
            {
                Type     type    = typeof(IGenericObject <>);
                IContext context = kernel.Components.ContextFactory.Create(type);
                IBinding binding = kernel.Components.BindingSelector.SelectBinding(type, context);

                Assert.That(binding, Is.Not.Null);
                Assert.That(binding.Provider, Is.InstanceOfType(typeof(GenericProvider)));
            }
        }
Exemplo n.º 26
0
        public void CanOverrideBehaviorViaBindingDeclaration()
        {
            var module = new InlineModule(m => m.Bind <IMock>().To <ImplA>().Using <SingletonBehavior>());

            using (var kernel = new StandardKernel(module))
            {
                var mock1 = kernel.Get <IMock>();
                var mock2 = kernel.Get <IMock>();

                Assert.That(mock1, Is.Not.Null);
                Assert.That(mock2, Is.Not.Null);
                Assert.That(mock1, Is.SameAs(mock2));
            }
        }
Exemplo n.º 27
0
        public void CanBindToParameterlessInstanceFactoryMethod()
        {
            var factory = new MockFactory();

            var module = new InlineModule(
                m => m.Bind(typeof(IMock)).ToMethod(ctx => factory.Create())
                );

            using (var kernel = new StandardKernel(module))
            {
                var mock = kernel.Get <IMock>();
                Assert.That(mock, Is.Not.Null);
                Assert.That(mock, Is.InstanceOfType(typeof(ImplA)));
            }
        }
Exemplo n.º 28
0
        private StandardModule GetSpecificModule(ITargetDevice targetDevice)
        {
            var display = targetDevice.Display;

            var inlineModule = new InlineModule(
                (m) => m.Bind <IDisplay>().ToConstant(display),
                (m) => m.Bind <ITargetDevice>().ToConstant(targetDevice),
                (m) => m.Bind <IGeometryContext>().To(targetDevice.GeometryContextType),
                (m) => m.Bind <IImageContext>().To(targetDevice.ImageContextType),
                (m) => m.Bind <ISpriteContext>().To(targetDevice.SpriteContextType),
                (m) => m.Bind <IFileLoader>().ToMethod((c) => CreateFileLoader(targetDevice))
                );

            return(inlineModule);
        }
Exemplo n.º 29
0
        public void ServiceBoundTypeReceivesMethodInjection()
        {
            var module = new InlineModule(m => m.Bind <IMock>().To <RequestsMethodInjection>());

            using (var kernel = new StandardKernel(module))
            {
                var mock = kernel.Get <IMock>();
                Assert.That(mock, Is.Not.Null);

                var typedMock = mock as RequestsMethodInjection;
                Assert.That(typedMock, Is.Not.Null);
                Assert.That(typedMock.Child, Is.Not.Null);
                Assert.That(typedMock.Child, Is.InstanceOfType(typeof(SimpleObject)));
            }
        }
Exemplo n.º 30
0
        public void MultipleMatchingConditionalBindingsWithDefaultBindingUsesDefaultBinding()
        {
            IModule module = new InlineModule(
                m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.IsRoot),
                m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Kernel.IsDefined),
                m => m.Bind(typeof(IMock)).To(typeof(ImplC))
                );

            using (var kernel = new StandardKernel(module))
            {
                var mock = kernel.Get <IMock>();
                Assert.That(mock, Is.Not.Null);
                Assert.That(mock, Is.InstanceOfType(typeof(ImplC)));
            }
        }