Add autowire by attribute for fields, properties, methods and constructors

This commit is contained in:
Thomas Trageser
2012-08-23 13:14:12 +01:00
parent e5c8a0396a
commit 773f3146da
14 changed files with 2501 additions and 4 deletions

View File

@@ -0,0 +1,160 @@
#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 System.Collections.Generic;
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 AutowireAttributeCollectionTests
{
private GenericApplicationContext _applicationContext;
[SetUp]
public void Setup()
{
_applicationContext = new GenericApplicationContext();
var objDef = new RootObjectDefinition(typeof(AutowiredAttributeObjectPostProcessor));
objDef.Role = ObjectRole.ROLE_INFRASTRUCTURE;
_applicationContext.RegisterObjectDefinition("AutowiredAttributeObjectPostProcessor", objDef);
objDef = new RootObjectDefinition(typeof(ColFoo1));
_applicationContext.RegisterObjectDefinition("Foo1", objDef);
objDef = new RootObjectDefinition(typeof(ColFoo2));
_applicationContext.RegisterObjectDefinition("Foo2", objDef);
objDef = new RootObjectDefinition(typeof(ColTestObject1));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("ColTestObject1", objDef);
objDef = new RootObjectDefinition(typeof(ColTestObject2));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("ColTestObject2", objDef);
objDef = new RootObjectDefinition(typeof(ColTestObject3));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("ColTestObject3", objDef);
objDef = new RootObjectDefinition(typeof(ColTestObject4));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("ColTestObject4", objDef);
_applicationContext.Refresh();
}
[Test]
public void InjectAListOfObjects()
{
var testObj = (ColTestObject1)_applicationContext.GetObject("ColTestObject1");
var objDef = _applicationContext.ObjectFactory.GetObjectDefinition("ColTestObject1");
Assert.That(testObj.Count, Is.EqualTo(2));
Assert.That(objDef.DependsOn.Count, Is.EqualTo(2));
}
[Test]
public void InjectASetOfObjects()
{
var testObj = (ColTestObject2)_applicationContext.GetObject("ColTestObject2");
Assert.That(testObj.Count, Is.EqualTo(2));
}
[Test]
public void InjectADictionaryOfObjects()
{
var testObj = (ColTestObject3)_applicationContext.GetObject("ColTestObject3");
Assert.That(testObj.Count, Is.EqualTo(2));
}
[Test]
public void InjectAnArrayOfObjects()
{
var testObj = (ColTestObject4)_applicationContext.GetObject("ColTestObject4");
Assert.That(testObj.Count, Is.EqualTo(2));
}
}
#region Test Objects
public interface IColFoo
{
string Name();
}
public class ColFoo1 : IColFoo
{
public string Name()
{
return "Foo1";
}
}
public class ColFoo2 : IColFoo
{
public string Name()
{
return "Foo2";
}
}
public class ColTestObject1
{
[Autowired]
private IList<IColFoo> _col;
public int Count { get { return _col.Count; } }
}
public class ColTestObject2
{
[Autowired]
private Spring.Collections.Generic.ISet<IColFoo> _col;
public int Count { get { return _col.Count; } }
}
public class ColTestObject3
{
[Autowired]
private IDictionary<string, IColFoo> _col;
public int Count { get { return _col.Count; } }
}
public class ColTestObject4
{
[Autowired]
private IColFoo[] _col;
public int Count { get { return _col.Length; } }
}
#endregion
}

View File

@@ -0,0 +1,343 @@
#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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 AutowireAttributeConstructorTest
{
private GenericApplicationContext _applicationContext;
[SetUp]
public void Setup()
{
_applicationContext = new GenericApplicationContext();
var objDef = new RootObjectDefinition(typeof(AutowiredAttributeObjectPostProcessor));
objDef.Role = ObjectRole.ROLE_INFRASTRUCTURE;
_applicationContext.RegisterObjectDefinition("AutowiredAttributeObjectPostProcessor", objDef);
objDef = new RootObjectDefinition(typeof(ConsSimple));
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsSimple", objDef);
objDef = new RootObjectDefinition(typeof(ConsAdvanced));
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsAdvanced", objDef);
objDef = new RootObjectDefinition(typeof(ConsHello));
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsHello", objDef);
objDef = new RootObjectDefinition(typeof(ConsCiao));
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsCiao", objDef);
objDef = new RootObjectDefinition(typeof(ConsTestObject1));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsTestObject1", objDef);
objDef = new RootObjectDefinition(typeof(ConsTestObject2));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsTestObject2", objDef);
objDef = new RootObjectDefinition(typeof(ConsTestObject3));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsTestObject3", objDef);
objDef = new RootObjectDefinition(typeof(ConsTestObject4));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsTestObject4", objDef);
objDef = new RootObjectDefinition(typeof(ConsTestObject5));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsTestObject5", objDef);
objDef = new RootObjectDefinition(typeof(ConsTestObject6));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsTestObject6", objDef);
objDef = new RootObjectDefinition(typeof(ConsTestObject7));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("ConsTestObject7", objDef);
_applicationContext.Refresh();
}
[Test]
public void ConstructorWithInjectParameter()
{
var testObj = (ConsTestObject1)_applicationContext.GetObject("ConsTestObject1");
Assert.That(testObj.IsSet(), Is.True);
}
[Test]
public void SelectInjectConstructorOverDefault()
{
var testObj = (ConsTestObject2)_applicationContext.GetObject("ConsTestObject2");
Assert.That(testObj.IsSet(), Is.True);
}
[Test]
public void SelectConstructorWithMostParamters()
{
var testObj = (ConsTestObject3)_applicationContext.GetObject("ConsTestObject3");
Assert.That(testObj.IsSet(), Is.True);
}
[Test]
public void FailIfParameterAreRequired()
{
Exception ex = null;
try
{
var testObj = (ConsTestObject4)_applicationContext.GetObject("ConsTestObject4");
}
catch (Exception e) { ex = e; }
Assert.That(ex, Is.Not.Null, "Exception should be thrown");
Assert.That(ex.Message, Is.StringContaining("Unsatisfied dependency expressed"));
}
[Test]
public void InjectCollection()
{
var testObj = (ConsTestObject5)_applicationContext.GetObject("ConsTestObject5");
Assert.That(testObj.IsSet(), Is.True);
Assert.That(testObj.ObjectCount(), Is.EqualTo(2));
}
[Test]
public void SetParameterByParameterName()
{
var testObj = (ConsTestObject6)_applicationContext.GetObject("ConsTestObject6");
Assert.That(testObj.IsSet(), Is.True);
}
[Test]
public void SetParameterByQualifier()
{
var testObj = (ConsTestObject7)_applicationContext.GetObject("ConsTestObject7");
Assert.That(testObj.IsSet(), Is.True);
Assert.That(testObj.CorrectObject(), Is.True);
}
}
#region Test Objects
public interface IConsSimple
{
string Message();
}
public class ConsSimple : IConsSimple
{
public string Message()
{
return "ok";
}
}
public interface IConsAdvanced
{
string Message();
}
public class ConsAdvanced : IConsAdvanced
{
public string Message()
{
return "ok";
}
}
public interface INotSet
{
}
public interface IConsCol
{
string Message();
}
public class ConsHello : IConsCol
{
public string Message()
{
return "hello";
}
}
public class ConsCiao : IConsCol
{
public string Message()
{
return "ciao";
}
}
public class ConsTestObject1
{
private IConsSimple _consSimple;
[Autowired]
public ConsTestObject1(IConsSimple consSimple)
{
_consSimple = consSimple;
}
public bool IsSet()
{
return _consSimple != null;
}
}
public class ConsTestObject2
{
private IConsSimple _consSimple;
public ConsTestObject2()
{
}
[Autowired]
public ConsTestObject2(IConsSimple consSimple)
{
_consSimple = consSimple;
}
public bool IsSet()
{
return _consSimple != null;
}
}
public class ConsTestObject3
{
private IConsSimple _consSimple;
private IConsAdvanced _consAdvanced;
[Autowired(Required = false)]
public ConsTestObject3(IConsSimple consSimple)
{
_consSimple = consSimple;
}
[Autowired(Required = false)]
public ConsTestObject3(IConsSimple consSimple, IConsAdvanced consAdvanced)
{
_consSimple = consSimple;
_consAdvanced = consAdvanced;
}
public bool IsSet()
{
return _consSimple != null && _consAdvanced != null;
}
}
public class ConsTestObject4
{
private INotSet _notSet;
[Autowired]
public ConsTestObject4(INotSet notSet)
{
_notSet = notSet;
}
public bool IsSet()
{
return _notSet != null;
}
}
public class ConsTestObject5
{
private IDictionary<string,IConsCol> _consCol;
[Autowired]
public ConsTestObject5(IDictionary<string, IConsCol> consCol)
{
_consCol = consCol;
}
public bool IsSet()
{
return _consCol != null;
}
public int ObjectCount()
{
return _consCol.Count;
}
}
public class ConsTestObject6
{
private IConsCol _consHello;
[Autowired]
public ConsTestObject6(IConsCol consHello)
{
_consHello = consHello;
}
public bool IsSet()
{
return _consHello != null;
}
}
public class ConsTestObject7
{
private IConsCol _consHello;
[Autowired]
public ConsTestObject7([Qualifier("ConsCiao")] IConsCol consHello)
{
_consHello = consHello;
}
public bool IsSet()
{
return _consHello != null;
}
public bool CorrectObject()
{
return _consHello.Message() == "ciao";
}
}
#endregion
}

View File

@@ -0,0 +1,290 @@
#region License
/*
* Copyright 2002-2010 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;
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 AutowireAttributeFieldTest
{
private GenericApplicationContext _applicationContext;
[SetUp]
public void Setup()
{
_applicationContext = new GenericApplicationContext();
var objDef = new RootObjectDefinition(typeof(AutowiredAttributeObjectPostProcessor));
objDef.Role = ObjectRole.ROLE_INFRASTRUCTURE;
_applicationContext.RegisterObjectDefinition("AutowiredAttributeObjectPostProcessor", objDef);
objDef = new RootObjectDefinition(typeof(FldFooImpl));
_applicationContext.RegisterObjectDefinition("FldFoo", objDef);
objDef = new RootObjectDefinition(typeof(FldHello));
_applicationContext.RegisterObjectDefinition("FldHello", objDef);
objDef = new RootObjectDefinition(typeof(FldCioa));
_applicationContext.RegisterObjectDefinition("FldCioa", objDef);
objDef = new RootObjectDefinition(typeof(FldHola));
_applicationContext.RegisterObjectDefinition("FldHola", objDef);
objDef = new RootObjectDefinition(typeof(FldTestObject1));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("FldTestObject1", objDef);
objDef = new RootObjectDefinition(typeof(FldTestObject2));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("FldTestObject2", objDef);
objDef = new RootObjectDefinition(typeof(FldTestObject3));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("FldTestObject3", objDef);
objDef = new RootObjectDefinition(typeof(FldTestObject4));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("FldTestObject4", objDef);
objDef = new RootObjectDefinition(typeof(FldTestObject5));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("FldTestObject5", objDef);
objDef = new RootObjectDefinition(typeof(FldTestObject6));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("FldTestObject6", objDef);
objDef = new RootObjectDefinition(typeof(FldTestObject7));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("FldTestObject7", objDef);
_applicationContext.Refresh();
}
[Test]
public void InjectPropertyBasedOnFieldType()
{
var testObj = (FldTestObject1)_applicationContext.GetObject("FldTestObject1");
var objectDefinition = _applicationContext.ObjectFactory.GetObjectDefinition("FldTestObject1");
Assert.That(testObj.Test(), Is.EqualTo("foo"));
Assert.That(objectDefinition.DependsOn.Count, Is.EqualTo(1));
}
[Test]
public void WithTwoTypesRegisteredAndNoNameShouldFail()
{
Assert.That(delegate { var testObj = (FldTestObject2)_applicationContext.GetObject("FldTestObject2"); }, Throws.Exception.TypeOf<ObjectCreationException>());
}
[Test]
public void WithQualifierName()
{
var testObj = (FldTestObject3)_applicationContext.GetObject("FldTestObject3");
Assert.That(testObj.Say(), Is.EqualTo("cioa"));
}
[Test]
public void WithTwoTypesAndNoQualifierUsePopertyName()
{
var testObj = (FldTestObject4)_applicationContext.GetObject("FldTestObject4");
Assert.That(testObj.Say(), Is.EqualTo("cioa"));
}
[Test]
public void FailIfTypeCantBeResolved()
{
Exception ex = null;
try
{
var testObj = (FldTestObject5)_applicationContext.GetObject("FldTestObject5");
}
catch (Exception e) { ex = e; }
Assert.That(ex, Is.Not.Null, "Should throw an exception");
Assert.That(ex.Message, Is.StringContaining("Injection of autowired dependencies failed"));
}
[Test]
public void InjectedObjectAssignedToTwoInterfaces()
{
var testObj = (FldTestObject6)_applicationContext.GetObject("FldTestObject6");
Assert.That(testObj.Test(), Is.EqualTo("test"));
}
[Test]
public void IsNotRequired()
{
var testObj = (FldTestObject7)_applicationContext.GetObject("FldTestObject7");
Assert.That(testObj.IsNull(), Is.True);
}
}
#region Test Objects
public interface IFldNotAnObject
{
}
public interface IFldAnotherOne
{
string Quite();
}
public interface IFldFoo
{
string Test();
}
public class FldFooImpl : IFldFoo
{
public string Test()
{
return "foo";
}
}
public interface IFldSay
{
string Say();
}
public class FldHello : IFldSay
{
public string Say()
{
return "hello";
}
}
public class FldCioa : IFldSay
{
public string Say()
{
return "cioa";
}
}
public class FldHola : IFldFoo, IFldAnotherOne
{
public string Quite()
{
return "test";
}
public string Test()
{
return "test";
}
}
public class FldTestObject1
{
[Autowired]
private IFldFoo _fldFoo;
public string Test()
{
return _fldFoo.Test();
}
}
// object with 2 possibilities should fail
public class FldTestObject2
{
[Autowired]
private IFldSay _wrongName;
public string Say()
{
return _wrongName.Say();
}
}
// should not fail but inject Cioa object
public class FldTestObject3
{
[Autowired]
[Qualifier("FldCioa")]
private IFldSay _fldCioa;
public string Say()
{
return _fldCioa.Say();
}
}
// should not fail but inject Hello via Propertyname
public class FldTestObject4
{
[Autowired]
private IFldSay _fldCioa;
public string Say()
{
return _fldCioa.Say();
}
}
// should not fail but inject Hello via Propertyname
public class FldTestObject5
{
[Autowired]
private IFldNotAnObject _ohhh;
}
public class FldTestObject6
{
[Autowired]
private IFldAnotherOne _hola;
public string Test()
{
return _hola.Quite();
}
}
public class FldTestObject7
{
[Autowired(Required = false)]
private IFldNotAnObject _nono;
public bool IsNull()
{
return (_nono == null);
}
}
#endregion
}

View File

@@ -0,0 +1,371 @@
#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 System;
using System.Collections.Generic;
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 AutowireAttributeMethodsTests
{
private GenericApplicationContext _applicationContext;
[SetUp]
public void Setup()
{
_applicationContext = new GenericApplicationContext();
var objDef = new RootObjectDefinition(typeof(AutowiredAttributeObjectPostProcessor));
objDef.Role = ObjectRole.ROLE_INFRASTRUCTURE;
_applicationContext.RegisterObjectDefinition("AutowiredAttributeObjectPostProcessor", objDef);
objDef = new RootObjectDefinition(typeof(Simple));
_applicationContext.ObjectFactory.RegisterObjectDefinition("Simple", objDef);
objDef = new RootObjectDefinition(typeof(MethodHello));
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodHello", objDef);
objDef = new RootObjectDefinition(typeof(MethodCiao));
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodCiao", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject1));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject1", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject2));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject2", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject3));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject3", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject4));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject4", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject5));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject5", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject6));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject6", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject7));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject7", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject8));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject8", objDef);
objDef = new RootObjectDefinition(typeof(MethodTestObject9));
objDef.Scope = "prototype";
_applicationContext.ObjectFactory.RegisterObjectDefinition("MethodTestObject9", objDef);
_applicationContext.Refresh();
}
[Test]
public void InObjectByType()
{
var testObj = (MethodTestObject1)_applicationContext.GetObject("MethodTestObject1");
var objectDefinition = _applicationContext.ObjectFactory.GetObjectDefinition("MethodTestObject1");
Assert.That(testObj.GetObject(), Is.Not.Null);
Assert.That(objectDefinition.DependsOn.Count, Is.EqualTo(1));
}
[Test]
public void InjectByParamterName()
{
var testObj = (MethodTestObject2)_applicationContext.GetObject("MethodTestObject2");
Assert.That(testObj.GetObject(), Is.Not.Null);
}
[Test]
public void InjectByQualifier()
{
var testObj = (MethodTestObject3)_applicationContext.GetObject("MethodTestObject3");
Assert.That(testObj.GetObject(), Is.Not.Null);
}
[Test]
public void FailIfTypeCantBeResolved()
{
Exception ex = null;
try
{
var testObj = (FldTestObject5)_applicationContext.GetObject("MethodTestObject4");
}
catch (Exception e) { ex = e; }
Assert.That(ex, Is.Not.Null, "Should throw an exception");
Assert.That(ex.Message, Is.StringContaining("Injection of autowired dependencies failed"));
}
[Test]
public void InjectListOfObjects()
{
var testObj = (MethodTestObject5)_applicationContext.GetObject("MethodTestObject5");
Assert.That(testObj.GetObject(), Is.Not.Null);
Assert.That(testObj.GetObject().Count, Is.EqualTo(2));
}
[Test]
public void InjectSeveralParameters()
{
var testObj = (MethodTestObject6)_applicationContext.GetObject("MethodTestObject6");
var objectDefinition = _applicationContext.ObjectFactory.GetObjectDefinition("MethodTestObject6");
Assert.That(testObj.GetObject(), Is.Not.Null);
Assert.That(testObj.GetSimple(), Is.Not.Null);
Assert.That(objectDefinition.DependsOn.Count, Is.EqualTo(2));
}
[Test]
public void FailIfObjecNotAvailable()
{
Assert.That(delegate { var testObj = (MethodTestObject7)_applicationContext.GetObject("MethodTestObject7"); },
Throws.Exception.TypeOf<ObjectCreationException>());
}
[Test]
public void PassIfObjectNotavailableButNotRequired()
{
var testObj1 = (MethodTestObject8)_applicationContext.GetObject("MethodTestObject8");
var objectDefinition = _applicationContext.ObjectFactory.GetObjectDefinition("MethodTestObject8");
Assert.That(testObj1.GetSimple(), Is.Null);
Assert.That(objectDefinition.DependsOn.Count, Is.EqualTo(0));
var testObj2 = (MethodTestObject9)_applicationContext.GetObject("MethodTestObject9");
objectDefinition = _applicationContext.ObjectFactory.GetObjectDefinition("MethodTestObject9");
Assert.That(testObj2.GetrAdvanced(), Is.Null);
Assert.That(objectDefinition.DependsOn.Count, Is.EqualTo(0));
}
}
#region Test Objects
public interface ISimple
{
string Foo();
}
public class Simple : ISimple
{
public string Foo()
{
return "simple";
}
}
public interface IAdvanced
{
}
public interface IMethodFoo
{
string Foo();
}
public class MethodHello : IMethodFoo
{
public string Foo()
{
return "hello";
}
}
public class MethodCiao : IMethodFoo
{
public string Foo()
{
return "ciao";
}
}
public class MethodTestObject1
{
private ISimple _impl;
[Autowired]
public void Prepare(ISimple impl)
{
_impl = impl;
}
public object GetObject()
{
return _impl;
}
}
public class MethodTestObject2
{
private IMethodFoo _methodCiao;
[Autowired]
public void Prepare(IMethodFoo methodCiao)
{
_methodCiao = methodCiao;
}
public object GetObject()
{
return _methodCiao;
}
}
public class MethodTestObject3
{
private IMethodFoo _impl;
[Autowired]
public void Prepare([Qualifier("MethodHello")] IMethodFoo impl)
{
_impl = impl;
}
public object GetObject()
{
return _impl;
}
}
public class MethodTestObject4
{
private IMethodFoo _impl;
[Autowired]
public void Prepare([Qualifier("MethodHello")] IMethodFoo impl, string test)
{
_impl = impl;
}
public object GetObject()
{
return _impl;
}
}
public class MethodTestObject5
{
private IList<IMethodFoo> _impl;
[Autowired]
public void Prepare(IList<IMethodFoo> impl)
{
_impl = impl;
}
public IList<IMethodFoo> GetObject()
{
return _impl;
}
}
public class MethodTestObject6
{
private IMethodFoo _impl;
private ISimple _simple;
[Autowired]
public void Prepare(IMethodFoo methodHello, ISimple simple)
{
_impl = methodHello;
_simple = simple;
}
public object GetObject()
{
return _impl;
}
public object GetSimple()
{
return _simple;
}
}
public class MethodTestObject7
{
private ISimple _simple;
[Autowired]
public void Prepare([Qualifier("NotAvailable")] ISimple simple)
{
_simple = simple;
}
public object GetSimple()
{
return _simple;
}
}
public class MethodTestObject8
{
private ISimple _simple;
[Autowired(Required = false)]
public void Prepare([Qualifier("NotAvailable")] ISimple simple)
{
_simple = simple;
}
public object GetSimple()
{
return _simple;
}
}
public class MethodTestObject9
{
private IAdvanced _advanced;
[Autowired(Required = false)]
public void Prepare(IAdvanced advanced)
{
_advanced = advanced;
}
public object GetrAdvanced()
{
return _advanced;
}
}
#endregion
}

View File

@@ -0,0 +1,254 @@
#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 System;
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 AutowireAttributePropertyTest
{
private GenericApplicationContext _applicationContext;
[SetUp]
public void Setup()
{
_applicationContext = new GenericApplicationContext();
var objDef = new RootObjectDefinition(typeof(AutowiredAttributeObjectPostProcessor));
objDef.Role = ObjectRole.ROLE_INFRASTRUCTURE;
_applicationContext.RegisterObjectDefinition("AutowiredAttributeObjectPostProcessor", objDef);
objDef = new RootObjectDefinition(typeof(PropFooImpl));
_applicationContext.RegisterObjectDefinition("PropFoo", objDef);
objDef = new RootObjectDefinition(typeof(PropHello));
_applicationContext.RegisterObjectDefinition("PropHello", objDef);
objDef = new RootObjectDefinition(typeof(PropCioa));
_applicationContext.RegisterObjectDefinition("PropCioa", objDef);
objDef = new RootObjectDefinition(typeof(PropTestObject1));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("PropTestObject1", objDef);
objDef = new RootObjectDefinition(typeof(PropTestObject2));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("PropTestObject2", objDef);
objDef = new RootObjectDefinition(typeof(PropTestObject3));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("PropTestObject3", objDef);
objDef = new RootObjectDefinition(typeof(PropTestObject4));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("PropTestObject4", objDef);
objDef = new RootObjectDefinition(typeof(PropTestObject5));
objDef.Scope = "prototype";
_applicationContext.RegisterObjectDefinition("PropTestObject5", objDef);
_applicationContext.Refresh();
}
[Test]
public void InjectPropertyBasedOnPropertyType()
{
var testObj = (PropTestObject1)_applicationContext.GetObject("PropTestObject1");
var objectDefinition = _applicationContext.ObjectFactory.GetObjectDefinition("PropTestObject1");
Assert.That(testObj.Foo, Is.Not.Null);
Assert.That(testObj.Test(), Is.EqualTo("foo"));
Assert.That(objectDefinition.DependsOn.Count, Is.EqualTo(1), "Should have one Dependant Object");
}
[Test]
public void WithTwoTypesRegisteredAndNoNameShouldFail()
{
Assert.That(delegate { var testObj = (PropTestObject2)_applicationContext.GetObject("PropTestObject2"); }, Throws.Exception.TypeOf<ObjectCreationException>());
}
[Test]
public void WithQualifierName()
{
var testObj = (PropTestObject3)_applicationContext.GetObject("PropTestObject3");
Assert.That(testObj.Cioa, Is.Not.Null);
Assert.That(testObj.Say(), Is.EqualTo("cioa"));
}
[Test]
public void WithTwoTypesAndNoQualifierUsePopertyName()
{
var testObj = (PropTestObject4)_applicationContext.GetObject("PropTestObject4");
Assert.That(testObj.PropCioa, Is.Not.Null);
Assert.That(testObj.Say(), Is.EqualTo("cioa"));
}
[Test]
public void FailIfTypeCantBeResolved()
{
Exception ex = null;
try
{
var testObj = (FldTestObject5)_applicationContext.GetObject("PropTestObject5");
}
catch (Exception e) { ex = e; }
Assert.That(ex, Is.Not.Null, "Should throw an exception");
Assert.That(ex.Message, Is.StringContaining("Injection of autowired dependencies failed"));
}
}
#region Test Objects
public interface IPropNotAnObject
{
}
public interface IPropFoo
{
string Test();
}
public class PropFooImpl : IPropFoo
{
public string Test()
{
return "foo";
}
}
public interface IPropSay
{
string PropSay();
}
public class PropHello : IPropSay
{
public string PropSay()
{
return "hello";
}
}
public class PropCioa : IPropSay
{
public string PropSay()
{
return "cioa";
}
}
public class PropTestObject1
{
private IPropFoo _foo;
[Autowired]
public IPropFoo Foo
{
get { return _foo; }
set { _foo = value; }
}
public string Test()
{
return _foo.Test();
}
}
public class PropTestObject2
{
private IPropSay _hello;
[Autowired]
public IPropSay WrongName
{
get { return _hello; }
set { _hello = value; }
}
public string Say()
{
return _hello.PropSay();
}
}
// should not fail but inject Cioa object
public class PropTestObject3
{
private IPropSay _cioa;
[Autowired]
[Qualifier("PropCioa")]
public IPropSay Cioa
{
get { return _cioa; }
set { _cioa = value; }
}
public string Say()
{
return _cioa.PropSay();
}
}
// should not fail but inject Hello via Propertyname
public class PropTestObject4
{
private IPropSay _obj;
[Autowired]
public IPropSay PropCioa
{
get { return _obj; }
set { _obj = value; }
}
public string Say()
{
return _obj.PropSay();
}
}
// should not fail but inject Hello via Propertyname
public class PropTestObject5
{
private IPropNotAnObject _obj;
[Autowired]
public IPropNotAnObject Ohhh
{
get { return _obj; }
set { _obj = value; }
}
}
#endregion
}

View File

@@ -318,6 +318,11 @@
</Compile>
<Compile Include="HookableContextHandler.cs" />
<Compile Include="Objects\ExpressionTestObject.cs" />
<Compile Include="Objects\Factory\Attributes\AutowireAttributeCollectionTests.cs" />
<Compile Include="Objects\Factory\Attributes\AutowireAttributeConstructorTest.cs" />
<Compile Include="Objects\Factory\Attributes\AutowireAttributeFieldTest.cs" />
<Compile Include="Objects\Factory\Attributes\AutowireAttributeMethodsTests.cs" />
<Compile Include="Objects\Factory\Attributes\AutowireAttributePropertyTest.cs" />
<Compile Include="Objects\Factory\Attributes\MyRequiredAttribute.cs" />
<Compile Include="Objects\Factory\Attributes\RequiredAttributeObjectPostProcessorTests.cs" />
<Compile Include="Objects\Factory\Attributes\RequiredTestObject.cs" />