SPRNET-1297

-implement IEnumerable in DictionaryVariableSource to support .NET 3.5 inline initializer syntax for dictionaries
This commit is contained in:
sbohlen
2010-07-26 15:06:09 +00:00
parent 611bdbb107
commit 5478adffb4
2 changed files with 72 additions and 44 deletions

View File

@@ -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 <see cref="IVariableSource"/>
/// </summary>
/// <author>Erich Eichinger</author>
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() ;
}
}
}

View File

@@ -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<ArgumentOutOfRangeException>(() => 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<ArgumentException>(() => dvs.ResolveVariable("key-not-found"));
}
[Test]
public void Test()
{
Hashtable hashtable = new Hashtable();
hashtable.Add("KEY1", "value1");
Assert.False(hashtable.ContainsKey("key1"));
}
}
}