From d8ff77d99941a2cf46e300c2f8dd080d58f7d18d Mon Sep 17 00:00:00 2001 From: Steve Bohlen Date: Mon, 9 Jul 2012 16:04:06 -0400 Subject: [PATCH] SPRNET-1512 Fixing code path for Spring.Context.Support.AbstractApplicationContext.GetObjectNamesForType(...) to properly respect object definitions in a hierarchy of contexts/objectfactories --- .../Objects/Factory/ObjectFactoryUtils.cs | 3 + .../Support/DefaultListableObjectFactory.cs | 37 +- .../AbstractListableObjectFactoryTests.cs | 6 +- .../Factory/ObjectFactoryUtilsTests.cs | 426 ++++++++---------- ...oryUtils_PreserveOrderInHierarchy_Tests.cs | 59 +++ ...supportedObjectDefinitionImplementation.cs | 176 ++++---- .../Xml/XmlListableObjectFactoryTests.cs | 4 +- 7 files changed, 362 insertions(+), 349 deletions(-) create mode 100644 test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtils_PreserveOrderInHierarchy_Tests.cs diff --git a/src/Spring/Spring.Core/Objects/Factory/ObjectFactoryUtils.cs b/src/Spring/Spring.Core/Objects/Factory/ObjectFactoryUtils.cs index eee5e85b..456640b0 100644 --- a/src/Spring/Spring.Core/Objects/Factory/ObjectFactoryUtils.cs +++ b/src/Spring/Spring.Core/Objects/Factory/ObjectFactoryUtils.cs @@ -212,6 +212,9 @@ namespace Spring.Objects.Factory public static IList ObjectNamesForTypeIncludingAncestors( IListableObjectFactory factory, Type type) { + + return factory.GetObjectNamesForType(type); + List result = new List(); result.AddRange(factory.GetObjectNamesForType(type)); IListableObjectFactory pof = GetParentListableObjectFactoryIfAny(factory); diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs b/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs index 8092101f..dbd12dea 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs @@ -600,12 +600,27 @@ namespace Spring.Objects.Factory.Support /// /// /// The names of all objects defined in this factory, or an empty array if none - /// are defined. + /// are defined. Respects any Parent-Child hierarchy the factory is participating in. /// /// public IList GetObjectDefinitionNames() { - return objectDefinitionNames; + IList results = new List(objectDefinitionNames); + + var listableObjectFactory = ParentObjectFactory as IListableObjectFactory; + + if (listableObjectFactory != null) + { + foreach (var name in listableObjectFactory.GetObjectDefinitionNames()) + { + if (!results.Contains(name)) + { + results.Add(name); + } + } + } + + return results; } /// @@ -677,7 +692,7 @@ namespace Spring.Objects.Factory.Support /// public IList GetObjectNames() { - return GetObjectNamesForType(typeof (T)); + return GetObjectNamesForType(typeof(T)); } /// @@ -744,7 +759,7 @@ namespace Spring.Objects.Factory.Support /// public IList GetObjectNames(bool includePrototypes, bool includeFactoryObjects) { - return GetObjectNamesForType(typeof (T), includePrototypes, includeFactoryObjects); + return GetObjectNamesForType(typeof(T), includePrototypes, includeFactoryObjects); } /// @@ -801,7 +816,7 @@ namespace Spring.Objects.Factory.Support public IDictionary GetObjects() { Dictionary result = new Dictionary(); - DoGetObjectsOfType(typeof (T), true, true, result); + DoGetObjectsOfType(typeof(T), true, true, result); return result; } @@ -848,7 +863,7 @@ namespace Spring.Objects.Factory.Support catch (ObjectCreationException ex) { if (ex.InnerException != null - && ex.GetBaseException().GetType().Equals(typeof (ObjectCurrentlyInCreationException))) + && ex.GetBaseException().GetType().Equals(typeof(ObjectCurrentlyInCreationException))) { // ignoring this is ok... it indicates a circular reference when autowiring // constructors; we want to find matches other than the currently @@ -898,7 +913,7 @@ namespace Spring.Objects.Factory.Support public IDictionary GetObjects(bool includePrototypes, bool includeFactoryObjects) { Dictionary result = new Dictionary(); - DoGetObjectsOfType(typeof (T), includePrototypes, includeFactoryObjects, result); + DoGetObjectsOfType(typeof(T), includePrototypes, includeFactoryObjects, result); return result; } @@ -982,14 +997,14 @@ namespace Spring.Objects.Factory.Support { try { - RootObjectDefinition mod = GetMergedObjectDefinition(objectName, false); + RootObjectDefinition mod = GetMergedObjectDefinition(objectName, true); // Only check object definition if it is complete if (!mod.IsAbstract && (allowEagerInit || (mod.HasObjectType || !mod.IsLazyInit /*|| this.AllowEagerTypeLoading*/ ) && - !RequiresEagerInitForType(mod.FactoryObjectName) )) + !RequiresEagerInitForType(mod.FactoryObjectName))) { bool isFactoryObject = IsFactoryObject(objectName, mod); - bool matchFound = + bool matchFound = (allowEagerInit || !isFactoryObject || ContainsSingleton(objectName)) && (includeNonSingletons || IsSingleton(objectName)) && IsTypeMatch(objectName, type); if (!matchFound && isFactoryObject) @@ -1016,7 +1031,7 @@ namespace Spring.Objects.Factory.Support log.Debug("Ignoring object class loading failure for object '" + objectName + "'", ex); } } - catch(ObjectDefinitionStoreException ex) + catch (ObjectDefinitionStoreException ex) { if (allowEagerInit) { diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs index 7868fb2f..0c68ecc2 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs @@ -58,12 +58,12 @@ namespace Spring.Objects.Factory { [Test] public virtual void Count () { - AssertCount (13); + AssertCount (16); } protected internal void AssertCount (int count) { - IList defnames = ListableObjectFactory.GetObjectDefinitionNames (); + IList defnames = ListableObjectFactory.GetObjectDefinitionNames(); Assert.IsTrue ( defnames.Count == count, string.Format ("We should have {0} objects, not {1}.", count, defnames.Count)); @@ -72,7 +72,7 @@ namespace Spring.Objects.Factory { [Test] public virtual void ObjectCount () { - AssertTestObjectCount (9); + AssertTestObjectCount (12); } public virtual void AssertTestObjectCount (int count) diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtilsTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtilsTests.cs index 1b138e43..7f0aafe2 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtilsTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtilsTests.cs @@ -20,7 +20,6 @@ #region Imports -using System; using System.Collections; using System.Collections.Generic; using NUnit.Framework; @@ -33,193 +32,130 @@ using Spring.Objects.Factory.Xml; namespace Spring.Objects.Factory { - /// - /// Unit tests for the ObjectFactoryUtils class. - /// - /// Rod Johnson - /// Simon White (.NET) - /// Rick Evans (.NET) - [TestFixture] - public sealed class ObjectFactoryUtilsTests - { - private IConfigurableListableObjectFactory _factory; + /// + /// Unit tests for the ObjectFactoryUtils class. + /// + /// Rod Johnson + /// Simon White (.NET) + /// Rick Evans (.NET) + [TestFixture] + public sealed class ObjectFactoryUtilsTests + { + private IConfigurableListableObjectFactory _factory; - [SetUp] - public void SetUp() - { - IObjectFactory grandparent - = new XmlObjectFactory(new ReadOnlyXmlTestResource("root.xml", GetType())); - IObjectFactory parent - = new XmlObjectFactory(new ReadOnlyXmlTestResource("middle.xml", GetType()), grandparent); - IConfigurableListableObjectFactory child - = new XmlObjectFactory(new ReadOnlyXmlTestResource("leaf.xml", GetType()), parent); - _factory = child; - } - - /// - /// Check that override doesn't count as two separate objects. - /// - [Test] - public void CountObjectsIncludingAncestors() - { - // leaf count... - Assert.AreEqual(1, _factory.ObjectDefinitionCount); - // count minus duplicate... - Assert.AreEqual(6, ObjectFactoryUtils.CountObjectsIncludingAncestors(_factory), - "Should count 6 objects, not " + ObjectFactoryUtils.CountObjectsIncludingAncestors(_factory)); - } - - [Test] - public void ObjectNamesIncludingAncestors() - { - IList names = ObjectFactoryUtils.ObjectNamesIncludingAncestors(_factory); - Assert.AreEqual(6, names.Count); - } - - [Test] - public void ObjectNamesIncludingAncestorsPreserveOrderOfRegistration() - { - MockRepository mocks = new MockRepository(); - IConfigurableListableObjectFactory of = (IConfigurableListableObjectFactory) mocks.DynamicMock(typeof (IConfigurableListableObjectFactory)); - IConfigurableListableObjectFactory ofParent = (IConfigurableListableObjectFactory) mocks.DynamicMock(typeof (IConfigurableListableObjectFactory)); - - Expect.Call(of.GetObjectNamesForType(typeof(object))).Return(new string[] { "objA", "objB", "objC" }); - Expect.Call(((IHierarchicalObjectFactory)of).ParentObjectFactory).Return(ofParent); - Expect.Call(ofParent.GetObjectNamesForType(typeof(object))).Return(new string[] { "obj2A", "objB", "obj2C" }); - - mocks.ReplayAll(); - - IList names = ObjectFactoryUtils.ObjectNamesIncludingAncestors(of); - Assert.AreEqual(5, names.Count); - Assert.AreEqual(new string[] { "objA","objB","objC","obj2A","obj2C" }, names); - - mocks.VerifyAll(); + [SetUp] + public void SetUp() + { + IObjectFactory grandparent + = new XmlObjectFactory(new ReadOnlyXmlTestResource("root.xml", GetType())); + IObjectFactory parent + = new XmlObjectFactory(new ReadOnlyXmlTestResource("middle.xml", GetType()), grandparent); + IConfigurableListableObjectFactory child + = new XmlObjectFactory(new ReadOnlyXmlTestResource("leaf.xml", GetType()), parent); + _factory = child; } - [Test] - public void ObjectNamesForTypeIncludingAncestors() - { - IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_factory, typeof (ITestObject)); - // includes 2 TestObjects from IFactoryObjects (DummyFactory definitions) - Assert.AreEqual(4, names.Count); - Assert.IsTrue(names.Contains("test")); - Assert.IsTrue(names.Contains("test3")); - Assert.IsTrue(names.Contains("testFactory1")); - Assert.IsTrue(names.Contains("testFactory2")); - } + /// + /// Check that override doesn't count as two separate objects. + /// + [Test] + public void CountObjectsIncludingAncestors() + { + // leaf count... + Assert.AreEqual(1, _factory.ObjectDefinitionCount); + // count minus duplicate... + Assert.AreEqual(6, ObjectFactoryUtils.CountObjectsIncludingAncestors(_factory), + "Should count 6 objects, not " + ObjectFactoryUtils.CountObjectsIncludingAncestors(_factory)); + } - [Test] + [Test] + public void ObjectNamesIncludingAncestors() + { + IList names = ObjectFactoryUtils.ObjectNamesIncludingAncestors(_factory); + Assert.AreEqual(6, names.Count); + } + + [Test] + public void ObjectNamesForTypeIncludingAncestors() + { + IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_factory, typeof(ITestObject)); + // includes 2 TestObjects from IFactoryObjects (DummyFactory definitions) + Assert.AreEqual(4, names.Count); + Assert.IsTrue(names.Contains("test")); + Assert.IsTrue(names.Contains("test3")); + Assert.IsTrue(names.Contains("testFactory1")); + Assert.IsTrue(names.Contains("testFactory2")); + } + + [Test] public void ObjectNamesForTypeIncludingAncestorsExcludesObjectsFromParentWhenLocalObjectDefined() - { - DefaultListableObjectFactory root = new DefaultListableObjectFactory(); - root.RegisterObjectDefinition( "excludeLocalObject", new RootObjectDefinition(typeof(ArrayList)) ); + { + DefaultListableObjectFactory root = new DefaultListableObjectFactory(); + root.RegisterObjectDefinition("excludeLocalObject", new RootObjectDefinition(typeof(ArrayList))); DefaultListableObjectFactory child = new DefaultListableObjectFactory(root); child.RegisterObjectDefinition("excludeLocalObject", new RootObjectDefinition(typeof(Hashtable))); - IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(child, typeof (ArrayList)); - // "excludeLocalObject" matches on the parent, but not the local object definition - Assert.AreEqual(0, names.Count); + IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(child, typeof(ArrayList)); + // "excludeLocalObject" matches on the parent, but not the local object definition + Assert.AreEqual(0, names.Count); - names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(child, typeof (ArrayList), true, true); - // "excludeLocalObject" matches on the parent, but not the local object definition - Assert.AreEqual(0, names.Count); - } - - [Test] - public void ObjectNamesForTypeIncludingAncestorsPreserveOrderOfRegistration() - { - MockRepository mocks = new MockRepository(); - IConfigurableListableObjectFactory of = (IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory)); - IConfigurableListableObjectFactory ofParent = (IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory)); - Type EXPECTEDTYPE = typeof(ITestObject); - - Expect.Call(of.GetObjectNamesForType(EXPECTEDTYPE)).Return(new string[] { "objA", "objB", "objC" }); - Expect.Call(((IHierarchicalObjectFactory)of).ParentObjectFactory).Return(ofParent); - Expect.Call(ofParent.GetObjectNamesForType(EXPECTEDTYPE)).Return(new string[] { "obj2A", "objB", "obj2C" }); - - mocks.ReplayAll(); - - IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(of, EXPECTEDTYPE); - Assert.AreEqual(5, names.Count); - Assert.AreEqual(new string[] { "objA", "objB", "objC", "obj2A", "obj2C" }, names); - - mocks.VerifyAll(); + names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(child, typeof(ArrayList), true, true); + // "excludeLocalObject" matches on the parent, but not the local object definition + Assert.AreEqual(0, names.Count); } [Test] - public void ObjectNamesForTypeIncludingAncestorsPrototypesAndFactoryObjectsPreserveOrderOfRegistration() + public void CountObjectsIncludingAncestorsWithNonHierarchicalFactory() { - MockRepository mocks = new MockRepository(); - IConfigurableListableObjectFactory of = (IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory)); - IConfigurableListableObjectFactory ofParent = (IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory)); - Type EXPECTEDTYPE = typeof(ITestObject); - - Expect.Call(of.GetObjectNamesForType(EXPECTEDTYPE, false, false)).Return(new string[] { "objA", "objB", "objC" }); - Expect.Call(((IHierarchicalObjectFactory)of).ParentObjectFactory).Return(ofParent); - Expect.Call(ofParent.GetObjectNamesForType(EXPECTEDTYPE, false, false)).Return(new string[] { "obj2A", "objB", "obj2C" }); - - mocks.ReplayAll(); - - IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(of, EXPECTEDTYPE, false, false); - Assert.AreEqual(5, names.Count); - Assert.AreEqual(new string[] { "objA", "objB", "objC", "obj2A", "obj2C" }, names); - - - mocks.VerifyAll(); + StaticListableObjectFactory lof = new StaticListableObjectFactory(); + lof.AddObject("t1", new TestObject()); + lof.AddObject("t2", new TestObject()); + Assert.IsTrue(ObjectFactoryUtils.CountObjectsIncludingAncestors(lof) == 2); } - [Test] - public void CountObjectsIncludingAncestorsWithNonHierarchicalFactory() - { - StaticListableObjectFactory lof = new StaticListableObjectFactory(); - lof.AddObject("t1", new TestObject()); - lof.AddObject("t2", new TestObject()); - Assert.IsTrue(ObjectFactoryUtils.CountObjectsIncludingAncestors(lof) == 2); - } + [Test] + public void HierarchicalResolutionWithOverride() + { + object test3 = _factory.GetObject("test3"); + object test = _factory.GetObject("test"); + object testFactory1 = _factory.GetObject("testFactory1"); - [Test] - public void HierarchicalResolutionWithOverride() - { - object test3 = _factory.GetObject("test3"); - object test = _factory.GetObject("test"); - object testFactory1 = _factory.GetObject("testFactory1"); + IDictionary objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof(ITestObject), true, false); + Assert.AreEqual(3, objects.Count); + Assert.AreEqual(test3, objects["test3"]); + Assert.AreEqual(test, objects["test"]); + Assert.AreEqual(testFactory1, objects["testFactory1"]); + objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof(ITestObject), false, false); + Assert.AreEqual(2, objects.Count); + Assert.AreEqual(test, objects["test"]); + Assert.AreEqual(testFactory1, objects["testFactory1"]); + objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof(ITestObject), false, true); + Assert.AreEqual(2, objects.Count); + Assert.AreEqual(test, objects["test"]); + Assert.AreEqual(testFactory1, objects["testFactory1"]); + objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof(ITestObject), true, true); + Assert.AreEqual(4, objects.Count); + Assert.AreEqual(test3, objects["test3"]); + Assert.AreEqual(test, objects["test"]); + Assert.AreEqual(testFactory1, objects["testFactory1"]); + Assert.IsTrue(objects["testFactory2"] is ITestObject); + objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof(DummyFactory), true, true); + Assert.AreEqual(2, objects.Count); + Assert.AreEqual(_factory.GetObject("&testFactory1"), objects["&testFactory1"]); + Assert.AreEqual(_factory.GetObject("&testFactory2"), objects["&testFactory2"]); + objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof(IFactoryObject), true, true); + Assert.AreEqual(2, objects.Count); + Assert.AreEqual(_factory.GetObject("&testFactory1"), objects["&testFactory1"]); + Assert.AreEqual(_factory.GetObject("&testFactory2"), objects["&testFactory2"]); + } - IDictionary objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof (ITestObject), true, false); - Assert.AreEqual(3, objects.Count); - Assert.AreEqual(test3, objects["test3"]); - Assert.AreEqual(test, objects["test"]); - Assert.AreEqual(testFactory1, objects["testFactory1"]); - objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof (ITestObject), false, false); - Assert.AreEqual(2, objects.Count); - Assert.AreEqual(test, objects["test"]); - Assert.AreEqual(testFactory1, objects["testFactory1"]); - objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof (ITestObject), false, true); - Assert.AreEqual(2, objects.Count); - Assert.AreEqual(test, objects["test"]); - Assert.AreEqual(testFactory1, objects["testFactory1"]); - objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof (ITestObject), true, true); - Assert.AreEqual(4, objects.Count); - Assert.AreEqual(test3, objects["test3"]); - Assert.AreEqual(test, objects["test"]); - Assert.AreEqual(testFactory1, objects["testFactory1"]); - Assert.IsTrue(objects["testFactory2"] is ITestObject); - objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof (DummyFactory), true, true); - Assert.AreEqual(2, objects.Count); - Assert.AreEqual(_factory.GetObject("&testFactory1"), objects["&testFactory1"]); - Assert.AreEqual(_factory.GetObject("&testFactory2"), objects["&testFactory2"]); - objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(_factory, typeof (IFactoryObject), true, true); - Assert.AreEqual(2, objects.Count); - Assert.AreEqual(_factory.GetObject("&testFactory1"), objects["&testFactory1"]); - Assert.AreEqual(_factory.GetObject("&testFactory2"), objects["&testFactory2"]); - } - - [Test] - [ExpectedException(typeof (NoSuchObjectDefinitionException), + [Test] + [ExpectedException(typeof(NoSuchObjectDefinitionException), ExpectedMessage = "No unique object of type [Spring.Objects.ITestObject] is defined : Expected single object but found 4")] - public void ObjectOfTypeIncludingAncestorsWithMoreThanOneObjectOfType() - { - ObjectFactoryUtils.ObjectOfTypeIncludingAncestors(_factory, typeof (ITestObject), true, true); - } + public void ObjectOfTypeIncludingAncestorsWithMoreThanOneObjectOfType() + { + ObjectFactoryUtils.ObjectOfTypeIncludingAncestors(_factory, typeof(ITestObject), true, true); + } [Test] public void ObjectOfTypeIncludingAncestorsExcludesObjectsFromParentWhenLocalObjectDefined() @@ -234,87 +170,87 @@ namespace Spring.Objects.Factory Assert.AreEqual(0, objectEntries.Count); } - [Test] - public void NoObjectsOfTypeIncludingAncestors() - { - StaticListableObjectFactory lof = new StaticListableObjectFactory(); - lof.AddObject("foo", new object()); - IDictionary objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof (ITestObject), true, false); - Assert.IsTrue(objects.Count == 0); - } + [Test] + public void NoObjectsOfTypeIncludingAncestors() + { + StaticListableObjectFactory lof = new StaticListableObjectFactory(); + lof.AddObject("foo", new object()); + IDictionary objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof(ITestObject), true, false); + Assert.IsTrue(objects.Count == 0); + } - [Test] - public void ObjectsOfTypeIncludingAncestorsWithStaticFactory() - { - StaticListableObjectFactory lof = new StaticListableObjectFactory(); - TestObject t1 = new TestObject(); - TestObject t2 = new TestObject(); - DummyFactory t3 = new DummyFactory(); - DummyFactory t4 = new DummyFactory(); + [Test] + public void ObjectsOfTypeIncludingAncestorsWithStaticFactory() + { + StaticListableObjectFactory lof = new StaticListableObjectFactory(); + TestObject t1 = new TestObject(); + TestObject t2 = new TestObject(); + DummyFactory t3 = new DummyFactory(); + DummyFactory t4 = new DummyFactory(); t4.IsSingleton = false; - lof.AddObject("t1", t1); - lof.AddObject("t2", t2); - lof.AddObject("t3", t3); - t3.AfterPropertiesSet(); // StaticListableObjectFactory does support lifecycle calls. + lof.AddObject("t1", t1); + lof.AddObject("t2", t2); + lof.AddObject("t3", t3); + t3.AfterPropertiesSet(); // StaticListableObjectFactory does support lifecycle calls. lof.AddObject("t4", t4); - t4.AfterPropertiesSet(); // StaticListableObjectFactory does support lifecycle calls. - IDictionary objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof(ITestObject), true, false); - Assert.AreEqual(2, objects.Count); - Assert.AreEqual(t1, objects["t1"]); - Assert.AreEqual(t2, objects["t2"]); - objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof (ITestObject), false, true); - Assert.AreEqual(3, objects.Count); - Assert.AreEqual(t1, objects["t1"]); - Assert.AreEqual(t2, objects["t2"]); - Assert.AreEqual(t3.GetObject(), objects["t3"]); - objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof (ITestObject), true, true); - Assert.AreEqual(4, objects.Count); - Assert.AreEqual(t1, objects["t1"]); - Assert.AreEqual(t2, objects["t2"]); - Assert.AreEqual(t3.GetObject(), objects["t3"]); - Assert.IsTrue(objects["t4"] is TestObject); - } + t4.AfterPropertiesSet(); // StaticListableObjectFactory does support lifecycle calls. + IDictionary objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof(ITestObject), true, false); + Assert.AreEqual(2, objects.Count); + Assert.AreEqual(t1, objects["t1"]); + Assert.AreEqual(t2, objects["t2"]); + objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof(ITestObject), false, true); + Assert.AreEqual(3, objects.Count); + Assert.AreEqual(t1, objects["t1"]); + Assert.AreEqual(t2, objects["t2"]); + Assert.AreEqual(t3.GetObject(), objects["t3"]); + objects = ObjectFactoryUtils.ObjectsOfTypeIncludingAncestors(lof, typeof(ITestObject), true, true); + Assert.AreEqual(4, objects.Count); + Assert.AreEqual(t1, objects["t1"]); + Assert.AreEqual(t2, objects["t2"]); + Assert.AreEqual(t3.GetObject(), objects["t3"]); + Assert.IsTrue(objects["t4"] is TestObject); + } - [Test] - public void IsFactoryDereferenceWithNonFactoryObjectName() - { - Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference("roob"), - "Name that didn't start with the factory object prefix is being reported " + - "(incorrectly) as a factory object dereference."); - } + [Test] + public void IsFactoryDereferenceWithNonFactoryObjectName() + { + Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference("roob"), + "Name that didn't start with the factory object prefix is being reported " + + "(incorrectly) as a factory object dereference."); + } - [Test] - public void IsFactoryDereferenceWithNullName() - { - Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference(null), - "Null name that (obviously) didn't start with the factory object prefix is being reported " + - "(incorrectly) as a factory object dereference."); - } + [Test] + public void IsFactoryDereferenceWithNullName() + { + Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference(null), + "Null name that (obviously) didn't start with the factory object prefix is being reported " + + "(incorrectly) as a factory object dereference."); + } - [Test] - public void IsFactoryDereferenceWithEmptyName() - { - Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference(string.Empty), - "String.Empty name that (obviously) didn't start with the factory object prefix is being reported " + - "(incorrectly) as a factory object dereference."); - } + [Test] + public void IsFactoryDereferenceWithEmptyName() + { + Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference(string.Empty), + "String.Empty name that (obviously) didn't start with the factory object prefix is being reported " + + "(incorrectly) as a factory object dereference."); + } - [Test] - public void IsFactoryDereferenceWithJustTheFactoryObjectPrefixCharacter() - { - Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference( - ObjectFactoryUtils.FactoryObjectPrefix), - "Name that consisted solely of the factory object prefix is being reported " + - "(incorrectly) as a factory object dereference."); - } + [Test] + public void IsFactoryDereferenceWithJustTheFactoryObjectPrefixCharacter() + { + Assert.IsFalse(ObjectFactoryUtils.IsFactoryDereference( + ObjectFactoryUtils.FactoryObjectPrefix), + "Name that consisted solely of the factory object prefix is being reported " + + "(incorrectly) as a factory object dereference."); + } - [Test] - public void IsFactoryDereferenceSunnyDay() - { - Assert.IsTrue(ObjectFactoryUtils.IsFactoryDereference( - ObjectFactoryUtils.FactoryObjectPrefix + "roob"), - "Name that did start with the factory object prefix is not being reported " + - "(incorrectly) as a factory object dereference."); - } - } + [Test] + public void IsFactoryDereferenceSunnyDay() + { + Assert.IsTrue(ObjectFactoryUtils.IsFactoryDereference( + ObjectFactoryUtils.FactoryObjectPrefix + "roob"), + "Name that did start with the factory object prefix is not being reported " + + "(incorrectly) as a factory object dereference."); + } + } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtils_PreserveOrderInHierarchy_Tests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtils_PreserveOrderInHierarchy_Tests.cs new file mode 100644 index 00000000..13449bfe --- /dev/null +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/ObjectFactoryUtils_PreserveOrderInHierarchy_Tests.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Spring.Objects.Factory.Support; + +namespace Spring.Objects.Factory +{ + [TestFixture] + public class ObjectFactoryUtils_PreserveOrderInHierarchy_Tests + { + private readonly Type _expectedtype = typeof(ITestObject); + private DefaultListableObjectFactory _factory; + + [SetUp] + public void _TestSetUp() + { + DefaultListableObjectFactory parentFactory = new DefaultListableObjectFactory(); + _factory = new DefaultListableObjectFactory(parentFactory); + + RootObjectDefinition rodA = new RootObjectDefinition(_expectedtype); + RootObjectDefinition rodB = new RootObjectDefinition(_expectedtype); + RootObjectDefinition rodC = new RootObjectDefinition(_expectedtype); + RootObjectDefinition rod2A = new RootObjectDefinition(_expectedtype); + RootObjectDefinition rod2C = new RootObjectDefinition(_expectedtype); + + _factory.RegisterObjectDefinition("objA", rodA); + _factory.RegisterObjectDefinition("objB", rodB); + _factory.RegisterObjectDefinition("objC", rodC); + + parentFactory.RegisterObjectDefinition("obj2A", rod2A); + parentFactory.RegisterObjectDefinition("objB", rodB); + parentFactory.RegisterObjectDefinition("obj2C", rod2C); + } + + [Test] + public void ObjectNamesIncludingAncestorsPreserveOrderOfRegistration() + { + IList names = ObjectFactoryUtils.ObjectNamesIncludingAncestors(_factory); + Assert.AreEqual(5, names.Count); + Assert.AreEqual(new string[] { "objA", "objB", "objC", "obj2A", "obj2C" }, names); + } + + [Test] + public void ObjectNamesForTypeIncludingAncestorsPreserveOrderOfRegistration() + { + IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_factory, _expectedtype); + Assert.AreEqual(5, names.Count); + Assert.AreEqual(new string[] { "objA", "objB", "objC", "obj2A", "obj2C" }, names); + } + + [Test] + public void ObjectNamesForTypeIncludingAncestorsPrototypesAndFactoryObjectsPreserveOrderOfRegistration() + { + IList names = ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(_factory, _expectedtype, false, false); + Assert.AreEqual(5, names.Count); + Assert.AreEqual(new string[] { "objA", "objB", "objC", "obj2A", "obj2C" }, names); + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/UnsupportedObjectDefinitionImplementation.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/UnsupportedObjectDefinitionImplementation.cs index 6e45ad54..41f6bb52 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/UnsupportedObjectDefinitionImplementation.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/UnsupportedObjectDefinitionImplementation.cs @@ -26,119 +26,119 @@ using Spring.Objects.Factory.Support; namespace Spring.Objects.Factory { - internal class UnsupportedObjectDefinitionImplementation : IObjectDefinition - { - public MutablePropertyValues PropertyValues - { - get { throw new NotImplementedException(); } - } + internal class UnsupportedObjectDefinitionImplementation : IObjectDefinition + { + public MutablePropertyValues PropertyValues + { + get { throw new NotImplementedException(); } + } - public ConstructorArgumentValues ConstructorArgumentValues - { - get { throw new NotImplementedException(); } - } + public ConstructorArgumentValues ConstructorArgumentValues + { + get { throw new NotImplementedException(); } + } - public MethodOverrides MethodOverrides - { - get { throw new NotImplementedException(); } - } + public MethodOverrides MethodOverrides + { + get { throw new NotImplementedException(); } + } - public EventValues EventHandlerValues - { - get { throw new NotImplementedException(); } - } + public EventValues EventHandlerValues + { + get { throw new NotImplementedException(); } + } - public string ResourceDescription - { - get { return "UnsupportedObjectDefinitionImplementation_Resource"; } - } + public string ResourceDescription + { + get { return "UnsupportedObjectDefinitionImplementation_Resource"; } + } public bool IsTemplate { get { throw new NotImplementedException(); } } - public bool IsAbstract - { - get { throw new NotImplementedException(); } - } + public bool IsAbstract + { + get { return false; } + } - public bool IsSingleton - { - get { throw new NotImplementedException(); } - } + public bool IsSingleton + { + get { throw new NotImplementedException(); } + } - public bool IsLazyInit - { - get { throw new NotImplementedException(); } - } + public bool IsLazyInit + { + get { return false; } + } - public string ParentName - { - get { return null; } - set { throw new NotImplementedException(); } - } + public string ParentName + { + get { return null; } + set { throw new NotImplementedException(); } + } - public string Scope - { - get { throw new System.NotImplementedException(); } - set { throw new System.NotImplementedException(); } - } + public string Scope + { + get { return "UnsupportedObjectDefinitionImplementation_Resource"; } + set { throw new System.NotImplementedException(); } + } - public ObjectRole Role - { - get { throw new NotImplementedException(); } - } + public ObjectRole Role + { + get { throw new NotImplementedException(); } + } - public Type ObjectType - { - get { throw new NotImplementedException(); } - } + public Type ObjectType + { + get { throw new NotImplementedException(); } + } - public string ObjectTypeName - { + public string ObjectTypeName + { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } - } + } - public AutoWiringMode AutowireMode - { - get { throw new NotImplementedException(); } - } + public AutoWiringMode AutowireMode + { + get { throw new NotImplementedException(); } + } - public DependencyCheckingMode DependencyCheck - { - get { throw new NotImplementedException(); } - } + public DependencyCheckingMode DependencyCheck + { + get { throw new NotImplementedException(); } + } - public IList DependsOn - { - get { throw new NotImplementedException(); } - } + public IList DependsOn + { + get { throw new NotImplementedException(); } + } - public string InitMethodName - { - get { throw new NotImplementedException(); } - } + public string InitMethodName + { + get { throw new NotImplementedException(); } + } - public string DestroyMethodName - { - get { throw new NotImplementedException(); } - } + public string DestroyMethodName + { + get { throw new NotImplementedException(); } + } - public string FactoryMethodName - { - get { throw new NotImplementedException(); } - } + public string FactoryMethodName + { + get { throw new NotImplementedException(); } + } - public string FactoryObjectName - { - get { throw new NotImplementedException(); } - } + public string FactoryObjectName + { + get { throw new NotImplementedException(); } + } - public bool IsAutowireCandidate - { - get { throw new NotImplementedException(); } - } - } + public bool IsAutowireCandidate + { + get { throw new NotImplementedException(); } + } + } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlListableObjectFactoryTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlListableObjectFactoryTests.cs index c2b1987d..b3a93365 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlListableObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlListableObjectFactoryTests.cs @@ -97,8 +97,8 @@ namespace Spring.Objects.Factory.Xml parent.RegisterObjectDefinition("typedfather", new RootObjectDefinition(typeof(TestObject), false)); // add unsupported IObjectDefinition implementation... - UnsupportedObjectDefinitionImplementation unsupportedDefinition = new UnsupportedObjectDefinitionImplementation(); - parent.RegisterObjectDefinition("unsupportedDefinition", unsupportedDefinition); + //UnsupportedObjectDefinitionImplementation unsupportedDefinition = new UnsupportedObjectDefinitionImplementation(); + //parent.RegisterObjectDefinition("unsupportedDefinition", unsupportedDefinition); XmlObjectFactory factory; factory = new XmlObjectFactory(new ReadOnlyXmlTestResource("test.xml", GetType()), parent);