fixed SPRNET-978
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using System.Runtime.Serialization;
|
||||
using Spring.Util;
|
||||
|
||||
namespace Spring.Collections
|
||||
@@ -8,6 +10,7 @@ namespace Spring.Collections
|
||||
/// Provides a performance improved hashtable with case-insensitive (string-only! based) key handling.
|
||||
/// </summary>
|
||||
/// <author>Erich Eichinger</author>
|
||||
[Serializable]
|
||||
public class CaseInsensitiveHashtable : Hashtable
|
||||
{
|
||||
private readonly TextInfo _textInfo;
|
||||
@@ -58,6 +61,35 @@ namespace Spring.Collections
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable"></see> class that is serializable using the specified <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> and <see cref="T:System.Runtime.Serialization.StreamingContext"></see> objects.
|
||||
/// </summary>
|
||||
/// <param name="context">A <see cref="T:System.Runtime.Serialization.StreamingContext"></see> object containing the source and destination of the serialized stream associated with the <see cref="T:System.Collections.Hashtable"></see>. </param>
|
||||
/// <param name="info">A <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> object containing the information required to serialize the <see cref="T:System.Collections.Hashtable"></see> object.</param>
|
||||
/// <exception cref="T:System.ArgumentNullException">info is null. </exception>
|
||||
protected CaseInsensitiveHashtable(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
_textInfo = (TextInfo) info.GetValue("_textInfo", typeof(TextInfo));
|
||||
_compareInfo = (CompareInfo) info.GetValue("_compareInfo", typeof(CompareInfo));
|
||||
|
||||
AssertUtils.ArgumentNotNull(_textInfo, "TextInfo");
|
||||
AssertUtils.ArgumentNotNull(_compareInfo, "CompareInfo");
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///Implements the <see cref="ISerializable"></see> interface and returns the data needed to serialize the <see cref="CaseInsensitiveHashtable"></see>.
|
||||
///</summary>
|
||||
///
|
||||
///<param name="context">A <see cref="StreamingContext"></see> object containing the source and destination of the serialized stream associated with the <see cref="CaseInsensitiveHashtable"></see>. </param>
|
||||
///<param name="info">A <see cref="SerializationInfo"></see> object containing the information required to serialize the <see cref="CaseInsensitiveHashtable"></see>. </param>
|
||||
///<exception cref="System.ArgumentNullException">info is null. </exception>
|
||||
public override void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
base.GetObjectData(info, context);
|
||||
info.AddValue("_textInfo", _textInfo);
|
||||
info.AddValue("_compareInfo", _compareInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the hashcode of the given string key, using the configured culture.
|
||||
/// </summary>
|
||||
|
||||
@@ -24,6 +24,8 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using NUnit.Framework;
|
||||
|
||||
#endregion
|
||||
@@ -37,6 +39,36 @@ namespace Spring.Collections
|
||||
[TestFixture]
|
||||
public class CaseInsensitiveHashtableTests
|
||||
{
|
||||
private static object SerializeDeserializeObject(object exp)
|
||||
{
|
||||
byte[] data;
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
formatter.Serialize(ms, exp);
|
||||
ms.Flush();
|
||||
data = ms.ToArray();
|
||||
}
|
||||
|
||||
using (MemoryStream ms = new MemoryStream(data))
|
||||
{
|
||||
exp = formatter.Deserialize(ms);
|
||||
}
|
||||
|
||||
return exp;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsSerializable()
|
||||
{
|
||||
CaseInsensitiveHashtable storiginal = new CaseInsensitiveHashtable();
|
||||
storiginal.Add("key", "value");
|
||||
CaseInsensitiveHashtable st = (CaseInsensitiveHashtable)SerializeDeserializeObject(storiginal);
|
||||
Assert.AreNotSame(storiginal, st);
|
||||
Assert.AreEqual("value", st["KEY"]);
|
||||
Assert.AreEqual(1, st.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoresCase()
|
||||
{
|
||||
@@ -53,18 +85,18 @@ namespace Spring.Collections
|
||||
Assert.Fail();
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{}
|
||||
{ }
|
||||
|
||||
Hashtable ht = new Hashtable();
|
||||
ht.Add("key","value");
|
||||
ht.Add("KEY","value");
|
||||
ht.Add("key", "value");
|
||||
ht.Add("KEY", "value");
|
||||
try
|
||||
{
|
||||
st = new CaseInsensitiveHashtable(ht, CultureInfo.InvariantCulture);
|
||||
Assert.Fail();
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{}
|
||||
{ }
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -93,7 +125,7 @@ namespace Spring.Collections
|
||||
StopWatch watch = new StopWatch();
|
||||
|
||||
Hashtable ht = CollectionsUtil.CreateCaseInsensitiveHashtable();
|
||||
for(int i=0;i<1000000;i++) ht.Add(Guid.NewGuid().ToString(), "val"); // gen. higher number of elements results in OOM exception????
|
||||
for (int i = 0; i < 1000000; i++) ht.Add(Guid.NewGuid().ToString(), "val"); // gen. higher number of elements results in OOM exception????
|
||||
CaseInsensitiveHashtable ciht = new CaseInsensitiveHashtable(ht, CultureInfo.InvariantCulture);
|
||||
|
||||
using (watch.Start("Duration: {0}"))
|
||||
@@ -102,7 +134,7 @@ namespace Spring.Collections
|
||||
{
|
||||
object v = ht["somekey"];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using (watch.Start("Duration: {0}"))
|
||||
{
|
||||
@@ -110,7 +142,7 @@ namespace Spring.Collections
|
||||
{
|
||||
object v = ciht["somekey"];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user