SPRNET-1243

SPRNET-1167
This commit is contained in:
eeichinger
2009-07-29 20:59:47 +00:00
parent 20346f459f
commit 18b7d1eedd
21 changed files with 562 additions and 225 deletions

View File

@@ -723,6 +723,18 @@ namespace Spring.Expressions
Assert.IsTrue((bool)ExpressionEvaluator.GetValue(tesla, "T(System.DateTime) == DOB.GetType()"));
}
/// <summary>
/// Tests type node
/// </summary>
[Test]
public void TestTypeNodeWithArrays()
{
Assert.AreEqual(typeof(DateTime[]), ExpressionEvaluator.GetValue(null, "T(System.DateTime[])"));
Assert.AreEqual(typeof(DateTime[,]), ExpressionEvaluator.GetValue(null, "T(System.DateTime[,])"));
Assert.AreEqual(typeof(DateTime[]), ExpressionEvaluator.GetValue(null, "T(System.DateTime[], mscorlib)"));
Assert.AreEqual(typeof(DateTime[,]), ExpressionEvaluator.GetValue(null, "T(System.DateTime[,], mscorlib)"));
}
/// <summary>
/// Tests type node
/// </summary>

View File

@@ -18,10 +18,8 @@
#endregion
using System;
using System.Collections;
using System.Collections.Specialized;
using NUnit.Framework;
using Spring.Objects.Factory.Support;
@@ -34,14 +32,22 @@ namespace Spring.Objects.Factory.Config
[TestFixture]
public class ObjectDefinitionVisitorTests
{
private NameValueCollectionVariableSource variableSource;
private Hashtable properties;
[SetUp]
public void SetUp()
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("Property", "Value");
variableSource = new NameValueCollectionVariableSource(nvc);
properties = CollectionsUtil.CreateCaseInsensitiveHashtable();
properties.Add("Property", "Value");
}
private string ParseAndResolveVariables(string rawText)
{
if (rawText.StartsWith("$"))
{
return (string) properties[rawText.Substring(1)];
}
return rawText;
}
[Test]
@@ -50,7 +56,7 @@ namespace Spring.Objects.Factory.Config
IObjectDefinition od = new RootObjectDefinition();
od.ObjectTypeName = "$Property";
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(variableSource);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));
odv.VisitObjectDefinition(od);
Assert.AreEqual("Value", od.ObjectTypeName);
@@ -62,7 +68,7 @@ namespace Spring.Objects.Factory.Config
IObjectDefinition od = new RootObjectDefinition();
od.PropertyValues.Add("PropertyName", "$Property");
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(variableSource);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));
odv.VisitObjectDefinition(od);
Assert.AreEqual("Value", od.PropertyValues.GetPropertyValue("PropertyName").Value);
@@ -78,7 +84,7 @@ namespace Spring.Objects.Factory.Config
ml.Add("$Property");
od.PropertyValues.Add("PropertyName", ml);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(variableSource);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));
odv.VisitObjectDefinition(od);
ManagedList list = od.PropertyValues.GetPropertyValue("PropertyName").Value as ManagedList;
@@ -96,7 +102,7 @@ namespace Spring.Objects.Factory.Config
ms.Add("$Property");
od.PropertyValues.Add("PropertyName", ms);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(variableSource);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));
odv.VisitObjectDefinition(od);
ManagedSet set = od.PropertyValues.GetPropertyValue("PropertyName").Value as ManagedSet;
@@ -117,7 +123,7 @@ namespace Spring.Objects.Factory.Config
md.Add("Key", "$Property");
od.PropertyValues.Add("PropertyName", md);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(variableSource);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));
odv.VisitObjectDefinition(od);
ManagedDictionary dictionary = od.PropertyValues.GetPropertyValue("PropertyName").Value as ManagedDictionary;
@@ -135,7 +141,7 @@ namespace Spring.Objects.Factory.Config
nvc["Key"] = "$Property";
od.PropertyValues.Add("PropertyName", nvc);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(variableSource);
ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));
odv.VisitObjectDefinition(od);
NameValueCollection visitedNvc =
@@ -143,31 +149,5 @@ namespace Spring.Objects.Factory.Config
Assert.AreEqual("Value", visitedNvc["Key"]);
}
#region Helper class
public class NameValueCollectionVariableSource : IVariableSource
{
private NameValueCollection properties;
public NameValueCollectionVariableSource(NameValueCollection properties)
{
this.properties = properties;
}
public string ResolveVariable(string name)
{
if (name.StartsWith("$"))
{
return properties[name.Substring(1)];
}
else
{
return name;
}
}
}
#endregion
}
}

View File

@@ -41,23 +41,7 @@ namespace Spring.Objects.Factory.Config
private static readonly DateTime TESTDATETIME = new DateTime(2007, 07, 06, 11, 12, 13);
private static readonly DateTime TESTDATETIME_DEFAULT = TESTDATETIME.AddDays(-1);
private class NameValueCollectionVariableSource : IVariableSource
{
private readonly NameValueCollection nameValues = new NameValueCollection();
public NameValueCollectionVariableSource Add(string name, string value)
{
nameValues.Add(name, value);
return this;
}
public string ResolveVariable(string name)
{
return nameValues.Get(name);
}
}
private readonly IVariableSource _testVariableSource = new NameValueCollectionVariableSource()
private readonly IVariableSource _testVariableSource = new DictionaryVariableSource(null, true)
.Add("ValidString", "String")
.Add("EmptyString", "")
.Add("ValidChar", "c")

View File

@@ -1,7 +1,7 @@
#region License
/*
* Copyright <20> 2002-2007 the original author or authors.
* Copyright <20> 2002-2009 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.
@@ -18,45 +18,49 @@
#endregion
#region Imports
using System;
using System.Collections;
using NUnit.Framework;
using Spring.Context.Support;
#endregion
using Spring.Objects.Factory.Support;
namespace Spring.Objects.Factory.Config
{
/// <summary>
/// This calss contains tests for
/// This class contains tests for
/// </summary>
/// <author>Mark Pollack</author>
[TestFixture]
public class VariablePlaceholderConfigurerTests
{
private class DictionaryVariableSource : IVariableSource
[Test]
public void ThrowsOnMissingVariableSources()
{
private Hashtable variables = new Hashtable();
public DictionaryVariableSource(params string[] args)
StaticApplicationContext ac = new StaticApplicationContext();
VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer();
try
{
for(int i=0;i<args.Length;i+=2)
{
variables[args[i]] = args[i+1];
}
}
public string ResolveVariable(string name)
{
return (string) variables[name];
vphc.PostProcessObjectFactory(ac.ObjectFactory);
Assert.Fail();
}
catch (ArgumentException)
{}
}
[SetUp]
public void Setup()
[Test]
public void ThrowsOnInvalidVariableSourcesElement()
{
StaticApplicationContext ac = new StaticApplicationContext();
VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer();
vphc.VariableSources = new ArrayList( new object[] { new object() } );
try
{
vphc.PostProcessObjectFactory(ac.ObjectFactory);
Assert.Fail();
}
catch (ArgumentException)
{}
}
[Test]
@@ -119,7 +123,85 @@ namespace Spring.Objects.Factory.Config
}
[Test]
[Ignore("Does not work yet because IVariableSource cannot differentiate between invalid key and key with a null value")]
public void MultiResolution()
{
DefaultListableObjectFactory of = new DefaultListableObjectFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.Add("Greeting", "Hello ${firstname} ${lastname}!");
of.RegisterObjectDefinition("tb1", new RootObjectDefinition("typename", null, pvs));
IList variableSources = new ArrayList();
variableSources.Add(new DictionaryVariableSource(new string[] { "firstname", "FirstName" }));
variableSources.Add(new DictionaryVariableSource(new string[] { "lastname", "LastName"}));
VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer(variableSources);
vphc.PostProcessObjectFactory(of);
RootObjectDefinition rod = (RootObjectDefinition) of.GetObjectDefinition("tb1");
Assert.AreEqual("Hello FirstName LastName!", rod.PropertyValues.GetPropertyValue("Greeting").Value);
}
[Test]
public void NestedResolution()
{
DefaultListableObjectFactory of = new DefaultListableObjectFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.Add("NameProperty", "${name}");
of.RegisterObjectDefinition("tb1", new RootObjectDefinition("typename", null, pvs));
IList variableSources = new ArrayList();
variableSources.Add(new DictionaryVariableSource(new string[] { "name", "${nickname}" }));
variableSources.Add(new DictionaryVariableSource(new string[] { "nickname", "nickname-value"}));
VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer(variableSources);
vphc.PostProcessObjectFactory(of);
RootObjectDefinition rod = (RootObjectDefinition) of.GetObjectDefinition("tb1");
Assert.AreEqual("nickname-value", rod.PropertyValues.GetPropertyValue("NameProperty").Value);
}
[Test]
public void ChainedResolution()
{
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.Add("name", "${name}");
pvs.Add("nickname", "${nickname}");
ac.RegisterSingleton("tb1", typeof(TestObject), pvs);
IList variableSources = new ArrayList();
variableSources.Add(new DictionaryVariableSource(new string[] { "name", "name-value" }));
variableSources.Add(new DictionaryVariableSource(new string[] { "nickname", "nickname-value"}));
VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer(variableSources);
ac.AddObjectFactoryPostProcessor(vphc);
ac.Refresh();
TestObject tb1 = (TestObject)ac.GetObject("tb1");
Assert.AreEqual("name-value", tb1.Name);
Assert.AreEqual("nickname-value", tb1.Nickname);
}
[Test]
public void ChainedResolutionWithNullValues()
{
DefaultListableObjectFactory of = new DefaultListableObjectFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.Add("NameProperty", "${name}");
pvs.Add("NickNameProperty", "${nickname}");
of.RegisterObjectDefinition("tb1", new RootObjectDefinition("typename", null, pvs));
IList variableSources = new ArrayList();
variableSources.Add(new DictionaryVariableSource(new string[] { "name", "name-value", "nickname", null }));
variableSources.Add(new DictionaryVariableSource(new string[] { "nickname", "nickname-value"}));
VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer(variableSources);
vphc.PostProcessObjectFactory(of);
RootObjectDefinition rod = (RootObjectDefinition) of.GetObjectDefinition("tb1");
Assert.AreEqual("name-value", rod.PropertyValues.GetPropertyValue("NameProperty").Value);
Assert.AreEqual(null, rod.PropertyValues.GetPropertyValue("NickNameProperty").Value);
}
[Test]
public void WhitespaceHandling()
{
StaticApplicationContext ac = new StaticApplicationContext();

View File

@@ -64,7 +64,6 @@ namespace Spring.Util
}
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void ContainsNullCollection()
{
CollectionUtils.Contains(null, null);
@@ -77,12 +76,12 @@ namespace Spring.Util
Assert.IsTrue(CollectionUtils.Contains(list, null));
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void ContainsCollectionDoesNotImplementContains()
public void ContainsCollectionThatDoesNotImplementContains()
{
NoContainsNoAddCollection noAddCollection = new NoContainsNoAddCollection();
NoContainsNoAddCollection noAddCollection = new NoContainsNoAddCollection();
CollectionUtils.Contains(noAddCollection, new object());
}
[Test]
public void ContainsValidElement()
{

View File

@@ -39,22 +39,6 @@ namespace Spring.Data.NHibernate.Support
[TestFixture]
public class ConfigSectionSessionScopeSettingsTests
{
private class NameValueCollectionVariableSource : IVariableSource
{
private readonly NameValueCollection nameValuePairs = new NameValueCollection();
public NameValueCollectionVariableSource Add(string key, string value)
{
nameValuePairs.Add(key, value);
return this;
}
public string ResolveVariable(string name)
{
return nameValuePairs[name];
}
}
[Test]
public void CanCreateWithDefaults()
{
@@ -104,7 +88,7 @@ namespace Spring.Data.NHibernate.Support
// simulate config section
string thisTypeName = this.GetType().FullName;
NameValueCollectionVariableSource variableSource = new NameValueCollectionVariableSource()
DictionaryVariableSource variableSource = new DictionaryVariableSource()
.Add(thisTypeName + ".SessionFactoryObjectName", SESSIONFACTORY_OBJECTNAME)
.Add(thisTypeName + ".EntityInterceptorObjectName", ENTITYINTERCEPTOR_OBJECTNAME)
.Add(thisTypeName + ".SingleSession", expectedSingleSession.ToString().ToLower() ) // case insensitive!