~a-schlapsi/nunit-3.0/linux-makefile

« back to all changes in this revision

Viewing changes to src/framework/NUnit/Core/SimpleTestRunner.cs

  • Committer: Andreas Schlapsi
  • Date: 2010-01-23 23:14:05 UTC
  • mfrom: (18.1.137 work)
  • Revision ID: a.schlapsi@gmx.at-20100123231405-17deqoh18nfnbq1j
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ***********************************************************************
2
 
// Copyright (c) 2007 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
 
 
24
 
using System;
25
 
using System.IO;
26
 
using System.Threading;
27
 
using System.Collections;
28
 
using System.Collections.Specialized;
29
 
//using NUnit.Core.Filters;
30
 
using System.Reflection;
31
 
 
32
 
namespace NUnit.Core
33
 
{
34
 
        /// <summary>
35
 
        /// SimpleTestRunner is the simplest direct-running TestRunner. It
36
 
        /// passes the event listener interface that is provided on to the tests
37
 
        /// to use directly and does nothing to redirect text output. Both
38
 
        /// Run and BeginRun are actually synchronous, although the client
39
 
        /// can usually ignore this. BeginRun + EndRun operates as expected.
40
 
        /// </summary>
41
 
        public class SimpleTestRunner : MarshalByRefObject, TestRunner
42
 
        {
43
 
        static Logger log = InternalTrace.GetLogger(typeof(SimpleTestRunner));
44
 
 
45
 
                #region Instance Variables
46
 
 
47
 
                /// <summary>
48
 
                /// Identifier for this runner. Must be unique among all
49
 
                /// active runners in order to locate tests. Default
50
 
                /// value of 0 is adequate in applications with a single
51
 
                /// runner or a non-branching chain of runners.
52
 
                /// </summary>
53
 
                private int runnerID = 0;
54
 
 
55
 
                /// <summary>
56
 
                /// The loaded test suite
57
 
                /// </summary>
58
 
                private Test test;
59
 
 
60
 
                /// <summary>
61
 
                /// The builder we use to load tests, created for each load
62
 
                /// </summary>
63
 
                private TestSuiteBuilder builder;
64
 
 
65
 
                /// <summary>
66
 
                /// Results from the last test run
67
 
                /// </summary>
68
 
                private TestResult testResult;
69
 
 
70
 
                /// <summary>
71
 
                /// The thread on which Run was called. Set to the
72
 
                /// current thread while a run is in process.
73
 
                /// </summary>
74
 
                private Thread runThread;
75
 
 
76
 
                #endregion
77
 
 
78
 
                #region Constructor
79
 
                public SimpleTestRunner() : this( 0 ) { }
80
 
 
81
 
                public SimpleTestRunner( int runnerID )
82
 
                {
83
 
                        this.runnerID = runnerID;
84
 
                }
85
 
                #endregion
86
 
 
87
 
                #region Properties
88
 
                public virtual int ID
89
 
                {
90
 
                        get { return runnerID; }
91
 
                }
92
 
 
93
 
                public TestAssemblyInfo[] AssemblyInfo
94
 
                {
95
 
                        get { return builder.AssemblyInfo; }
96
 
                }
97
 
                
98
 
                public ITest Test
99
 
                {
100
 
                        get { return test; }
101
 
                }
102
 
 
103
 
                /// <summary>
104
 
                /// Results from the last test run
105
 
                /// </summary>
106
 
                public TestResult TestResult
107
 
                {
108
 
                        get { return testResult; }
109
 
                }
110
 
 
111
 
                public virtual bool Running
112
 
                {
113
 
                        get { return runThread != null && runThread.IsAlive; }
114
 
                }
115
 
                #endregion
116
 
 
117
 
                #region Methods for Loading Tests
118
 
                /// <summary>
119
 
                /// Load a TestPackage
120
 
                /// </summary>
121
 
                /// <param name="package">The package to be loaded</param>
122
 
                /// <returns>True on success, false on failure</returns>
123
 
                public bool Load( TestPackage package )
124
 
                {
125
 
            log.Debug("Loading package " + package.Name);
126
 
 
127
 
                        this.builder = new TestSuiteBuilder();
128
 
 
129
 
                        this.test = builder.Build( package );
130
 
                        if ( test == null ) return false;
131
 
 
132
 
                        test.SetRunnerID( this.runnerID, true );
133
 
                        return true;
134
 
                }
135
 
 
136
 
                /// <summary>
137
 
                /// Unload all tests previously loaded
138
 
                /// </summary>
139
 
                public void Unload()
140
 
                {
141
 
            log.Debug("Unloading");
142
 
                        this.test = null; // All for now
143
 
                }
144
 
                #endregion
145
 
 
146
 
                #region CountTestCases
147
 
                public int CountTestCases( TestFilter filter )
148
 
                {
149
 
                        return test.CountTestCases( filter );
150
 
                }
151
 
                #endregion
152
 
 
153
 
                #region Methods for Running Tests
154
 
                public virtual TestResult Run( ITestListener listener )
155
 
                {
156
 
                        return Run( listener, TestFilter.Empty );
157
 
                }
158
 
 
159
 
                public virtual TestResult Run( ITestListener listener, TestFilter filter )
160
 
                {
161
 
                        try
162
 
                        {
163
 
                log.Debug("Starting test run");
164
 
 
165
 
                                // Take note of the fact that we are running
166
 
                                this.runThread = Thread.CurrentThread;
167
 
 
168
 
                                listener.RunStarted( this.Test.TestName, test.CountTestCases( filter ) );
169
 
                                
170
 
                                testResult = test.Run( listener, filter );
171
 
 
172
 
                                // Signal that we are done
173
 
                                listener.RunFinished( testResult );
174
 
                log.Debug("Test run complete");
175
 
 
176
 
                                // Return result array
177
 
                                return testResult;
178
 
                        }
179
 
                        catch( Exception exception )
180
 
                        {
181
 
                                // Signal that we finished with an exception
182
 
                                listener.RunFinished( exception );
183
 
                                // Rethrow - should we do this?
184
 
                                throw;
185
 
                        }
186
 
                        finally
187
 
                        {
188
 
                                runThread = null;
189
 
                        }
190
 
                }
191
 
 
192
 
                public void BeginRun( ITestListener listener )
193
 
                {
194
 
                        testResult = this.Run( listener );
195
 
                }
196
 
 
197
 
                public void BeginRun( ITestListener listener, TestFilter filter )
198
 
                {
199
 
                        testResult = this.Run( listener, filter );
200
 
                }
201
 
 
202
 
                public virtual TestResult EndRun()
203
 
                {
204
 
                        return TestResult;
205
 
                }
206
 
 
207
 
                /// <summary>
208
 
                /// Wait is a NOP for SimpleTestRunner
209
 
                /// </summary>
210
 
                public virtual void Wait()
211
 
                {
212
 
                }
213
 
 
214
 
                public virtual void CancelRun()
215
 
                {
216
 
                        if (this.runThread != null)
217
 
                        {
218
 
                                // Cancel Synchronous run only if on another thread
219
 
                                if ( runThread == Thread.CurrentThread )
220
 
                                        throw new InvalidOperationException( "May not CancelRun on same thread that is running the test" );
221
 
 
222
 
                                // Make a copy of runThread, which will be set to 
223
 
                                // null when the thread terminates.
224
 
                                Thread cancelThread = this.runThread;
225
 
 
226
 
                                // Tell the thread to abort
227
 
                                this.runThread.Abort();
228
 
                                
229
 
                                // Wake up the thread if necessary
230
 
                                // Figure out if we need to do an interupt
231
 
                                if ( (cancelThread.ThreadState & ThreadState.WaitSleepJoin ) != 0 )
232
 
                                        cancelThread.Interrupt();
233
 
                        }
234
 
                }
235
 
                #endregion
236
 
 
237
 
        #region InitializeLifetimeService Override
238
 
        public override object InitializeLifetimeService()
239
 
        {
240
 
            return null;
241
 
        }
242
 
        #endregion
243
 
        }
244
 
}