You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ibatis.apache.org by gb...@apache.org on 2005/07/23 15:30:47 UTC
svn commit: r224463 - in /ibatis/trunk/cs/mapper:
IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs
IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs
Author: gbayon
Date: Sat Jul 23 06:30:36 2005
New Revision: 224463
URL: http://svn.apache.org/viewcvs?rev=224463&view=rev
Log:
- Added Nunit test for : IBATISNET-97 + more
Modified:
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs
Modified: ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs
URL: http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs?rev=224463&r1=224462&r2=224463&view=diff
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs (original)
+++ ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs Sat Jul 23 06:30:36 2005
@@ -1,18 +1,15 @@
+
+//<-- To access the definition of the deleagte RowDelegate
using System;
using System.Collections;
using System.Threading;
-using System.Configuration;
-
-using NUnit.Framework;
-
using IBatisNet.Common;
using IBatisNet.Common.Utilities;
-using IBatisNet.DataMapper; //<-- To access the definition of the deleagte RowDelegate
+using IBatisNet.DataMapper.Configuration.Cache;
using IBatisNet.DataMapper.MappedStatements;
-
-using IBatisNet.DataMapper.Test;
using IBatisNet.DataMapper.Test.Domain;
+using NUnit.Framework;
namespace IBatisNet.DataMapper.Test.NUnit.SqlMapTests
{
@@ -65,14 +62,14 @@
{
IList list = sqlMap.QueryForList("GetCachedAccountsViaResultMap", null);
- int firstId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int firstId = HashCodeProvider.GetIdentityHashCode(list);
//System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(list);
list = sqlMap.QueryForList("GetCachedAccountsViaResultMap", null);
//Console.WriteLine(sqlMap.GetDataCacheStats());
- int secondId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int secondId = HashCodeProvider.GetIdentityHashCode(list);
//System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(list);
Assert.AreEqual(firstId, secondId);
@@ -83,13 +80,14 @@
list = sqlMap.QueryForList("GetCachedAccountsViaResultMap", null);
- int thirdId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int thirdId = HashCodeProvider.GetIdentityHashCode(list);
Assert.IsTrue(firstId != thirdId);
//Console.WriteLine(sqlMap.GetDataCacheStats());
}
+
/// <summary>
/// Test flush Cache
/// </summary>
@@ -98,11 +96,11 @@
{
IList list = sqlMap.QueryForList("GetCachedAccountsViaResultMap", null);
- int firstId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int firstId = HashCodeProvider.GetIdentityHashCode(list);
list = sqlMap.QueryForList("GetCachedAccountsViaResultMap", null);
- int secondId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int secondId = HashCodeProvider.GetIdentityHashCode(list);
Assert.AreEqual(firstId, secondId);
@@ -110,7 +108,7 @@
list = sqlMap.QueryForList("GetCachedAccountsViaResultMap", null);
- int thirdId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int thirdId = HashCodeProvider.GetIdentityHashCode(list);
Assert.IsTrue(firstId != thirdId);
}
@@ -141,13 +139,91 @@
list = sqlMap.QueryForList("GetCachedAccountsViaResultMap", null);
- int thirdId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int thirdId = HashCodeProvider.GetIdentityHashCode(list);
Assert.IsTrue(firstId != thirdId);
}
+ /// <summary>
+ /// Test Cache Null Object
+ /// </summary>
+ [Test]
+ public void TestCacheNullObject()
+ {
+ CacheModel cache = GetCacheModel();
+ cache["testKey"] = null;
+
+ object returnedObject = cache["testKey"];
+ Assert.IsNull(returnedObject);
+ }
+
+
+ /// <summary>
+ /// Test CacheHit
+ /// </summary>
+ [Test]
+ public void TestCacheHit()
+ {
+ CacheModel cache = GetCacheModel();
+ string value = "testValue";
+ cache["testKey"] = value;
+
+ object returnedObject = cache["testKey"];
+ Assert.AreEqual(value, returnedObject);
+ Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), HashCodeProvider.GetIdentityHashCode(returnedObject));
+ Assert.AreEqual(1, cache.HitRatio);
+ }
+
+ /// <summary>
+ /// Test CacheMiss
+ /// </summary>
+ [Test]
+ public void TestCacheMiss()
+ {
+ CacheModel cache = GetCacheModel();
+ string value = "testValue";
+ cache["testKey"] = value;
+
+ object returnedObject = cache["wrongKey"];
+ Assert.IsTrue(!value.Equals(returnedObject));
+ Assert.IsNull(returnedObject) ;
+ Assert.AreEqual(0, cache.HitRatio);
+ }
+
+ /// <summary>
+ /// Test CacheHitMiss
+ /// </summary>
+ [Test]
+ public void TestCacheHitMiss()
+ {
+ CacheModel cache = GetCacheModel();
+ string value = "testValue";
+ cache["testKey"] = value;
+
+ object returnedObject = cache["testKey"];
+ Assert.AreEqual(value, returnedObject);
+ Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), HashCodeProvider.GetIdentityHashCode(returnedObject));
+
+ returnedObject = cache["wrongKey"];
+ Assert.IsTrue(!value.Equals(returnedObject));
+ Assert.IsNull(returnedObject) ;
+ Assert.AreEqual(0.5, cache.HitRatio);
+ }
+
+
+ private CacheModel GetCacheModel()
+ {
+ CacheModel cache = new CacheModel();
+ cache.FlushInterval = new FlushInterval();
+ cache.FlushInterval.Minutes = 5;
+ cache.Implementation = "LRU";
+ cache.Initialize();
+
+ return cache;
+ }
+
#endregion
@@ -168,15 +244,15 @@
{
try
{
- MappedStatement statement = sqlMap.GetMappedStatement( _statementName );
+ MappedStatement statement = _sqlMap.GetMappedStatement( _statementName );
IDalSession session = new SqlMapSession(sqlMap.DataSource);
session.OpenConnection();
IList list = statement.ExecuteQueryForList(session, null);
- int firstId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ //int firstId = HashCodeProvider.GetIdentityHashCode(list);
list = statement.ExecuteQueryForList(session, null);
- int secondId = IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(list);
+ int secondId = HashCodeProvider.GetIdentityHashCode(list);
_results.Add("id", secondId );
_results.Add("list", list);
Modified: ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs
URL: http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs?rev=224463&r1=224462&r2=224463&view=diff
==============================================================================
--- ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs (original)
+++ ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs Sat Jul 23 06:30:36 2005
@@ -25,16 +25,18 @@
#endregion
#region Using
+
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Xml.Serialization;
-
using IBatisNet.Common.Exceptions;
using IBatisNet.Common.Logging;
+using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper.Exceptions;
using IBatisNet.DataMapper.MappedStatements;
+
#endregion
namespace IBatisNet.DataMapper.Configuration.Cache
@@ -50,7 +52,7 @@
#region Fields
[NonSerialized]
- private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
+ private static readonly ILog _logger = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );
/// <summary>
/// Constant to turn off periodic cache flushes
@@ -138,9 +140,9 @@
/// </summary>
static CacheModel()
{
- _cacheControllerAliases.Add("MEMORY","IBatisNet.DataMapper.Configuration.Cache.Memory.MemoryCacheControler");
- _cacheControllerAliases.Add("LRU","IBatisNet.DataMapper.Configuration.Cache.Lru.LruCacheController");
- _cacheControllerAliases.Add("FIFO","IBatisNet.DataMapper.Configuration.Cache.Fifo.FifoCacheController");
+ _cacheControllerAliases.Add("MEMORY","IBatisNet.DataMapper.Configuration.Cache.Memory.MemoryCacheControler, IBatisNet.DataMapper");
+ _cacheControllerAliases.Add("LRU","IBatisNet.DataMapper.Configuration.Cache.Lru.LruCacheController, IBatisNet.DataMapper");
+ _cacheControllerAliases.Add("FIFO","IBatisNet.DataMapper.Configuration.Cache.Fifo.FifoCacheController, IBatisNet.DataMapper");
}
/// <summary>
@@ -148,7 +150,7 @@
/// </summary>
public CacheModel()
{
- _lastFlush = System.DateTime.Now.Ticks;
+ _lastFlush = DateTime.Now.Ticks;
}
#endregion
@@ -162,9 +164,6 @@
_flushInterval.Initialize();
// Initialize controller
- Assembly assembly = null;
- Type type = null;
-
_implementation = _cacheControllerAliases[_implementation.ToUpper()] as string;
try
@@ -174,10 +173,8 @@
throw new DataMapperException ("Error instantiating cache controller for cache named '"+_id+"'. Cause: The class for name '"+_implementation+"' could not be found.");
}
- assembly = Assembly.GetCallingAssembly();
-
// Build the CacheController
- type = assembly.GetType(_implementation, true);
+ Type type = Resources.TypeForName(_implementation);
object[] arguments = new object[0];
_controller = (ICacheController)Activator.CreateInstance(type, arguments);
@@ -234,7 +231,7 @@
/// </summary>
public void Flush()
{
- _lastFlush = System.DateTime.Now.Ticks;
+ _lastFlush = DateTime.Now.Ticks;
_controller.Flush();
}
@@ -255,7 +252,7 @@
lock(this)
{
if (_lastFlush != NO_FLUSH_INTERVAL
- && (System.DateTime.Now.Ticks - _lastFlush > _flushInterval.Interval))
+ && (DateTime.Now.Ticks - _lastFlush > _flushInterval.Interval))
{
Flush();
}