Return to Snippet

Revision: 65134
at October 28, 2013 03:41 by ashvin8085hotmail


Initial Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace XmlTestApp
{
    //   <TestList>
    //  <Test>
    //    <TestName>NOT_NULL_TITLE</TestName>
    //    <ParentPath></ParentPath>
    //    <TestString>{/bookstore/book/title1} All Not Null;;{/bookstore/book/author/first-name} All In {aas}</TestString>
    //    <PreCondition></PreCondition>
    //  </Test>
    //  <Test>
    //    <TestName>OTHER_EXISTS</TestName>
    //    <ParentPath></ParentPath>
    //    <TestString>{/bookstore/book/@style} Any In {xpath:/bookstore/book/title}</TestString>
    //    <PreCondition></PreCondition>
    //  </Test>
    //</TestList>

    /// </summary>
    public class XmlTestRobot
    {
        public String DocPath { get; set; }
        public String TestDocument { get; set; }
        private XmlDocument doc;

        Dictionary<string, Test> testCases = new Dictionary<string, Test>();
        public void Start()
        {
            foreach (String item in testCases.Keys)
            {
                Console.WriteLine("\n\n\t\t ## Starting Test - {0} ##", item);

                Test t = testCases[item];

                if (!String.IsNullOrEmpty(t.PreConditionTest))
                {
                    if (!testCases.Keys.Contains<String>(t.PreConditionTest))
                        throw new Exception(string.Format("Test Case {0} Not Found", t.PreConditionTest));
                    Console.WriteLine("Test has PreCondition Test - {0}", t.PreConditionTest);

                    bool? preCondition = null;
                    if (testCases[t.PreConditionTest].TestResult == null)
                    {
                        t.TestResult = testCases[t.PreConditionTest].Evaluate(doc);
                    }
                    else
                    {
                        preCondition = testCases[t.PreConditionTest].TestResult;
                    }
                    if (preCondition == true)
                    {
                        Console.WriteLine("PreCondition Test- {0} Success.", t.PreConditionTest);
                        t.TestResult = t.Evaluate(doc);
                    }
                    else
                    {
                        Console.WriteLine("PreCondition Test - {0} Failed.", t.PreConditionTest);
                    }

                }
                else
                {
                    t.TestResult = t.Evaluate(doc);
                }
                Console.WriteLine("Test {0} - {1}", t.TestName, t.TestResult);
            }
            foreach (string item in testCases.Keys)
            {
                Console.WriteLine("{0}-{1}", testCases[item].TestName, testCases[item].TestResult);
            }
        }

        public void Load()
        {
            doc = new XmlDocument();
            doc.Load(DocPath);
            LoadTestCases();
        }
        private void LoadTestCases()
        {
            XDocument xDoc = XDocument.Load(TestDocument);
            foreach (XElement item in xDoc.Root.Elements(XName.Get("Test", "")))
            {
                Test t = new Test();
                t.TestName = item.Element(XName.Get("TestName", "")).Value;
                t.PreConditionTest = item.Element(XName.Get("PreCondition", "")).Value;
                t.ParentPath = item.Element(XName.Get("ParentPath", "")).Value;
                t.TestString = item.Element(XName.Get("TestString", "")).Value;
                t.TestResult = null;
                testCases.Add(t.TestName, t);
            }
        }

    }
    public class Test
    {
        public String TestName { get; set; }
        public bool? TestResult { get; set; }
        public String PreConditionTest { get; set; }
        public String TestString { get; set; }
        public String ParentPath { get; set; }
        private bool AllIn(List<string> scValues, List<String> tgtValues)
        {
            return scValues.Intersect<String>(tgtValues).Count<String>() == scValues.Count<String>();
        }
        private bool AllNotIn(List<string> scValues, List<String> tgtValues)
        {
            return scValues.Intersect<String>(tgtValues).Count<String>() == 0;
        }
        private bool AnyIn(List<string> scValues, List<String> tgtValues)
        {
            return scValues.Intersect<String>(tgtValues).Count<String>() > 0;
        }
        private bool AllNotNull(List<string> scValues)
        {
            var v = from String s in scValues
                    where String.IsNullOrEmpty(s)
                    select s;
            return v.Count<string>() == 0;
        }
        public bool Evaluate(XmlNode node)
        {
            List<bool> results = new List<bool>();
            foreach (String testPart in TestString.Split(new string[] { ";;" }, StringSplitOptions.None))
            {
                results.Add(
                EvaluateTestPart(testPart, node));
            }
            return results.Contains<bool>(false) ? false : true;

        }

        public bool EvaluateTestPart(String testPart, XmlNode node)
        {

            bool finalReturnValue = false;

            int firstIndex = testPart.IndexOf('{');
            int secondIndex = testPart.IndexOf('}', firstIndex);
            int thirdIndex = testPart.IndexOf('{', secondIndex);

            int fourthIndex = -1;
            if (thirdIndex > 0)
                fourthIndex = testPart.IndexOf('}', thirdIndex);

            String sourceValueString = testPart.Substring(firstIndex + 1, secondIndex - firstIndex - 1);
            String targetValuesString = "";
            if (fourthIndex > -1)
                targetValuesString = testPart.Substring(thirdIndex + 1, fourthIndex - thirdIndex - 1);
            String operationString = "";
            if (fourthIndex > -1)
                operationString = testPart.Substring(secondIndex + 1, thirdIndex - secondIndex - 1);
            else
                operationString = testPart.Substring(secondIndex + 1);

            Console.WriteLine(testPart);
            List<String> scValues = GetSourceValues(sourceValueString, node);
            List<string> tgtValues = GetTargetValues(targetValuesString, node);
            Console.WriteLine("[{0}] {1} [{2}]", String.Join(",", scValues.ToArray()),
                                 operationString, String.Join(",", tgtValues.ToArray()));

            switch (operationString.Trim())
            {
                case "All In":
                    finalReturnValue = AllIn(scValues, tgtValues);

                    break;
                case "Any In":
                    finalReturnValue = AnyIn(scValues, tgtValues);
                    break;
                case "All Not In":
                    finalReturnValue = AllNotIn(scValues, tgtValues);
                    break;
                case "All Not Null":
                    finalReturnValue = AllNotNull(scValues);
                    break;
            }
            Console.WriteLine("Test part [{0}] Result={1}", testPart, finalReturnValue);
            return finalReturnValue;
        }
        private List<string> GetSourceValues(string sourceValueString, XmlNode node)
        {
            List<String> finalReturnValues = new List<string>();
            XmlNodeList nl = node.SelectNodes(sourceValueString);
            foreach (XmlNode n in nl)
            {
                finalReturnValues.Add(n.InnerText);
            }
            return finalReturnValues;
        }
        private List<string> GetTargetValues(string targetValueString, XmlNode node)
        {
            if (targetValueString.StartsWith("xpath:"))
            {
                targetValueString = targetValueString.Replace("xpath:", "");
                return GetSourceValues(targetValueString, node);
            }
            return targetValueString.Split(new char[] { ',' }).ToList<string>();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            XmlTestRobot robot = new XmlTestRobot();
            robot.TestDocument = @"c:\users\ashvin\documents\visual studio 2012\Projects\XmlTestApp\XmlTestApp\TestCases.xml";
            robot.DocPath = @"c:\users\ashvin\documents\visual studio 2012\Projects\XmlTestApp\XmlTestApp\test.xml";
            robot.Load();
            robot.Start();

            XmlDocument doc = new XmlDocument();
            doc.Load(@"c:\users\ashvin\documents\visual studio 2012\Projects\XmlTestApp\XmlTestApp\test.xml");
            XmlNode root = doc.DocumentElement;
            XmlNodeList nl = root.SelectNodes(@"/bookstore/book/title");

            Test t = new Test();
            t.TestName = "Test";
            t.PreConditionTest = null;
            t.TestString = @"{/bookstore/book/title} All In {The Handmaid1's Tale,The Poisonwood Bible,The Bean Trees}";
            t.Evaluate(root);

            // t.EvaluateTestPart(@"{assetClassXpath} All In {Stock}");
            //{} [All|Any] [not] in {}
            //All In
            //All Not In
            //Any In
            //
        }
    }
}

Initial URL

                                

Initial Description
Simple C# library that can be used to test xml. Test rules are defined in the form of xml file.

Initial Title
xml test library

Initial Tags
xml

Initial Language
C#