PageRenderTime 109ms CodeModel.GetById 16ms RepoModel.GetById 2ms app.codeStats 0ms

/src/NUnit/UiException/ExceptionItem.cs

#
C# | 252 lines | 133 code | 41 blank | 78 comment | 28 complexity | 8b4f1bcd05396f1c6306579131b5b260 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You may
  3. // obtain a copy of the license at http://nunit.org
  4. // ****************************************************************
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.IO;
  9. namespace NUnit.UiException
  10. {
  11. /// <summary>
  12. /// (Formerly named ExceptionItem)
  13. ///
  14. /// This is the output analysis of one error line coming from
  15. /// a stack trace that still gathers the same data but in more
  16. /// convenient way to be read from.
  17. /// An ErrorItem represents one error with possible location
  18. /// informations such as:
  19. /// - filename where the error has occured
  20. /// - file's line number
  21. /// - method name
  22. /// </summary>
  23. public class ErrorItem
  24. {
  25. /// <summary>
  26. /// An access path to the source file referred by this item.
  27. /// </summary>
  28. private string _path;
  29. /// <summary>
  30. /// The full qualified name of the member method referred by this item.
  31. /// </summary>
  32. private string _fullyQualifiedMethodName;
  33. /// <summary>
  34. /// The line index where the exception occured.
  35. /// </summary>
  36. private int _line;
  37. /// <summary>
  38. /// Store the content of the file pointed by _path.
  39. /// </summary>
  40. private string _text;
  41. /// <summary>
  42. /// Create an instance of ErrorItem that
  43. /// has source code attachments.
  44. /// </summary>
  45. public ErrorItem(string path, int lineNumber)
  46. {
  47. UiExceptionHelper.CheckNotNull(path, "path");
  48. _path = path;
  49. _line = lineNumber;
  50. return;
  51. }
  52. /// <summary>
  53. /// Create a new exception item.
  54. /// </summary>
  55. /// <param name="path">An absolute path to the source code file.</param>
  56. /// <param name="fullMethodName">A full qualified name of a member method.</param>
  57. /// <param name="lineNumber">A line index where the exception occured.</param>
  58. public ErrorItem(string path, string fullMethodName, int lineNumber)
  59. {
  60. _path = path;
  61. _fullyQualifiedMethodName = fullMethodName;
  62. _line = lineNumber;
  63. return;
  64. }
  65. /// <summary>
  66. /// Create an instance of ErrorItem that doesn't have
  67. /// any source code attachments.
  68. /// </summary>
  69. public ErrorItem()
  70. {
  71. // nothing to do
  72. }
  73. /// <summary>
  74. /// Reads and returns the part of Path that contains the filename
  75. /// of the source code file.
  76. /// </summary>
  77. public string FileName
  78. {
  79. get { return (System.IO.Path.GetFileName(_path)); }
  80. }
  81. /// <summary>
  82. /// Gets the absolute path to the source code file.
  83. /// </summary>
  84. public string Path
  85. {
  86. get { return (_path); }
  87. }
  88. /// <summary>
  89. /// Returns the file language - e.g.: the string after
  90. /// the last dot or null -
  91. /// </summary>
  92. public string FileExtension
  93. {
  94. get
  95. {
  96. int dotIndex;
  97. if (_path == null)
  98. return (null);
  99. dotIndex = _path.LastIndexOf(".", StringComparison.Ordinal);
  100. if (dotIndex > -1 && dotIndex < _path.Length - 1)
  101. return (_path.Substring(dotIndex + 1));
  102. return (null);
  103. }
  104. }
  105. /// <summary>
  106. /// Gets the full qualified name of the member method.
  107. /// </summary>
  108. public string FullyQualifiedMethodName
  109. {
  110. get { return (_fullyQualifiedMethodName); }
  111. }
  112. /// <summary>
  113. /// Reads and return the method part from the FullyQualifiedMethodName.
  114. /// The value contains the signature of the method.
  115. /// </summary>
  116. public string MethodName
  117. {
  118. get
  119. {
  120. int index;
  121. if (FullyQualifiedMethodName == null)
  122. return ("");
  123. if ((index = FullyQualifiedMethodName.LastIndexOf(".",
  124. StringComparison.Ordinal)) == -1)
  125. return (FullyQualifiedMethodName);
  126. return (FullyQualifiedMethodName.Substring(index + 1));
  127. }
  128. }
  129. /// <summary>
  130. /// Gets the method name without the argument list.
  131. /// </summary>
  132. public string BaseMethodName
  133. {
  134. get
  135. {
  136. string method = MethodName;
  137. int index = method.IndexOf("(", StringComparison.Ordinal);
  138. if (index > 0)
  139. return (method.Substring(0, index));
  140. return (method);
  141. }
  142. }
  143. /// <summary>
  144. /// Reads and returns the class part from the FullyQualifiedMethodName.
  145. /// </summary>
  146. public string ClassName
  147. {
  148. get
  149. {
  150. int end_index;
  151. int start_index;
  152. if (FullyQualifiedMethodName == null)
  153. return ("");
  154. if ((end_index = FullyQualifiedMethodName.LastIndexOf(".",
  155. StringComparison.Ordinal)) == -1)
  156. return ("");
  157. start_index = end_index - 1;
  158. while (start_index > 0 && FullyQualifiedMethodName[start_index] != '.')
  159. start_index--;
  160. if (start_index >= 0 && FullyQualifiedMethodName[start_index] == '.')
  161. start_index++;
  162. return (FullyQualifiedMethodName.Substring(start_index, end_index - start_index));
  163. }
  164. }
  165. /// <summary>
  166. /// Gets the line number where the exception occured.
  167. /// </summary>
  168. public int LineNumber
  169. {
  170. get { return (_line); }
  171. }
  172. /// <summary>
  173. /// Gets a boolean that says whether this item has source
  174. /// code localization attachments.
  175. /// </summary>
  176. public bool HasSourceAttachment {
  177. get { return (_path != null); }
  178. }
  179. /// <summary>
  180. /// Read and return the content of the underlying file. If the file
  181. /// cannot be found or read an exception is raised.
  182. /// </summary>
  183. public string ReadFile()
  184. {
  185. if (!System.IO.File.Exists(_path))
  186. throw new FileNotFoundException("File does not exist. File: " + _path);
  187. if (_text == null)
  188. {
  189. StreamReader rder = new StreamReader(_path);
  190. _text = rder.ReadToEnd();
  191. rder.Close();
  192. }
  193. return (_text);
  194. }
  195. public override string ToString() {
  196. return ("TraceItem: {'" + _path + "', " + _fullyQualifiedMethodName + ", " + _line + "}");
  197. }
  198. public override bool Equals(object obj)
  199. {
  200. ErrorItem item = obj as ErrorItem;
  201. if (item == null)
  202. return (false);
  203. return (_path == item._path &&
  204. _fullyQualifiedMethodName == item._fullyQualifiedMethodName &&
  205. _line == item._line);
  206. }
  207. public override int GetHashCode() {
  208. return base.GetHashCode();
  209. }
  210. }
  211. }