resolved SPRNET-1060

This commit is contained in:
eeichinger
2008-11-03 19:29:58 +00:00
parent 09619f42cb
commit 3bc38fb610
8 changed files with 150 additions and 174 deletions

View File

@@ -25,6 +25,7 @@ using System.Reflection;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
@@ -77,6 +78,7 @@ namespace Spring.Testing.NUnit
/// <author>Aleksandar Seovic (.NET)</author>
public abstract class AbstractDependencyInjectionSpringContextTests : AbstractSpringContextTests
{
private bool registerContextWithContextRegistry = true;
private bool populateProtectedVariables = false;
private AutoWiringMode autowireMode = AutoWiringMode.ByType;
private bool dependencyCheck = true;
@@ -98,6 +100,17 @@ namespace Spring.Testing.NUnit
public AbstractDependencyInjectionSpringContextTests()
{}
/// <summary>
/// Controls, whether the <see cref="applicationContext"/> instance will
/// be registered/unregistered with the global <see cref="ContextRegistry"/> before and after each test.
/// Defaults to <c>true</c>.
/// </summary>
public bool RegisterContextWithContextRegistry
{
get { return registerContextWithContextRegistry; }
set { registerContextWithContextRegistry = value; }
}
/// <summary>
/// Gets or sets a flag specifying whether to populate protected
/// variables of this test case.
@@ -163,9 +176,13 @@ namespace Spring.Testing.NUnit
/// Test setup method.
/// </summary>
[SetUp]
protected virtual void SetUp()
public virtual void SetUp()
{
this.applicationContext = GetContext(ContextKey);
if (RegisterContextWithContextRegistry)
{
ContextRegistry.RegisterContext(this.applicationContext);
}
InjectDependencies();
try
{
@@ -339,7 +356,7 @@ namespace Spring.Testing.NUnit
/// Test teardown method.
/// </summary>
[TearDown]
protected void TearDown()
public void TearDown()
{
try
{
@@ -349,6 +366,13 @@ namespace Spring.Testing.NUnit
{
logger.Error("OnTearDown error", ex);
}
finally
{
if (RegisterContextWithContextRegistry)
{
ContextRegistry.Clear();
}
}
}
/// <summary>

View File

@@ -43,17 +43,37 @@ namespace Spring.Testing.NUnit
/// Map of context keys returned by subclasses of this class, to
/// Spring contexts.
/// </summary>
private static IDictionary contextKeyToContextMap = new Hashtable();
private static readonly IDictionary contextKeyToContextMap;
/// <summary>
/// Static ctor to avoid "beforeFieldInit" problem.
/// </summary>
static AbstractSpringContextTests()
{
contextKeyToContextMap = new Hashtable();
}
/// <summary>
/// Disposes any cached context instance and removes it from cache.
/// </summary>
public static void ClearContextCache()
{
foreach(IApplicationContext ctx in contextKeyToContextMap.Values)
{
ctx.Dispose();
}
contextKeyToContextMap.Clear();
}
/// <summary>
/// Logger available to subclasses.
/// </summary>
protected ILog logger;
protected readonly ILog logger;
/// <summary>
/// Default constructor for AbstractSpringContextTests.
/// </summary>
public AbstractSpringContextTests()
protected AbstractSpringContextTests()
{
logger = LogManager.GetLogger(GetType());
}

View File

@@ -94,6 +94,11 @@
Project = "{C5585365-561C-4EC4-8956-87FFBD9AB1CD}"
Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
/>
<Reference
Name = "Spring.Core.2003"
Project = "{710961A3-0DF4-49E4-A26E-F5B9C044AC84}"
Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
/>
</References>
</Build>
<Files>
@@ -108,12 +113,7 @@
BuildAction = "None"
/>
<File
RelPath = "Testing\NUnit\BaseTestAppContextTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Testing\NUnit\CachedAppContextTests.cs"
RelPath = "Testing\NUnit\AbstractDependencyInjectionSpringContextTestsTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
@@ -121,11 +121,6 @@
RelPath = "Testing\NUnit\TestApplicationContext.xml"
BuildAction = "EmbeddedResource"
/>
<File
RelPath = "Testing\NUnit\AnotherTest\LoadAppContextTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
</Include>
</Files>
</CSHARP>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{4D6D616B-7643-4D6B-8E5E-14ECFDB9AF82}</ProjectGuid>
<OutputType>Library</OutputType>
@@ -45,9 +45,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Testing\NUnit\BaseTestAppContextTests.cs" />
<Compile Include="Testing\NUnit\AnotherTest\LoadAppContextTests.cs" />
<Compile Include="Testing\NUnit\CachedAppContextTests.cs" />
<Compile Include="Testing\NUnit\AbstractDependencyInjectionSpringContextTestsTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Spring\Spring.Core\Spring.Core.2008.csproj">
@@ -71,13 +69,11 @@
<Name>Spring.Data.Tests.2008</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Testing\NUnit\TestApplicationContext.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="Spring.Testing.NUnit.Tests.dll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Testing\NUnit\TestApplicationContext.xml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -0,0 +1,92 @@
using System;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
namespace Spring.Testing.NUnit
{
/// <summary>
/// Summary description for AbstractDependencyInjectionSpringContextTestsTests.
/// </summary>
[TestFixture]
public class AbstractDependencyInjectionSpringContextTestsTests
{
private class TestAbstractDependencyInjectionSpringContextTests :AbstractDependencyInjectionSpringContextTests
{
public TestAbstractDependencyInjectionSpringContextTests()
{}
public TestAbstractDependencyInjectionSpringContextTests(bool registerWithContextRegistry)
{
base.RegisterContextWithContextRegistry = registerWithContextRegistry;
}
public IConfigurableApplicationContext ApplicationContext
{
get { return base.applicationContext; }
}
protected override string[] ConfigLocations
{
get { return new string[] {"assembly://Spring.Testing.NUnit.Tests/Spring.Testing.NUnit/TestApplicationContext.xml"}; }
}
}
private TestAbstractDependencyInjectionSpringContextTests fixtureInstance ;
[TearDown]
public void TearDown()
{
AbstractSpringContextTests.ClearContextCache();
}
[Test]
public void RegistersAndUnregistersWithContextRegistryByDefault()
{
fixtureInstance = new TestAbstractDependencyInjectionSpringContextTests();
Assert.IsTrue(fixtureInstance.RegisterContextWithContextRegistry);
fixtureInstance.SetUp();
Assert.IsTrue( ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name) );
fixtureInstance.TearDown();
Assert.IsFalse( ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name) );
}
[Test]
public void DoesNotRegisterContextWithContextRegistry()
{
fixtureInstance = new TestAbstractDependencyInjectionSpringContextTests();
Assert.IsTrue(fixtureInstance.RegisterContextWithContextRegistry);
fixtureInstance.RegisterContextWithContextRegistry = false;
fixtureInstance.SetUp();
Assert.IsFalse( ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name) );
fixtureInstance.TearDown();
Assert.IsFalse( ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name) );
}
[Test]
public void CachesApplicationContexts()
{
fixtureInstance = new TestAbstractDependencyInjectionSpringContextTests(false);
fixtureInstance.SetUp();
Assert.IsNotNull(fixtureInstance.ApplicationContext);
Assert.AreEqual(1, fixtureInstance.LoadCount); // context has been loaded
fixtureInstance.TearDown();
TestAbstractDependencyInjectionSpringContextTests otherFixtureInstance = new TestAbstractDependencyInjectionSpringContextTests(false);
otherFixtureInstance.SetUp();
Assert.IsNotNull(otherFixtureInstance.ApplicationContext);
Assert.AreEqual(0, otherFixtureInstance.LoadCount); // context was obtained from cache
Assert.AreSame(fixtureInstance.ApplicationContext, otherFixtureInstance.ApplicationContext);
otherFixtureInstance.SetDirty(); // dispose
otherFixtureInstance.TearDown();
otherFixtureInstance = new TestAbstractDependencyInjectionSpringContextTests(false);
otherFixtureInstance.SetUp();
Assert.IsNotNull(otherFixtureInstance.ApplicationContext);
Assert.AreEqual(1, otherFixtureInstance.LoadCount); // context was reloaded because of SetDirty() above
Assert.AreNotSame(fixtureInstance.ApplicationContext, otherFixtureInstance.ApplicationContext);
otherFixtureInstance.TearDown();
}
}
}

View File

@@ -1,57 +0,0 @@
#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
#if !NET_1_0
#region Imports
using System;
using NUnit.Framework;
#endregion
namespace Spring.Testing.NUnit.Another
{
/// <summary>
/// This class should be the first one executed that inherits from BaseSpringTests to be loaded by
/// NUnit. The second one should be CachedAppContextTests. The package name starting with the
/// letter 'A' is on purpose, since this seems to match how NUnit iterates over tests in this
/// assembly.
/// </summary>
/// <author>Mark Pollack</author>
[TestFixture]
public class LoadAppContextTests : BaseTestAppContextTests
{
/// <summary>
/// Loads the application context. If this test fails, see if you can coerce NUnit to
/// execut it before CachedAppContextTests, or check if application context caching in
/// Spring.Testing.NUnit is broken.
/// </summary>
[Test]
public void LoadApplicationContext()
{
Assert.AreEqual(1, LoadCount, "Either caching of application context is broken or this test was executed before CachedAppContextTests.");
}
}
}
#endif

View File

@@ -1,40 +0,0 @@
#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
using System;
namespace Spring.Testing.NUnit
{
/// <summary>
/// This is
/// </summary>
/// <remarks>
///
/// </remarks>
/// <author>Mark Pollack</author>
public class BaseTestAppContextTests : AbstractTransactionalDbProviderSpringContextTests
{
protected override string[] ConfigLocations
{
get { return new string[] {"assembly://Spring.Testing.NUnit.Tests/Spring.Testing.NUnit/TestApplicationContext.xml"}; }
}
}
}

View File

@@ -1,54 +0,0 @@
#if !NET_1_0
#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 System;
using NUnit.Framework;
#endregion
namespace Spring.Testing.NUnit
{
/// <summary>
/// This class should be the second one executed that inherits from BaseSpringTests to be loaded by
/// NUnit. The first one should be LoadAppContextTests.
/// </summary>
/// <author>Mark Pollack</author>
[TestFixture]
public class CachedAppContextTests : BaseTestAppContextTests
{
/// <summary>
/// Loads the cached application context. If this test fails, see if you can coerce NUnit to
/// execut it before CachedAppContextTests, or check if application context caching in
/// Spring.Testing.NUnit is broken.
/// </summary>
[Test]
public void CachedApplicationContext()
{
Assert.AreEqual(0, LoadCount);
}
}
}
#endif