From 5478adffb4ef4288a63c95f4d675c5f0b70d4cbc Mon Sep 17 00:00:00 2001 From: sbohlen Date: Mon, 26 Jul 2010 15:06:09 +0000 Subject: [PATCH] SPRNET-1297 -implement IEnumerable in DictionaryVariableSource to support .NET 3.5 inline initializer syntax for dictionaries --- .../Config/DictionaryVariableSource.cs | 35 ++++---- .../Config/DictionaryVariableSourceTests.cs | 81 ++++++++++++------- 2 files changed, 72 insertions(+), 44 deletions(-) diff --git a/src/Spring/Spring.Core/Objects/Factory/Config/DictionaryVariableSource.cs b/src/Spring/Spring.Core/Objects/Factory/Config/DictionaryVariableSource.cs index b8054f02..5dd67553 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Config/DictionaryVariableSource.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Config/DictionaryVariableSource.cs @@ -1,19 +1,19 @@ #region License -/* - * Copyright 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. - * 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. +/* + * Copyright 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. + * 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 @@ -28,7 +28,7 @@ namespace Spring.Objects.Factory.Config /// A very simple, hashtable-based implementation of /// /// Erich Eichinger - public class DictionaryVariableSource : IVariableSource + public class DictionaryVariableSource : IVariableSource, IEnumerable { private readonly Hashtable variables; @@ -146,5 +146,10 @@ namespace Spring.Objects.Factory.Config } return (string)variables[name]; } + + public IEnumerator GetEnumerator() + { + return (variables as IEnumerable).GetEnumerator() ; + } } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Config/DictionaryVariableSourceTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Config/DictionaryVariableSourceTests.cs index 51ee22d1..b9d72439 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/Config/DictionaryVariableSourceTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Config/DictionaryVariableSourceTests.cs @@ -9,14 +9,36 @@ namespace Spring.Objects.Factory.Config public class DictionaryVariableSourceTests { [Test] - public void Initialize_WithCaseSensitiveFlag_AddsCaseSensitiveKeys() + public void CanEnumerateDictionary() { - DictionaryVariableSource dvs = new DictionaryVariableSource(false); - dvs.Add("key1", "lowercasevalue"); - dvs.Add("KEY1", "uppercasevalue"); + DictionaryVariableSource dvs = new DictionaryVariableSource(); - Assert.AreEqual("lowercasevalue", dvs.ResolveVariable("key1")); - Assert.AreEqual("uppercasevalue", dvs.ResolveVariable("KEY1")); + dvs.Add("key1", "theValue"); + dvs.Add("key2", "theValue"); + + foreach (DictionaryEntry dv in dvs) + { + Assert.AreEqual("theValue", dv.Value); + } + + } + + [Test] + public void CanResolveVariable_RespectsCaseInsensitivity() + { + DictionaryVariableSource caseInsensitive = new DictionaryVariableSource(); + caseInsensitive.Add("key1", "value1"); + + Assert.True(caseInsensitive.CanResolveVariable("KEY1")); + } + + [Test] + public void CanResolveVariable_RespectsCaseSensitivity() + { + DictionaryVariableSource caseSensitive = new DictionaryVariableSource(false); + caseSensitive.Add("KEY1", "value1"); + + Assert.False(caseSensitive.CanResolveVariable("key1")); } [Test] @@ -34,6 +56,16 @@ namespace Spring.Objects.Factory.Config Assert.Throws(() => new DictionaryVariableSource(new string[] { "key1", "value1", "key2", "value2", "orphanedKey1" })); } + [Test] + public void Initialize_WithCaseSensitiveFlag_AddsCaseSensitiveKeys() + { + DictionaryVariableSource dvs = new DictionaryVariableSource(false); + dvs.Add("key1", "lowercasevalue"); + dvs.Add("KEY1", "uppercasevalue"); + + Assert.AreEqual("lowercasevalue", dvs.ResolveVariable("key1")); + Assert.AreEqual("uppercasevalue", dvs.ResolveVariable("KEY1")); + } [Test] public void Initialize_WithDictionaryConstructor_AddsCaseInsensitiveKeys() @@ -80,33 +112,15 @@ namespace Spring.Objects.Factory.Config } [Test] - public void Test() + public void Initialize_WithInlineDictionarySyntax() { - Hashtable hashtable = new Hashtable(); - hashtable.Add("KEY1", "value1"); + DictionaryVariableSource dvs = new DictionaryVariableSource() { { "key1", "value1" }, { "key2", "value2" } }; + + Assert.AreEqual("value1", dvs.ResolveVariable("key1")); + Assert.AreEqual("value2", dvs.ResolveVariable("key2")); - Assert.False(hashtable.ContainsKey("key1")); } - [Test] - public void CanResolveVariable_RespectsCaseSensitivity() - { - DictionaryVariableSource caseSensitive = new DictionaryVariableSource(false); - caseSensitive.Add("KEY1", "value1"); - - Assert.False(caseSensitive.CanResolveVariable("key1")); - } - - [Test] - public void CanResolveVariable_RespectsCaseInsensitivity() - { - DictionaryVariableSource caseInsensitive = new DictionaryVariableSource(); - caseInsensitive.Add("key1", "value1"); - - Assert.True(caseInsensitive.CanResolveVariable("KEY1")); - } - - [Test] public void Requesting_KeyNotFound_ThrowsException() { @@ -116,5 +130,14 @@ namespace Spring.Objects.Factory.Config Assert.Throws(() => dvs.ResolveVariable("key-not-found")); } + [Test] + public void Test() + { + Hashtable hashtable = new Hashtable(); + hashtable.Add("KEY1", "value1"); + + Assert.False(hashtable.ContainsKey("key1")); + } + } }