PageRenderTime 1186ms CodeModel.GetById 24ms RepoModel.GetById 2ms app.codeStats 0ms

/src/NUnit/util/XmlResultWriter.cs

#
C# | 335 lines | 268 code | 45 blank | 22 comment | 42 complexity | 2aabafcee34144630906546940f16376 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. namespace NUnit.Util
  7. {
  8. using System;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Xml;
  12. using System.Collections;
  13. using System.Reflection;
  14. using NUnit.Core;
  15. /// <summary>
  16. /// Summary description for XmlResultWriter.
  17. /// </summary>
  18. public class XmlResultWriter
  19. {
  20. private XmlTextWriter xmlWriter;
  21. private TextWriter writer;
  22. private MemoryStream memoryStream;
  23. #region Constructors
  24. public XmlResultWriter(string fileName)
  25. {
  26. xmlWriter = new XmlTextWriter( new StreamWriter(fileName, false, System.Text.Encoding.UTF8) );
  27. }
  28. public XmlResultWriter( TextWriter writer )
  29. {
  30. this.memoryStream = new MemoryStream();
  31. this.writer = writer;
  32. this.xmlWriter = new XmlTextWriter( new StreamWriter( memoryStream, System.Text.Encoding.UTF8 ) );
  33. }
  34. #endregion
  35. private void InitializeXmlFile(TestResult result)
  36. {
  37. ResultSummarizer summaryResults = new ResultSummarizer(result);
  38. xmlWriter.Formatting = Formatting.Indented;
  39. xmlWriter.WriteStartDocument(false);
  40. xmlWriter.WriteComment("This file represents the results of running a test suite");
  41. xmlWriter.WriteStartElement("test-results");
  42. xmlWriter.WriteAttributeString("name", summaryResults.Name);
  43. xmlWriter.WriteAttributeString("total", summaryResults.TestsRun.ToString());
  44. xmlWriter.WriteAttributeString("errors", summaryResults.Errors.ToString());
  45. xmlWriter.WriteAttributeString("failures", summaryResults.Failures.ToString());
  46. xmlWriter.WriteAttributeString("not-run", summaryResults.TestsNotRun.ToString());
  47. xmlWriter.WriteAttributeString("inconclusive", summaryResults.Inconclusive.ToString());
  48. xmlWriter.WriteAttributeString("ignored", summaryResults.Ignored.ToString());
  49. xmlWriter.WriteAttributeString("skipped", summaryResults.Skipped.ToString());
  50. xmlWriter.WriteAttributeString("invalid", summaryResults.NotRunnable.ToString());
  51. DateTime now = DateTime.Now;
  52. xmlWriter.WriteAttributeString("date", XmlConvert.ToString( now, "yyyy-MM-dd" ) );
  53. xmlWriter.WriteAttributeString("time", XmlConvert.ToString( now, "HH:mm:ss" ));
  54. WriteEnvironment();
  55. WriteCultureInfo();
  56. }
  57. private void WriteCultureInfo() {
  58. xmlWriter.WriteStartElement("culture-info");
  59. xmlWriter.WriteAttributeString("current-culture",
  60. CultureInfo.CurrentCulture.ToString());
  61. xmlWriter.WriteAttributeString("current-uiculture",
  62. CultureInfo.CurrentUICulture.ToString());
  63. xmlWriter.WriteEndElement();
  64. }
  65. private void WriteEnvironment() {
  66. xmlWriter.WriteStartElement("environment");
  67. xmlWriter.WriteAttributeString("nunit-version",
  68. Assembly.GetExecutingAssembly().GetName().Version.ToString());
  69. xmlWriter.WriteAttributeString("clr-version",
  70. Environment.Version.ToString());
  71. xmlWriter.WriteAttributeString("os-version",
  72. Environment.OSVersion.ToString());
  73. xmlWriter.WriteAttributeString("platform",
  74. Environment.OSVersion.Platform.ToString());
  75. xmlWriter.WriteAttributeString("cwd",
  76. Environment.CurrentDirectory);
  77. xmlWriter.WriteAttributeString("machine-name",
  78. Environment.MachineName);
  79. xmlWriter.WriteAttributeString("user",
  80. Environment.UserName);
  81. xmlWriter.WriteAttributeString("user-domain",
  82. Environment.UserDomainName);
  83. xmlWriter.WriteEndElement();
  84. }
  85. #region Public Methods
  86. public void SaveTestResult( TestResult result )
  87. {
  88. InitializeXmlFile( result );
  89. WriteResultElement( result );
  90. TerminateXmlFile();
  91. }
  92. private void WriteResultElement( TestResult result )
  93. {
  94. StartTestElement( result );
  95. WriteCategoriesElement(result);
  96. WritePropertiesElement(result);
  97. switch (result.ResultState)
  98. {
  99. case ResultState.Ignored:
  100. case ResultState.NotRunnable:
  101. case ResultState.Skipped:
  102. WriteReasonElement(result);
  103. break;
  104. case ResultState.Failure:
  105. case ResultState.Error:
  106. case ResultState.Cancelled:
  107. if (!result.Test.IsSuite || result.FailureSite == FailureSite.SetUp)
  108. WriteFailureElement(result);
  109. break;
  110. case ResultState.Success:
  111. case ResultState.Inconclusive:
  112. if (result.Message != null)
  113. WriteReasonElement(result);
  114. break;
  115. }
  116. if ( result.HasResults )
  117. WriteChildResults( result );
  118. xmlWriter.WriteEndElement(); // test element
  119. }
  120. private void TerminateXmlFile()
  121. {
  122. try
  123. {
  124. xmlWriter.WriteEndElement(); // test-results
  125. xmlWriter.WriteEndDocument();
  126. xmlWriter.Flush();
  127. if ( memoryStream != null && writer != null )
  128. {
  129. memoryStream.Position = 0;
  130. using ( StreamReader rdr = new StreamReader( memoryStream ) )
  131. {
  132. writer.Write( rdr.ReadToEnd() );
  133. }
  134. }
  135. xmlWriter.Close();
  136. }
  137. finally
  138. {
  139. //writer.Close();
  140. }
  141. }
  142. #endregion
  143. #region Element Creation Helpers
  144. private void StartTestElement(TestResult result)
  145. {
  146. if (result.Test.IsSuite)
  147. {
  148. xmlWriter.WriteStartElement("test-suite");
  149. xmlWriter.WriteAttributeString("type", result.Test.TestType);
  150. xmlWriter.WriteAttributeString("name", result.Name);
  151. }
  152. else
  153. {
  154. xmlWriter.WriteStartElement("test-case");
  155. xmlWriter.WriteAttributeString("name", result.FullName);
  156. }
  157. if (result.Description != null)
  158. xmlWriter.WriteAttributeString("description", result.Description);
  159. xmlWriter.WriteAttributeString("executed", result.Executed.ToString());
  160. xmlWriter.WriteAttributeString("result", result.ResultState.ToString());
  161. if ( result.Executed )
  162. {
  163. xmlWriter.WriteAttributeString("success", result.IsSuccess.ToString());
  164. xmlWriter.WriteAttributeString("time", result.Time.ToString("#####0.000", NumberFormatInfo.InvariantInfo));
  165. xmlWriter.WriteAttributeString("asserts", result.AssertCount.ToString());
  166. }
  167. }
  168. private void WriteCategoriesElement(TestResult result)
  169. {
  170. if (result.Test.Categories != null && result.Test.Categories.Count > 0)
  171. {
  172. xmlWriter.WriteStartElement("categories");
  173. foreach (string category in result.Test.Categories)
  174. {
  175. xmlWriter.WriteStartElement("category");
  176. xmlWriter.WriteAttributeString("name", category);
  177. xmlWriter.WriteEndElement();
  178. }
  179. xmlWriter.WriteEndElement();
  180. }
  181. }
  182. private void WritePropertiesElement(TestResult result)
  183. {
  184. IDictionary props = result.Test.Properties;
  185. if (result.Test.Properties != null && props.Count > 0)
  186. {
  187. int nprops = 0;
  188. foreach (string key in result.Test.Properties.Keys)
  189. {
  190. if ( !key.StartsWith("_") )
  191. {
  192. object val = result.Test.Properties[key];
  193. if (val != null)
  194. {
  195. if ( nprops == 0 )
  196. xmlWriter.WriteStartElement("properties");
  197. xmlWriter.WriteStartElement("property");
  198. xmlWriter.WriteAttributeString("name", key);
  199. xmlWriter.WriteAttributeString("value", val.ToString());
  200. xmlWriter.WriteEndElement();
  201. ++nprops;
  202. }
  203. }
  204. }
  205. if ( nprops > 0 )
  206. xmlWriter.WriteEndElement();
  207. }
  208. }
  209. private void WriteReasonElement(TestResult result)
  210. {
  211. xmlWriter.WriteStartElement("reason");
  212. xmlWriter.WriteStartElement("message");
  213. xmlWriter.WriteCData(result.Message);
  214. xmlWriter.WriteEndElement();
  215. xmlWriter.WriteEndElement();
  216. }
  217. private void WriteFailureElement(TestResult result)
  218. {
  219. xmlWriter.WriteStartElement("failure");
  220. xmlWriter.WriteStartElement("message");
  221. WriteCData(result.Message);
  222. xmlWriter.WriteEndElement();
  223. xmlWriter.WriteStartElement("stack-trace");
  224. if (result.StackTrace != null)
  225. WriteCData(StackTraceFilter.Filter(result.StackTrace));
  226. xmlWriter.WriteEndElement();
  227. xmlWriter.WriteEndElement();
  228. }
  229. private void WriteChildResults(TestResult result)
  230. {
  231. xmlWriter.WriteStartElement("results");
  232. if ( result.HasResults )
  233. foreach (TestResult childResult in result.Results)
  234. WriteResultElement( childResult );
  235. xmlWriter.WriteEndElement();
  236. }
  237. #endregion
  238. #region Output Helpers
  239. /// <summary>
  240. /// Makes string safe for xml parsing, replacing control chars with '?'
  241. /// </summary>
  242. /// <param name="encodedString">string to make safe</param>
  243. /// <returns>xml safe string</returns>
  244. private static string CharacterSafeString(string encodedString)
  245. {
  246. /*The default code page for the system will be used.
  247. Since all code pages use the same lower 128 bytes, this should be sufficient
  248. for finding uprintable control characters that make the xslt processor error.
  249. We use characters encoded by the default code page to avoid mistaking bytes as
  250. individual characters on non-latin code pages.*/
  251. char[] encodedChars = System.Text.Encoding.Default.GetChars(System.Text.Encoding.Default.GetBytes(encodedString));
  252. System.Collections.ArrayList pos = new System.Collections.ArrayList();
  253. for(int x = 0 ; x < encodedChars.Length ; x++)
  254. {
  255. char currentChar = encodedChars[x];
  256. //unprintable characters are below 0x20 in Unicode tables
  257. //some control characters are acceptable. (carriage return 0x0D, line feed 0x0A, horizontal tab 0x09)
  258. if(currentChar < 32 && (currentChar != 9 && currentChar != 10 && currentChar != 13))
  259. {
  260. //save the array index for later replacement.
  261. pos.Add(x);
  262. }
  263. }
  264. foreach(int index in pos)
  265. {
  266. encodedChars[index] = '?';//replace unprintable control characters with ?(3F)
  267. }
  268. return System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(encodedChars));
  269. }
  270. private void WriteCData(string text)
  271. {
  272. int start = 0;
  273. while (true)
  274. {
  275. int illegal = text.IndexOf("]]>", start);
  276. if (illegal < 0)
  277. break;
  278. xmlWriter.WriteCData(text.Substring(start, illegal - start + 2));
  279. start = illegal + 2;
  280. if (start >= text.Length)
  281. return;
  282. }
  283. if (start > 0)
  284. xmlWriter.WriteCData(text.Substring(start));
  285. else
  286. xmlWriter.WriteCData(text);
  287. }
  288. #endregion
  289. }
  290. }