SPRNET-1002 - Object name generation when no name specified produces duplicate names

This commit is contained in:
markpollack
2008-08-14 15:21:22 +00:00
parent 6f06732d37
commit 4141fb0cab
6 changed files with 118 additions and 2 deletions

View File

@@ -1638,6 +1638,7 @@ namespace Spring.Objects.Factory.Support
{
return false;
}
//TODO investigate looking in parent context as this differs from java.
}
/// <summary>

View File

@@ -164,7 +164,7 @@ namespace Spring.Objects.Factory.Support
} else
{
int counter = -1;
while (counter == -1 && registry.ContainsObjectDefinition(id))
while (counter == -1 || registry.ContainsObjectDefinition(id))
{
counter++;
id = generatedObjectName + GENERATED_OBJECT_NAME_SEPARATOR + counter;

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 type="Spring.Objects.Factory.Xml.DependenciesObject, Spring.Core.Tests">
<property name="Spouse">
<object type="Spring.Objects.TestObject, Spring.Core.Tests"/>
</property>
</object>
<object type="Spring.Objects.Factory.Xml.DependenciesObject, Spring.Core.Tests">
<property name="Spouse">
<object type="Spring.Objects.TestObject, Spring.Core.Tests"/>
</property>
</object>
<object type="Spring.Objects.Factory.Xml.DependenciesObject, Spring.Core.Tests">
<property name="Spouse">
<object type="Spring.Objects.TestObject, Spring.Core.Tests"/>
</property>
</object>
</objects>

View File

@@ -0,0 +1,83 @@
#region License
/*
* Copyright <20> 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
#region Imports
using NUnit.Framework;
using Spring.Objects.Factory.Support;
#endregion
namespace Spring.Objects.Factory.Xml
{
/// <summary>
/// This class contains tests for the object naming algorithm used when an object name is not specified.
/// </summary>
/// <author>Mark Pollack</author>
[TestFixture]
public class ObjectNameGenerationTests
{
private DefaultListableObjectFactory objectFactory;
[SetUp]
public void Setup()
{
objectFactory = new DefaultListableObjectFactory();
XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(objectFactory);
reader.LoadObjectDefinitions(new ReadOnlyXmlTestResource("objectNameGeneration.xml", GetType()));
}
[Test]
public void AssignObjectNames()
{
string className = typeof (DependenciesObject).FullName;
string targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "0";
DependenciesObject topLevel1 = (DependenciesObject) objectFactory.GetObject(targetName);
Assert.IsNotNull(topLevel1);
targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "1";
DependenciesObject topLevel2 = (DependenciesObject)objectFactory.GetObject(targetName);
Assert.IsNotNull(topLevel1);
targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "2";
DependenciesObject topLevel3 = (DependenciesObject)objectFactory.GetObject(targetName);
Assert.IsNotNull(topLevel1);
string childClassName = typeof(TestObject).FullName;
TestObject child1 = (TestObject) topLevel1.Spouse;
Assert.IsNotNull(child1);
Assert.IsTrue(child1.ObjectName.IndexOf(childClassName) != -1);
TestObject child2 = (TestObject)topLevel2.Spouse;
Assert.IsNotNull(child2);
Assert.IsTrue(child2.ObjectName.IndexOf(childClassName) != -1);
TestObject child3 = (TestObject)topLevel3.Spouse;
Assert.IsNotNull(child3);
Assert.IsTrue(child3.ObjectName.IndexOf(childClassName) != -1);
Assert.AreNotEqual(child1.ObjectName, child2.ObjectName);
}
}
}

View File

@@ -31,6 +31,7 @@ using Spring.Collections;
using Spring.Core.IO;
using Spring.Expressions;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
#endregion
@@ -210,7 +211,10 @@ namespace Spring.Objects.Factory.Xml
Assert.IsFalse(objectNames.Contains("aliasWithoutId2"));
Assert.IsFalse(objectNames.Contains("aliasWithoutId3"));
TestObject tb4 = (TestObject) xof.GetObject(typeof (TestObject).FullName);
string className = typeof(TestObject).FullName;
string targetName = className + ObjectDefinitionReaderUtils.GENERATED_OBJECT_NAME_SEPARATOR + "0";
TestObject tb4 = (TestObject)xof.GetObject(targetName);
Assert.AreEqual(null, tb4.Name);
}

View File

@@ -327,6 +327,7 @@
<Compile Include="Objects\Factory\Xml\ArrayCtorDependencyObject.cs" />
<Compile Include="Objects\Factory\Xml\LocaleTests.cs" />
<Compile Include="Objects\Factory\Xml\ObjectFactorySectionHandlerTests.cs" />
<Compile Include="Objects\Factory\Xml\ObjectNameGenerationTests.cs" />
<Compile Include="Objects\Factory\Xml\SiimpleCtorWiringTests.cs" />
<Compile Include="Objects\LazyTestObject.cs" />
<Compile Include="Objects\Support\MethodInvokerTests.cs">
@@ -782,6 +783,7 @@
<Content Include="Data\Spring\Objects\Factory\Xml\collections.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\constructor-arg.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\array-autowire.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\objectNameGeneration.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\simple-constructor-arg.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\expressions.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\default-autowire.xml" />