From 4141fb0cab5a245750f6f0a665ddf68048e20696 Mon Sep 17 00:00:00 2001 From: markpollack Date: Thu, 14 Aug 2008 15:21:22 +0000 Subject: [PATCH] SPRNET-1002 - Object name generation when no name specified produces duplicate names --- .../Factory/Support/AbstractObjectFactory.cs | 1 + .../Support/ObjectDefinitionReaderUtils.cs | 2 +- .../Factory/Xml/objectNameGeneration.xml | 26 ++++++ .../Factory/Xml/ObjectNameGenerationTests.cs | 83 +++++++++++++++++++ .../Factory/Xml/XmlObjectCollectionTests.cs | 6 +- .../Spring.Core.Tests.2005.csproj | 2 + 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/objectNameGeneration.xml create mode 100644 test/Spring/Spring.Core.Tests/Objects/Factory/Xml/ObjectNameGenerationTests.cs diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs index 3b59d699..20b526d0 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs @@ -1638,6 +1638,7 @@ namespace Spring.Objects.Factory.Support { return false; } + //TODO investigate looking in parent context as this differs from java. } /// diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/ObjectDefinitionReaderUtils.cs b/src/Spring/Spring.Core/Objects/Factory/Support/ObjectDefinitionReaderUtils.cs index 056dcde3..68a297af 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/ObjectDefinitionReaderUtils.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/ObjectDefinitionReaderUtils.cs @@ -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; diff --git a/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/objectNameGeneration.xml b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/objectNameGeneration.xml new file mode 100644 index 00000000..75850119 --- /dev/null +++ b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/objectNameGeneration.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/ObjectNameGenerationTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/ObjectNameGenerationTests.cs new file mode 100644 index 00000000..f5ed0e87 --- /dev/null +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/ObjectNameGenerationTests.cs @@ -0,0 +1,83 @@ +#region License + +/* + * Copyright © 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 +{ + /// + /// This class contains tests for the object naming algorithm used when an object name is not specified. + /// + /// Mark Pollack + [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); + } + + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectCollectionTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectCollectionTests.cs index 9e42035d..ba020eba 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectCollectionTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectCollectionTests.cs @@ -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); } diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2005.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2005.csproj index 4e1da104..cb72fead 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2005.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2005.csproj @@ -327,6 +327,7 @@ + @@ -782,6 +783,7 @@ +