Click here to Skip to main content
12,296,188 members (63,366 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

170.9K views
2.4K downloads
197 bookmarked
Posted

Advanced Unit Testing, Part II - Core Implementation

, 22 Sep 2003
This article illustrates how a unit test automation framework is implemented and continues the case study developed in Part I.
UnitTest.suo
Lib
bin
Debug
Lib.dll
Lib.pdb
KALib.csproj.user
obj
Debug
KALib.projdata
Lib.dll
Lib.pdb
Lib.projdata
temp
TempPE
UnitTest
bin
Debug
UnitTest.dll
UnitTest.pdb
obj
Debug
temp
TempPE
UnitTest.dll
UnitTest.pdb
UnitTest.projdata
UnitTest.csproj.user
UTCore
bin
Debug
Lib.dll
Lib.pdb
UnitTest.dll
UnitTest.pdb
UTCore.dll
UTCore.pdb
obj
Debug
temp
TempPE
UTCore.dll
UTCore.pdb
UTCore.projdata
UTCore.csproj.user
UTExample
bin
Debug
UnitTest.dll
UnitTest.pdb
UTExample.dll
UTExample.pdb
obj
Debug
temp
TempPE
UTExample.dll
UTExample.pdb
UTExample.projdata
Stubs
UTExample.csproj.user
UTWinForm
App.ico
bin
Debug
Fail.ico
Ignore.ico
ka.ico
Lib.dll
Lib.pdb
Pass.ico
UnitTest.dll
UnitTest.pdb
Untested.ico
UTCore.dll
UTCore.pdb
UTExample.dll
UTExample.pdb
UTWinForm.exe
UTWinForm.pdb
obj
Debug
temp
TempPE
UTWinForm.exe
UTWinForm.Form1.resources
UTWinForm.pdb
UTWinForm.projdata
UTWinForm.csproj.user
using System;
using System.Xml;
using System.IO;
using System.Threading;

/*
	code originally from: http://www.codeproject.com/useritems/XmlRegistry.asp by Nadeem Ghias
		removed lazy write feature
		changed key creation from an indexer to a method
		some other minor cleanup
		changed XmlRegistryKey class to handle value as a get accessor
		removed extra unecessary constructors
		added default values for getters
*/	


namespace KALib
{
	public class XmlRegistryKey
	{
		private XmlElement Elem;
		private XmlRegistry Reg;

		public string Value
		{
			get
			{
				return Elem.Name;
			}
		}

		internal XmlRegistryKey(XmlElement e, XmlRegistry r)
		{
			Elem = e;
			Reg = r;
		}

		public void SetValue(string name, object val)
		{
			XmlAttribute at = Elem.Attributes[name];
			if (at == null)
			{
				Elem.Attributes.Append(at = Elem.OwnerDocument.CreateAttribute(name));
			}
			at.Value = val.ToString();
			Reg.Save();
		}

		public bool GetBooleanValue(string name)
		{
			return GetBooleanValue(name, false);
		}

		public bool GetBooleanValue(string name, bool defaultval)
		{
			XmlAttribute at = Elem.Attributes[name];
			bool ret=defaultval;
			if (at != null)
			{
				string val = at.Value.ToUpper();
				ret= val == "YES" || val == "TRUE";
			}
			return ret;
		}

		public string GetStringValue(string name)
		{
			return GetStringValue(name, "");
		}

		public string GetStringValue(string name, string defaultval)
		{
			XmlAttribute at = Elem.Attributes[name];
			string ret=defaultval;
			if (at != null)
			{
				ret=at.Value;
			}
			return ret;
		}

		public int GetIntValue(string name)
		{
			return GetIntValue(name, 0);
		}

		public int GetIntValue(string name, int defaultval)
		{
			XmlAttribute at = Elem.Attributes[name];
			int ret=defaultval;
			if (at != null)
			{
				ret= int.Parse(at.Value);
			}
			return ret;
		}


		public XmlRegistryKey[] GetSubKeys()
		{
			int keycount = Elem.ChildNodes.Count;
			XmlRegistryKey[] keys = new XmlRegistryKey[keycount];
			for (int i=0; i < keycount; i++)
			{
				keys[i] = new XmlRegistryKey((XmlElement)Elem.ChildNodes[i], Reg);
			}
			return keys;
		}


		public XmlRegistryKey GetSubKey(string path, bool createpath)
		{
			XmlElement e = Elem, parent = null;
			for (int len, start=0; start < path.Length; start += len+1)
			{
				len = path.IndexOf('/', start);
				if (len == -1)
				{
					len = path.Length;
				}
				len -= start;
				string node = path.Substring(start, len);
				parent = e;
				e = e[node];
				if (e == null)
				{
					if (createpath)
					{
						parent.AppendChild(e = Elem.OwnerDocument.CreateElement(node));
					}
					else
					{
						return null;
					}
				}
			}
			return new XmlRegistryKey(e, Reg);
		}
	}

	public class XmlRegistry
	{
		private FileInfo Fileinfo;
		private XmlDocument Doc = new XmlDocument();
		private const string MsgPrefix = "XmlRegistry:";

		public XmlRegistryKey RootKey
		{
			get
			{
				return new XmlRegistryKey(Doc.DocumentElement, this);
			}
		}

		public XmlRegistry(string filename) : this(filename, null, null)
		{
		}

		public XmlRegistry(string filename, string rootkeyname, string encoding)
		{
			Fileinfo = new FileInfo(filename);
			if (Fileinfo.Exists)
			{
				Doc.Load(Fileinfo.FullName);
				if (rootkeyname != null && Doc.DocumentElement.Name != rootkeyname)
				{
					string msg = string.Format("{0} Specified root node name '{1}' does not match root name '{2}' in loaded document.", MsgPrefix, rootkeyname, Doc.DocumentElement.Name);
					throw new Exception(msg);
				}
			}
			else
			{
				if (rootkeyname == null)
				{
					rootkeyname = "XmlRegistryRoot";
				}
				XmlDeclaration dec = Doc.CreateXmlDeclaration("1.0", encoding, null);
				Doc.AppendChild(dec);
				XmlElement root = Doc.CreateElement(rootkeyname);
				Doc.AppendChild(root);
			}
		}

		public void Save()
		{
			Doc.Save(Fileinfo.FullName);
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marc Clifton
United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.

Marc lives in Philmont, NY.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 23 Sep 2003
Article Copyright 2003 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid