Click here to Skip to main content
15,884,836 members
Articles / Desktop Programming / Windows Forms

Traceract

Rate me:
Please Sign up or sign in to vote.
3.69/5 (9 votes)
3 Sep 20059 min read 98.5K   1.6K   37  
A prototype debug tracer with an added dimension.
/*
Copyright (c) 2005, Marc Clifton
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list
  of conditions and the following disclaimer. 

* Redistributions in binary form must reproduce the above copyright notice, this 
  list of conditions and the following disclaimer in the documentation and/or other
  materials provided with the distribution. 
 
* Neither the name of MyXaml nor the names of its contributors may be
  used to endorse or promote products derived from this software without specific
  prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;

using Clifton.Tools.Xml;

using Vts.UnitTest;

namespace Clifton.Tools.Xml.UnitTests
{
	/// <summary>
	/// XmlDatabase test fixture.
	/// </summary>
	[TestFixture]
	[ProcessTest]
	public class XmlDatabaseUnitTests
	{
		XmlDatabase config;

		/// <summary>
		/// Setup the database.
		/// </summary>
		[Test, Sequence(1)]
		public void Setup()
		{
			config=new XmlDatabase("Test");
		}

		/// <summary>
		/// Do a simple record insert.
		/// </summary>
		[Test, Sequence(2)]
		public void InsertRecord()
		{
			config.Insert("TestChildNode");
		}

		/// <summary>
		/// Insert a record along with a attribute-value.
		/// </summary>
		[Test, Sequence(3)]
		public void InsertRecordWithValue()
		{
			config.Insert("TestChildNode2", "MyAttribute", "MyValue");
			string ret=config.QueryScalar("TestChildNode2", "MyAttribute");
			Assertion.Assert(ret=="MyValue", "Unexpected attribute value not set.");
		}

		/// <summary>
		/// Insert a record with multiple values.
		/// </summary>
		[Test, Sequence(4)]
		public void InsertRecordWithMultipleValues()
		{
			XmlDatabase.FieldValuePair[] fields=new XmlDatabase.FieldValuePair[]
			{
				new XmlDatabase.FieldValuePair("PK", "1"),
				new XmlDatabase.FieldValuePair("Data", "Marc")
			};
			config.Insert("TestChildNode3", fields);
		}

		/// <summary>
		/// Update the field of a record.
		/// </summary>
		[Test, Sequence(5)]
		public void UpdateField()
		{
			config.Update("TestChildNode2", "MyAttribute", "MyNewValue");
			string ret=config.QueryScalar("TestChildNode2", "MyAttribute");
			Assertion.Assert(ret=="MyNewValue", "Unexpected attribute value.");
		}

		/// <summary>
		/// Delete a field.
		/// </summary>
		[Test, Sequence(6)]
		public void DeleteField()
		{
			config.DeleteField("TestChildNode2", "MyAttribute");
		}

		/// <summary>
		/// Insert multiple unique records.
		/// </summary>
		[Test, Sequence(7)]
		public void InsertMultipleUniqueRecords()
		{
			XmlDatabase.FieldValuePair[] fields=new XmlDatabase.FieldValuePair[]
			{
				new XmlDatabase.FieldValuePair("PK", "1"),
				new XmlDatabase.FieldValuePair("Data", "Marc")
			};
			config.Insert("Child/Grandchild", fields);

			fields[0].Value="2";
			fields[1].Value="Ian";
			config.Insert("Child/Grandchild", fields);

			fields[0].Value="3";
			fields[1].Value="Karen";
			config.Insert("Child/Grandchild", fields);
		}

		/// <summary>
		/// Query the unique records.
		/// </summary>
		[Test, Sequence(8)]
		public void QueryUniqueRecords()
		{
			string data;
			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='1'");
			Assertion.Assert(data=="Marc", "Unexpected attribute value");

			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='2'");
			Assertion.Assert(data=="Ian", "Unexpected attribute value");
			
			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='3'");
			Assertion.Assert(data=="Karen", "Unexpected attribute value");
		}

		/// <summary>
		/// Delete a unique record.
		/// </summary>
		[Test, Sequence(9)]
		public void DeleteUniqueRecord()
		{
			config.Delete("Child/Grandchild", "@PK='2'");
		}

		/// <summary>
		/// Verify that a missing record query returns null.
		/// </summary>
		[Test, Sequence(10)]
		public void VerifyMissingRecordNullReturn()
		{
			string data=config.QueryScalar("Child/Grandchild", "Data", "@PK='2'");
			Assertion.Assert(data==null, "Null expected");
		}

		/// <summary>
		/// Test updating multiple record attributes.
		/// </summary>
		[Test, Sequence(11)]
		public void UpdateMultipleRows()
		{
			config.Update("Child/Grandchild", "Data", "Bob");

			string data;
			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='1'");
			Assertion.Assert(data=="Bob", "Unexpected attribute value");
			
			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='3'");
			Assertion.Assert(data=="Bob", "Unexpected attribute value");
		}

		/// <summary>
		/// Delete multiple records.
		/// </summary>
		[Test, Sequence(12)]
		public void DeleteMultipleRows()
		{
			// Insert a new record to verify only selected records are being deleted.
			XmlDatabase.FieldValuePair[] fields=new XmlDatabase.FieldValuePair[]
			{
				new XmlDatabase.FieldValuePair("PK", "2"),
				new XmlDatabase.FieldValuePair("Data", "Anders")
			};
			config.Insert("Child/Grandchild", fields);

			config.Delete("Child/Grandchild", "@Data='Bob'");

			string data;
			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='1'");
			Assertion.Assert(data==null, "Null expected");
			
			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='3'");
			Assertion.Assert(data==null, "Null expected");
			
			data=config.QueryScalar("Child/Grandchild", "Data", "@PK='2'");
			Assertion.Assert(data=="Anders", "Unexpected attribute value");
		}

		/// <summary>
		/// Update mulitple rows with multiple attribute fields.
		/// </summary>
		[Test, Sequence(13)]
		public void MultiRowMultiFieldUpdate()
		{
			XmlDatabase.FieldValuePair[] fields;
			
			fields=new XmlDatabase.FieldValuePair[]
			{
				new XmlDatabase.FieldValuePair("PK", "1"),
				new XmlDatabase.FieldValuePair("Name", "Marc"),
				new XmlDatabase.FieldValuePair("PendingMail", "true"),
				new XmlDatabase.FieldValuePair("LastSendTime", ""),
			};
			config.Insert("Email", fields);
			
			fields=new XmlDatabase.FieldValuePair[]
			{
				new XmlDatabase.FieldValuePair("PK", "2"),
				new XmlDatabase.FieldValuePair("Name", "Karen"),
				new XmlDatabase.FieldValuePair("PendingMail", "true"),
				new XmlDatabase.FieldValuePair("LastSendTime", ""),
			};
			config.Insert("Email", fields);
			
			fields=new XmlDatabase.FieldValuePair[]
			{
				new XmlDatabase.FieldValuePair("PK", "3"),
				new XmlDatabase.FieldValuePair("Name", "Ian"),
				new XmlDatabase.FieldValuePair("PendingMail", "true"),
				new XmlDatabase.FieldValuePair("LastSendTime", ""),
			};
			config.Insert("Email", fields);

			string now=DateTime.Now.ToString();
			fields=new XmlDatabase.FieldValuePair[]
			{
				new XmlDatabase.FieldValuePair("PendingMail", "false"),
				new XmlDatabase.FieldValuePair("LastSendTime", now),
			};
			config.Update("Email", "@PendingMail='true'", fields);
		}

		/// <summary>
		/// Query multiple rows into a DataTable.
		/// </summary>
		[Test, Sequence(14)]
		public void MultirowQuery()
		{
			DataTable dt=config.Query("Email", "@PendingMail='false'");
			Assertion.Assert(dt.Rows.Count==3, "Didn't return expected number of rows");
		}

		/// <summary>
		/// Get the field data from multiple rows of a specified field.
		/// </summary>
		[Test, Sequence(15)]
		public void MultiRowQueryOfSingleField()
		{
			string[] data=config.QueryField("Email", "@PendingMail='false'", "PendingMail");
			Assertion.Assert(data.Length==3, "Didn't return expected number of rows");
		}

		/// <summary>
		/// Get the field data from multiple rows of multiple fields, into a DataTable.
		/// </summary>
		[Test, Sequence(16)]
		public void MultiRowQueryOfMultipleFields()
		{
			DataTable dt=config.Query("Email", "@PendingMail='false'", new string[] {"PendingMail", "LastSendTime"});
			Assertion.Assert(dt.Rows.Count==3, "Didn't return expected number of rows");
			Assertion.Assert(dt.Columns.Count==2, "Didn't return expected number of columns");
		}

		/// <summary>
		/// Save the database.
		/// </summary>
		[Test, Sequence(17)]
		public void Save()
		{
			StringBuilder sb=new StringBuilder();
			StringWriter sw=new StringWriter(sb);
			config.SaveAs(sw);
			Trace.WriteLine(sb.ToString());
		}

//		/// <summary>
//		/// Create a test DataSet to compare with how it writes out data.
//		/// </summary>
//		[Test, Sequence(18)]
//		public void TestDataSet()
//		{
//			DataSet ds=new DataSet();
//			DataTable dt1=new DataTable("Table1");
//			DataTable dt2=new DataTable("Table2");
//			ds.Tables.Add(dt1);
//			ds.Tables.Add(dt2);
//			dt1.Columns.Add(new DataColumn("C1"));
//			dt1.Columns.Add(new DataColumn("C2"));
//			dt1.Columns.Add(new DataColumn("C3"));
//			dt2.Columns.Add(new DataColumn("C4"));
//			dt2.Columns.Add(new DataColumn("C5"));
//			dt2.Columns.Add(new DataColumn("C6"));
//			DataRow dr=dt1.NewRow();
//			dr["C1"]="1";
//			dr["C2"]="2";
//			dr["C3"]="3";
//			dt1.Rows.Add(dr);
//			dr=dt1.NewRow();
//			dr["C1"]="A";
//			dr["C2"]="B";
//			dr["C3"]="C";
//			dt1.Rows.Add(dr);
//
//			dr=dt2.NewRow();
//			dr["C4"]="4";
//			dr["C5"]="5";
//			dr["C6"]="6";
//			dt2.Rows.Add(dr);
//
//			ds.WriteXml("dataset.xml");
//		}
	}
}

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