[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / ExceptionHelper.cs
1 // ***********************************************************************
2 // Copyright (c) 2010 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Globalization;
31 using System.Collections.Generic;
32 using System.Reflection;
33 using System.Text;
34
35 namespace NUnit.Framework.Internal
36 {
37     /// <summary>
38     /// ExceptionHelper provides static methods for working with exceptions
39     /// </summary>
40     public class ExceptionHelper
41     {
42 #if !NET_4_5 && !PORTABLE && !SILVERLIGHT && !NETCF
43         private static readonly Action<Exception> PreserveStackTrace;
44
45         static ExceptionHelper()
46         {
47             var method = typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic);
48
49             if (method != null)
50             {
51                 try
52                 {
53                     PreserveStackTrace = (Action<Exception>)Delegate.CreateDelegate(typeof(Action<Exception>), method);
54                     return;
55                 }
56                 catch (InvalidOperationException) { }
57             }
58             PreserveStackTrace = _ => { };
59         }
60 #endif
61
62 #if !NETCF && !SILVERLIGHT
63         /// <summary>
64         /// Rethrows an exception, preserving its stack trace
65         /// </summary>
66         /// <param name="exception">The exception to rethrow</param>
67         public static void Rethrow(Exception exception)
68         {
69 #if NET_4_5 || PORTABLE
70             System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(exception).Throw();
71 #else
72             PreserveStackTrace(exception);
73             throw exception;
74 #endif
75         }
76 #endif
77
78         // TODO: Move to a utility class
79         /// <summary>
80         /// Builds up a message, using the Message field of the specified exception
81         /// as well as any InnerExceptions.
82         /// </summary>
83         /// <param name="exception">The exception.</param>
84         /// <returns>A combined message string.</returns>
85         public static string BuildMessage(Exception exception)
86         {
87             StringBuilder sb = new StringBuilder();
88             sb.AppendFormat(CultureInfo.CurrentCulture, "{0} : {1}", exception.GetType().ToString(), exception.Message);
89
90             foreach (Exception inner in FlattenExceptionHierarchy(exception))
91             {
92                 sb.Append(NUnit.Env.NewLine);
93                 sb.AppendFormat(CultureInfo.CurrentCulture, "  ----> {0} : {1}", inner.GetType().ToString(), inner.Message);
94             }
95
96             return sb.ToString();
97         }
98
99         /// <summary>
100         /// Builds up a message, using the Message field of the specified exception
101         /// as well as any InnerExceptions.
102         /// </summary>
103         /// <param name="exception">The exception.</param>
104         /// <returns>A combined stack trace.</returns>
105         public static string BuildStackTrace(Exception exception)
106         {
107             StringBuilder sb = new StringBuilder(GetStackTrace(exception));
108
109             foreach (Exception inner in FlattenExceptionHierarchy(exception))
110             {
111                 sb.Append(NUnit.Env.NewLine);
112                 sb.Append("--");
113                 sb.Append(inner.GetType().Name);
114                 sb.Append(NUnit.Env.NewLine);
115                 sb.Append(GetStackTrace(inner));
116             }
117
118             return sb.ToString();
119         }
120
121         /// <summary>
122         /// Gets the stack trace of the exception.
123         /// </summary>
124         /// <param name="exception">The exception.</param>
125         /// <returns>A string representation of the stack trace.</returns>
126         public static string GetStackTrace(Exception exception)
127         {
128             try
129             {
130                 return exception.StackTrace;
131             }
132             catch (Exception)
133             {
134                 return "No stack trace available";
135             }
136         }
137
138         private static List<Exception> FlattenExceptionHierarchy(Exception exception)
139         {
140             var result = new List<Exception>();
141
142 #if NET_4_0 || NET_4_5 || SILVERLIGHT || PORTABLE
143             if (exception is AggregateException)
144             {
145                 var aggregateException = (exception as AggregateException);
146                 result.AddRange(aggregateException.InnerExceptions);
147
148                 foreach (var innerException in aggregateException.InnerExceptions)
149                     result.AddRange(FlattenExceptionHierarchy(innerException));
150             }
151             else
152 #endif
153             if (exception.InnerException != null)
154             {
155                 result.Add(exception.InnerException);
156                 result.AddRange(FlattenExceptionHierarchy(exception.InnerException));
157             }
158
159             return result;
160         }
161     }
162 }