From 038bcb17c069308d59af718fd0bff2fb52d34f04 Mon Sep 17 00:00:00 2001 From: markpollack Date: Fri, 24 Oct 2008 21:44:04 +0000 Subject: [PATCH] SPRNET-863 - Passing name of inner object to IApplicationContext.IsSingleton() throws NullReferenceException --- .../Factory/Support/AbstractObjectFactory.cs | 5 +- .../Support/DefaultListableObjectFactory.cs | 2 +- .../Support/XmlApplicationContextTests.cs | 52 +++++++++++++++++++ .../Support/innerObjectsWithPostProcessor.xml | 26 ++++++++++ .../Factory/Xml/XmlObjectFactoryTests.cs | 4 ++ .../Spring.Core.Tests.2008.csproj | 3 +- 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 test/Spring/Spring.Core.Tests/Context/Support/innerObjectsWithPostProcessor.xml diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs index b4bd41f0..0cbd2ec1 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs @@ -1507,7 +1507,10 @@ namespace Spring.Objects.Factory.Support return pof.IsSingleton( OriginalObjectName( name ) ); } RootObjectDefinition od = GetMergedObjectDefinition( objectName, false ); - + if (od == null) + { + throw new NoSuchObjectDefinitionException(objectName); + } // In case of IFactoryObject, return singleton status of created object if not a dereference if (od.IsSingleton) { diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs b/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs index 63e9fd50..af4c3194 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/DefaultListableObjectFactory.cs @@ -596,7 +596,7 @@ namespace Spring.Objects.Factory.Support ((AbstractObjectFactory) ParentObjectFactory).GetObjectDefinition(name, includeAncestors); } } - return definition; + return definition; } #endregion diff --git a/test/Spring/Spring.Core.Tests/Context/Support/XmlApplicationContextTests.cs b/test/Spring/Spring.Core.Tests/Context/Support/XmlApplicationContextTests.cs index 48da4adc..17934d2a 100644 --- a/test/Spring/Spring.Core.Tests/Context/Support/XmlApplicationContextTests.cs +++ b/test/Spring/Spring.Core.Tests/Context/Support/XmlApplicationContextTests.cs @@ -35,6 +35,27 @@ namespace Spring.Context.Support [TestFixture] public sealed class XmlApplicationContextTests { + [Test] + public void InnerObjectWithPostProcessing() + { + try + { + XmlApplicationContext ctx = new XmlApplicationContext(false, "assembly://Spring.Core.Tests/Spring.Context.Support/innerObjectsWithPostProcessor.xml"); + ctx.GetObject("hasInnerObjects"); + } catch (System.NullReferenceException e) + { + Assert.Fail("Should not throw NullReferenceException",e); + } catch (ObjectCreationException e) + { + NoSuchObjectDefinitionException ex = e.InnerException as NoSuchObjectDefinitionException; + Assert.IsNotNull(ex); + //Pass + } catch (Exception e) + { + Assert.Fail("Expect only ObjectCreationException to be thrown", e); + } + } + [Test] public void SingleConfigLocation() { @@ -346,6 +367,37 @@ namespace Spring.Context.Support #endregion } + #endregion + } + public class SingletonTestingObjectPostProcessor : IObjectPostProcessor, IApplicationContextAware + { + private IApplicationContext applicationContext; + #region IObjectPostProcessor Members + + public object PostProcessBeforeInitialization(object instance, string name) + { + return instance; + } + + public object PostProcessAfterInitialization(object instance, string objectName) + { + Console.WriteLine("post process " + objectName); + if (this.applicationContext.IsSingleton(objectName)) + { + return instance; + } + return instance; + } + + #endregion + + #region IApplicationContextAware Members + + public IApplicationContext ApplicationContext + { + set { this.applicationContext = value; } + } + #endregion } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Context/Support/innerObjectsWithPostProcessor.xml b/test/Spring/Spring.Core.Tests/Context/Support/innerObjectsWithPostProcessor.xml new file mode 100644 index 00000000..fd32386a --- /dev/null +++ b/test/Spring/Spring.Core.Tests/Context/Support/innerObjectsWithPostProcessor.xml @@ -0,0 +1,26 @@ + + + + + + + + + hasInner + + + 5 + + + + + inner1 + + + 6 + + + + + + \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs index ca91ab8c..97216537 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs @@ -32,6 +32,7 @@ using Common.Logging; using Common.Logging.Simple; using DotNetMock.Dynamic; using NUnit.Framework; +using Spring.Context; using Spring.Context.Support; using Spring.Core.IO; using Spring.Core.TypeResolution; @@ -560,6 +561,7 @@ namespace Spring.Objects.Factory.Xml Assert.IsTrue(davesJen == jen, "1 jen instance"); } + [Test] public void InnerObjects() { @@ -2042,4 +2044,6 @@ namespace Spring.Objects.Factory.Xml #endregion } + + } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj index 65d51fe2..9a08df84 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj @@ -757,6 +757,7 @@ + @@ -886,4 +887,4 @@ xcopy "$(ProjectDir)Data" ..\..\..\..\build\VS.Net.2008\Spring.Core.Tests\$(ConfigurationName)\ /y /s /q /d xcopy "$(ProjectDir)$(TargetFileName).config" ..\..\..\..\build\VS.Net.2008\Spring.Core.Tests\$(ConfigurationName)\ /y /s /q - \ No newline at end of file +