SPRNET-1534 add logic to prevent registration of alias values that match object defintion name values

This commit is contained in:
Steve Bohlen
2012-11-27 18:13:13 -05:00
parent 844cd20782
commit c1e9dd63e3
5 changed files with 190 additions and 138 deletions

View File

@@ -1327,7 +1327,7 @@ namespace Spring.Objects.Factory.Support
/// </exception>
public bool IsTypeMatch<T>(string name)
{
return IsTypeMatch(name, typeof (T));
return IsTypeMatch(name, typeof(T));
}
/// <summary>
@@ -1606,7 +1606,7 @@ namespace Spring.Objects.Factory.Support
/// <summary>
/// String Resolver applied to Autowired value injections
/// </summary>
private ISet embeddedValueResolvers = new SortedSet();
private ISet embeddedValueResolvers = new SortedSet();
/// <summary>
/// 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
/// <returns>the resolved value (may be the original value as-is)</returns>
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;
}
/// <summary>
/// Add a new <see cref="Spring.Objects.Factory.Config.IObjectPostProcessor"/>
@@ -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)

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<object id="rod" type="Spring.Objects.TestObject, Spring.Core.Tests">
<property name="name">
<value>Rod</value>
@@ -15,14 +15,14 @@
<ref object="father"/>
</property>
</object>
<object id="roderick" parent="rod">
<property name="name">
<value>Roderick</value>
</property>
<!-- Should inherit age -->
<!-- Should inherit age -->
</object>
<object id="kerry" type="Spring.Objects.TestObject, Spring.Core.Tests">
<property name="name">
<value>Kerry</value>
@@ -34,7 +34,7 @@
<ref local="rod"/>
</property>
</object>
<object id="kathy" type="Spring.Objects.TestObject, Spring.Core.Tests" singleton="false">
<property name="name">
<value>Kathy</value>
@@ -46,7 +46,7 @@
<ref object="father"/>
</property>
</object>
<object id="typeMismatch" type="Spring.Objects.TestObject, Spring.Core.Tests" singleton="false">
<property name="name">
<value>typeMismatch</value>
@@ -59,24 +59,24 @@
</property>
</object>
<!--
<!--
Tests of lifecycle callbacks
-->
<object id="mustBeInitialized" type="Spring.Objects.Factory.MustBeInitialized, Spring.Core.Tests"></object>
<object id="lifecycle" type="Spring.Objects.Factory.LifecycleObject, Spring.Core.Tests"></object>
<!-- Factory objects are automatically treated differently -->
<!-- Factory objects are automatically treated differently -->
<object id="singletonFactory" type="Spring.Objects.Factory.DummyFactory, Spring.Core.Tests"></object>
<object id="prototypeFactory" type="Spring.Objects.Factory.DummyFactory, Spring.Core.Tests">
<property name="issingleton">
<value>false</value>
</property>
</object>
<!-- Check that the circular reference resolution mechanism doesn't break
<!-- Check that the circular reference resolution mechanism doesn't break
repeated references to the same FactoryObject -->
<object id="factoryReferencer" type="Spring.Objects.Factory.Xml.DummyReferencer, Spring.Core.Tests">
<property name="testObject1">
@@ -87,7 +87,7 @@
</property>
</object>
<!-- Check that the circular reference resolution mechanism doesn't break
<!-- Check that the circular reference resolution mechanism doesn't break
prototype instantiation -->
<object id="prototypeReferencer" type="Spring.Objects.Factory.Xml.DummyReferencer, Spring.Core.Tests" singleton="false">
<property name="testObject1">
@@ -97,7 +97,7 @@
<ref object="kathy"/>
</property>
</object>
<object id="listenerVeto" type="Spring.Objects.TestObject, Spring.Core.Tests">
<property name="name">
<value>listenerVeto</value>
@@ -106,7 +106,11 @@
<value>66</value>
</property>
</object>
<object id="validEmpty" type="Spring.Objects.TestObject, Spring.Core.Tests"/>
<object id="object1-with-same-id-and-name" name="object1-with-same-id-and-name" type="Spring.Objects.NameIdTestObject, Spring.Core.Tests" />
<object id="object2-with-same-id-and-name" name="object2-with-same-id-and-name" type="Spring.Objects.NameIdTestObject, Spring.Core.Tests" />
<object id="object-with-different-id-and-name" name="name-id-test-object-name" type="Spring.Objects.NameIdTestObject, Spring.Core.Tests" />
</objects>

View File

@@ -58,7 +58,7 @@ namespace Spring.Objects.Factory {
[Test]
public virtual void Count ()
{
AssertCount (16);
AssertCount (19);
}
protected internal void AssertCount (int count)

View File

@@ -32,143 +32,175 @@ using Spring.Objects.Factory.Support;
namespace Spring.Objects.Factory.Xml
{
/// <summary>
/// Unit tests for the XmlListableObjectFactory class.
/// </summary>
/// <author>Juergen Hoeller</author>
/// <author>Rick Evans (.NET)</author>
[TestFixture]
public class XmlListableObjectFactoryTests : AbstractListableObjectFactoryTests
{
#region Inner Class : AnonymousClassObjectPostProcessor
/// <summary>
/// Unit tests for the XmlListableObjectFactory class.
/// </summary>
/// <author>Juergen Hoeller</author>
/// <author>Rick Evans (.NET)</author>
[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<string, object> m = new Dictionary<string, object>();
m["name"] = "Albert";
parent.RegisterObjectDefinition("father", new RootObjectDefinition(typeof (TestObject), new MutablePropertyValues(m)));
[SetUp]
protected void SetUp()
{
parent = new DefaultListableObjectFactory();
IDictionary<string, object> m = new Dictionary<string, object>();
m["name"] = "Albert";
parent.RegisterObjectDefinition("father", new RootObjectDefinition(typeof(TestObject), new MutablePropertyValues(m)));
m = new Dictionary<string, object>();
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);
}
/// <summary>
/// Test the number of singletons in test.xml
/// </summary>
[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<NameIdTestObject>();
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
{
}
/// <summary>
/// Test the number of singletons in test.xml
/// </summary>
[Test]
public virtual void CountSingletons()
{
Assert.AreEqual(10, ObjectFactory.GetSingletonCount(), "Number of singletons incorrect");
}
}
}

View File

@@ -872,7 +872,9 @@
<Content Include="Data\Spring\Objects\Factory\Xml\satisfiedobjectdependencycheck.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\satisfiedsimpledependencycheck.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\schema-validation.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\test.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\test.xml">
<SubType>Designer</SubType>
</Content>
<Content Include="Data\Spring\Objects\Factory\Xml\unsatisfiedalldependencycheckmissingobjects.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\unsatisfiedAllDependencyCheckMissingSimple.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\unsatisfiedobjectdependencycheck.xml" />