Add InitDestroyPostProcessor to allow init and destroy via attributes

This commit is contained in:
Thomas Trageser
2012-08-20 16:40:03 +01:00
parent 75a58b90a3
commit af3f454b88
9 changed files with 799 additions and 0 deletions

View File

@@ -0,0 +1,178 @@
#region License
/*
* Copyright © 2002-2011 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
using NUnit.Framework;
using Spring.Context.Support;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
namespace Spring.Objects.Factory.Attributes
{
[TestFixture]
public class PostConstructAttributeTests
{
private GenericApplicationContext _applicationContext;
[SetUp]
public void Setup()
{
_applicationContext = new GenericApplicationContext();
var objDef = new RootObjectDefinition(typeof (InitDestroyAttributeObjectPostProcessor));
objDef.Role = ObjectRole.ROLE_INFRASTRUCTURE;
_applicationContext.ObjectFactory.RegisterObjectDefinition("InitDestroyAttributeObjectPostProcessor", objDef);
objDef = new RootObjectDefinition(typeof(PostContructTestObject1));
_applicationContext.ObjectFactory.RegisterObjectDefinition("PostContructTestObject1", objDef);
objDef = new RootObjectDefinition(typeof(PostContructTestObject2));
_applicationContext.ObjectFactory.RegisterObjectDefinition("PostContructTestObject2", objDef);
objDef = new RootObjectDefinition(typeof(PostContructTestObject3));
_applicationContext.ObjectFactory.RegisterObjectDefinition("PostContructTestObject3", objDef);
objDef = new RootObjectDefinition(typeof(PostContructTestObject4));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("PostContructTestObject4", objDef);
objDef = new RootObjectDefinition(typeof(PostContructTestObject5));
_applicationContext.ObjectFactory.RegisterObjectDefinition("PostContructTestObject5", objDef);
objDef = new RootObjectDefinition(typeof(PostContructTestObject1));
objDef.InitMethodName = "Init1";
_applicationContext.ObjectFactory.RegisterObjectDefinition("PostContructTestObject6", objDef);
_applicationContext.Refresh();
}
[Test]
public void PostContructMethodExecuted()
{
var testObj = (PostContructTestObject1)_applicationContext.GetObject("PostContructTestObject1");
Assert.That(testObj.InitCalled, Is.EqualTo(1));
}
[Test]
public void ExecutedInCorrectOrder()
{
var testObj = (PostContructTestObject2)_applicationContext.GetObject("PostContructTestObject2");
Assert.That(testObj.InitCalled, Is.EqualTo(2), "Two PostContruct methods defined, need to have two method calls.");
Assert.That(testObj.CalledAfter, Is.True, "Order of PostConstruct not followed.");
}
[Test]
public void NoExceptionIfMethodHasReturnValue()
{
var testObj = (PostContructTestObject3)_applicationContext.GetObject("PostContructTestObject3");
Assert.That(testObj.InitCalled, Is.EqualTo(1), "A PostContruct method with return value should run the init method.");
}
[Test]
public void WithArgumentMustThrowException()
{
Assert.That(delegate { _applicationContext.GetObject("PostContructTestObject4"); }, Throws.Exception.TypeOf<ObjectCreationException>());
}
[Test]
public void AttributeOnbaseTypeMethod()
{
var testObj = (PostContructTestObject5)_applicationContext.GetObject("PostContructTestObject5");
Assert.That(testObj.InitCalled, Is.EqualTo(1));
}
[Test]
public void SameMethodDefinedInXml()
{
var testObj = (PostContructTestObject1)_applicationContext.GetObject("PostContructTestObject6");
Assert.That(testObj.InitCalled, Is.EqualTo(1));
}
}
public class PostContructTestObject1
{
public int InitCalled { get; set; }
[PostConstruct]
public void Init1()
{
InitCalled++;
}
}
public class PostContructTestObject2
{
public int InitCalled { get; set; }
public bool Init1Called { get; set; }
public bool CalledAfter { get; set; }
[PostConstruct(Order = 1)]
public void Init1()
{
InitCalled++;
Init1Called = true;
}
[PostConstruct(Order = 2)]
public void Init2()
{
InitCalled++;
if (Init1Called)
CalledAfter = true;
}
}
public class PostContructTestObject3
{
public int InitCalled { get; set; }
[PostConstruct]
private bool Init1()
{
InitCalled++;
return true;
}
}
public class PostContructTestObject4
{
public int InitCalled { get; set; }
[PostConstruct]
private void Init1(bool enabled)
{
InitCalled++;
}
}
public class PostContructTestObject5 : PostContructTestObject1
{
}
}

View File

@@ -0,0 +1,192 @@
#region License
/*
* Copyright © 2002-2011 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
using NUnit.Framework;
using Spring.Context.Support;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
namespace Spring.Objects.Factory.Attributes
{
[TestFixture]
public class PreDestroyAttributeTests
{
private GenericApplicationContext _applicationContext;
[SetUp]
public void Setup()
{
_applicationContext = new GenericApplicationContext();
var objDef = new RootObjectDefinition(typeof(InitDestroyAttributeObjectPostProcessor));
objDef.Role = ObjectRole.ROLE_INFRASTRUCTURE;
_applicationContext.ObjectFactory.RegisterObjectDefinition("InitDestroyAttributeObjectPostProcessor", objDef);
objDef = new RootObjectDefinition(typeof(PreDestroyTestObject1));
_applicationContext.ObjectFactory.RegisterObjectDefinition("PreDestroyTestObject1", objDef);
objDef = new RootObjectDefinition(typeof(PreDestroyTestObject2));
_applicationContext.ObjectFactory.RegisterObjectDefinition("PreDestroyTestObject2", objDef);
objDef = new RootObjectDefinition(typeof(PreDestroyTestObject3));
objDef.DestroyMethodName = "Destroy";
_applicationContext.ObjectFactory.RegisterObjectDefinition("PreDestroyTestObject3", objDef);
objDef = new RootObjectDefinition(typeof(PreDestroyTestObject4));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("PreDestroyTestObject4", objDef);
objDef = new RootObjectDefinition(typeof(PreDestroyTestObject5));
_applicationContext.ObjectFactory.RegisterObjectDefinition("PreDestroyTestObject5", objDef);
_applicationContext.Refresh();
}
[Test]
public void PreDestroyMethodExecution()
{
DestroyTester.ExecutionCount1 = 0;
var testObj = _applicationContext.GetObject("PreDestroyTestObject1");
_applicationContext.Dispose();
Assert.That(DestroyTester.ExecutionCount1, Is.EqualTo(1));
}
[Test]
public void PreDestroyInBaseType()
{
DestroyTester.ExecutionCount2 = 0;
var testObj = _applicationContext.GetObject("PreDestroyTestObject2");
_applicationContext.Dispose();
Assert.That(DestroyTester.ExecutionCount2, Is.EqualTo(1));
}
[Test]
public void SameMethodDefinedInXml()
{
DestroyTester.ExecutionCount3 = 0;
var testObj = _applicationContext.GetObject("PreDestroyTestObject3");
_applicationContext.Dispose();
Assert.That(DestroyTester.ExecutionCount3, Is.EqualTo(1));
}
[Test]
public void InCorrectOrder()
{
DestroyTester.ExecutionCount4 = 0;
var testObj = (PreDestroyTestObject4)_applicationContext.GetObject("PreDestroyTestObject4");
_applicationContext.Dispose();
Assert.That(DestroyTester.ExecutionCount4, Is.EqualTo(2));
Assert.That(DestroyTester.CorrectOrder, Is.True);
}
[Test]
public void WithArgumentMustThrowException()
{
Assert.That(delegate
{
DestroyTester.ExecutionCount5 = 0;
var testObj = (PreDestroyTestObject5)_applicationContext.GetObject("PreDestroyTestObject5");
_applicationContext.Dispose();
},
Throws.Nothing);
}
}
public class PreDestroyTestObject1
{
[PreDestroy]
public void Destroy()
{
DestroyTester.ExecutionCount1++;
}
}
public class PreDestroyBase
{
[PreDestroy]
public void Destroy()
{
DestroyTester.ExecutionCount2++;
}
}
public class PreDestroyTestObject2 : PreDestroyBase
{
}
public class PreDestroyTestObject3
{
[PreDestroy]
public void Destroy()
{
DestroyTester.ExecutionCount3++;
}
}
public class PreDestroyTestObject4
{
public PreDestroyTestObject4()
{
DestroyTester.Destroy1Called = false;
DestroyTester.CorrectOrder = false;
}
[PreDestroy(Order = 1)]
public void Destroy1()
{
DestroyTester.ExecutionCount4++;
DestroyTester.Destroy1Called = true;
}
[PreDestroy(Order = 2)]
public void Destroy2()
{
DestroyTester.ExecutionCount4++;
if (DestroyTester.Destroy1Called)
DestroyTester.CorrectOrder = true;
}
}
public class PreDestroyTestObject5
{
[PreDestroy]
public void Destroy(bool arugment)
{
DestroyTester.ExecutionCount5++;
}
}
public static class DestroyTester
{
public static int ExecutionCount1 { get; set; }
public static int ExecutionCount2 { get; set; }
public static int ExecutionCount3 { get; set; }
public static int ExecutionCount4 { get; set; }
public static int ExecutionCount5 { get; set; }
public static bool Destroy1Called { get; set; }
public static bool CorrectOrder { get; set; }
}
}

View File

@@ -316,7 +316,9 @@
</Compile>
<Compile Include="HookableContextHandler.cs" />
<Compile Include="Objects\ExpressionTestObject.cs" />
<Compile Include="Objects\Factory\Attributes\PostConstructAttributeTests.cs" />
<Compile Include="Objects\Factory\Attributes\MyRequiredAttribute.cs" />
<Compile Include="Objects\Factory\Attributes\PreDestroyAttributeTests.cs" />
<Compile Include="Objects\Factory\Attributes\RequiredAttributeObjectPostProcessorTests.cs" />
<Compile Include="Objects\Factory\Attributes\RequiredTestObject.cs" />
<Compile Include="Objects\Factory\ConcurrentObjectFactoryTests.cs" />

View File

@@ -318,7 +318,9 @@
</Compile>
<Compile Include="HookableContextHandler.cs" />
<Compile Include="Objects\ExpressionTestObject.cs" />
<Compile Include="Objects\Factory\Attributes\PostConstructAttributeTests.cs" />
<Compile Include="Objects\Factory\Attributes\MyRequiredAttribute.cs" />
<Compile Include="Objects\Factory\Attributes\PreDestroyAttributeTests.cs" />
<Compile Include="Objects\Factory\Attributes\RequiredAttributeObjectPostProcessorTests.cs" />
<Compile Include="Objects\Factory\Attributes\RequiredTestObject.cs" />
<Compile Include="Objects\Factory\ConcurrentObjectFactoryTests.cs" />