1 // ****************************************************************
\r
2 // This is free software licensed under the NUnit license. You
\r
3 // may obtain a copy of the license as well as information regarding
\r
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
\r
5 // ****************************************************************
\r
9 using System.Collections;
\r
10 using System.Reflection;
\r
11 using System.Diagnostics;
\r
12 using NUnit.Core.Extensibility;
\r
14 namespace NUnit.Core
\r
17 /// CoreExtensions is a singleton class that groups together all
\r
18 /// the extension points that are supported in the test domain.
\r
19 /// It also provides access to the test builders and decorators
\r
20 /// by other parts of the NUnit core.
\r
22 public class CoreExtensions : ExtensionHost, IService
\r
24 #region Instance Fields
\r
25 private IAddinRegistry addinRegistry;
\r
26 private bool initialized;
\r
28 private SuiteBuilderCollection suiteBuilders;
\r
29 private TestCaseBuilderCollection testBuilders;
\r
30 private TestDecoratorCollection testDecorators;
\r
31 private EventListenerCollection listeners;
\r
34 #region CoreExtensions Singleton
\r
35 private static CoreExtensions host;
\r
36 public static CoreExtensions Host
\r
42 host = new CoreExtensions();
\r
43 // host.InitializeService();
\r
51 #region Constructors
\r
52 public CoreExtensions()
\r
54 this.suiteBuilders = new SuiteBuilderCollection();
\r
55 this.testBuilders = new TestCaseBuilderCollection();
\r
56 this.testDecorators = new TestDecoratorCollection();
\r
57 this.listeners = new EventListenerCollection();
\r
59 this.extensions = new IExtensionPoint[]
\r
60 { suiteBuilders, testBuilders, testDecorators };
\r
61 this.supportedTypes = ExtensionType.Core;
\r
67 public bool Initialized
\r
69 get { return initialized; }
\r
73 /// Our AddinRegistry may be set from outside or passed into the domain
\r
75 public IAddinRegistry AddinRegistry
\r
79 if ( addinRegistry == null )
\r
80 addinRegistry = AppDomain.CurrentDomain.GetData( "AddinRegistry" ) as IAddinRegistry;
\r
82 return addinRegistry;
\r
84 set { addinRegistry = value; }
\r
87 public ISuiteBuilder SuiteBuilders
\r
89 get { return suiteBuilders; }
\r
92 public TestCaseBuilderCollection TestBuilders
\r
94 get { return testBuilders; }
\r
97 public ITestDecorator TestDecorators
\r
99 get { return testDecorators; }
\r
102 public FrameworkRegistry TestFrameworks
\r
104 get { return frameworks; }
\r
108 #region Public Methods
\r
109 public void InstallBuiltins()
\r
111 //Trace.WriteLine( "Installing Builtins" );
\r
113 // Define NUnit Framework
\r
114 FrameworkRegistry.Register( "NUnit", "nunit.framework" );
\r
116 // Install builtin SuiteBuilders - Note that the
\r
117 // NUnitTestCaseBuilder is installed whenever
\r
118 // an NUnitTestFixture is being populated and
\r
119 // removed afterward.
\r
120 suiteBuilders.Install( new Builders.NUnitTestFixtureBuilder() );
\r
121 suiteBuilders.Install( new Builders.SetUpFixtureBuilder() );
\r
123 //testDecorators.InstallFinal(new Builders.MultiCultureDecorator());
\r
126 public void InstallAddins()
\r
128 //Trace.WriteLine( "Installing Addins" );
\r
130 if( AddinRegistry != null )
\r
132 foreach (Addin addin in AddinRegistry.Addins)
\r
134 if ( (this.ExtensionTypes & addin.ExtensionType) != 0 )
\r
138 Type type = Type.GetType(addin.TypeName);
\r
139 if ( type == null )
\r
140 AddinRegistry.SetStatus( addin.Name, AddinStatus.Error, "Could not locate type" );
\r
141 else if ( !InstallAddin( type ) )
\r
142 AddinRegistry.SetStatus( addin.Name, AddinStatus.Error, "Install returned false" );
\r
144 AddinRegistry.SetStatus( addin.Name, AddinStatus.Loaded, null );
\r
146 catch( Exception ex )
\r
148 AddinRegistry.SetStatus( addin.Name, AddinStatus.Error, ex.Message );
\r
155 public void InstallAdhocExtensions( Assembly assembly )
\r
157 foreach ( Type type in assembly.GetExportedTypes() )
\r
159 if ( type.GetCustomAttributes(typeof(NUnitAddinAttribute), false).Length == 1 )
\r
160 InstallAddin( type );
\r
165 #region Helper Methods
\r
166 private bool InstallAddin( Type type )
\r
168 ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
\r
169 object obj = ctor.Invoke( new object[0] );
\r
170 IAddin theAddin = (IAddin)obj;
\r
172 return theAddin.Install(this);
\r
176 #region Type Safe Install Helpers
\r
177 //internal void Install( ISuiteBuilder builder )
\r
179 // suiteBuilders.Install( builder );
\r
182 //internal void Install( ITestCaseBuilder builder )
\r
184 // testBuilders.Install( builder );
\r
187 //internal void Install( ITestDecorator decorator )
\r
189 // testDecorators.Install( decorator );
\r
192 //internal void Install( EventListener listener )
\r
194 // listeners.Install( listener );
\r
198 #region IService Members
\r
200 public void UnloadService()
\r
202 // TODO: Add CoreExtensions.UnloadService implementation
\r
205 public void InitializeService()
\r
210 initialized = true;
\r