Exemple #1
0
 public void Singlecall_components_should_yield_unique_instances()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.AreNotEqual(builder.Resolve <SinglecallComponent>(), builder.Resolve <SinglecallComponent>());
     }
 }
Exemple #2
0
 public void Requesting_an_unregistered_component_should_throw()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         InitializeBuilder(builder);
         Assert.That(() => builder.Resolve <UnregisteredComponent>(), Throws.Exception);
     }
 }
Exemple #3
0
 public void Generic_interfaces_should_be_registered()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         builder.Register <ISomeGenericInterface <string>, ComponentWithGenericInterface>();
         Assert.NotNull(builder.Resolve(typeof(ISomeGenericInterface <string>)));
     }
 }
Exemple #4
0
 public void Component_should_be_log_by_logInterceptor()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         //builder.RegisterComponent<LogComponent>();
         //var obj = builder.Resolve<IPublicInterface>();
         //obj.PublicMethod();
     }
 }
 public void Component_should_be_interceptor_by_logInterceptor()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         ObjectContainer.RegisterComponent <LogComponent>();
         IPublicInterface obj = builder.Resolve <IPublicInterface>();
         Assert.AreEqual(11, obj.PublicMethod());
     }
 }
Exemple #6
0
 public void Register_singleton_should_be_supported()
 {
     using (var builder = TestContainerBuilder.ConstructBuilder())
     {
         var singleton = new SingletonComponent();
         builder.RegisterInstance <ISingletonComponent, SingletonComponent>(singleton);
         Assert.AreEqual(builder.Resolve <ISingletonComponent>(), singleton);
     }
 }
Exemple #7
0
        public void Singleton_components_should_yield_the_same_instance()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                InitializeBuilder(builder);

                Assert.AreEqual(builder.Resolve <IComponent>(), builder.Resolve <IComponent>());
                Assert.AreEqual(builder.Resolve <IComponent>().Now, builder.Resolve <IComponent>().Now);
            }
        }
        public void TestExceptionHandlingInterceptor()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType <LogComponent>();
                var obj = builder.Resolve <IInterceptor>();

                Assert.Throws <Exception>(() => obj.PublicMethod());
            }
        }
Exemple #9
0
        public void Singleton_components_should_get_their_dependencies_autowired()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Register <ISingletonComponentWithPropertyDependency, SingletonComponentWithPropertyDependency>();
                builder.Register <SingletonComponent, SingletonComponent>();

                var singleton = (SingletonComponentWithPropertyDependency)builder.Resolve <ISingletonComponentWithPropertyDependency>();
                Assert.IsNotNull(singleton.Dependency);
            }
        }
Exemple #10
0
        public void A_registration_should_update_default_component_for_interface()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType(typeof(SomeClass), LifeStyle.Transient);
                builder.RegisterType(typeof(SomeOtherClass), LifeStyle.Transient);

                Assert.IsNotInstanceOf <SomeClass>(builder.Resolve(typeof(ISomeInterface)));
                Assert.IsInstanceOf <SomeOtherClass>(builder.Resolve(typeof(ISomeInterface)));
            }
        }
Exemple #11
0
        public void A_registration_should_be_allowed_to_be_updated()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Register <ISingletonComponent, SingletonComponent>();
                builder.Register <ISingletonComponent, AnotherSingletonComponent>();

                Assert.IsInstanceOf <AnotherSingletonComponent>(builder.Resolve(typeof(ISingletonComponent)));
            }

            //Not supported by, typeof(SpringObjectBuilder));
        }
Exemple #12
0
        public void Multiple_implementations_will_be_override()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType(typeof(SomeClass), LifeStyle.Transient);
                builder.RegisterType(typeof(SomeOtherClass), LifeStyle.Transient);

                Assert.IsTrue(builder.IsRegistered <ISomeInterface>());
                Assert.AreNotSame(builder.Resolve <SomeClass>().GetType(), builder.Resolve <ISomeInterface>().GetType());
            }
            //Not supported by,typeof(WindsorObjectBuilder));
        }
Exemple #13
0
        public void Concrete_classes_should_get_the_same_lifecycle_as_their_interfaces()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType(typeof(SingletonComponent), LifeStyle.Singleton);

                var instance = builder.Resolve(typeof(SingletonComponent)) as SingletonComponent;
                builder.RegisterInstance <SingletonComponent, SingletonComponent>(instance);
                builder.RegisterInstance <ISingletonComponent, SingletonComponent>(instance);

                Assert.AreSame(builder.Resolve(typeof(SingletonComponent)), builder.Resolve(typeof(ISingletonComponent)));
            }
        }
Exemple #14
0
        public void Should_be_able_to_build_components_registered_after_first_build()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                InitializeBuilder(builder);
                builder.Resolve <SingletonComponent>();
                builder.RegisterType(typeof(UnregisteredComponent), LifeStyle.Singleton);

                var unregisteredComponent = builder.Resolve <UnregisteredComponent>();
                Assert.NotNull(unregisteredComponent);
                Assert.NotNull(unregisteredComponent.SingletonComponent);
            }
            //Not supported by,typeof(SpringObjectBuilder));
        }
Exemple #15
0
        public void All_implemented_interfaces_should_be_registered()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType(typeof(ComponentWithMultipleInterfaces),
                                     LifeStyle.Transient);

                Assert.IsTrue(builder.IsRegistered <ISomeInterface>());

                Assert.IsTrue(builder.IsRegistered <ISomeOtherInterface>());

                Assert.IsTrue(builder.IsRegistered <IYetAnotherInterface>());
            }
        }
Exemple #16
0
        public void Should_support_mixed_dependency_styles()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                InitializeBuilder(builder);
                builder.RegisterType(typeof(ComponentWithBothConstructorAndSetterInjection), LifeStyle.Transient);
                builder.RegisterType(typeof(ConstructorDependency), LifeStyle.Transient);
                builder.RegisterType(typeof(SetterDependency), LifeStyle.Transient);

                var component = builder.Resolve <ComponentWithBothConstructorAndSetterInjection>();
                Assert.NotNull(component.ConstructorDependency);
                Assert.NotNull(component.SetterDependency);
            }
        }
Exemple #17
0
        public void Resovle_MemoryUsage_test()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType(typeof(SinglecallComponent), LifeStyle.Transient);

                var process          = Process.GetCurrentProcess();
                var startMemoryUsage = process.WorkingSet64;
                for (int i = 0; i < 10000; i++)
                {
                    var comp = builder.Resolve <SinglecallComponent>();
                }
                Debug.WriteLine((process.WorkingSet64 - startMemoryUsage) / 1024 + " kb");
            }
        }
Exemple #18
0
        public void Component_should_be_load_at_moduleLevel_core()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                var moduleLevel = ModuleLevel.Core;
                ObjectContainer.RegisterComponent <CoreComponent>(moduleLevel);
                ObjectContainer.RegisterComponent <NormalComponent>(moduleLevel);
                ObjectContainer.RegisterComponent <ThirdPartComponent>(moduleLevel);

                Assert.IsTrue(builder.IsRegistered <CoreComponent>());

                Assert.IsFalse(builder.IsRegistered <NormalComponent>());

                Assert.IsFalse(builder.IsRegistered <ThirdPartComponent>());
            }
        }
        public void Should_dispose_all_IDisposable_components()
        {
            var builder = TestContainerBuilder.ConstructBuilder();

            DisposableComponent.DisposeCalled       = false;
            AnotherSingletonComponent.DisposeCalled = false;

            builder.RegisterType(typeof(DisposableComponent), LifeStyle.Singleton);
            builder.Register <AnotherSingletonComponent, AnotherSingletonComponent>();

            builder.Resolve(typeof(DisposableComponent));
            builder.Resolve(typeof(AnotherSingletonComponent));
            builder.Dispose();

            Assert.True(DisposableComponent.DisposeCalled, "Dispose should be called on DisposableComponent");
            Assert.True(AnotherSingletonComponent.DisposeCalled, "Dispose should be called on AnotherSingletonComponent");
        }
Exemple #20
0
        public void Transient_component_should_be_destructed_called()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType(typeof(TransientClass), LifeStyle.Transient);

                using (var comp = builder.Resolve <TransientClass>())
                {
                    comp.Name = "Jon";
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();

                Assert.IsTrue(TransientClass.Destructed);
            }
        }
Exemple #21
0
        public void Setter_dependencies_should_be_supported_when_resolving_interfaces()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterType(typeof(SomeClass), LifeStyle.Transient);
                builder.Register <IWithSetterDependencies, ClassWithSetterDependencies>();

                builder.Register <ISomeInterface, SomeClass>();

                var component = (ClassWithSetterDependencies)builder.Resolve(typeof(IWithSetterDependencies));


                Assert.NotNull(component.ConcreteDependency, "Concrete classed should be property injected");
                Assert.NotNull(component.InterfaceDependency, "Interfaces should be property injected");
                Assert.NotNull(component.concreteDependencyWithSetOnly, "Set only properties should be supported");
            }
        }
        public void TestExceptionHandlingInterceptorHeighCpuUsage()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.RegisterComponent <LogComponent>(LifeStyle.Transient, Interceptor.ExceptionHandle);
                var obj = builder.Resolve <IInterceptor>();

                for (int i = 0; i < 1000; i++)
                {
                    try
                    {
                        obj.PublicMethod();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
        }
Exemple #23
0
        public void Registering_the_same_singleton_for_different_interfaces_should_be_supported()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                var singleton = new SingletonThatImplementsToInterfaces();
                builder.RegisterInstance <ISingleton1, SingletonThatImplementsToInterfaces>(singleton);
                builder.RegisterInstance <ISingleton2, SingletonThatImplementsToInterfaces>(singleton);

                builder.RegisterType(typeof(ComponentThatDependsOnMultiSingletons), LifeStyle.Transient);

                var dependency = (ComponentThatDependsOnMultiSingletons)builder.Resolve(typeof(ComponentThatDependsOnMultiSingletons));

                Assert.NotNull(dependency.Singleton1);
                Assert.NotNull(dependency.Singleton2);

                Assert.AreEqual(builder.Resolve(typeof(ISingleton1)), singleton);
                Assert.AreEqual(builder.Resolve(typeof(ISingleton2)), singleton);
            }

            //Not supported by,typeof(SpringObjectBuilder));
        }
Exemple #24
0
        public void Should_support_meta()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                InitializeBuilder(builder);
                builder.RegisterType(typeof(BpmMeta), LifeStyle.Transient);
                builder.RegisterType(typeof(ErpMeta), LifeStyle.Transient);
                builder.RegisterType(typeof(MetaClass), LifeStyle.Transient);
                builder.RegisterType(typeof(DefaultMeta), LifeStyle.Transient);

                var component = builder.Resolve <IMetaClass>();
                Assert.NotNull(component.Bpm);
                Assert.That(component.Bpm, Is.InstanceOf(typeof(BpmMeta)));
                Assert.NotNull(component.Erp);
                Assert.That(component.Erp, Is.InstanceOf(typeof(ErpMeta)));
                Assert.NotNull(component.Default);
                Assert.That(component.Default, Is.InstanceOf(typeof(DefaultMeta)));

                List <Meta <IMetaInterface> > comps = builder.Resolve <IEnumerable <Meta <IMetaInterface> > >().ToList();
                Assert.AreEqual(3, comps.Count());
                Assert.True(comps.Any(c => c.Metadata.Any(d => d.Key == "Type" && d.Value is DbTypes.Erp)));
            }
        }
        public void Component_interceptor_performence_test()
        {
            Stopwatch st = new Stopwatch();

            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                //no interceptor performence test
                ObjectContainer.RegisterComponent <NoInterceptorComponent>();
                int k = 0;
                st.Start();
                Parallel.For(0, 100000, i =>
                {
                    try
                    {
                        IPublicInterface obj = builder.Resolve <IPublicInterface>();
                        int ret = obj.PublicMethod();
                        k++;
                        if (k % 10000 == 0)
                        {
                            Console.WriteLine(k + " -- " + ret);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                });
                Console.WriteLine("no interceptor ={0}", st.Elapsed);

                //MeasureAndLog performence test
                ObjectContainer.RegisterComponent <MeasureAndLogComponent>();
                k = 0;
                st.Restart();
                Parallel.For(0, 100000, i =>
                {
                    try
                    {
                        IPublicInterface obj = builder.Resolve <IPublicInterface>();
                        int ret = obj.PublicMethod();
                        k++;
                        if (k % 10000 == 0)
                        {
                            Console.WriteLine(k + " -- " + ret);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                });
                Console.WriteLine("MeasureAndLog ={0}", st.Elapsed);

                //no ioc performence test
                k = 0;
                st.Restart();
                Parallel.For(0, 100000, i =>
                {
                    try
                    {
                        int ret = new NoInterceptorComponent().PublicMethod();
                        k++;
                        if (k % 10000 == 0)
                        {
                            Console.WriteLine(k + " -- " + ret);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                });
                Console.WriteLine("no ioc ={0}", st.Elapsed);
            }
        }