SPRNET-881 Add XML configuration option for default-autowire-candidates/autowire-candidate and modify IObjectFactory.GetObject<T>() to return requested object with the use of autowire-candidate filter

This commit is contained in:
Thomas Trageser
2012-09-27 16:24:42 +01:00
parent eb59b8f26e
commit f02b3244ec
12 changed files with 295 additions and 48 deletions

View File

@@ -1608,6 +1608,36 @@ namespace Spring.Objects.Factory
Assert.AreEqual(ExpectedAge, child.Age);
}
[Test]
public void GetObjectByTypeWithAmbiguity()
{
DefaultListableObjectFactory lbf = new DefaultListableObjectFactory();
RootObjectDefinition bd1 = new RootObjectDefinition(typeof(TestObject));
RootObjectDefinition bd2 = new RootObjectDefinition(typeof(TestObject));
lbf.RegisterObjectDefinition("bd1", bd1);
lbf.RegisterObjectDefinition("bd2", bd2);
Assert.That(delegate { lbf.GetObject<TestObject>(); }, Throws.Exception.TypeOf<NoSuchObjectDefinitionException>());
}
[Test]
public void GetObjectByTypeFiltersOutNonAutowireCandidates()
{
DefaultListableObjectFactory lbf = new DefaultListableObjectFactory();
RootObjectDefinition bd1 = new RootObjectDefinition(typeof(TestObject));
RootObjectDefinition bd2 = new RootObjectDefinition(typeof(TestObject));
RootObjectDefinition na1 = new RootObjectDefinition(typeof(TestObject));
na1.IsAutowireCandidate = false;
lbf.RegisterObjectDefinition("bd1", bd1);
lbf.RegisterObjectDefinition("na1", na1);
TestObject actual = lbf.GetObject<TestObject>(); // na1 was filtered
Assert.That(lbf.GetObject("bd1", typeof(TestObject)), Is.SameAs(actual));
lbf.RegisterObjectDefinition("bd2", bd2);
Assert.That(delegate { lbf.GetObject<TestObject>(); }, Throws.Exception.TypeOf<NoSuchObjectDefinitionException>());
}
[Test]
public void GetObjectDefinitionResolvesAliases()
{

View File

@@ -354,5 +354,37 @@ namespace Spring.Objects.Factory.Xml
Assert.AreEqual("test1", od2.DependsOn[0]);
Assert.AreEqual(DependencyCheckingMode.Simple, od2.DependencyCheck);
}
[Test]
public void ParsesAutowireCandidate()
{
DefaultListableObjectFactory of = new DefaultListableObjectFactory();
XmlObjectDefinitionReader reader = new XmlObjectDefinitionReader(of);
reader.LoadObjectDefinitions(new StringResource(
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net' default-autowire-candidates='test1*,test4*'>
<object id='test1' type='Spring.Objects.TestObject, Spring.Core.Tests' />
<object id='test2' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='false' />
<object id='test3' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='true' />
<object id='test4' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='default' />
<object id='test5' type='Spring.Objects.TestObject, Spring.Core.Tests' autowire-candidate='default' />
</objects>
"));
var od = (AbstractObjectDefinition)of.GetObjectDefinition("test1");
Assert.That(od.IsAutowireCandidate, Is.True, "No attribute set should default to true");
od = (AbstractObjectDefinition)of.GetObjectDefinition("test2");
Assert.That(od.IsAutowireCandidate, Is.False, "Specifically attribute set to false should set to false");
od = (AbstractObjectDefinition)of.GetObjectDefinition("test3");
Assert.That(od.IsAutowireCandidate, Is.True, "Specifically attribute set to true should set to false");
od = (AbstractObjectDefinition)of.GetObjectDefinition("test4");
Assert.That(od.IsAutowireCandidate, Is.True, "Attribute set to default should check pattern and return true");
od = (AbstractObjectDefinition)of.GetObjectDefinition("test5");
Assert.That(od.IsAutowireCandidate, Is.False, "Attribute set to default should check pattern and return false");
}
}
}