Merge pull request #966 from ermshiperete/bug-xamarin-18511
[mono.git] / mcs / nunit24 / NUnitCore / core / TestRunnerThread.cs
1 // ****************************************************************\r
2 // This is free software licensed under the NUnit license. You\r
3 // may obtain a copy of the license as well as information regarding\r
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.\r
5 // ****************************************************************\r
6 \r
7 using System;\r
8 using System.Threading;\r
9 using System.Configuration;\r
10 using System.Collections.Specialized;\r
11 \r
12 namespace NUnit.Core\r
13 {\r
14         /// <summary>\r
15         /// TestRunnerThread encapsulates running a test on a thread.\r
16         /// It knows how to create the thread based on configuration\r
17         /// settings and can cancel abort the test if necessary.\r
18         /// </summary>\r
19         public class TestRunnerThread\r
20         {\r
21                 #region Private Fields\r
22 \r
23                 /// <summary>\r
24                 /// The Test runner to be used in running tests on the thread\r
25                 /// </summary>\r
26                 private TestRunner runner;\r
27 \r
28                 /// <summary>\r
29                 /// The System.Threading.Thread created by the object\r
30                 /// </summary>\r
31                 private Thread thread;\r
32 \r
33                 /// <summary>\r
34                 /// Collection of TestRunner settings from the config file\r
35                 /// </summary>\r
36                 private NameValueCollection settings;\r
37 \r
38                 /// <summary>\r
39                 /// The EventListener interface to receive test events\r
40                 /// </summary>\r
41                 private NUnit.Core.EventListener listener;\r
42 \r
43                 /// <summary>\r
44                 /// Array of test names for ues by the thread proc\r
45                 /// </summary>\r
46                 //private string[] testNames;\r
47                 private ITestFilter filter;\r
48                         \r
49                 /// <summary>\r
50                 /// Array of returned results\r
51                 /// </summary>\r
52                 private TestResult[] results;\r
53 \r
54                 #endregion\r
55 \r
56                 #region Properties\r
57 \r
58                 /// <summary>\r
59                 /// True if the thread is executing\r
60                 /// </summary>\r
61                 public bool IsAlive\r
62                 {\r
63                         get     { return this.thread.IsAlive; }\r
64                 }\r
65 \r
66                 /// <summary>\r
67                 /// Array of returned results\r
68                 /// </summary>\r
69                 public TestResult[] Results\r
70                 {\r
71                         get { return results; }\r
72                 }\r
73 \r
74                 #endregion\r
75 \r
76                 #region Constructor\r
77 \r
78                 public TestRunnerThread( TestRunner runner ) \r
79                 { \r
80                         this.runner = runner;\r
81                         this.thread = new Thread( new ThreadStart( TestRunnerThreadProc ) );\r
82                         thread.IsBackground = true;\r
83                         thread.Name = "TestRunnerThread";\r
84 \r
85                         this.settings = (NameValueCollection)\r
86                                 ConfigurationSettings.GetConfig( "NUnit/TestRunner" );\r
87         \r
88                         if ( settings != null )\r
89                         {\r
90                                 try\r
91                                 {\r
92                                         string apartment = settings["ApartmentState"];\r
93                                         if ( apartment != null )\r
94                                                 thread.ApartmentState = (ApartmentState)\r
95                                                         System.Enum.Parse( typeof( ApartmentState ), apartment, true );\r
96                 \r
97                                         string priority = settings["ThreadPriority"];\r
98                                         if ( priority != null )\r
99                                                 thread.Priority = (ThreadPriority)\r
100                                                         System.Enum.Parse( typeof( ThreadPriority ), priority, true );\r
101                                 }\r
102                                 catch( ArgumentException ex )\r
103                                 {\r
104                                         string msg = string.Format( "Invalid configuration setting in {0}", \r
105                                                 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile );\r
106                                         throw new ArgumentException( msg, ex );\r
107                                 }\r
108                         }\r
109                 }\r
110 \r
111                 #endregion\r
112 \r
113                 #region Public Methods\r
114 \r
115                 public void Wait()\r
116                 {\r
117                         if ( this.thread.IsAlive )\r
118                                 this.thread.Join();\r
119                 }\r
120 \r
121                 public void Cancel()\r
122                 {\r
123                         ThreadUtility.Kill(this.thread);\r
124                 }\r
125 \r
126                 public void StartRun( EventListener listener )\r
127                 {\r
128                         StartRun( listener, TestFilter.Empty );\r
129                 }\r
130 \r
131                 public void StartRun( EventListener listener, ITestFilter filter )\r
132                 {\r
133                         this.listener = listener;\r
134                         this.filter = filter;\r
135 \r
136                         thread.Start();\r
137                 }\r
138 \r
139                 #endregion\r
140 \r
141                 #region Thread Proc\r
142                 /// <summary>\r
143                 /// The thread proc for our actual test run\r
144                 /// </summary>\r
145                 private void TestRunnerThreadProc()\r
146                 {\r
147             try\r
148             {\r
149                 results = new TestResult[] { runner.Run(this.listener, this.filter) };\r
150             }\r
151             catch (Exception ex)\r
152             {\r
153                 throw new ApplicationException("Exception in TestRunnerThread", ex);\r
154             }\r
155                 }\r
156                 #endregion\r
157         }\r
158 }\r