From 768f26559b75b3ec1e7dbe09e1df992738c6c44a Mon Sep 17 00:00:00 2001 From: bbaia Date: Mon, 4 Apr 2011 10:59:56 +0000 Subject: [PATCH] Create ConfigurableVariableSource to allow inline variables definitions with VariablePlaceholderConfigurer (SPRNET-1426) --- doc/reference/src/objects.xml | 4 + .../Config/ConfigurableVariableSource.cs | 85 +++++++++++++++++++ .../Spring.Core/Spring.Core.2002.csproj | 5 ++ .../Spring.Core/Spring.Core.2003.csproj | 5 ++ .../Spring.Core/Spring.Core.2005.csproj | 1 + .../Spring.Core/Spring.Core.2008.csproj | 1 + .../Spring.Core/Spring.Core.2010.csproj | 1 + .../Config/ConfigurableVariableSourceTests.cs | 51 +++++++++++ .../Spring.Core.Tests.2002.csproj | 5 ++ .../Spring.Core.Tests.2003.csproj | 5 ++ .../Spring.Core.Tests.2005.csproj | 1 + .../Spring.Core.Tests.2008.csproj | 1 + .../Spring.Core.Tests.2010.csproj | 1 + 13 files changed, 166 insertions(+) create mode 100644 src/Spring/Spring.Core/Objects/Factory/Config/ConfigurableVariableSource.cs create mode 100644 test/Spring/Spring.Core.Tests/Objects/Factory/Config/ConfigurableVariableSourceTests.cs diff --git a/doc/reference/src/objects.xml b/doc/reference/src/objects.xml index 9bb0aa32..5a250dfd 100644 --- a/doc/reference/src/objects.xml +++ b/doc/reference/src/objects.xml @@ -5006,6 +5006,10 @@ cfg.PostProcessObjectFactory(factory); ConnectionStringsVariableSource + + + ConfigurableVariableSource + You use this by defining an instance of diff --git a/src/Spring/Spring.Core/Objects/Factory/Config/ConfigurableVariableSource.cs b/src/Spring/Spring.Core/Objects/Factory/Config/ConfigurableVariableSource.cs new file mode 100644 index 00000000..9605c88c --- /dev/null +++ b/src/Spring/Spring.Core/Objects/Factory/Config/ConfigurableVariableSource.cs @@ -0,0 +1,85 @@ +#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.Globalization; +using System.Collections; +using System.Collections.Specialized; + +using Spring.Util; + +namespace Spring.Objects.Factory.Config +{ + /// + /// Implementation of that + /// resolves variable name against provided variables. + /// + /// + /// Variable name resolution is case insensitive. + /// + /// Bruno Baia + [Serializable] + public class ConfigurableVariableSource : IVariableSource + { + protected NameValueCollection _variables; + + /// + /// Initializes a new instance of . + /// + public ConfigurableVariableSource() + { + this._variables = new NameValueCollection(); + } + + /// + /// Gets or sets variables. + /// + public NameValueCollection Variables + { + get { return _variables; } + set { _variables = value; } + } + + /// + /// Before requesting a variable resolution, a client should + /// ask, whether the source can resolve a particular variable name. + /// + /// the name of the variable to resolve + /// true if the variable can be resolved, false otherwise + public bool CanResolveVariable(string name) + { + return this._variables.Get(name) != null; + } + + /// + /// Resolves variable value for the specified variable name. + /// + /// + /// The name of the variable to resolve. + /// + /// + /// The variable value if able to resolve, null otherwise. + /// + public string ResolveVariable(string name) + { + return this._variables.Get(name); + } + } +} \ No newline at end of file diff --git a/src/Spring/Spring.Core/Spring.Core.2002.csproj b/src/Spring/Spring.Core/Spring.Core.2002.csproj index bf15f6e5..ac237d0a 100644 --- a/src/Spring/Spring.Core/Spring.Core.2002.csproj +++ b/src/Spring/Spring.Core/Spring.Core.2002.csproj @@ -1456,6 +1456,11 @@ SubType = "Code" BuildAction = "Compile" /> + + + diff --git a/src/Spring/Spring.Core/Spring.Core.2008.csproj b/src/Spring/Spring.Core/Spring.Core.2008.csproj index e1dd0fdd..d5fd05f7 100644 --- a/src/Spring/Spring.Core/Spring.Core.2008.csproj +++ b/src/Spring/Spring.Core/Spring.Core.2008.csproj @@ -668,6 +668,7 @@ + diff --git a/src/Spring/Spring.Core/Spring.Core.2010.csproj b/src/Spring/Spring.Core/Spring.Core.2010.csproj index dd761999..905cd759 100644 --- a/src/Spring/Spring.Core/Spring.Core.2010.csproj +++ b/src/Spring/Spring.Core/Spring.Core.2010.csproj @@ -681,6 +681,7 @@ + diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Config/ConfigurableVariableSourceTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Config/ConfigurableVariableSourceTests.cs new file mode 100644 index 00000000..0016edfc --- /dev/null +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Config/ConfigurableVariableSourceTests.cs @@ -0,0 +1,51 @@ +#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.Core.IO; + +namespace Spring.Objects.Factory.Config +{ + /// + /// Unit tests for the ConfigurableVariableSource class. + /// + /// Bruno Baia + [TestFixture] + public sealed class ConfigurableVariableSourceTests + { + [Test] + public void Basic() + { + ConfigurableVariableSource vs = new ConfigurableVariableSource(); + vs.Variables.Add("Key1", "Value1"); + vs.Variables.Add("Key2", "Value2"); + + // existing vars + Assert.IsTrue(vs.CanResolveVariable("key1")); // case insensitive + Assert.AreEqual("Value1", vs.ResolveVariable("key1")); // case insensitive + Assert.IsTrue(vs.CanResolveVariable("Key2")); + Assert.AreEqual("Value2", vs.ResolveVariable("Key2")); + + // non-existant variable + Assert.IsFalse(vs.CanResolveVariable("Key3")); + Assert.IsNull(vs.ResolveVariable("Key3")); + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2002.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2002.csproj index f93a4361..7f1c3f04 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2002.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2002.csproj @@ -1132,6 +1132,11 @@ SubType = "Code" BuildAction = "Compile" /> + + + diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj index 6e7dcfb0..d3676146 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2008.csproj @@ -320,6 +320,7 @@ + diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj index 9dc2dcc5..c3e07eac 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj @@ -329,6 +329,7 @@ +