Click here to Skip to main content
15,895,772 members
Articles / Programming Languages / C#

Writing NHibernate Level 2 Cache Usage Tests

Rate me:
Please Sign up or sign in to vote.
4.69/5 (7 votes)
15 Jan 2009CPOL8 min read 53K   121   22  
Writing NHibernate Level 2 caching related tests by utilizing a custom log4net appender.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using log4net.Core;
using log4net.Appender;
using log4net.Repository;

namespace Tests
{
  public delegate void CacheLogAppendDelegate(CachedEntityInfo cacheInfo);

  /// <summary>
  /// Log4Net appender implementation
  /// </summary>
  public class CacheLogAppender : AppenderSkeleton
  {
    /// <summary>
    /// Cache action enumeration.
    /// </summary>
    public enum CacheActionType
    {
      Unknow,
      Invalidate,
      Release,
      Caching,
      Cached,
      CacheLookup,
      CacheMiss,
      CacheHit,
      Locked,
      Inserting,
      Inserted
    }

    private CacheLogAppendDelegate _onLogAppend;
    public event CacheLogAppendDelegate OnLogAppend
    {
      add { _onLogAppend += value; }
      remove { _onLogAppend -= value; }
    }

    /// <summary>
    /// Parse CacheActionType from name
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static CacheActionType ParseCacheActionTypeFromName(string name)
    {
      string invariantName = name.ToLowerInvariant();
      switch (invariantName)
      {
        case "invalidating":
          return CacheActionType.Invalidate;
        case "releasing":
          return CacheActionType.Release;
        case "caching":
          return CacheActionType.Caching;
        case "cached":
          return CacheActionType.Cached;
        case "cache lookup":
          return CacheActionType.CacheLookup;
        case "cache miss":
          return CacheActionType.CacheMiss;
        case "cache hit":
          return CacheActionType.CacheHit;
        case "cached item was locked":
          return CacheActionType.Locked;
        case "inserting":
          return CacheActionType.Inserting;
        case "inserted":
          return CacheActionType.Inserted;
        default:
          return CacheActionType.Unknow;
      }
    }

    /// <summary>
    /// Append log
    /// </summary>
    /// <param name="loggingEvent"></param>
    protected override void Append(LoggingEvent loggingEvent)
    {
      if (loggingEvent.MessageObject == null)
        return;

      CachedEntityInfo cachedEntity = ParseMessageObject(loggingEvent.MessageObject);
      if(cachedEntity != null && _onLogAppend != null)
        _onLogAppend(cachedEntity);
    }

    /// <summary>
    /// Append logs
    /// </summary>
    /// <param name="loggingEvents"></param>
    protected override void Append(LoggingEvent[] loggingEvents)
    {
      base.Append(loggingEvents);
    }

    /// <summary>
    /// Parse message object to produce a CachedEntityInfo instance.
    /// </summary>
    /// <param name="messageObject"></param>
    /// <returns></returns>
    private CachedEntityInfo ParseMessageObject(object messageObject)
    {
      if (messageObject == null)
        throw new NullReferenceException("Message object is null. Can not parse log message object.");

      string logMessage = messageObject.ToString();

      Match m = Regex.Match(logMessage, @"(?<ActionName>.*)\s*:\s*(?<TypeName>.*)\s*\#\s*(?<Id>.*)", RegexOptions.IgnoreCase);
      if (!m.Success)
        throw new Exception("Log message does not match the pattern!");

      string actionName = m.Groups["ActionName"].Value;
      string typeName = m.Groups["TypeName"].Value;
      string id = m.Groups["Id"].Value;

      return new CachedEntityInfo(String.IsNullOrEmpty(typeName) ? typeName : typeName.Trim()
        , String.IsNullOrEmpty(id) ? id : id.Trim()
        , String.IsNullOrEmpty(actionName) ? actionName : actionName.Trim());
    }
  }


  
 

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader PragmaTouch
Turkey Turkey
- Software developer
- Has BS degree in Computer Engineering
- Has MBA degree
- Programmed with C, C++, Delphi, T-SQL and recently C#
- Little educational experience with Prolog
- Feel enthusiasm about NHibernate and LINQ
- Love to develop on Cuyahoga Web Framework
- Developer of PragmaSQL Editor
(Code Project Members Choice Winner for 2009 and 2010)
- Developed JiraTouch and MoodleTouch for iPhone
- PragmaTouch Lead (www.pragmatouch.com)

Comments and Discussions