PageRenderTime 31ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/src/NUnit/util/DefaultTestRunnerFactory.cs

#
C# | 82 lines | 53 code | 11 blank | 18 comment | 11 complexity | 914c9f89e0151b63f8b8ad966ae5e006 MD5 | raw file
 1// ****************************************************************
 2// Copyright 2010, Charlie Poole
 3// This is free software licensed under the NUnit license. You may
 4// obtain a copy of the license at http://nunit.org
 5// ****************************************************************
 6
 7using System;
 8using NUnit.Core;
 9
10namespace NUnit.Util
11{
12    /// <summary>
13    /// DefaultTestRunnerFactory handles creation of a suitable test 
14    /// runner for a given package to be loaded and run either in a 
15    /// separate process or within the same process. 
16    /// </summary>
17    public class DefaultTestRunnerFactory : InProcessTestRunnerFactory, ITestRunnerFactory
18    {
19        private RuntimeFrameworkSelector selector = new RuntimeFrameworkSelector();        
20        
21        /// <summary>
22        /// Returns a test runner based on the settings in a TestPackage.
23        /// Any setting that is "consumed" by the factory is removed, so
24        /// that downstream runners using the factory will not repeatedly
25        /// create the same type of runner.
26        /// </summary>
27        /// <param name="package">The TestPackage to be loaded and run</param>
28        /// <returns>A TestRunner</returns>
29        public override TestRunner MakeTestRunner(TestPackage package)
30        {
31            ProcessModel processModel = GetTargetProcessModel(package);
32
33            switch (processModel)
34            {
35                case ProcessModel.Multiple:
36                    package.Settings.Remove("ProcessModel");
37                    return new MultipleTestProcessRunner();
38                case ProcessModel.Separate:
39                    package.Settings.Remove("ProcessModel");
40                    return new ProcessRunner();
41                default:
42                    return base.MakeTestRunner(package);
43            }
44        }
45
46        public override bool CanReuse(TestRunner runner, TestPackage package)
47        {
48            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;
49            RuntimeFramework targetFramework = selector.SelectRuntimeFramework(package);
50
51            ProcessModel processModel = (ProcessModel)package.GetSetting("ProcessModel", ProcessModel.Default);
52            if (processModel == ProcessModel.Default)
53                if (!targetFramework.Matches(currentFramework))
54                    processModel = ProcessModel.Separate;
55
56            switch (processModel)
57            {
58                case ProcessModel.Multiple:
59                    return runner is MultipleTestProcessRunner;
60                case ProcessModel.Separate:
61                    ProcessRunner processRunner = runner as ProcessRunner;
62                    return processRunner != null && processRunner.RuntimeFramework == targetFramework;
63                default:
64                    return base.CanReuse(runner, package);
65            }
66        }
67
68        private ProcessModel GetTargetProcessModel(TestPackage package)
69        {
70            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;
71            RuntimeFramework targetFramework = selector.SelectRuntimeFramework(package);
72
73            ProcessModel processModel = (ProcessModel)package.GetSetting("ProcessModel", ProcessModel.Default);
74            if (processModel == ProcessModel.Default)
75                if (!targetFramework.Matches(currentFramework))
76                    processModel = ProcessModel.Separate;
77            return processModel;
78        }
79    }
80
81
82}