PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/src/NUnit/util/TestDomain.cs

#
C# | 126 lines | 91 code | 24 blank | 11 comment | 12 complexity | d30cef1ad04c65fe248d3f5c1f4208be MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You
  3// may obtain a copy of the license as well as information regarding
  4// copyright ownership at http://nunit.org
  5// ****************************************************************
  6
  7using System;
  8
  9namespace NUnit.Util
 10{
 11	using System.Diagnostics;
 12	using System.Security.Policy;
 13	using System.Reflection;
 14	using System.Collections;
 15	using System.Configuration;
 16	using System.IO;
 17
 18	using NUnit.Core;
 19
 20	public class TestDomain : ProxyTestRunner, TestRunner
 21	{
 22        static Logger log = InternalTrace.GetLogger(typeof(TestDomain));
 23
 24		#region Instance Variables
 25
 26		/// <summary>
 27		/// The appdomain used  to load tests
 28		/// </summary>
 29		private AppDomain domain; 
 30
 31		/// <summary>
 32		/// The TestAgent in the domain
 33		/// </summary>
 34		private DomainAgent agent;
 35
 36		#endregion
 37
 38		#region Constructors
 39		public TestDomain() : base( 0 ) { }
 40
 41		public TestDomain( int runnerID ) : base( runnerID ) { }
 42		#endregion
 43
 44		#region Properties
 45		public AppDomain AppDomain
 46		{
 47			get { return domain; }
 48		}
 49		#endregion
 50
 51		#region Loading and Unloading Tests
 52		public override bool Load( TestPackage package )
 53		{
 54			Unload();
 55
 56            log.Info("Loading " + package.Name);
 57			try
 58			{
 59				if ( this.domain == null )
 60					this.domain = Services.DomainManager.CreateDomain( package );
 61
 62                if (this.agent == null)
 63                {
 64                    this.agent = DomainAgent.CreateInstance(domain);
 65                    this.agent.Start();
 66                }
 67            
 68				if ( this.TestRunner == null )
 69					this.TestRunner = this.agent.CreateRunner( this.ID );
 70
 71                log.Info("Loading tests in AppDomain, see {0}.log", domain.FriendlyName);
 72				return TestRunner.Load( package );
 73			}
 74			catch
 75			{
 76                log.Error("Load failure");
 77				Unload();
 78				throw;
 79			}
 80		}
 81
 82		public override void Unload()
 83		{
 84            if (this.TestRunner != null)
 85            {
 86                log.Info("Unloading");
 87                this.TestRunner.Unload();
 88                this.TestRunner = null;
 89            }
 90
 91            if (this.agent != null)
 92            {
 93                log.Info("Stopping DomainAgent");
 94                this.agent.Dispose();
 95                this.agent = null;
 96            }
 97
 98			if(domain != null) 
 99			{
100                log.Info("Unloading AppDomain " + domain.FriendlyName);
101				Services.DomainManager.Unload(domain);
102				domain = null;
103			}
104		}
105		#endregion
106
107        #region Running Tests
108        public override void BeginRun(EventListener listener, ITestFilter filter)
109        {
110            log.Info("BeginRun in AppDomain {0}", domain.FriendlyName);
111            base.BeginRun(listener, filter);
112        }
113        #endregion
114
115        #region IDisposable Members
116
117        public override void Dispose()
118        {
119            base.Dispose();
120
121            Unload();
122        }
123
124        #endregion
125    }
126}