2004-06-10 Atsushi Enomoto <atsushi@ximian.com>
[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 - needed?
48                 /// </summary>
49                 private TestResult[] results;
50
51                 #endregion
52
53                 #region Constructor
54         
55                 public TestRunnerThread( TestRunner runner ) 
56                 { 
57                         this.runner = runner;
58                         this.thread = new Thread( new ThreadStart( TestRunnerThreadProc ) );
59
60                         this.settings = (NameValueCollection)
61                                 ConfigurationSettings.GetConfig( "NUnit/TestRunner" );
62                 
63                         try
64                         {
65                                 string apartment = (string)settings["ApartmentState"];
66                                 if ( apartment == "STA" )
67                                         thread.ApartmentState = ApartmentState.STA;
68                                 else if ( apartment == "MTA" )
69                                         thread.ApartmentState = ApartmentState.MTA;
70                                 
71                                 string priority = (string)settings["ThreadPriority"];
72                                 if ( priority != null )
73                                         thread.Priority = (ThreadPriority)
74                                                 System.Enum.Parse( typeof( ThreadPriority ), priority, true );
75                         }
76                         catch
77                         {
78                                 // Ignore any problems for now - test will run using default settings
79                         }
80                 }
81
82                 #endregion
83
84                 #region Public Methods
85
86                 public void Wait()
87                 {
88                         if ( this.thread.IsAlive )
89                                 this.thread.Join();
90                 }
91
92                 public void Cancel()
93                 {
94                         this.thread.Abort();
95                         this.thread.Join();
96                 }
97
98                 public void Run( EventListener listener )
99                 {
100                         this.listener = listener;
101
102                         thread.Start();}
103
104                 public void Run( EventListener listener, string testName )
105                 {
106                         this.listener = listener;
107                         this.testNames = new string[] { testName };
108
109                         thread.Start();         }
110
111                 public void Run( EventListener listener, string[] testNames )
112                 {
113                         this.listener = listener;
114                         this.testNames = testNames;
115
116                         thread.Start();
117                 }
118
119                 #endregion
120
121                 #region Thread Proc
122
123                 /// <summary>
124                 /// The thread proc for our actual test run
125                 /// </summary>
126                 private void TestRunnerThreadProc()
127                 {
128                         try
129                         {
130                                 //TODO: do we need a run started event?
131                                 int count = runner.CountTestCases( testNames );
132
133                                 Directory.SetCurrentDirectory( AppDomain.CurrentDomain.BaseDirectory );
134                                 results = runner.Run(listener, testNames );
135                                 
136                                 //TODO: do we need a run finished event?
137                         }
138                         catch( Exception exception )
139                         {
140                                 lastException = exception;
141                                 //TODO: do we need a run finished event?
142                         }
143                         finally
144                         {
145                                 testNames = null;       // Do we need this?
146                                 //runningThread = null; // Ditto
147                         }
148                 }
149
150                 #endregion
151         }
152 }