Merge pull request #2353 from ludovic-henry/fix-servicemodel-15153
[mono.git] / mcs / nunit24 / ConsoleRunner / nunit-console / EventCollector.cs
1 using System;\r
2 using System.IO;\r
3 using System.Diagnostics;\r
4 using System.Text.RegularExpressions;\r
5 using System.Collections;\r
6 using System.Collections.Specialized;\r
7 using NUnit.Core;\r
8 using NUnit.Util;\r
9 \r
10 namespace NUnit.ConsoleRunner\r
11 {\r
12         /// <summary>\r
13         /// Summary description for EventCollector.\r
14         /// </summary>\r
15         public class EventCollector : MarshalByRefObject, EventListener\r
16         {\r
17                 private int testRunCount;\r
18                 private int testIgnoreCount;\r
19                 private int failureCount;\r
20                 private int level;\r
21 \r
22                 private ConsoleOptions options;\r
23                 private TextWriter outWriter;\r
24                 private TextWriter errorWriter;\r
25 \r
26                 StringCollection messages;\r
27                 \r
28                 private bool progress = false;\r
29                 private string currentTestName;\r
30 \r
31                 private ArrayList unhandledExceptions = new ArrayList();\r
32 \r
33                 public EventCollector( ConsoleOptions options, TextWriter outWriter, TextWriter errorWriter )\r
34                 {\r
35                         level = 0;\r
36                         this.options = options;\r
37                         this.outWriter = outWriter;\r
38                         this.errorWriter = errorWriter;\r
39                         this.currentTestName = string.Empty;\r
40                         this.progress = !options.xmlConsole && !options.labels && !options.nodots;\r
41 \r
42                         AppDomain.CurrentDomain.UnhandledException += \r
43                                 new UnhandledExceptionEventHandler(OnUnhandledException);\r
44                 }\r
45 \r
46                 public bool HasExceptions\r
47                 {\r
48                         get { return unhandledExceptions.Count > 0; }\r
49                 }\r
50 \r
51                 public void WriteExceptions()\r
52                 {\r
53                         Console.WriteLine();\r
54                         Console.WriteLine("Unhandled exceptions:");\r
55                         int index = 1;\r
56                         foreach( string msg in unhandledExceptions )\r
57                                 Console.WriteLine( "{0}) {1}", index++, msg );\r
58                 }\r
59 \r
60                 public void RunStarted(string name, int testCount)\r
61                 {\r
62                 }\r
63 \r
64                 public void RunFinished(TestResult result)\r
65                 {\r
66                 }\r
67 \r
68                 public void RunFinished(Exception exception)\r
69                 {\r
70                 }\r
71 \r
72                 public void TestFinished(TestCaseResult testResult)\r
73                 {\r
74                         if(testResult.Executed)\r
75                         {\r
76                                 testRunCount++;\r
77 \r
78                                 if(testResult.IsFailure)\r
79                                 {       \r
80                                         failureCount++;\r
81                                                 \r
82                                         if ( progress )\r
83                                                 Console.Write("F");\r
84                                                 \r
85                                         messages.Add( string.Format( "{0}) {1} :", failureCount, testResult.Test.TestName.FullName ) );\r
86                                         messages.Add( testResult.Message.Trim( Environment.NewLine.ToCharArray() ) );\r
87 \r
88                                         string stackTrace = StackTraceFilter.Filter( testResult.StackTrace );\r
89                                         if ( stackTrace != null && stackTrace != string.Empty )\r
90                                         {\r
91                                                 string[] trace = stackTrace.Split( System.Environment.NewLine.ToCharArray() );\r
92                                                 foreach( string s in trace )\r
93                                                 {\r
94                                                         if ( s != string.Empty )\r
95                                                         {\r
96                                                                 string link = Regex.Replace( s.Trim(), @".* in (.*):line (.*)", "$1($2)");\r
97                                                                 messages.Add( string.Format( "at\n{0}", link ) );\r
98                                                         }\r
99                                                 }\r
100                                         }\r
101                                 }\r
102                         }\r
103                         else\r
104                         {\r
105                                 testIgnoreCount++;\r
106                                         \r
107                                 if ( progress )\r
108                                         Console.Write("N");\r
109                         }\r
110 \r
111                         currentTestName = string.Empty;\r
112                 }\r
113 \r
114                 public void TestStarted(TestName testName)\r
115                 {\r
116                         currentTestName = testName.FullName;\r
117 \r
118                         if ( options.labels )\r
119                                 outWriter.WriteLine("***** {0}", currentTestName );\r
120                                 \r
121                         if ( progress )\r
122                                 Console.Write(".");\r
123                 }\r
124 \r
125                 public void SuiteStarted(TestName testName)\r
126                 {\r
127                         if ( level++ == 0 )\r
128                         {\r
129                                 messages = new StringCollection();\r
130                                 testRunCount = 0;\r
131                                 testIgnoreCount = 0;\r
132                                 failureCount = 0;\r
133                                 Trace.WriteLine( "################################ UNIT TESTS ################################" );\r
134                                 Trace.WriteLine( "Running tests in '" + testName.FullName + "'..." );\r
135                         }\r
136                 }\r
137 \r
138                 public void SuiteFinished(TestSuiteResult suiteResult) \r
139                 {\r
140                         if ( --level == 0) \r
141                         {\r
142                                 Trace.WriteLine( "############################################################################" );\r
143 \r
144                                 if (messages.Count == 0) \r
145                                 {\r
146                                         Trace.WriteLine( "##############                 S U C C E S S               #################" );\r
147                                 }\r
148                                 else \r
149                                 {\r
150                                         Trace.WriteLine( "##############                F A I L U R E S              #################" );\r
151                                                 \r
152                                         foreach ( string s in messages ) \r
153                                         {\r
154                                                 Trace.WriteLine(s);\r
155                                         }\r
156                                 }\r
157 \r
158                                 Trace.WriteLine( "############################################################################" );\r
159                                 Trace.WriteLine( "Executed tests       : " + testRunCount );\r
160                                 Trace.WriteLine( "Ignored tests        : " + testIgnoreCount );\r
161                                 Trace.WriteLine( "Failed tests         : " + failureCount );\r
162                                 Trace.WriteLine( "Unhandled exceptions : " + unhandledExceptions.Count);\r
163                                 Trace.WriteLine( "Total time           : " + suiteResult.Time + " seconds" );\r
164                                 Trace.WriteLine( "############################################################################");\r
165                         }\r
166                 }\r
167 \r
168                 private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)\r
169                 {\r
170                         if (e.ExceptionObject.GetType() != typeof(System.Threading.ThreadAbortException))\r
171                         {\r
172                                 this.UnhandledException((Exception)e.ExceptionObject);\r
173                         }\r
174                 }\r
175 \r
176 \r
177                 public void UnhandledException( Exception exception )\r
178                 {\r
179                         // If we do labels, we already have a newline\r
180                         unhandledExceptions.Add(currentTestName + " : " + exception.ToString());\r
181                         //if (!options.labels) outWriter.WriteLine();\r
182                         string msg = string.Format("##### Unhandled Exception while running {0}", currentTestName);\r
183                         //outWriter.WriteLine(msg);\r
184                         //outWriter.WriteLine(exception.ToString());\r
185 \r
186                         Trace.WriteLine(msg);\r
187                         Trace.WriteLine(exception.ToString());\r
188                 }\r
189 \r
190                 public void TestOutput( TestOutput output)\r
191                 {\r
192                         switch ( output.Type )\r
193                         {\r
194                                 case TestOutputType.Out:\r
195                                         outWriter.Write( output.Text );\r
196                                         break;\r
197                                 case TestOutputType.Error:\r
198                                         errorWriter.Write( output.Text );\r
199                                         break;\r
200                         }\r
201                 }\r
202 \r
203 \r
204                 public override object InitializeLifetimeService()\r
205                 {\r
206                         return null;\r
207                 }\r
208         }\r
209 }\r