PageRenderTime 33ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/ExceptionItem.cs

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