From c1e9dd63e35e7e7b846efc90ec7c831c2f307e92 Mon Sep 17 00:00:00 2001 From: Steve Bohlen Date: Tue, 27 Nov 2012 18:13:13 -0500 Subject: [PATCH] SPRNET-1534 add logic to prevent registration of alias values that match object defintion name values --- .../Factory/Support/AbstractObjectFactory.cs | 32 ++- .../Data/Spring/Objects/Factory/Xml/test.xml | 36 +-- .../AbstractListableObjectFactoryTests.cs | 2 +- .../Xml/XmlListableObjectFactoryTests.cs | 254 ++++++++++-------- .../Spring.Core.Tests.2010.csproj | 4 +- 5 files changed, 190 insertions(+), 138 deletions(-) diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs index 2958e596..0233369b 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs @@ -1327,7 +1327,7 @@ namespace Spring.Objects.Factory.Support /// public bool IsTypeMatch(string name) { - return IsTypeMatch(name, typeof (T)); + return IsTypeMatch(name, typeof(T)); } /// @@ -1606,7 +1606,7 @@ namespace Spring.Objects.Factory.Support /// /// String Resolver applied to Autowired value injections /// - private ISet embeddedValueResolvers = new SortedSet(); + private ISet embeddedValueResolvers = new SortedSet(); /// /// Indicates whether any IInstantiationAwareBeanPostProcessors have been registered @@ -1834,7 +1834,7 @@ namespace Spring.Objects.Factory.Support { if (0 == string.Compare((string)aliasEntry.Value, objectName, !this.IsCaseSensitive)) { - matches.Add((string) aliasEntry.Key); + matches.Add((string)aliasEntry.Key); } } } @@ -2438,13 +2438,13 @@ namespace Spring.Objects.Factory.Support /// the resolved value (may be the original value as-is) public string ResolveEmbeddedValue(string value) { - string result = value; - foreach(IStringValueResolver resolver in embeddedValueResolvers) + string result = value; + foreach (IStringValueResolver resolver in embeddedValueResolvers) { - result = resolver.ParseAndResolveVariables(result); - } - return result; - } + result = resolver.ParseAndResolveVariables(result); + } + return result; + } /// /// Add a new @@ -2504,6 +2504,20 @@ namespace Spring.Objects.Factory.Support #endregion + if (name == alias) + { + #region Instrumentation + + if (log.IsDebugEnabled) + { + log.Debug(string.Format("Ignoring attempt to Register alias '{0}' for object with name '{1}' because name and alias would be the same value.", alias, name)); + } + + #endregion + + return; + } + #region Instrumentation if (log.IsDebugEnabled) diff --git a/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/test.xml b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/test.xml index a17a8609..5f2abfcf 100644 --- a/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/test.xml +++ b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/test.xml @@ -1,9 +1,9 @@ - - + Rod @@ -15,14 +15,14 @@ - + Roderick - + - + Kerry @@ -34,7 +34,7 @@ - + Kathy @@ -46,7 +46,7 @@ - + typeMismatch @@ -59,24 +59,24 @@ - - + - + - + false - @@ -87,7 +87,7 @@ - @@ -97,7 +97,7 @@ - + listenerVeto @@ -106,7 +106,11 @@ 66 - + + + + + diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs index 0c68ecc2..6288fcdc 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/AbstractListableObjectFactoryTests.cs @@ -58,7 +58,7 @@ namespace Spring.Objects.Factory { [Test] public virtual void Count () { - AssertCount (16); + AssertCount (19); } protected internal void AssertCount (int count) 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 b3a93365..41a6b718 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlListableObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlListableObjectFactoryTests.cs @@ -32,143 +32,175 @@ using Spring.Objects.Factory.Support; namespace Spring.Objects.Factory.Xml { - /// - /// Unit tests for the XmlListableObjectFactory class. - /// - /// Juergen Hoeller - /// Rick Evans (.NET) - [TestFixture] - public class XmlListableObjectFactoryTests : AbstractListableObjectFactoryTests - { - #region Inner Class : AnonymousClassObjectPostProcessor + /// + /// Unit tests for the XmlListableObjectFactory class. + /// + /// Juergen Hoeller + /// Rick Evans (.NET) + [TestFixture] + public class XmlListableObjectFactoryTests : AbstractListableObjectFactoryTests + { + #region Inner Class : AnonymousClassObjectPostProcessor - private class AnonymousClassObjectPostProcessor : IObjectPostProcessor - { - public AnonymousClassObjectPostProcessor() - { - } + private class AnonymousClassObjectPostProcessor : IObjectPostProcessor + { + public AnonymousClassObjectPostProcessor() + { + } - public virtual object PostProcessBeforeInitialization( - object obj, string name) - { - if (obj is TestObject) - { - ((TestObject) obj).PostProcessed = true; - } - if (obj is DummyFactory) - { - ((DummyFactory) obj).PostProcessed = true; - } - return obj; - } + public virtual object PostProcessBeforeInitialization( + object obj, string name) + { + if (obj is TestObject) + { + ((TestObject)obj).PostProcessed = true; + } + if (obj is DummyFactory) + { + ((DummyFactory)obj).PostProcessed = true; + } + return obj; + } - public virtual object PostProcessAfterInitialization( - object obj, string objectName) - { - return obj; - } - } + public virtual object PostProcessAfterInitialization( + object obj, string objectName) + { + return obj; + } + } - #endregion + #endregion - protected internal override AbstractObjectFactory CreateObjectFactory(bool caseSensitive) - { + protected internal override AbstractObjectFactory CreateObjectFactory(bool caseSensitive) + { return new DefaultListableObjectFactory(caseSensitive); - } + } - private DefaultListableObjectFactory parent; -// private XmlObjectFactory factory; + private DefaultListableObjectFactory parent; + // private XmlObjectFactory factory; - #region Test SetUp + #region Test SetUp - [SetUp] - protected void SetUp() - { - parent = new DefaultListableObjectFactory(); - IDictionary m = new Dictionary(); - m["name"] = "Albert"; - parent.RegisterObjectDefinition("father", new RootObjectDefinition(typeof (TestObject), new MutablePropertyValues(m))); + [SetUp] + protected void SetUp() + { + parent = new DefaultListableObjectFactory(); + IDictionary m = new Dictionary(); + m["name"] = "Albert"; + parent.RegisterObjectDefinition("father", new RootObjectDefinition(typeof(TestObject), new MutablePropertyValues(m))); m = new Dictionary(); m["name"] = "Roderick"; - parent.RegisterObjectDefinition("rod", new RootObjectDefinition(typeof (TestObject), new MutablePropertyValues(m))); + parent.RegisterObjectDefinition("rod", new RootObjectDefinition(typeof(TestObject), new MutablePropertyValues(m))); // for testing dynamic ctor arguments + parent.GetObject() call propagation parent.RegisterObjectDefinition("namedfather", new RootObjectDefinition(typeof(TestObject), false)); parent.RegisterObjectDefinition("typedfather", new RootObjectDefinition(typeof(TestObject), false)); - // add unsupported IObjectDefinition implementation... - //UnsupportedObjectDefinitionImplementation unsupportedDefinition = new UnsupportedObjectDefinitionImplementation(); - //parent.RegisterObjectDefinition("unsupportedDefinition", unsupportedDefinition); + // add unsupported IObjectDefinition implementation... + //UnsupportedObjectDefinitionImplementation unsupportedDefinition = new UnsupportedObjectDefinitionImplementation(); + //parent.RegisterObjectDefinition("unsupportedDefinition", unsupportedDefinition); + + XmlObjectFactory factory; + factory = new XmlObjectFactory(new ReadOnlyXmlTestResource("test.xml", GetType()), parent); - XmlObjectFactory factory; - factory = new XmlObjectFactory(new ReadOnlyXmlTestResource("test.xml", GetType()), parent); - // TODO: should this be allowed? //this.factory.RegisterObjectDefinition("typedfather", new RootObjectDefinition(typeof(object), false)); - factory.AddObjectPostProcessor(new AnonymousClassObjectPostProcessor()); - factory.AddObjectPostProcessor(new LifecycleObject.PostProcessor()); + factory.AddObjectPostProcessor(new AnonymousClassObjectPostProcessor()); + factory.AddObjectPostProcessor(new LifecycleObject.PostProcessor()); - factory.PreInstantiateSingletons(); - base.ObjectFactory = factory; - } + factory.PreInstantiateSingletons(); + base.ObjectFactory = factory; + } - #endregion + #endregion - [Test] - public virtual void FactoryNesting() - { - ITestObject father = (ITestObject) ObjectFactory.GetObject("father"); - Assert.IsTrue(father != null, "Object from root context"); + [Test] + public virtual void FactoryNesting() + { + ITestObject father = (ITestObject)ObjectFactory.GetObject("father"); + Assert.IsTrue(father != null, "Object from root context"); - ITestObject rod = (ITestObject) ObjectFactory.GetObject("rod"); - Assert.IsTrue("Rod".Equals(rod.Name), "Object from child context"); - Assert.IsTrue(rod.Spouse == father, "Object has external reference"); + ITestObject rod = (ITestObject)ObjectFactory.GetObject("rod"); + Assert.IsTrue("Rod".Equals(rod.Name), "Object from child context"); + Assert.IsTrue(rod.Spouse == father, "Object has external reference"); - rod = (ITestObject) parent.GetObject("rod"); - Assert.IsTrue("Roderick".Equals(rod.Name), "Object from root context"); - } + rod = (ITestObject)parent.GetObject("rod"); + Assert.IsTrue("Roderick".Equals(rod.Name), "Object from root context"); + } - [Test] - public virtual void FactoryReferences() - { - DummyReferencer dref = (DummyReferencer) ObjectFactory.GetObject("factoryReferencer"); - Assert.IsTrue(dref.TestObject1 == dref.TestObject2); - } + [Test] + public virtual void FactoryReferences() + { + DummyReferencer dref = (DummyReferencer)ObjectFactory.GetObject("factoryReferencer"); + Assert.IsTrue(dref.TestObject1 == dref.TestObject2); + } - [Test] - public virtual void PrototypeReferences() - { - // check that not broken by circular reference resolution mechanism - DummyReferencer ref1 = (DummyReferencer) ObjectFactory.GetObject("prototypeReferencer"); - Assert.IsTrue(ref1.TestObject1 != ref1.TestObject2, "Not referencing same Object twice"); - DummyReferencer ref2 = (DummyReferencer) ObjectFactory.GetObject("prototypeReferencer"); - Assert.IsTrue(ref1 != ref2, "Not the same referencer"); - Assert.IsTrue(ref2.TestObject1 != ref2.TestObject2, "Not referencing same Object twice"); - Assert.IsTrue(ref1.TestObject1 != ref2.TestObject1, "Not referencing same Object twice"); - Assert.IsTrue(ref1.TestObject2 != ref2.TestObject2, "Not referencing same Object twice"); - Assert.IsTrue(ref1.TestObject1 != ref2.TestObject2, "Not referencing same Object twice"); - } + [Test] + public virtual void PrototypeReferences() + { + // check that not broken by circular reference resolution mechanism + DummyReferencer ref1 = (DummyReferencer)ObjectFactory.GetObject("prototypeReferencer"); + Assert.IsTrue(ref1.TestObject1 != ref1.TestObject2, "Not referencing same Object twice"); + DummyReferencer ref2 = (DummyReferencer)ObjectFactory.GetObject("prototypeReferencer"); + Assert.IsTrue(ref1 != ref2, "Not the same referencer"); + Assert.IsTrue(ref2.TestObject1 != ref2.TestObject2, "Not referencing same Object twice"); + Assert.IsTrue(ref1.TestObject1 != ref2.TestObject1, "Not referencing same Object twice"); + Assert.IsTrue(ref1.TestObject2 != ref2.TestObject2, "Not referencing same Object twice"); + Assert.IsTrue(ref1.TestObject1 != ref2.TestObject2, "Not referencing same Object twice"); + } - [Test] - public virtual void ObjectPostProcessor() - { - TestObject kerry = (TestObject) ObjectFactory.GetObject("kerry"); - TestObject kathy = (TestObject) ObjectFactory.GetObject("kathy"); - DummyFactory factory = (DummyFactory) ObjectFactory.GetObject("&singletonFactory"); - TestObject factoryCreated = (TestObject) ObjectFactory.GetObject("singletonFactory"); - Assert.IsTrue(kerry.PostProcessed); - Assert.IsTrue(kathy.PostProcessed); - Assert.IsTrue(factory.PostProcessed); - Assert.IsTrue(factoryCreated.PostProcessed); - } + [Test] + public virtual void ObjectPostProcessor() + { + TestObject kerry = (TestObject)ObjectFactory.GetObject("kerry"); + TestObject kathy = (TestObject)ObjectFactory.GetObject("kathy"); + DummyFactory factory = (DummyFactory)ObjectFactory.GetObject("&singletonFactory"); + TestObject factoryCreated = (TestObject)ObjectFactory.GetObject("singletonFactory"); + Assert.IsTrue(kerry.PostProcessed); + Assert.IsTrue(kathy.PostProcessed); + Assert.IsTrue(factory.PostProcessed); + Assert.IsTrue(factoryCreated.PostProcessed); + } + + /// + /// Test the number of singletons in test.xml + /// + [Test] + public virtual void CountSingletons() + { + Assert.AreEqual(13, ObjectFactory.GetSingletonCount(), "Number of singletons incorrect"); + } + + [Test] + public void CanRetrieveByType_Using_GetObjects_T_Method() + { + var objsByGenericMethod = ((DefaultListableObjectFactory)ObjectFactory).GetObjects(); + Assert.That(objsByGenericMethod.Count, Is.EqualTo(3)); + } + + [Test] + public void CanRetrieveByType_Using_GetObjectsOfType_Method() + { + var objsByOldMethod = ((DefaultListableObjectFactory)ObjectFactory).GetObjectsOfType(typeof(NameIdTestObject)); + Assert.That(objsByOldMethod.Count, Is.EqualTo(3)); + } + + [Test] + public void CanRetrieveAllNameIdObjectsByName() + { + Assert.That(ObjectFactory.GetObject("object1-with-same-id-and-name"), Is.Not.Null); + Assert.That(ObjectFactory.GetObject("object2-with-same-id-and-name"), Is.Not.Null); + Assert.That(ObjectFactory.GetObject("name-id-test-object-name"), Is.Not.Null); + } + + } +} + +namespace Spring.Objects +{ + public class NameIdTestObject + { + + } - /// - /// Test the number of singletons in test.xml - /// - [Test] - public virtual void CountSingletons() - { - Assert.AreEqual(10, ObjectFactory.GetSingletonCount(), "Number of singletons incorrect"); - } - } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj index 781700f9..2cb48597 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj @@ -872,7 +872,9 @@ - + + Designer +