public void DoesNotAcceptInfrastructureAdvisorsDuringScanning()
        {
            DefaultListableObjectFactory of = new DefaultListableObjectFactory();

            GenericObjectDefinition infrastructureAdvisorDefinition = new GenericObjectDefinition();
            infrastructureAdvisorDefinition.ObjectType = typeof (TestAdvisor);
            infrastructureAdvisorDefinition.PropertyValues.Add("Name", "InfrastructureAdvisor");
            infrastructureAdvisorDefinition.Role = ObjectRole.ROLE_INFRASTRUCTURE;
            of.RegisterObjectDefinition("infrastructure", infrastructureAdvisorDefinition);

            GenericObjectDefinition regularAdvisorDefinition = new GenericObjectDefinition();
            regularAdvisorDefinition.ObjectType = typeof (TestAdvisor);
            regularAdvisorDefinition.PropertyValues.Add("Name", "RegularAdvisor");
            //            regularAdvisorDefinition.Role = ObjectRole.ROLE_APPLICATION;
            of.RegisterObjectDefinition("regular", regularAdvisorDefinition);

            TestAdvisorAutoProxyCreator apc = new TestAdvisorAutoProxyCreator();
            apc.ObjectFactory = of;
            object[] advisors = apc.GetAdvicesAndAdvisorsForObject(typeof (object), "dummyTarget");
            Assert.AreEqual(1, advisors.Length);
            Assert.AreEqual( "RegularAdvisor", ((TestAdvisor)advisors[0]).Name );

            Assert.AreEqual(1, apc.CheckedAdvisors.Count);
            Assert.AreEqual("regular", apc.CheckedAdvisors[0]);
        }
 public void ShouldNotTryToResolveAbstractDefinitionsToType()
 {
     GenericObjectDefinition definition = new GenericObjectDefinition();
     definition.ObjectTypeName = "~/Default.aspx";
     definition.IsAbstract = true;
     Assert.That(ConfigurationClassObjectDefinitionReader.CheckConfigurationClassCandidate(definition), Is.False);
 }
        /// <summary>
        /// Creates a new <see cref="GenericObjectDefinition"/> as deep copy of the given
        /// object definition.
        /// </summary>
        /// <param name="original">the original object definition to copy from</param>
        public GenericObjectDefinition(IObjectDefinition original)
            : base(original)
        {
            GenericObjectDefinition god = original as GenericObjectDefinition;

            if (god != null)
            {
                this.parentName = god.parentName;
            }
        }
    private static AbstractApplicationContext CreateConfigContext(IConfigurationRoot configurationRoot)
    {
        StaticApplicationContext configContext = new StaticApplicationContext();

        foreach (var currentConfiguration in configurationRoot.AsEnumerable())
        {
            Spring.Objects.Factory.Support.GenericObjectDefinition objectDefinition = new Spring.Objects.Factory.Support.GenericObjectDefinition
            {
                ObjectType        = typeof(string),
                FactoryMethodName = "Copy"
            };
            objectDefinition.ConstructorArgumentValues.AddIndexedArgumentValue(0, currentConfiguration.Value);
            //System.Diagnostics.Debug.WriteLine($"{currentConfiguration.Key} : {currentConfiguration.Value}");
            configContext.RegisterObjectDefinition($"CONFIG:{currentConfiguration.Key}", objectDefinition);
        }
        return(configContext);
    }
        private static void WireParentAndChildWWithImpliedDependencyByConstructorInjection(bool parentIsSingleton, bool childIsSingleton)
        {
            GenericObjectDefinition child = new GenericObjectDefinition();
            child.ObjectTypeName = typeof(Child).FullName;
            child.IsSingleton = childIsSingleton;
            ObjectFactory.RegisterObjectDefinition("child", child);

            GenericObjectDefinition parent = new GenericObjectDefinition();
            parent.ObjectTypeName = typeof(Parent).FullName;
            parent.IsSingleton = parentIsSingleton;
            parent.ConstructorArgumentValues.AddIndexedArgumentValue(0, new RuntimeObjectReference("child"));
            ObjectFactory.RegisterObjectDefinition("parent", parent);
        }
        private void WireParentAndChildWithDependsOnDeclarationDependency(bool parentIsSingleton, bool childIsSingleton)
        {
            GenericObjectDefinition child = new GenericObjectDefinition();
            child.ObjectTypeName = typeof(Child).FullName;
            child.IsSingleton = childIsSingleton;
            ObjectFactory.RegisterObjectDefinition("child", child);

            GenericObjectDefinition parent = new GenericObjectDefinition();
            parent.ObjectTypeName = typeof(Parent).FullName;
            parent.IsSingleton = parentIsSingleton;
            parent.DependsOn = new string[] { "child" };
            ObjectFactory.RegisterObjectDefinition("parent", parent);
        }
        public void _SetUp()
        {
            ObjectFactory = CreateObjectFactory(true);

            GenericObjectDefinition threadCreatorInsideConstructor = new GenericObjectDefinition();
            threadCreatorInsideConstructor.ObjectTypeName = typeof(ThreadCreatorInsideConstructor).FullName;
            threadCreatorInsideConstructor.IsSingleton = true;
            ObjectFactory.RegisterObjectDefinition("threadCreatorInsideConstructor", threadCreatorInsideConstructor);

            GenericObjectDefinition threadCreatorInsideDispose = new GenericObjectDefinition();
            threadCreatorInsideDispose.ObjectTypeName = typeof(ThreadCreatorInsideDispose).FullName;
            threadCreatorInsideDispose.IsSingleton = true;
            ObjectFactory.RegisterObjectDefinition("threadCreatorInsideDispose", threadCreatorInsideDispose);
        }
        public void GetObjectIsThreadSafe()
        {
            ObjectFactory = CreateObjectFactory(true);

            GenericObjectDefinition theSpouse = new GenericObjectDefinition();
            theSpouse.ObjectTypeName = typeof(TestObject).FullName;
            theSpouse.IsSingleton = false;
            ObjectFactory.RegisterObjectDefinition("theSpouse", theSpouse);

            GenericObjectDefinition theObject = new GenericObjectDefinition();
            theObject.ObjectTypeName = typeof(TestObject).FullName;
            theObject.IsSingleton = false;
            theObject.PropertyValues.Add("Spouse", theSpouse);
            ObjectFactory.RegisterObjectDefinition("theObject", theObject);

            AsyncTestTask t1 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();
            AsyncTestTask t2 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();
            AsyncTestTask t3 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();
            AsyncTestTask t4 = new AsyncTestMethod(20000, new ThreadStart(GetTheTestObject)).Start();

            t1.AssertNoException();
            t2.AssertNoException();
            t3.AssertNoException();
            t4.AssertNoException();
        }
        public void ThrowsOnCyclicDependenciesOnNonSingletons()
        {
            AbstractObjectFactory of = this.CreateObjectFactory(true);

            GenericObjectDefinition od = new GenericObjectDefinition();
            od.ObjectTypeName = typeof(TestObject).FullName;
            od.IsSingleton = false;
            od.PropertyValues.Add(new PropertyValue("Spouse", new RuntimeObjectReference("product2")));
            of.RegisterObjectDefinition("product1", od);

            GenericObjectDefinition od2 = new GenericObjectDefinition();
            od2.ObjectTypeName = typeof(TestObject).FullName;
            od2.IsSingleton = false;
            od2.PropertyValues.Add(new PropertyValue("Sibling", new RuntimeObjectReference("product1")));
            of.RegisterObjectDefinition("product2", od2);

            try
            {
                of.GetObject("product1");
                Assert.Fail();
            }
            catch (ObjectCurrentlyInCreationException ex)
            {
                Assert.AreEqual("product1", ex.ObjectName);
            }
        }
        public void CanResolveCyclicSingletonFactoryObjectProductDependencies()
        {
            AbstractObjectFactory of = this.CreateObjectFactory(true);

            GenericObjectDefinition od = new GenericObjectDefinition();
            od.ObjectTypeName = typeof(TestObject).FullName;
            od.IsSingleton = true;
            od.PropertyValues.Add(new PropertyValue("Spouse", new RuntimeObjectReference("product2")));
            of.RegisterObjectDefinition("product1Target", od);

            GenericObjectDefinition od2 = new GenericObjectDefinition();
            od2.ObjectTypeName = typeof(TestObject).FullName;
            od2.IsSingleton = true;
            od2.PropertyValues.Add(new PropertyValue("Sibling", new RuntimeObjectReference("product1")));
            of.RegisterObjectDefinition("product2Target", od2);

            of.RegisterSingleton("product1", new ObjectReferenceFactoryObject("product1Target", of));
            of.RegisterSingleton("product2", new ObjectReferenceFactoryObject("product2Target", of));

            TestObject to = (TestObject)of.GetObject("product1");
            Assert.NotNull(to);
            Assert.NotNull(to.Spouse);
            Assert.NotNull(((TestObject)to.Spouse).Sibling);
        }
        private void LoadObjectDefinitionForConfigurationClassIfNecessary(ConfigurationClass configClass)
        {
            if (configClass.ObjectName != null)
            {
                // a Object definition already exists for this configuration class -> nothing to do
                return;
            }

            // no Object definition exists yet -> this must be an imported configuration class ([Import]).
            GenericObjectDefinition configObjectDef = new GenericObjectDefinition();
            String className = configClass.ConfigurationClassType.Name;
            configObjectDef.ObjectTypeName = className;
            configObjectDef.ObjectType = configClass.ConfigurationClassType;
            if (CheckConfigurationClassCandidate(configClass.ConfigurationClassType))
            {
                String configObjectName = ObjectDefinitionReaderUtils.RegisterWithGeneratedName(configObjectDef,
                                                                                                _registry);
                configClass.ObjectName = configObjectName;
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(String.Format("Registered object definition for imported [Configuration] class {0}",
                                                configObjectName));
                }
            }
        }