public void ExtensiveCircularReference()
 {
     DefaultListableObjectFactory lof = new DefaultListableObjectFactory();
     for (int i = 0; i < 1000; i++)
     {
         MutablePropertyValues pvs = new MutablePropertyValues();
         pvs.Add(new PropertyValue("Spouse", new RuntimeObjectReference("object" + (i < 99 ? i + 1 : 0))));
         RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject), pvs);
         lof.RegisterObjectDefinition("object" + i, rod);
     }
     lof.PreInstantiateSingletons();
     for (int i = 0; i < 1000; i++)
     {
         TestObject obj = (TestObject)lof.GetObject("object" + i);
         TestObject otherObj = (TestObject)lof.GetObject("object" + (i < 99 ? i + 1 : 0));
         Assert.IsTrue(obj.Spouse == otherObj);
     }
 }
        public void SingletonFactoryObjectMustNotCreatePrototypeOnPreInstantiateSingletonsCall()
        {
            DefaultListableObjectFactory lof = new DefaultListableObjectFactory();

            RootObjectDefinition def = new RootObjectDefinition();
            def.ObjectType = typeof(DummyFactory);
            def.IsSingleton = true;
            def.PropertyValues.Add("IsSingleton", false);

            DummyFactory.Reset();

            Assert.IsFalse(DummyFactory.WasPrototypeCreated,
                           "Prototype appears to be instantiated before the test is even run.");
            lof.RegisterObjectDefinition("x1", def);
            Assert.IsFalse(DummyFactory.WasPrototypeCreated,
                           "Prototype instantiated after object definition registration (must NOT be).");
            lof.PreInstantiateSingletons();
            Assert.IsFalse(DummyFactory.WasPrototypeCreated,
                           "Prototype instantiated after call to PreInstantiateSingletons(); must NOT be.");
            lof.GetObject("x1");
            Assert.IsTrue(DummyFactory.WasPrototypeCreated, "Prototype was not instantiated.");
        }
 public void PreInstantiateSingletonsMustNotIgnoreObjectsWithUnresolvedObjectTypes()
 {
     KnowsIfInstantiated.ClearInstantiationRecord();
     DefaultListableObjectFactory lof = new DefaultListableObjectFactory();
     Assert.IsTrue(!KnowsIfInstantiated.WasInstantiated, "Singleton appears to be instantiated before the test is even run.");
     RootObjectDefinition def = new RootObjectDefinition();
     def.ObjectTypeName = typeof(KnowsIfInstantiated).FullName;
     lof.RegisterObjectDefinition("x1", def);
     Assert.IsTrue(!KnowsIfInstantiated.WasInstantiated, "Singleton appears to be instantiated before PreInstantiateSingletons() is invoked.");
     lof.PreInstantiateSingletons();
     Assert.IsTrue(KnowsIfInstantiated.WasInstantiated, "Singleton was not instantiated by the container (it must be).");
 }
        public void LazyInitialization()
        {
            KnowsIfInstantiated.ClearInstantiationRecord();
            DefaultListableObjectFactory lof = new DefaultListableObjectFactory();

            RootObjectDefinition def = new RootObjectDefinition();
            def.ObjectTypeName = typeof(KnowsIfInstantiated).FullName;
            def.IsLazyInit = true;
            lof.RegisterObjectDefinition("x1", def);

            Assert.IsTrue(!KnowsIfInstantiated.WasInstantiated, "Singleton appears to be instantiated before the test is even run.");
            lof.RegisterObjectDefinition("x1", def);
            Assert.IsTrue(!KnowsIfInstantiated.WasInstantiated, "Singleton appears to be instantiated before PreInstantiateSingletons() is invoked.");
            lof.PreInstantiateSingletons();
            Assert.IsFalse(KnowsIfInstantiated.WasInstantiated, "Singleton was instantiated by the container (it must NOT be 'cos LazyInit was set to TRUE).");
            lof.GetObject("x1");
            Assert.IsTrue(KnowsIfInstantiated.WasInstantiated, "Singleton was not instantiated by the container (it must be).");
        }
        public void DisposeCyclesThroughAllSingletonsEvenIfTheirDisposeThrowsAnException()
        {
            RootObjectDefinition foo = new RootObjectDefinition(typeof(GoodDisposable));
            foo.IsSingleton = true;
            RootObjectDefinition bar = new RootObjectDefinition(typeof(BadDisposable));
            bar.IsSingleton = true;
            RootObjectDefinition baz = new RootObjectDefinition(typeof(GoodDisposable));
            baz.IsSingleton = true;

            using (DefaultListableObjectFactory fac = new DefaultListableObjectFactory())
            {
                fac.RegisterObjectDefinition("foo", foo);
                fac.RegisterObjectDefinition("bar", bar);
                fac.RegisterObjectDefinition("baz", baz);
                fac.PreInstantiateSingletons();
            }
            Assert.AreEqual(2, GoodDisposable.DisposeCount, "All IDisposable singletons must have their Dispose() method called... one of them bailed, and as a result the rest were (apparently) not Dispose()d.");
            GoodDisposable.DisposeCount = 0;
        }