Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / C#

Advanced Unit Testing, Part III - Testing Processes

Rate me:
Please Sign up or sign in to vote.
4.89/5 (53 votes)
28 Sep 200314 min read 435.3K   2.6K   206  
Extend Unit Testing So That Entire Processes Can Be Tested
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


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions