xml test library


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7.  
  8. namespace XmlTestApp
  9. {
  10. // <TestList>
  11. // <Test>
  12. // <TestName>NOT_NULL_TITLE</TestName>
  13. // <ParentPath></ParentPath>
  14. // <TestString>{/bookstore/book/title1} All Not Null;;{/bookstore/book/author/first-name} All In {aas}</TestString>
  15. // <PreCondition></PreCondition>
  16. // </Test>
  17. // <Test>
  18. // <TestName>OTHER_EXISTS</TestName>
  19. // <ParentPath></ParentPath>
  20. // <TestString>{/bookstore/book/@style} Any In {xpath:/bookstore/book/title}</TestString>
  21. // <PreCondition></PreCondition>
  22. // </Test>
  23. //</TestList>
  24.  
  25. /// </summary>
  26. public class XmlTestRobot
  27. {
  28. public String DocPath { get; set; }
  29. public String TestDocument { get; set; }
  30. private XmlDocument doc;
  31.  
  32. Dictionary<string, Test> testCases = new Dictionary<string, Test>();
  33. public void Start()
  34. {
  35. foreach (String item in testCases.Keys)
  36. {
  37. Console.WriteLine("\n\n\t\t ## Starting Test - {0} ##", item);
  38.  
  39. Test t = testCases[item];
  40.  
  41. if (!String.IsNullOrEmpty(t.PreConditionTest))
  42. {
  43. if (!testCases.Keys.Contains<String>(t.PreConditionTest))
  44. throw new Exception(string.Format("Test Case {0} Not Found", t.PreConditionTest));
  45. Console.WriteLine("Test has PreCondition Test - {0}", t.PreConditionTest);
  46.  
  47. bool? preCondition = null;
  48. if (testCases[t.PreConditionTest].TestResult == null)
  49. {
  50. t.TestResult = testCases[t.PreConditionTest].Evaluate(doc);
  51. }
  52. else
  53. {
  54. preCondition = testCases[t.PreConditionTest].TestResult;
  55. }
  56. if (preCondition == true)
  57. {
  58. Console.WriteLine("PreCondition Test- {0} Success.", t.PreConditionTest);
  59. t.TestResult = t.Evaluate(doc);
  60. }
  61. else
  62. {
  63. Console.WriteLine("PreCondition Test - {0} Failed.", t.PreConditionTest);
  64. }
  65.  
  66. }
  67. else
  68. {
  69. t.TestResult = t.Evaluate(doc);
  70. }
  71. Console.WriteLine("Test {0} - {1}", t.TestName, t.TestResult);
  72. }
  73. foreach (string item in testCases.Keys)
  74. {
  75. Console.WriteLine("{0}-{1}", testCases[item].TestName, testCases[item].TestResult);
  76. }
  77. }
  78.  
  79. public void Load()
  80. {
  81. doc = new XmlDocument();
  82. doc.Load(DocPath);
  83. LoadTestCases();
  84. }
  85. private void LoadTestCases()
  86. {
  87. XDocument xDoc = XDocument.Load(TestDocument);
  88. foreach (XElement item in xDoc.Root.Elements(XName.Get("Test", "")))
  89. {
  90. Test t = new Test();
  91. t.TestName = item.Element(XName.Get("TestName", "")).Value;
  92. t.PreConditionTest = item.Element(XName.Get("PreCondition", "")).Value;
  93. t.ParentPath = item.Element(XName.Get("ParentPath", "")).Value;
  94. t.TestString = item.Element(XName.Get("TestString", "")).Value;
  95. t.TestResult = null;
  96. testCases.Add(t.TestName, t);
  97. }
  98. }
  99.  
  100. }
  101. public class Test
  102. {
  103. public String TestName { get; set; }
  104. public bool? TestResult { get; set; }
  105. public String PreConditionTest { get; set; }
  106. public String TestString { get; set; }
  107. public String ParentPath { get; set; }
  108. private bool AllIn(List<string> scValues, List<String> tgtValues)
  109. {
  110. return scValues.Intersect<String>(tgtValues).Count<String>() == scValues.Count<String>();
  111. }
  112. private bool AllNotIn(List<string> scValues, List<String> tgtValues)
  113. {
  114. return scValues.Intersect<String>(tgtValues).Count<String>() == 0;
  115. }
  116. private bool AnyIn(List<string> scValues, List<String> tgtValues)
  117. {
  118. return scValues.Intersect<String>(tgtValues).Count<String>() > 0;
  119. }
  120. private bool AllNotNull(List<string> scValues)
  121. {
  122. var v = from String s in scValues
  123. where String.IsNullOrEmpty(s)
  124. select s;
  125. return v.Count<string>() == 0;
  126. }
  127. public bool Evaluate(XmlNode node)
  128. {
  129. List<bool> results = new List<bool>();
  130. foreach (String testPart in TestString.Split(new string[] { ";;" }, StringSplitOptions.None))
  131. {
  132. results.Add(
  133. EvaluateTestPart(testPart, node));
  134. }
  135. return results.Contains<bool>(false) ? false : true;
  136.  
  137. }
  138.  
  139. public bool EvaluateTestPart(String testPart, XmlNode node)
  140. {
  141.  
  142. bool finalReturnValue = false;
  143.  
  144. int firstIndex = testPart.IndexOf('{');
  145. int secondIndex = testPart.IndexOf('}', firstIndex);
  146. int thirdIndex = testPart.IndexOf('{', secondIndex);
  147.  
  148. int fourthIndex = -1;
  149. if (thirdIndex > 0)
  150. fourthIndex = testPart.IndexOf('}', thirdIndex);
  151.  
  152. String sourceValueString = testPart.Substring(firstIndex + 1, secondIndex - firstIndex - 1);
  153. String targetValuesString = "";
  154. if (fourthIndex > -1)
  155. targetValuesString = testPart.Substring(thirdIndex + 1, fourthIndex - thirdIndex - 1);
  156. String operationString = "";
  157. if (fourthIndex > -1)
  158. operationString = testPart.Substring(secondIndex + 1, thirdIndex - secondIndex - 1);
  159. else
  160. operationString = testPart.Substring(secondIndex + 1);
  161.  
  162. Console.WriteLine(testPart);
  163. List<String> scValues = GetSourceValues(sourceValueString, node);
  164. List<string> tgtValues = GetTargetValues(targetValuesString, node);
  165. Console.WriteLine("[{0}] {1} [{2}]", String.Join(",", scValues.ToArray()),
  166. operationString, String.Join(",", tgtValues.ToArray()));
  167.  
  168. switch (operationString.Trim())
  169. {
  170. case "All In":
  171. finalReturnValue = AllIn(scValues, tgtValues);
  172.  
  173. break;
  174. case "Any In":
  175. finalReturnValue = AnyIn(scValues, tgtValues);
  176. break;
  177. case "All Not In":
  178. finalReturnValue = AllNotIn(scValues, tgtValues);
  179. break;
  180. case "All Not Null":
  181. finalReturnValue = AllNotNull(scValues);
  182. break;
  183. }
  184. Console.WriteLine("Test part [{0}] Result={1}", testPart, finalReturnValue);
  185. return finalReturnValue;
  186. }
  187. private List<string> GetSourceValues(string sourceValueString, XmlNode node)
  188. {
  189. List<String> finalReturnValues = new List<string>();
  190. XmlNodeList nl = node.SelectNodes(sourceValueString);
  191. foreach (XmlNode n in nl)
  192. {
  193. finalReturnValues.Add(n.InnerText);
  194. }
  195. return finalReturnValues;
  196. }
  197. private List<string> GetTargetValues(string targetValueString, XmlNode node)
  198. {
  199. if (targetValueString.StartsWith("xpath:"))
  200. {
  201. targetValueString = targetValueString.Replace("xpath:", "");
  202. return GetSourceValues(targetValueString, node);
  203. }
  204. return targetValueString.Split(new char[] { ',' }).ToList<string>();
  205. }
  206. }
  207. class Program
  208. {
  209. static void Main(string[] args)
  210. {
  211.  
  212. XmlTestRobot robot = new XmlTestRobot();
  213. robot.TestDocument = @"c:\users\ashvin\documents\visual studio 2012\Projects\XmlTestApp\XmlTestApp\TestCases.xml";
  214. robot.DocPath = @"c:\users\ashvin\documents\visual studio 2012\Projects\XmlTestApp\XmlTestApp\test.xml";
  215. robot.Load();
  216. robot.Start();
  217.  
  218. XmlDocument doc = new XmlDocument();
  219. doc.Load(@"c:\users\ashvin\documents\visual studio 2012\Projects\XmlTestApp\XmlTestApp\test.xml");
  220. XmlNode root = doc.DocumentElement;
  221. XmlNodeList nl = root.SelectNodes(@"/bookstore/book/title");
  222.  
  223. Test t = new Test();
  224. t.TestName = "Test";
  225. t.PreConditionTest = null;
  226. t.TestString = @"{/bookstore/book/title} All In {The Handmaid1's Tale,The Poisonwood Bible,The Bean Trees}";
  227. t.Evaluate(root);
  228.  
  229. // t.EvaluateTestPart(@"{assetClassXpath} All In {Stock}");
  230. //{} [All|Any] [not] in {}
  231. //All In
  232. //All Not In
  233. //Any In
  234. //
  235. }
  236. }
  237. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.