* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / nunit20 / core / TestRunnerThread.cs
1 using System;
2 using System.IO;
3 using System.Threading;
4 using System.Configuration;
5 using System.Collections.Specialized;
6
7 namespace NUnit.Core
8 {
9         /// <summary>
10         /// Summary description for TestRunnerThread.
11         /// </summary>
12         public class TestRunnerThread
13         {
14                 #region Private Fields
15
16                 /// <summary>
17                 /// The Test runner to be used in running tests on the thread
18                 /// </summary>
19                 private TestRunner runner;
20
21                 /// <summary>
22                 /// The System.Threading.Thread created by the object
23                 /// </summary>
24                 private Thread thread;
25
26                 /// <summary>
27                 /// Collection of TestRunner settings from the config file
28                 /// </summary>
29                 private NameValueCollection settings;
30
31                 /// <summary>
32                 /// The exception that terminated a test run
33                 /// </summary>
34                 private Exception lastException;
35
36                 /// <summary>
37                 /// The EventListener interface to receive test events
38                 /// </summary>
39                 private NUnit.Core.EventListener listener;
40                         
41                 /// <summary>
42                 /// Array of test names for ues by the thread proc
43                 /// </summary>
44                 private string[] testNames;
45                         
46                 /// <summary>
47                 /// Array of returned results
48                 /// </summary>
49                 private TestResult[] results;
50
51                 #endregion
52
53                 #region Properties
54
55                 /// <summary>
56                 /// Array of returned results
57                 /// </summary>
58                 public TestResult[] Results
59                 {
60                         get { return results; }
61                 }
62
63                 #endregion
64
65                 #region Constructor
66         
67                 public TestRunnerThread( TestRunner runner ) 
68                 { 
69                         this.runner = runner;
70                         this.thread = new Thread( new ThreadStart( TestRunnerThreadProc ) );
71
72                         this.settings = (NameValueCollection)
73                                 ConfigurationSettings.GetConfig( "NUnit/TestRunner" );
74                 
75                         try
76                         {
77                                 string apartment = (string)settings["ApartmentState"];
78                                 if ( apartment == "STA" )
79                                         thread.ApartmentState = ApartmentState.STA;
80                                 else if ( apartment == "MTA" )
81                                         thread.ApartmentState = ApartmentState.MTA;
82                                 
83                                 string priority = (string)settings["ThreadPriority"];
84                                 if ( priority != null )
85                                         thread.Priority = (ThreadPriority)
86                                                 System.Enum.Parse( typeof( ThreadPriority ), priority, true );
87                         }
88                         catch
89                         {
90                                 // Ignore any problems for now - test will run using default settings
91                         }
92                 }
93
94                 #endregion
95
96                 #region Public Methods
97
98                 public void Wait()
99                 {
100                         if ( this.thread.IsAlive )
101                                 this.thread.Join();
102                 }
103
104                 public void Cancel()
105                 {
106                         this.thread.Abort();
107                         this.thread.Join();
108                 }
109
110                 public void Run( EventListener listener )
111                 {
112                         this.listener = listener;
113
114                         thread.Start();}
115
116                 public void Run( EventListener listener, string testName )
117                 {
118                         this.listener = listener;
119                         this.testNames = new string[] { testName };
120
121                         thread.Start();         }
122
123                 public void Run( EventListener listener, string[] testNames )
124                 {
125                         this.listener = listener;
126                         this.testNames = testNames;
127
128                         thread.Start();
129                 }
130
131                 #endregion
132
133                 #region Thread Proc
134
135                 /// <summary>
136                 /// The thread proc for our actual test run
137                 /// </summary>
138                 private void TestRunnerThreadProc()
139                 {
140                         try
141                         {
142                                 //TODO: do we need a run started event?
143
144                                 results = runner.Run(listener, testNames );
145                                 
146                                 //TODO: do we need a run finished event?
147                         }
148                         catch( Exception exception )
149                         {
150                                 lastException = exception;
151                                 //TODO: do we need a run finished event?
152                         }
153                         finally
154                         {
155                                 testNames = null;       // Do we need this?
156                                 //runningThread = null; // Ditto
157                         }
158                 }
159
160                 #endregion
161         }
162 }