/////////////////////////////////////////////////////////////////////////
// TestExec.cs - Prototype for test executor //
// //
// Jim Fawcett, CSE784 - Software Studio, Fall 2008 //
/////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Xml;
namespace TextExec
{
class TestExec
{
//
XmlDocument CreateTestDocument()
{
XmlDocument doc = new XmlDocument();
XmlNode decl = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(decl);
XmlNode root = doc.CreateElement("TestData");
doc.AppendChild(root);
return doc;
}
//
private List GetPaths()
{
List paths = new List();
int i = 0;
while (true)
{
string path = ConfigurationSettings.AppSettings[(++i).ToString()];
if (path == null)
break;
paths.Add(path);
}
return paths;
}
//
XmlDocument LoadAndExecute()
{
XmlDocument doc = CreateTestDocument();
try
{
XmlNode root = doc.DocumentElement;
List paths = this.GetPaths();
foreach (string path in paths)
{
string fullpath = Path.GetFullPath(path);
XmlNode node = doc.CreateElement("path");
node.AppendChild(doc.CreateTextNode(fullpath));
root.AppendChild(node);
Console.Write("\n Executing tests from {0}", fullpath);
string[] files = Directory.GetFiles(fullpath, "*.dll");
foreach (string file in files)
{
Assembly asm = Assembly.LoadFile(file);
Type[] types = asm.GetExportedTypes();
foreach (Type t in types)
{
Test.ITest test;
if (t.IsClass && typeof(Test.ITest).IsAssignableFrom(t))
test = (Test.ITest)Activator.CreateInstance(t);
else
continue;
bool result = test.test();
if (test.test())
{
XmlNode child = doc.CreateElement(t.Name.ToString());
child.AppendChild(doc.CreateTextNode("status - passed"));
root.AppendChild(child);
Console.Write("\n test {0} passed", t.Name);
}
else
{
XmlNode child = doc.CreateElement(t.Name.ToString());
child.AppendChild(doc.CreateTextNode("status - failed"));
root.AppendChild(child);
Console.Write("\n test {0} failed", t.Name);
}
}
}
}
}
catch (Exception ex)
{
Console.Write("\n\n {0}", ex.Message);
}
Console.Write("\n\n");
return doc;
}
//
static void Main(string[] args)
{
TestExec exec = new TestExec();
XmlDocument testData = exec.LoadAndExecute();
Console.Write("\n {0}", testData.OuterXml);
XmlWriter xw = new XmlTextWriter("testData.xml", Encoding.ASCII);
testData.WriteContentTo(xw);
xw.Close();
}
}
}
//