1
#region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2
/************************************************************************************
4
' Copyright � 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5
' Copyright � 2000-2003 Philip A. Craig
7
' This software is provided 'as-is', without any express or implied warranty. In no
8
' event will the authors be held liable for any damages arising from the use of this
11
' Permission is granted to anyone to use this software for any purpose, including
12
' commercial applications, and to alter it and redistribute it freely, subject to the
13
' following restrictions:
15
' 1. The origin of this software must not be misrepresented; you must not claim that
16
' you wrote the original software. If you use this software in a product, an
17
' acknowledgment (see the following) in the product documentation is required.
19
' Portions Copyright � 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20
' or Copyright � 2000-2003 Philip A. Craig
22
' 2. Altered source versions must be plainly marked as such, and must not be
23
' misrepresented as being the original software.
25
' 3. This notice may not be removed or altered from any source distribution.
27
'***********************************************************************************/
31
using System.Reflection;
35
public class AddinManager : ISuiteBuilder, ITestCaseBuilder
39
private static readonly string SuiteBuilderAttributeType = typeof( SuiteBuilderAttribute ).FullName;
40
private static readonly string SuiteBuilderInterfaceType = typeof( ISuiteBuilder ).FullName;
41
private static readonly string TestCaseBuilderAttributeName = typeof( TestCaseBuilderAttribute ).FullName;
42
private static readonly string TestCaseBuilderInterfaceName = typeof( ITestCaseBuilder ).FullName;
46
#region Instance Fields
47
private AddinManager priorState = null;
49
private SuiteBuilderCollection suiteBuilders = new SuiteBuilderCollection();
50
private TestCaseBuilderCollection testBuilders = new TestCaseBuilderCollection();
56
public AddinManager() { }
58
public AddinManager( AddinManager priorState )
60
this.priorState = priorState;
61
this.suiteBuilders = new SuiteBuilderCollection( priorState.suiteBuilders );
62
this.testBuilders = new TestCaseBuilderCollection( priorState.testBuilders );
68
public AddinManager PriorState
70
get { return priorState; }
74
#region ISuiteBuilder Members
75
public bool CanBuildFrom(Type type)
77
return suiteBuilders.CanBuildFrom( type );
80
public TestSuite BuildFrom(Type type)
82
return suiteBuilders.BuildFrom( type );
86
#region ITestCaseBuilder Members
87
public bool CanBuildFrom(MethodInfo method)
89
return testBuilders.CanBuildFrom( method );
92
public TestCase BuildFrom(MethodInfo method)
94
return testBuilders.BuildFrom( method );
98
#region Addin Registration
99
public void Register( Assembly assembly )
101
foreach( Type type in assembly.GetExportedTypes() )
103
if ( Reflect.HasAttribute( type, SuiteBuilderAttributeType, false )
104
&& Reflect.HasInterface( type, SuiteBuilderInterfaceType ) )
106
object builderObject = Reflect.Construct( type );
107
ISuiteBuilder builder = builderObject as ISuiteBuilder;
108
// May not be able to cast, if the builder uses an earlier
109
// version of the interface.
110
// TODO: Wrap the object and use reflection
111
if ( builder != null )
112
suiteBuilders.Add( builder );
114
suiteBuilders.Add( new SuiteBuilderWrapper( builderObject ) );
115
// TODO: Figure out when to unload - this is
116
// not important now, since we use a different
117
// appdomain for each load, but may be in future.
119
else if ( Reflect.HasAttribute( type, TestCaseBuilderAttributeName, false )
120
&& Reflect.HasInterface( type, TestCaseBuilderInterfaceName ) )
122
object builderObject = Reflect.Construct( type );
123
ITestCaseBuilder builder = (ITestCaseBuilder)builderObject;
124
if ( builder != null )
125
testBuilders.Add( builder );
130
public void Register( ISuiteBuilder builder )
132
suiteBuilders.Add( builder );
135
public void Register( ITestCaseBuilder builder )
137
testBuilders.Add( builder );
142
suiteBuilders.Clear();
143
testBuilders.Clear();
148
#region Nested SuiteBuilderWrapper Class
150
private class SuiteBuilderWrapper : ISuiteBuilder
152
private object builder;
153
private MethodInfo canBuildFromMethod;
154
private MethodInfo buildFromMethod;
156
public SuiteBuilderWrapper( object builder )
158
this.builder = builder;
159
this.canBuildFromMethod = Reflect.GetNamedMethod(
162
BindingFlags.Public | BindingFlags.Instance );
163
this.buildFromMethod = Reflect.GetNamedMethod(
166
BindingFlags.Public | BindingFlags.Instance );
167
if ( buildFromMethod == null || canBuildFromMethod == null )
168
throw new ArgumentException( "Invalid suite builder" );
169
// TODO: Check for proper signature - put in Reflect?
172
#region ISuiteBuilder Members
174
public bool CanBuildFrom(Type type)
176
return (bool)canBuildFromMethod.Invoke( builder, new object[] { type } );
179
public TestSuite BuildFrom(Type type)
181
return (TestSuite)buildFromMethod.Invoke( builder, new object[] { type } );